From 26fb8bbd04c643b0939db9c1159dcd3355c11184 Mon Sep 17 00:00:00 2001 From: Bernhard Bermeitinger Date: Tue, 30 May 2017 17:54:45 +0200 Subject: [PATCH] - Use custom JSONFeature (less dependencies) - added testcases --- pom.xml | 9 +- wikipedia-core/pom.xml | 9 +- .../wikipedia/model/WikipediaArticle.java | 12 ++- .../data/wikipedia/WikipediaFromAPITest.java | 6 +- .../data/wikipedia/WikipediaFromDumpTest.java | 42 +++++++-- wikipedia-rest/pom.xml | 44 +++------ .../data/wikipedia/rest/JsonFeature.java | 45 +++++++++ .../data/wikipedia/rest/JsonProvider.java | 61 ++++++++++++ .../wikipedia/rest/WikipediaRESTServer.java | 25 +++-- .../data/wikipedia/rest/ServerTest.java | 58 ++++++++++++ .../rest/WikipediaMultipleDumpRequests.java | 93 +++++++++++++++++++ .../rest/WikipediaSingleDumpRequest.java | 79 ++++++++++++++++ 12 files changed, 419 insertions(+), 64 deletions(-) create mode 100644 wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/JsonFeature.java create mode 100644 wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/JsonProvider.java create mode 100644 wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/ServerTest.java create mode 100644 wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/WikipediaMultipleDumpRequests.java create mode 100644 wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/WikipediaSingleDumpRequest.java diff --git a/pom.xml b/pom.xml index ff181c5..7abed1b 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,7 @@ org.lambda3.data.wikipedia wikipedia-api - 2.0.2 + 2.0.3 Small library that accesses the English Wikipedia API to download articles. @@ -63,7 +63,6 @@ 1.7.25 1.8 - 4.12 6.11 UTF-8 @@ -89,12 +88,6 @@ - - junit - junit - ${junit.version} - test - org.testng testng diff --git a/wikipedia-core/pom.xml b/wikipedia-core/pom.xml index ee89fd0..a50c064 100644 --- a/wikipedia-core/pom.xml +++ b/wikipedia-core/pom.xml @@ -32,7 +32,7 @@ org.lambda3.data.wikipedia wikipedia-api - 2.0.2 + 2.0.3 4.0.0 @@ -110,5 +110,12 @@ de.tudarmstadt.ukp.wikipedia de.tudarmstadt.ukp.wikipedia.datamachine + + + + org.testng + testng + test + diff --git a/wikipedia-core/src/main/java/org/lambda3/data/wikipedia/model/WikipediaArticle.java b/wikipedia-core/src/main/java/org/lambda3/data/wikipedia/model/WikipediaArticle.java index 636edca..2f8f9e2 100644 --- a/wikipedia-core/src/main/java/org/lambda3/data/wikipedia/model/WikipediaArticle.java +++ b/wikipedia-core/src/main/java/org/lambda3/data/wikipedia/model/WikipediaArticle.java @@ -39,10 +39,14 @@ public class WikipediaArticle { private final static Gson GSON = new GsonBuilder().create(); - private final String title; - private final String text; - private final Date date; - private final Integer pageId; + private String title; + private String text; + private Date date; + private Integer pageId; + + // for json deserializer + private WikipediaArticle() { + } WikipediaArticle(WikipediaArticleBuilder builder) { this.title = builder.getTitle(); diff --git a/wikipedia-core/src/test/java/org/lambda3/data/wikipedia/WikipediaFromAPITest.java b/wikipedia-core/src/test/java/org/lambda3/data/wikipedia/WikipediaFromAPITest.java index a14bcce..a1417c4 100644 --- a/wikipedia-core/src/test/java/org/lambda3/data/wikipedia/WikipediaFromAPITest.java +++ b/wikipedia-core/src/test/java/org/lambda3/data/wikipedia/WikipediaFromAPITest.java @@ -29,11 +29,11 @@ import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; -import org.junit.Assert; -import org.junit.Test; import org.lambda3.data.wikipedia.exceptions.WikipediaAccessException; import org.lambda3.data.wikipedia.exceptions.WikipediaArticleNotFoundException; import org.lambda3.data.wikipedia.model.WikipediaArticle; +import org.testng.Assert; +import org.testng.annotations.Test; import java.util.Date; @@ -58,7 +58,7 @@ public void testFetchArticlePassau() throws WikipediaAccessException, WikipediaA } - @Test(expected = WikipediaArticleNotFoundException.class) + @Test(expectedExceptions = WikipediaArticleNotFoundException.class) public void testNonExistentArticle() throws WikipediaAccessException, WikipediaArticleNotFoundException { Config config = ConfigFactory.load(); diff --git a/wikipedia-core/src/test/java/org/lambda3/data/wikipedia/WikipediaFromDumpTest.java b/wikipedia-core/src/test/java/org/lambda3/data/wikipedia/WikipediaFromDumpTest.java index 3fed518..0aea14b 100644 --- a/wikipedia-core/src/test/java/org/lambda3/data/wikipedia/WikipediaFromDumpTest.java +++ b/wikipedia-core/src/test/java/org/lambda3/data/wikipedia/WikipediaFromDumpTest.java @@ -29,28 +29,28 @@ import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; import org.lambda3.data.wikipedia.exceptions.WikipediaAccessException; import org.lambda3.data.wikipedia.exceptions.WikipediaArticleNotFoundException; import org.lambda3.data.wikipedia.model.WikipediaArticle; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; public class WikipediaFromDumpTest { - private static WikipediaAPI wikipediaAPI; + private static WikipediaAPI wikipediaDump; @BeforeClass public static void setup() { Config config = ConfigFactory.parseResources("wiki-api.local.conf"); - wikipediaAPI = new WikipediaFromDump(config); + wikipediaDump = new WikipediaFromDump(config); } @Test public void testFetchArticlePassau() throws WikipediaAccessException, WikipediaArticleNotFoundException { - WikipediaArticle article = wikipediaAPI.fetchArticle("Passau"); + WikipediaArticle article = wikipediaDump.fetchArticle("Passau"); Assert.assertNotNull(article.getTitle()); Assert.assertNotNull(article.getText()); @@ -63,10 +63,36 @@ public void testFetchArticlePassau() throws WikipediaAccessException, WikipediaA } - @Test(expected = WikipediaArticleNotFoundException.class) + @Test(expectedExceptions = WikipediaArticleNotFoundException.class) public void testNonExistentArticle() throws WikipediaAccessException, WikipediaArticleNotFoundException { - wikipediaAPI.fetchArticle("JunitException"); + wikipediaDump.fetchArticle("JunitException"); } + @Test + public void testFetchMultipleArticles() throws WikipediaAccessException, WikipediaArticleNotFoundException { + String[] articles = { + "George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe", + "John Quincy Adams", "Andrew Jackson", "Martin Van Buren", "William Henry Harrison", "John Tyler", + "James K. Polk", "Zachary Taylor", "Millard Fillmore", "Franklin Pierce", "James Buchanan", + "Abraham Lincoln", "Andrew Johnson", "Ulysses S. Grant", "Rutherford B. Hayes", "James A. Garfield", + "Chester A. Arthur", "Grover Cleveland", "Benjamin Harrison", "Grover Cleveland", "William McKinley", + "Theodore Roosevelt", "William Howard Taft", "Woodrow Wilson", "Warren G. Harding", "Calvin Coolidge", + "Herbert Hoover", "Franklin D. Roosevelt", "Harry S. Truman", "Dwight D. Eisenhower", + "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford", "Jimmy Carter", + "Ronald Reagan", "George H. W. Bush", "Bill Clinton", "George W. Bush", "Barack Obama", "Donald Trump" + }; + + for (String articleName : articles) { + WikipediaArticle article = wikipediaDump.fetchArticle(articleName); + Assert.assertNotNull(article.getTitle()); + Assert.assertNotNull(article.getText()); + Assert.assertNull(article.getDate()); + Assert.assertNotNull(article.getPageId()); + + Assert.assertEquals(articleName, article.getTitle()); + Assert.assertTrue(article.getText().length() > 1000); + } + + } } diff --git a/wikipedia-rest/pom.xml b/wikipedia-rest/pom.xml index 5022617..9084066 100644 --- a/wikipedia-rest/pom.xml +++ b/wikipedia-rest/pom.xml @@ -32,7 +32,7 @@ org.lambda3.data.wikipedia wikipedia-api - 2.0.2 + 2.0.3 4.0.0 @@ -66,12 +66,6 @@ ${jersey.version} - - org.glassfish.jersey.media - jersey-media-json-jackson - ${jersey.version} - - org.glassfish.jersey.ext jersey-bean-validation @@ -79,34 +73,17 @@ - com.fasterxml.jackson.core - jackson-annotations + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider ${jackson.version} + - com.fasterxml.jackson.core - jackson-core - ${jackson.version} - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - org.glassfish.jersey.media - jersey-media-json-processing - ${jersey.version} - - - org.glassfish.jersey.media - jersey-media-sse - ${jersey.version} + org.testng + testng + test - - org.glassfish.jersey.test-framework jersey-test-framework-core @@ -123,8 +100,15 @@ org.glassfish.jersey.test-framework.providers jersey-test-framework-provider-jetty ${jersey.version} + + + junit + junit + + test + diff --git a/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/JsonFeature.java b/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/JsonFeature.java new file mode 100644 index 0000000..bb11635 --- /dev/null +++ b/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/JsonFeature.java @@ -0,0 +1,45 @@ +/* + * ==========================License-Start============================= + * wikipedia-rest : JsonFeature + * + * Copyright © 2017 Lambda³ + * + * MIT License + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ==========================License-End============================== + */ + +package org.lambda3.data.wikipedia.rest; + +import javax.ws.rs.core.Feature; +import javax.ws.rs.core.FeatureContext; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.MessageBodyWriter; + +/** + * + */ +public class JsonFeature implements Feature { + + @Override + public boolean configure(FeatureContext context) { + context.register(JsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class); + return true; + } +} diff --git a/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/JsonProvider.java b/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/JsonProvider.java new file mode 100644 index 0000000..bad9d68 --- /dev/null +++ b/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/JsonProvider.java @@ -0,0 +1,61 @@ +/* + * ==========================License-Start============================= + * wikipedia-rest : JsonProvider + * + * Copyright © 2017 Lambda³ + * + * MIT License + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ==========================License-End============================== + */ + +package org.lambda3.data.wikipedia.rest; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; + +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.ext.Provider; + +/** + * + */ +@Provider +@Produces(MediaType.APPLICATION_JSON) +public class JsonProvider extends JacksonJaxbJsonProvider { + private static ObjectMapper mapper = new ObjectMapper(); + + static { + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); + mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); + mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.PUBLIC_ONLY); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + } + + public JsonProvider() { + super(); + setMapper(mapper); + } +} diff --git a/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/WikipediaRESTServer.java b/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/WikipediaRESTServer.java index 4cd18cd..fb80462 100644 --- a/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/WikipediaRESTServer.java +++ b/wikipedia-rest/src/main/java/org/lambda3/data/wikipedia/rest/WikipediaRESTServer.java @@ -30,7 +30,6 @@ import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import org.eclipse.jetty.server.Server; -import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.jetty.JettyHttpContainerFactory; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.ServerProperties; @@ -56,24 +55,30 @@ class WikipediaRESTServer { log.debug("Initializing Wikipedia REST Server"); + ResourceConfig rc = generateResourceConfig(config); + + String uriString = config.getString("wiki-api.server.url"); + + this.server = JettyHttpContainerFactory.createServer( + URI.create(uriString), rc, false); + + log.info("Server successfully initialized, waiting for start"); + + } + + static ResourceConfig generateResourceConfig(Config config) { ResourceConfig rc = new ResourceConfig(); rc.property(ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true); - rc.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true); // TODO: remove in production + rc.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true); // basic features - rc.register(JacksonFeature.class); + rc.register(JsonFeature.class); rc.register(ValidationFeature.class); rc.register(new WikipediaResource(config)); - String uriString = config.getString("wiki-api.server.url"); - - this.server = JettyHttpContainerFactory.createServer( - URI.create(uriString), rc, false); - - log.info("Server successfully initialized, waiting for start"); - + return rc; } void start() { diff --git a/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/ServerTest.java b/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/ServerTest.java new file mode 100644 index 0000000..7b5e32c --- /dev/null +++ b/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/ServerTest.java @@ -0,0 +1,58 @@ +/* + * ==========================License-Start============================= + * wikipedia-rest : ServerTest + * + * Copyright © 2017 Lambda³ + * + * MIT License + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ==========================License-End============================== + */ + +package org.lambda3.data.wikipedia.rest; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.test.JerseyTestNg; + +import javax.ws.rs.core.Application; + +/** + * + */ +public abstract class ServerTest extends JerseyTestNg.ContainerPerClassTest { + + @Override + protected Application configure() { + + Config config = ConfigFactory + .load("wiki-api.local.conf") + .resolveWith(ConfigFactory.load()) + .withFallback(ConfigFactory.load("reference.conf")); + + return WikipediaRESTServer.generateResourceConfig(config); + } + + @Override + protected void configureClient(ClientConfig clientConfig) { + clientConfig.register(JsonFeature.class); + super.configureClient(clientConfig); + } +} diff --git a/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/WikipediaMultipleDumpRequests.java b/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/WikipediaMultipleDumpRequests.java new file mode 100644 index 0000000..fbfb312 --- /dev/null +++ b/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/WikipediaMultipleDumpRequests.java @@ -0,0 +1,93 @@ +/* + * ==========================License-Start============================= + * wikipedia-rest : WikipediaMultipleDumpRequests + * + * Copyright © 2017 Lambda³ + * + * MIT License + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ==========================License-End============================== + */ + +package org.lambda3.data.wikipedia.rest; + +import org.lambda3.data.wikipedia.model.WikipediaArticle; +import org.lambda3.data.wikipedia.model.WikipediaArticleBuilder; +import org.testng.Assert; +import org.testng.annotations.Test; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + + +/** + * + */ +public class WikipediaMultipleDumpRequests extends ServerTest { + + @Test + public void testFetchMultipleArticles() { + String[] articles = { + "George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe", + "John Quincy Adams", "Andrew Jackson", "Martin Van Buren", "William Henry Harrison", "John Tyler", + "James K. Polk", "Zachary Taylor", "Millard Fillmore", "Franklin Pierce", "James Buchanan", + "Abraham Lincoln", "Andrew Johnson", "Ulysses S. Grant", "Rutherford B. Hayes", "James A. Garfield", + "Chester A. Arthur", "Grover Cleveland", "Benjamin Harrison", "Grover Cleveland", "William McKinley", + "Theodore Roosevelt", "William Howard Taft", "Woodrow Wilson", "Warren G. Harding", "Calvin Coolidge", + "Herbert Hoover", "Franklin D. Roosevelt", "Harry S. Truman", "Dwight D. Eisenhower", + "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford", "Jimmy Carter", + "Ronald Reagan", "George H. W. Bush", "Bill Clinton", "George W. Bush", "Barack Obama", "Donald Trump" + }; + + for (String articleName : articles) { + + WikipediaArticle expectedArticle = new WikipediaArticleBuilder() + .addTitle(articleName) + .build(); + + WikipediaRequest request = new WikipediaRequest(); + request.setArticleName(articleName); + request.setSourceType(SourceType.DUMP); + + final Response response = target("/") + .path("article") + .queryParam("articleName", request.getArticleName()) + .queryParam("sourceType", request.getSourceType()) + .request(MediaType.TEXT_PLAIN) + .accept(MediaType.APPLICATION_JSON_TYPE) + .get(); + + Assert.assertEquals(response.getStatus(), 200); + Assert.assertTrue(response.hasEntity()); + + WikipediaArticle actual = response.readEntity(WikipediaArticle.class); + + Assert.assertNotNull(actual); + Assert.assertNotNull(actual.getText()); + Assert.assertTrue(actual.getText().length() > 1000); + Assert.assertNotNull(actual.getPageId()); + Assert.assertTrue(actual.getPageId() > 0, "An article's page id is a positive integer."); + Assert.assertNull(actual.getDate()); // articles from dump have no date + + Assert.assertEquals(actual.getTitle(), expectedArticle.getTitle()); + } + + } + +} diff --git a/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/WikipediaSingleDumpRequest.java b/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/WikipediaSingleDumpRequest.java new file mode 100644 index 0000000..a2b12c2 --- /dev/null +++ b/wikipedia-rest/src/test/java/org/lambda3/data/wikipedia/rest/WikipediaSingleDumpRequest.java @@ -0,0 +1,79 @@ +/* + * ==========================License-Start============================= + * wikipedia-rest : WikipediaSingleDumpRequest + * + * Copyright © 2017 Lambda³ + * + * MIT License + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ==========================License-End============================== + */ + +package org.lambda3.data.wikipedia.rest; + +import org.lambda3.data.wikipedia.model.WikipediaArticle; +import org.lambda3.data.wikipedia.model.WikipediaArticleBuilder; +import org.testng.Assert; +import org.testng.annotations.Test; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + + +/** + * + */ +public class WikipediaSingleDumpRequest extends ServerTest { + + @Test + public void testRequestPassauArticleFromDump() { + + WikipediaArticle expectedArticle = new WikipediaArticleBuilder() + .addTitle("Passau") + .addPageId(227880) + .build(); + + WikipediaRequest request = new WikipediaRequest(); + request.setArticleName("Passau"); + request.setSourceType(SourceType.DUMP); + + final Response response = target("/") + .path("article") + .queryParam("articleName", request.getArticleName()) + .queryParam("sourceType", request.getSourceType()) + .request(MediaType.TEXT_PLAIN) + .accept(MediaType.APPLICATION_JSON_TYPE) + .get(); + + Assert.assertEquals(response.getStatus(), 200); + + Assert.assertTrue(response.hasEntity()); + + WikipediaArticle actual = response.readEntity(WikipediaArticle.class); + + Assert.assertNotNull(actual); + Assert.assertNotNull(actual.getText()); + Assert.assertTrue(actual.getText().length() > 1000); + Assert.assertNull(actual.getDate()); // articles from dump have no date + + Assert.assertEquals(actual.getTitle(), expectedArticle.getTitle()); + Assert.assertEquals(actual.getPageId(), expectedArticle.getPageId()); + } + +}