View Javadoc
1   package de.dlr.shepard.context.semantic;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertFalse;
5   import static org.junit.jupiter.api.Assertions.assertTrue;
6   import static org.mockito.Mockito.doThrow;
7   import static org.mockito.Mockito.verify;
8   import static org.mockito.Mockito.when;
9   
10  import de.dlr.shepard.BaseTestCase;
11  import jakarta.ws.rs.ProcessingException;
12  import jakarta.ws.rs.client.Client;
13  import jakarta.ws.rs.client.Invocation;
14  import jakarta.ws.rs.client.Invocation.Builder;
15  import jakarta.ws.rs.client.WebTarget;
16  import jakarta.ws.rs.core.MediaType;
17  import jakarta.ws.rs.core.Response;
18  import java.util.Collections;
19  import java.util.Map;
20  import org.junit.jupiter.api.Test;
21  import org.junit.jupiter.params.ParameterizedTest;
22  import org.junit.jupiter.params.provider.ValueSource;
23  import org.mockito.InjectMocks;
24  import org.mockito.Mock;
25  
26  public class SparqlConnectorTest extends BaseTestCase {
27  
28    @Mock
29    private Client client;
30  
31    @Mock
32    private WebTarget webTarget;
33  
34    @Mock
35    private Builder builder;
36  
37    @Mock
38    private Invocation invocation;
39  
40    @Mock
41    private Response response;
42  
43    @InjectMocks
44    private SparqlConnector connector = new SparqlConnector("endpoint");
45  
46    private final String query =
47      """
48      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
49  
50      SELECT DISTINCT ?o WHERE {
51          ?s rdfs:label ?o .
52          FILTER ( ?s = <http://example.com> )
53      }""";
54    private final String result =
55      """
56      {
57         "results":{
58            "bindings":[
59               {
60                  "o":{
61                     "type":"label",
62                     "value":"kelvin"
63                  }
64               },
65               {
66                  "o":{
67                     "type":"label",
68                     "xml:lang":"en",
69                     "value":"kelvin@en"
70                  }
71               },
72               {
73                  "o":{
74                     "type":"label",
75                     "xml:lang":"de",
76                     "value":"kelvin@de"
77                  }
78               }
79            ]
80         }
81      }
82      """;
83    private final String resultOBlank =
84      """
85      {
86         "results":{
87            "bindings":[
88               {
89                  "o":{
90                     "type":"bla",
91                     "value":""
92                  }
93               }
94            ]
95         }
96      }
97      """;
98  
99    private final String askQuery = "ASK { ?x ?y ?z }";
100   private final String askResult =
101     """
102     {
103       "head": { "link": [] },
104       "boolean": true
105     }
106     """;
107   private final String resultBooleanBlank =
108     """
109     {
110       "head": { "link": [] },
111       "boolean": ""
112     }
113     """;
114   private final String resultBooleanFalse =
115     """
116     {
117       "head": { "link": [] },
118       "boolean": false
119     }
120     """;
121 
122   private final String resultInvalid =
123     """
124     {
125       "invalid": "JSON"
126     }
127     """;
128 
129   @Test
130   public void getTermTest() {
131     when(client.target("endpoint")).thenReturn(webTarget);
132     when(webTarget.queryParam("query", query)).thenReturn(webTarget);
133     when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(builder);
134     when(builder.buildGet()).thenReturn(invocation);
135     when(invocation.invoke()).thenReturn(response);
136     when(response.readEntity(String.class)).thenReturn(result);
137 
138     var actual = connector.getTerm("http://example.com");
139 
140     assertEquals(Map.of("", "kelvin", "de", "kelvin@de", "en", "kelvin@en"), actual);
141     verify(client).close();
142   }
143 
144   @Test
145   public void getTermTest_RequestFailsException() {
146     when(client.target("endpoint")).thenReturn(webTarget);
147     when(webTarget.queryParam("query", query)).thenReturn(webTarget);
148     when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(builder);
149     when(builder.buildGet()).thenReturn(invocation);
150     doThrow(ProcessingException.class).when(invocation).invoke();
151 
152     var actual = connector.getTerm("http://example.com");
153 
154     assertEquals(Collections.emptyMap(), actual);
155     verify(client).close();
156   }
157 
158   @ParameterizedTest
159   @ValueSource(strings = { "", "  ", "No JSON", resultInvalid, resultOBlank })
160   public void getTermTest_RequestFailsBlank(String requestResult) {
161     when(client.target("endpoint")).thenReturn(webTarget);
162     when(webTarget.queryParam("query", query)).thenReturn(webTarget);
163     when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(builder);
164     when(builder.buildGet()).thenReturn(invocation);
165     when(invocation.invoke()).thenReturn(response);
166     when(response.readEntity(String.class)).thenReturn(requestResult);
167 
168     var actual = connector.getTerm("http://example.com");
169 
170     assertEquals(Collections.emptyMap(), actual);
171     verify(client).close();
172   }
173 
174   @Test
175   public void healthCheckTest() {
176     when(client.target("endpoint")).thenReturn(webTarget);
177     when(webTarget.queryParam("query", askQuery)).thenReturn(webTarget);
178     when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(builder);
179     when(builder.buildGet()).thenReturn(invocation);
180     when(invocation.invoke()).thenReturn(response);
181     when(response.readEntity(String.class)).thenReturn(askResult);
182 
183     var actual = connector.healthCheck();
184 
185     assertTrue(actual);
186     verify(client).close();
187   }
188 
189   @ParameterizedTest
190   @ValueSource(strings = { resultInvalid, resultBooleanBlank, resultBooleanFalse })
191   public void healthCheckTest_False(String requestResult) {
192     when(client.target("endpoint")).thenReturn(webTarget);
193     when(webTarget.queryParam("query", askQuery)).thenReturn(webTarget);
194     when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(builder);
195     when(builder.buildGet()).thenReturn(invocation);
196     when(invocation.invoke()).thenReturn(response);
197     when(response.readEntity(String.class)).thenReturn(requestResult);
198 
199     var actual = connector.healthCheck();
200 
201     assertFalse(actual);
202     verify(client).close();
203   }
204 }