Commit 9e15c0f5 authored by Florian Schäfer's avatar Florian Schäfer

Outsource common code for testing API queries into abstract class

parent f4d4ed4b
......@@ -16,7 +16,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.wikipedia.api.SerializationSchema;
......
package org.wikipedia.api.wdq;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Rule;
import org.junit.Test;
import org.openstreetmap.josm.testutils.JOSMTestRules;
......@@ -70,7 +68,7 @@ public class WdqApiQueryTest {
testFindInstancesOfClassesOrTheirSubclasses(MIXED_LIST, Collections.singletonList(BUILDING_CLASS), BUILDING_LIST);
}
private void testFindInstancesOfClassesOrTheirSubclasses(final Collection<String> itemList, final Collection<String> classesList, final Collection<String> expectedResultList) throws IOException {
private static void testFindInstancesOfClassesOrTheirSubclasses(final Collection<String> itemList, final Collection<String> classesList, final Collection<String> expectedResultList) throws IOException {
final SparqlResult result = ApiQueryClient.query(WdqApiQuery.findInstancesOfClassesOrTheirSubclasses(itemList, classesList));
for (final String expectedEntry : expectedResultList) {
assertEquals("Entry " + expectedEntry + " not found in the result!", 1, result.getRows().stream().filter(row -> ("http://www.wikidata.org/entity/" + expectedEntry).equals(row.get(0).getValue())).count());
......
package org.wikipedia.api.wikidata_action;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.openstreetmap.josm.testutils.JOSMTestRules;
public abstract class AbstractWikidataActionApiTest {
@Rule
public WireMockRule wmRule = new WireMockRule(wireMockConfig().dynamicPort());
@Rule
public JOSMTestRules josmRule = new JOSMTestRules().preferences();
private URL oldDefaultUrl = null;
@Before
public void before() throws MalformedURLException {
oldDefaultUrl = setApiUrl(new URL("http://localhost:" + wmRule.port()));
}
@After
public void after() {
setApiUrl(oldDefaultUrl);
}
/**
* Sets {@link WikidataActionApiQuery#defaultUrl} to the supplied URL
* @param url the new URL
* @return the URL to which {@link WikidataActionApiQuery#defaultUrl} was set before
*/
private static URL setApiUrl(final URL url) {
final URL prevURL = WikidataActionApiQuery.defaultUrl;
WikidataActionApiQuery.defaultUrl = url;
return prevURL;
}
protected static void simpleJsonStub(final byte[] bytes) {
stubFor(post("/")
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(bytes)
)
);
}
protected static void simpleRequestVerify(final String expectedQueryString) {
verify(postRequestedFor(urlEqualTo("/")).withRequestBody(new EqualToPattern(expectedQueryString)));
}
}
// License: GPL. For details, see LICENSE file.
package org.wikipedia.api.wikidata_action;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.wikipedia.api.ApiQueryClient;
import org.wikipedia.api.wikidata_action.json.WbgetentitiesResult;
import org.wikipedia.testutils.ResourceFileLoader;
public class WikidataActionApiQueryTest {
@Rule
public WireMockRule wmRule = new WireMockRule(wireMockConfig().dynamicPort());
@Rule
public JOSMTestRules josmRule = new JOSMTestRules().preferences();
private URL oldDefaultUrl = null;
@Before
public void before() throws MalformedURLException {
oldDefaultUrl = setApiUrl(new URL("http://localhost:" + wmRule.port()));
}
@After
public void after() {
setApiUrl(oldDefaultUrl);
}
public class WikidataActionApiQueryTest extends AbstractWikidataActionApiTest {
@Test(expected = IllegalArgumentException.class)
public void testWbgetentities_nonQId() {
WikidataActionApiQuery.wbgetentities(Collections.singletonList("X1"));
}
@Test(expected = IllegalArgumentException.class)
public void testCheckEntityExists_nonQId2() {
WikidataActionApiQuery.wbgetentities(Arrays.asList("Q1", "Q2", "X1"));
}
......@@ -70,6 +37,16 @@ public class WikidataActionApiQueryTest {
WikidataActionApiQuery.wbgetentities(Collections.emptyList());
}
@Test(expected = IllegalArgumentException.class)
public void testWbgetentitiesLabels_invalidQid() {
WikidataActionApiQuery.wbgetentitiesLabels("X1");
}
@Test
public void testApiName() {
assertEquals("Wikidata Action API", WikidataActionApiQuery.sitematrix().getApiName());
}
@Test
public void testWbgetentitiesQuery() {
assertEquals(
......@@ -84,16 +61,7 @@ public class WikidataActionApiQueryTest {
@Test
public void testWikidataForArticles1() throws IOException, URISyntaxException {
stubFor(post("/")
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/dewiki_Berlin.json"))
)
);
simpleJsonStub(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/dewiki_Berlin.json"));
final WbgetentitiesResult result = ApiQueryClient.query(WikidataActionApiQuery.wbgetentities("dewiki", Collections.singletonList("Berlin")));
......@@ -109,20 +77,12 @@ public class WikidataActionApiQueryTest {
assertEquals("dewiki", sitelinks.iterator().next().getSite());
assertEquals("Berlin", sitelinks.iterator().next().getTitle());
verify(postRequestedFor(urlEqualTo("/")).withRequestBody(new EqualToPattern("format=json&utf8=1&formatversion=1&action=wbgetentities&props=sitelinks&sites=dewiki&sitefilter=dewiki&titles=Berlin")));
simpleRequestVerify("format=json&utf8=1&formatversion=1&action=wbgetentities&props=sitelinks&sites=dewiki&sitefilter=dewiki&titles=Berlin");
}
@Test
public void testWikidataForArticles2() throws IOException, URISyntaxException {
stubFor(post("/")
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/enwiki_2entities2missing.json"))
)
);
simpleJsonStub(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/enwiki_2entities2missing.json"));
final WbgetentitiesResult result = ApiQueryClient.query(WikidataActionApiQuery.wbgetentities("enwiki", Arrays.asList("United States", "missing-article", "Great Britain", "Another missing article")));
......@@ -148,20 +108,12 @@ public class WikidataActionApiQueryTest {
assertNull(missing2.getId());
assertEquals("enwiki", missing2.getSite());
verify(postRequestedFor(urlEqualTo("/")).withRequestBody(new EqualToPattern("format=json&utf8=1&formatversion=1&action=wbgetentities&props=sitelinks&sites=enwiki&sitefilter=enwiki&titles=United+States%7Cmissing-article%7CGreat+Britain%7CAnother+missing+article")));
simpleRequestVerify("format=json&utf8=1&formatversion=1&action=wbgetentities&props=sitelinks&sites=enwiki&sitefilter=enwiki&titles=United+States%7Cmissing-article%7CGreat+Britain%7CAnother+missing+article");
}
@Test
public void testWikidataItemLabelQuery() throws IOException, URISyntaxException {
stubFor(post("/")
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/labels_Q42.json"))
)
);
simpleJsonStub(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/labels_Q42.json"));
final Map<String, String> result = ApiQueryClient.query(WikidataActionApiQuery.wbgetentitiesLabels("Q42"));
assertEquals(138, result.size());
......@@ -171,18 +123,7 @@ public class WikidataActionApiQueryTest {
assertEquals("더글러스 애덤스", result.get("ko"));
assertEquals("ಡಾಗ್ಲಸ್ ಆಡಮ್ಸ್", result.get("tcy"));
verify(postRequestedFor(urlEqualTo("/")).withRequestBody(new EqualToPattern("format=json&utf8=1&formatversion=1&action=wbgetentities&props=labels&ids=Q42")));
}
/**
* Sets {@link WikidataActionApiQuery#defaultUrl} to the supplied URL
* @param url the new URL
* @return the URL to which {@link WikidataActionApiQuery#defaultUrl} was set before
*/
public static URL setApiUrl(final URL url) {
final URL prevURL = WikidataActionApiQuery.defaultUrl;
WikidataActionApiQuery.defaultUrl = url;
return prevURL;
simpleRequestVerify("format=json&utf8=1&formatversion=1&action=wbgetentities&props=labels&ids=Q42");
}
}
// License: GPL. For details, see LICENSE file.
package org.wikipedia.data;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertEquals;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.wikipedia.api.wikidata_action.AbstractWikidataActionApiTest;
import org.wikipedia.api.wikidata_action.WikidataActionApiQueryTest;
import org.wikipedia.testutils.ResourceFileLoader;
public class WikipediaSiteTest {
@Rule
public WireMockRule wmRule = new WireMockRule(wireMockConfig().dynamicPort());
@Rule
public final JOSMTestRules rules = new JOSMTestRules().preferences();
private URL oldDefaultUrl;
@Before
public void before() throws IOException, URISyntaxException {
oldDefaultUrl = WikidataActionApiQueryTest.setApiUrl(new URL("http://localhost:" + wmRule.port()));
stubFor(post("/")
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/sitematrix/sitematrix.json"))
)
);
}
@After
public void after() {
WikidataActionApiQueryTest.setApiUrl(oldDefaultUrl);
}
public class WikipediaSiteTest extends AbstractWikidataActionApiTest {
@Test
public void testSites() throws IOException {
public void testSites() throws IOException, URISyntaxException {
simpleJsonStub(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/sitematrix/sitematrix.json"));
assertEquals("https://de.wikipedia.org", new WikipediaSite("de").getSite().getUrl());
assertEquals("https://simple.wikipedia.org", new WikipediaSite("simple").getSite().getUrl());
assertEquals("https://be-tarask.wikipedia.org", new WikipediaSite("be-x-old").getSite().getUrl());
......@@ -60,7 +24,8 @@ public class WikipediaSiteTest {
}
@Test(expected = IllegalArgumentException.class)
public void testUnknownSite() throws IOException {
public void testUnknownSite() throws IOException, URISyntaxException {
simpleJsonStub(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/sitematrix/sitematrix.json"));
new WikipediaSite("xy");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment