View Javadoc
1   package de.dlr.shepard.integrationtests;
2   
3   import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4   import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
5   import static com.github.tomakehurst.wiremock.client.WireMock.get;
6   import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
7   import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
8   
9   import com.github.tomakehurst.wiremock.WireMockServer;
10  import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  public class WireMockResource implements QuarkusTestResourceLifecycleManager {
15  
16    private static WireMockServer wireMockServer;
17  
18    private static final String SELECT_TEMPLATE =
19      """
20      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
21  
22      SELECT DISTINCT ?o WHERE {
23          ?s rdfs:label ?o .
24          FILTER ( ?s = <%s> )
25      }""";
26  
27    @Override
28    public Map<String, String> start() {
29      wireMockServer = new WireMockServer(options().dynamicPort());
30      wireMockServer.start();
31  
32      wireMockServer.stubFor(
33        // stub for health check on: https://dbpedia.org/sparql/
34        get(urlPathEqualTo("/sparql"))
35          .withQueryParam("query", equalTo("ASK { ?x ?y ?z }"))
36          .willReturn(aResponse().withStatus(200).withBody("{ \"head\": {\"link\": []  }, \"boolean\": true }"))
37      );
38      wireMockServer.stubFor(
39        // stub for PropertyIRI SparqlConnector.request_term
40        get(urlPathEqualTo("/sparql"))
41          .withQueryParam("query", equalTo(String.format(SELECT_TEMPLATE, "http://dbpedia.org/ontology/ingredient")))
42          .willReturn(
43            aResponse()
44              .withStatus(200)
45              .withBody(
46                "{\"head\":{\"link\":[],\"vars\":[\"o\"]},\"results\":{\"distinct\":false,\"ordered\":true,\"bindings\":[{\"o\":{\"type\":\"literal\",\"xml:lang\":\"de\",\"value\":\"Zutat\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"en\",\"value\":\"ingredient\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"fr\",\"value\":\"ingrédient\"}}]}}"
47              )
48          )
49      );
50      // stub for ValueIRI SparqlConnector.request_term
51      wireMockServer.stubFor(
52        get(urlPathEqualTo("/sparql"))
53          .withQueryParam("query", equalTo(String.format(SELECT_TEMPLATE, "http://dbpedia.org/resource/Almond_milk")))
54          .willReturn(
55            aResponse()
56              .withStatus(200)
57              .withBody(
58                "{\"head\":{\"link\":[],\"vars\":[\"o\"]},\"results\":{\"distinct\":false,\"ordered\":true,\"bindings\":[{\"o\":{\"type\":\"literal\",\"xml:lang\":\"en\",\"value\":\"Almond milk\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"ar\",\"value\":\"حليب اللوز\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"ca\",\"value\":\"Llet d'ametlla\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"de\",\"value\":\"Mandelmilch\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"el\",\"value\":\"Γάλα αμυγδάλου\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"eo\",\"value\":\"Migdallakto\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"es\",\"value\":\"Leche de almendra\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"fr\",\"value\":\"Lait d'amande\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"in\",\"value\":\"Sari kacang almond\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"it\",\"value\":\"Latte di mandorla\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"ko\",\"value\":\"아몬드밀크\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"ja\",\"value\":\"アーモンドミルク\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"nl\",\"value\":\"Amandelmelk\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"pl\",\"value\":\"Mleko migdałowe\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"pt\",\"value\":\"Leite-de-amêndoa\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"ru\",\"value\":\"Миндальное молоко\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"sv\",\"value\":\"Mandelmjölk\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"uk\",\"value\":\"Мигдалеве молоко\"}},{\"o\":{\"type\":\"literal\",\"xml:lang\":\"zh\",\"value\":\"扁桃仁奶\"}}]}}"
59              )
60          )
61      );
62  
63      return new HashMap<>();
64    }
65  
66    @Override
67    public synchronized void stop() {
68      if (wireMockServer != null) {
69        wireMockServer.stop();
70        wireMockServer = null;
71      }
72    }
73  
74    public static String getWireMockServerURlWithPath(String path) {
75      return wireMockServer.url(path);
76    }
77  }