Commit eaedcd06 authored by Florian Schäfer's avatar Florian Schäfer

Fix failing unit test, reset API URL after mocking with Wiremock

Add also another test for API queries with missing items.
parent 90525b06
{"entities":{"-1":{"site":"enwiki","title":"missing-article","missing":""},"-2":{"site":"enwiki","title":"Another missing article","missing":""},"Q30":{"type":"item","id":"Q30","sitelinks":{"enwiki":{"site":"enwiki","title":"United States","badges":["Q17437798"]}}},"Q23666":{"type":"item","id":"Q23666","sitelinks":{"enwiki":{"site":"enwiki","title":"Great Britain","badges":[]}}}},"success":1}
\ No newline at end of file
......@@ -10,6 +10,7 @@ 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 java.io.IOException;
import java.net.MalformedURLException;
......@@ -17,12 +18,14 @@ 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;
import java.util.Map;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
......@@ -37,13 +40,21 @@ public class WikidataActionApiQueryTest {
@Rule
public JOSMTestRules josmRule = new JOSMTestRules().preferences();
private URL oldDefaultUrl = null;
@Before
public void before() throws MalformedURLException {
oldDefaultUrl = WikidataActionApiQuery.defaultUrl;
WikidataActionApiQuery.defaultUrl = new URL("http://localhost:" + wmRule.port());
}
@After
public void after() {
WikidataActionApiQuery.defaultUrl = oldDefaultUrl;
}
@Test
public void test() throws IOException, URISyntaxException {
public void testWikidataForArticles1() throws IOException, URISyntaxException {
stubFor(post("/")
.withHeader("Accept", equalTo("application/json"))
......@@ -72,7 +83,46 @@ public class WikidataActionApiQueryTest {
verify(postRequestedFor(urlEqualTo("/")).withRequestBody(new EqualToPattern("format=json&utf8=1&formatversion=1&action=wbgetentities&props=sitelinks&sites=dewiki&sitefilter=dewiki&titles=Berlin")));
}
private byte[] getFileContentsFromResource(final String path) throws URISyntaxException, IOException {
@Test
public void testWikidataForArticles2() throws IOException, URISyntaxException {
stubFor(post("/")
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(getFileContentsFromResource("response/wbgetentities/enwiki:2entities2missing.json"))
)
);
final CheckEntityExistsResult result = ApiQueryClient.query(WikidataActionApiQuery.wbgetentities("enwiki", Arrays.asList("United States", "missing-article", "Great Britain", "Another missing article")));
assertEquals(2, result.getEntities().size());
assertEquals(2, result.getMissingEntities().size());
assertEquals("Q30", result.getEntities().get("Q30").getId());
assertEquals("item", result.getEntities().get("Q30").getType());
assertEquals(1, result.getEntities().get("Q30").getSitelinks().size());
assertEquals("enwiki", result.getEntities().get("Q30").getSitelinks().iterator().next().getSite());
assertEquals("United States", result.getEntities().get("Q30").getSitelinks().iterator().next().getTitle());
assertEquals("Q23666", result.getEntities().get("Q23666").getId());
assertEquals("item", result.getEntities().get("Q23666").getType());
assertEquals(1, result.getEntities().get("Q23666").getSitelinks().size());
assertEquals("enwiki", result.getEntities().get("Q23666").getSitelinks().iterator().next().getSite());
assertEquals("Great Britain", result.getEntities().get("Q23666").getSitelinks().iterator().next().getTitle());
final CheckEntityExistsResult.MissingEntity missing1 = result.getMissingEntities().stream().filter(it -> "missing-article".equals(it.getTitle())).findFirst().orElse(null);
assertNull(missing1.getId());
assertEquals("enwiki", missing1.getSite());
final CheckEntityExistsResult.MissingEntity missing2 = result.getMissingEntities().stream().filter(it -> "Another missing article".equals(it.getTitle())).findFirst().orElse(null);
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")));
}
private static byte[] getFileContentsFromResource(final String path) throws URISyntaxException, IOException {
return Files.readAllBytes(Paths.get(WikidataActionApiQueryTest.class.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