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

Mock WDQ API responses, so unit tests are not so much dependent on live data from wikidata.org

parent 20cf0ee2
......@@ -15,10 +15,11 @@ 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 final String queryString;
public WdqApiQuery(final URL url, final String queryString, final SerializationSchema<T> schema) {
private WdqApiQuery(final URL url, final String queryString, final SerializationSchema<T> schema) {
super(url, schema);
this.queryString = Objects.requireNonNull(queryString);
}
......@@ -48,9 +49,9 @@ public class WdqApiQuery<T> extends ApiQuery<T> {
public static WdqApiQuery<SparqlResult> findInstancesOfClassesOrTheirSubclasses(final Collection<String> items, final Collection<String> classes) {
Objects.requireNonNull(items);
Objects.requireNonNull(classes);
if (items.size() >= 1 && classes.size() >= 1 && Stream.concat(items.stream(), classes.stream()).allMatch(RegexUtil::isValidQId)) {
if (!items.isEmpty() && !classes.isEmpty() && Stream.concat(items.stream(), classes.stream()).allMatch(RegexUtil::isValidQId)) {
return new WdqApiQuery<>(
ApiUrl.url("https://query.wikidata.org/sparql"),
ApiUrl.url(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),
......
{
"head" : {
"vars" : [ "item", "itemLabel", "classes", "classesLabel" ]
},
"results" : {
"bindings" : [ {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q54495"
},
"itemLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Sydney Harbour Bridge"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q99236"
},
"itemLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Viaduc de Millau"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q83125"
},
"itemLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Tower Bridge"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q18109819"
},
"itemLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Beipanjiang-Brücke"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q459086"
},
"itemLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Jungfernbrücke"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q5459867"
},
"itemLabel" : {
"type" : "literal",
"value" : "Q5459867"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q44440"
},
"itemLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Golden Gate Bridge"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q805835"
},
"itemLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Baluarte-Brücke"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q52505"
},
"itemLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Rialtobrücke"
},
"classesLabel" : {
"xml:lang" : "de",
"type" : "literal",
"value" : "Brücke"
}
} ]
}
}
\ No newline at end of file
{
"head" : {
"vars" : [ "item", "itemLabel", "classes", "classesLabel" ]
},
"results" : {
"bindings" : [ {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q507939"
},
"itemLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "世界一號"
},
"classesLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "建筑物"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q48435"
},
"itemLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "圣家堂"
},
"classesLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "建筑物"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q201013"
},
"itemLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "斯瓦尔巴全球种子库"
},
"classesLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "建筑物"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q379080"
},
"itemLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "耶稣堡"
},
"classesLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "建筑物"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q3368242"
},
"itemLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "烏德勒支聖馬丁主教座堂塔樓"
},
"classesLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "建筑物"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q18712428"
},
"itemLabel" : {
"type" : "literal",
"value" : "Q18712428"
},
"classesLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "建筑物"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q494895"
},
"itemLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "樂天世界塔"
},
"classesLabel" : {
"xml:lang" : "zh",
"type" : "literal",
"value" : "建筑物"
}
} ]
}
}
\ No newline at end of file
{
"head" : {
"vars" : [ "item", "itemLabel", "classes", "classesLabel" ]
},
"results" : {
"bindings" : [ {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q52505"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Puente de Rialto"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q54495"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente de la bahía de Sídney"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q201013"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Bóveda Global de Semillas de Svalbard"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "edificio"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q99236"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Viaducto de Millau"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q459086"
},
"itemLabel" : {
"type" : "literal",
"value" : "Q459086"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q3368242"
},
"itemLabel" : {
"type" : "literal",
"value" : "Q3368242"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "edificio"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q18109819"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Puente Duge"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q494895"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Lotte World Tower"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "edificio"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q48435"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Templo Expiatorio de la Sagrada Familia"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "edificio"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q83125"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Puente de la Torre"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q379080"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Fuerte Jesús"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "edificio"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q18712428"
},
"itemLabel" : {
"type" : "literal",
"value" : "Q18712428"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "edificio"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q41176"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q507939"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "World One"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "edificio"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q5459867"
},
"itemLabel" : {
"type" : "literal",
"value" : "Q5459867"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q44440"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Puente Golden Gate"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
}, {
"classes" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q12280"
},
"item" : {
"type" : "uri",
"value" : "http://www.wikidata.org/entity/Q805835"
},
"itemLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "Puente Baluarte Bicentenario"
},
"classesLabel" : {
"xml:lang" : "es",
"type" : "literal",
"value" : "puente"
}
} ]
}
}
\ No newline at end of file