Commit 4e22aa03 authored by Florian Schäfer's avatar Florian Schäfer

Add unit test for the query action of the Wikipedia API

parent dcde48f2
{"batchcomplete":"","query":{"redirects":[{"from":"USA","to":"United States of America"},{"from":"United States of America","to":"United States"},{"from":"US","to":"United States"}],"pages":{"31880":{"pageid":31880,"ns":0,"title":"Universe"},"3434750":{"pageid":3434750,"ns":0,"title":"United States"}}}}
......@@ -18,8 +18,6 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
......@@ -31,6 +29,7 @@ import org.junit.Test;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.wikipedia.api.ApiQueryClient;
import org.wikipedia.api.wikidata_action.json.CheckEntityExistsResult;
import org.wikipedia.testutils.ResourceFileLoader;
public class WikidataActionApiQueryTest {
......@@ -92,7 +91,7 @@ public class WikidataActionApiQueryTest {
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(getApiResponseBytesFromResource("wbgetentities/dewiki:Berlin.json"))
.withBody(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/dewiki:Berlin.json"))
)
);
......@@ -121,7 +120,7 @@ public class WikidataActionApiQueryTest {
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(getApiResponseBytesFromResource("wbgetentities/enwiki:2entities2missing.json"))
.withBody(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/enwiki:2entities2missing.json"))
)
);
......@@ -152,10 +151,6 @@ public class WikidataActionApiQueryTest {
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")));
}
public static byte[] getApiResponseBytesFromResource(final String path) throws URISyntaxException, IOException {
return Files.readAllBytes(Paths.get(WikidataActionApiQueryTest.class.getResource("response/" + path).toURI()));
}
/**
* Sets {@link WikidataActionApiQuery#defaultUrl} to the supplied URL
* @param url the new URL
......
// License: GPL. For details, see LICENSE file.
package org.wikipedia.api.wikipedia_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.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
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.SitematrixResult;
import org.wikipedia.api.wikipedia_action.json.QueryResult;
import org.wikipedia.data.IWikipediaSite;
import org.wikipedia.testutils.ResourceFileLoader;
public class WikipediaActionApiQueryTest {
@Rule
public WireMockRule wmRule = new WireMockRule(wireMockConfig().dynamicPort());
@Rule
public JOSMTestRules josmRule = new JOSMTestRules().preferences();
@Test
public void test() throws IOException, URISyntaxException {
stubFor(post("/w/api.php")
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(ResourceFileLoader.getResourceBytes(WikipediaActionApiQueryTest.class, "response/query/en:US_USA_Universe.json"))
)
);
final QueryResult result = ApiQueryClient.query(
WikipediaActionApiQuery.query(new WikiSiteMock("en"), Arrays.asList("US", "USA", "Universe"))
);
assertEquals("United States", result.getQuery().getRedirects().resolveRedirect("USA"));
assertEquals("United States", result.getQuery().getRedirects().resolveRedirect("US"));
assertEquals("United States", result.getQuery().getRedirects().resolveRedirect("United States"));
assertEquals("Universe", result.getQuery().getRedirects().resolveRedirect("Universe"));
assertEquals("non-existent-title", result.getQuery().getRedirects().resolveRedirect("non-existent-title"));
assertEquals(2, result.getQuery().getPages().size());
assertTrue(result.getQuery().getPages().stream().anyMatch(it -> "Universe".equals(it.getTitle())));
assertTrue(result.getQuery().getPages().stream().anyMatch(it -> "United States".equals(it.getTitle())));
verify(postRequestedFor(urlEqualTo("/w/api.php"))
.withRequestBody(new EqualToPattern("format=json&utf8=1&formatversion=1&action=query&redirects=1&titles=US%7CUSA%7CUniverse")));
}
private class WikiSiteMock implements IWikipediaSite {
private final String langCode;
private WikiSiteMock(final String langCode) {
this.langCode = langCode;
}
@Override
public SitematrixResult.Sitematrix.Site getSite() {
return new SitematrixResult.Sitematrix.Site("http://localhost:" + wmRule.port(), "dbname", "code", "false");
}
@Override
public String getLanguageCode() {
return langCode;
}
}
}
......@@ -18,6 +18,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.wikipedia.api.wikidata_action.WikidataActionApiQueryTest;
import org.wikipedia.testutils.ResourceFileLoader;
public class WikipediaSiteTest {
......@@ -39,7 +40,7 @@ public class WikipediaSiteTest {
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(WikidataActionApiQueryTest.getApiResponseBytesFromResource("sitematrix/sitematrix.json"))
.withBody(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/sitematrix/sitematrix.json"))
)
);
}
......
// License: GPL. For details, see LICENSE file.
package org.wikipedia.testutils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
public final class ResourceFileLoader {
private ResourceFileLoader() {
// Private constructor to avoid instantiation
}
public static byte[] getResourceBytes(final Class<?> relativeToClass, final String path) throws URISyntaxException, IOException {
return Files.readAllBytes(Paths.get(relativeToClass.getResource(path).toURI()));
}
}
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