Commit 963dce46 authored by Florian Schäfer's avatar Florian Schäfer

Add WikipediaSite, that can only instantiated if that Wikipedia really exists

parent 5eebcb43
// License: GPL. For details, see LICENSE file.
package org.wikipedia.data;
import java.io.IOException;
import java.util.Objects;
import java.util.Optional;
import org.openstreetmap.josm.tools.I18n;
import org.wikipedia.api.ApiQuery;
import org.wikipedia.api.ApiQueryClient;
import org.wikipedia.api.wikidata_action.WikidataActionApiQuery;
import org.wikipedia.api.wikidata_action.json.SitematrixResult;
/**
* A Wikipedia site for a certain language. For each instance of this class you can assume that there is a Wikipedia in this language.
*/
public class WikipediaSite {
private final SitematrixResult.Sitematrix.Site site;
/**
* Constructs a Wikipedia site for a given language (iff such a Wikipedia exists).
* @param langCode the language code of the wiki
* @throws IOException if the sitematrix can't be read, see {@link ApiQueryClient#query(ApiQuery)}
* @throws IllegalArgumentException if there is no Wikipedia for the given language
*/
public WikipediaSite(final String langCode) throws IOException, IllegalArgumentException {
Objects.requireNonNull(langCode);
final SitematrixResult sitematrix = ApiQueryClient.query(WikidataActionApiQuery.sitematrix());
final Optional<SitematrixResult.Sitematrix.Language> language = sitematrix.getSitematrix().getLanguages().stream()
.filter(it -> langCode.equals(it.getCode()))
.findFirst();
this.site = language
.orElseThrow(() -> new IllegalArgumentException(I18n.tr("{0} is an illegal language code!", langCode)))
.getSites().stream()
.filter(it -> "wiki".equals(it.getCode()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(
I18n.tr("For language {0} there is no Wikipedia site!", language.get().getCode())
));
}
/**
* @return the site for a certain language, as returned by {@code action=sitematrix} with the Wikidata Action API
*/
public SitematrixResult.Sitematrix.Site getSite() {
return site;
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -12,6 +12,8 @@ import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMoc
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;
......@@ -22,9 +24,6 @@ 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;
......@@ -45,13 +44,12 @@ public class WikidataActionApiQueryTest {
@Before
public void before() throws MalformedURLException {
oldDefaultUrl = WikidataActionApiQuery.defaultUrl;
WikidataActionApiQuery.defaultUrl = new URL("http://localhost:" + wmRule.port());
oldDefaultUrl = setApiUrl(new URL("http://localhost:" + wmRule.port()));
}
@After
public void after() {
WikidataActionApiQuery.defaultUrl = oldDefaultUrl;
setApiUrl(oldDefaultUrl);
}
@Test(expected = IllegalArgumentException.class)
......@@ -94,7 +92,7 @@ public class WikidataActionApiQueryTest {
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(getFileContentsFromResource("response/wbgetentities/dewiki:Berlin.json"))
.withBody(getApiResponseBytesFromResource("wbgetentities/dewiki:Berlin.json"))
)
);
......@@ -123,7 +121,7 @@ public class WikidataActionApiQueryTest {
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(getFileContentsFromResource("response/wbgetentities/enwiki:2entities2missing.json"))
.withBody(getApiResponseBytesFromResource("wbgetentities/enwiki:2entities2missing.json"))
)
);
......@@ -154,8 +152,19 @@ 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")));
}
private static byte[] getFileContentsFromResource(final String path) throws URISyntaxException, IOException {
return Files.readAllBytes(Paths.get(WikidataActionApiQueryTest.class.getResource(path).toURI()));
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
* @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;
}
}
// 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.WikidataActionApiQueryTest;
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(WikidataActionApiQueryTest.getApiResponseBytesFromResource("sitematrix/sitematrix.json"))
)
);
}
@After
public void after() {
WikidataActionApiQueryTest.setApiUrl(oldDefaultUrl);
}
@Test
public void testSites() throws IOException {
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());
assertEquals("https://cbk-zam.wikipedia.org", new WikipediaSite("cbk-zam").getSite().getUrl());
assertEquals("https://zh-min-nan.wikipedia.org", new WikipediaSite("zh-min-nan").getSite().getUrl());
}
@Test(expected = IllegalArgumentException.class)
public void testUnknownSite() throws IOException {
new WikipediaSite("xy");
}
}
......@@ -4,13 +4,13 @@ package org.wikipedia.io;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.Rule;
import org.junit.Test;
import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
......
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