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

Remove the need to use reflection in `WdqApiQueryTest`

The base URL can now be modified from the same package.
parent 846910b0
......@@ -15,8 +15,8 @@ import org.wikipedia.tools.RegexUtil;
import org.wikipedia.tools.WikiProperties;
public class WdqApiQuery<T> extends ApiQuery<T> {
private static String baseUrl = "https://query.wikidata.org/sparql";
private static final String[] TICKET_KEYWORDS = {"wikidata", "QueryService"};
private static URL baseUrl = ApiUrl.url("https://query.wikidata.org/sparql");
private final String queryString;
private WdqApiQuery(final URL url, final String queryString, final SerializationSchema<T> schema) {
......@@ -39,6 +39,11 @@ public class WdqApiQuery<T> extends ApiQuery<T> {
.setRequestBody(queryString.getBytes(StandardCharsets.UTF_8));
}
static void setBaseUrl(final URL baseUrl) {
Objects.requireNonNull(baseUrl);
WdqApiQuery.baseUrl = baseUrl;
}
/**
* @param items the items for which we check if they are instances of one of the items provided by {@code classes}
* or instances of any subclass of the items provided by {@code classes}.
......@@ -51,7 +56,7 @@ public class WdqApiQuery<T> extends ApiQuery<T> {
Objects.requireNonNull(classes);
if (!items.isEmpty() && !classes.isEmpty() && Stream.concat(items.stream(), classes.stream()).allMatch(RegexUtil::isValidQId)) {
return new WdqApiQuery<>(
ApiUrl.url(baseUrl),
baseUrl,
"format=json&query=" + Utils.encodeUrl(String.format(
"SELECT DISTINCT ?item ?itemLabel ?classes ?classesLabel WHERE { VALUES ?item { wd:%s }. VALUES ?classes { wd:%s }. ?item wdt:P31/wdt:P279* ?supertype. ?supertype wdt:P279* ?classes. SERVICE wikibase:label { bd:serviceParam wikibase:language \"%s\" }. }",
String.join(" wd:", items),
......
......@@ -14,7 +14,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
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.ArrayList;
import java.util.Arrays;
import java.util.Collection;
......@@ -81,8 +83,8 @@ public class WdqApiQueryTest {
public WireMockRule wmRule = new WireMockRule(wireMockConfig().dynamicPort());
@Before
public void setUp() throws ReflectiveOperationException {
TestUtils.setPrivateStaticField(WdqApiQuery.class, "baseUrl", "http://localhost:" + wmRule.port() + URL_PATH);
public void setUp() throws MalformedURLException {
WdqApiQuery.setBaseUrl(new URL("http://localhost:" + wmRule.port() + URL_PATH));
}
@Test
......
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