Skip to content

Commit 3040699

Browse files
authored
JCL-411: RDF4J service test coverage (#575)
* Fix RDF test dependencies for core module: The core module test rely on library-specific interfaces to perform RDF operations, so they need to be duplicated for each RDF dependency in order to have full coverage. * Run RDF4J tests on integration module: The integration tests for the core module are Jena-specific, so they are excluded from the RDF4J tests. The most important part is to get coverage for the domain models mapping, which is where we want to have the RDFService be independant from the underlying implementation.
1 parent 3e9cec7 commit 3040699

File tree

10 files changed

+637
-208
lines changed

10 files changed

+637
-208
lines changed

core/pom.xml

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,6 @@
147147
<version>${project.version}</version>
148148
<scope>test</scope>
149149
</dependency>
150-
<dependency>
151-
<groupId>com.inrupt.client</groupId>
152-
<artifactId>inrupt-client-rdf-legacy</artifactId>
153-
<version>${project.version}</version>
154-
<scope>test</scope>
155-
</dependency>
156150
<dependency>
157151
<groupId>com.inrupt.client</groupId>
158152
<artifactId>inrupt-client-httpclient</artifactId>
@@ -190,11 +184,13 @@
190184
<goal>test</goal>
191185
</goals>
192186
<configuration>
187+
<excludes>
188+
<exclude>**/*RDF4JTest.java</exclude>
189+
</excludes>
193190
<classpathDependencyExcludes>
194191
<classpathDependencyExclude>com.inrupt.client:inrupt-client-okhttp</classpathDependencyExclude>
195192
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jsonb</classpathDependencyExclude>
196193
<classpathDependencyExclude>com.inrupt.client:inrupt-client-rdf4j</classpathDependencyExclude>
197-
<classpathDependencyExclude>com.inrupt.client:inrupt-client-rdf-legacy</classpathDependencyExclude>
198194
</classpathDependencyExcludes>
199195
</configuration>
200196
</execution>
@@ -205,11 +201,13 @@
205201
<goal>test</goal>
206202
</goals>
207203
<configuration>
204+
<excludes>
205+
<exclude>**/*RDF4JTest.java</exclude>
206+
</excludes>
208207
<classpathDependencyExcludes>
209208
<classpathDependencyExclude>com.inrupt.client:inrupt-client-httpclient</classpathDependencyExclude>
210209
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jsonb</classpathDependencyExclude>
211210
<classpathDependencyExclude>com.inrupt.client:inrupt-client-rdf4j</classpathDependencyExclude>
212-
<classpathDependencyExclude>com.inrupt.client:inrupt-client-rdf-legacy</classpathDependencyExclude>
213211
</classpathDependencyExcludes>
214212
</configuration>
215213
</execution>
@@ -220,11 +218,13 @@
220218
<goal>test</goal>
221219
</goals>
222220
<configuration>
221+
<excludes>
222+
<exclude>**/*RDF4JTest.java</exclude>
223+
</excludes>
223224
<classpathDependencyExcludes>
224225
<classpathDependencyExclude>com.inrupt.client:inrupt-client-httpclient</classpathDependencyExclude>
225226
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jackson</classpathDependencyExclude>
226227
<classpathDependencyExclude>com.inrupt.client:inrupt-client-rdf4j</classpathDependencyExclude>
227-
<classpathDependencyExclude>com.inrupt.client:inrupt-client-rdf-legacy</classpathDependencyExclude>
228228
</classpathDependencyExcludes>
229229
</configuration>
230230
</execution>
@@ -235,11 +235,81 @@
235235
<goal>test</goal>
236236
</goals>
237237
<configuration>
238+
<excludes>
239+
<exclude>**/*RDF4JTest.java</exclude>
240+
</excludes>
238241
<classpathDependencyExcludes>
239242
<classpathDependencyExclude>com.inrupt.client:inrupt-client-httpclient</classpathDependencyExclude>
240243
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jackson</classpathDependencyExclude>
241244
<classpathDependencyExclude>com.inrupt.client:inrupt-client-rdf4j</classpathDependencyExclude>
242-
<classpathDependencyExclude>com.inrupt.client:inrupt-client-rdf-legacy</classpathDependencyExclude>
245+
</classpathDependencyExcludes>
246+
</configuration>
247+
</execution>
248+
<execution>
249+
<id>httpclient-jackson-rdf4j-test</id>
250+
<phase>test</phase>
251+
<goals>
252+
<goal>test</goal>
253+
</goals>
254+
<configuration>
255+
<excludes>
256+
<exclude>**/*JenaTest.java</exclude>
257+
</excludes>
258+
<classpathDependencyExcludes>
259+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-okhttp</classpathDependencyExclude>
260+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jsonb</classpathDependencyExclude>
261+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jena</classpathDependencyExclude>
262+
</classpathDependencyExcludes>
263+
</configuration>
264+
</execution>
265+
<execution>
266+
<id>okhttp-jackson-rdf4j-test</id>
267+
<phase>test</phase>
268+
<goals>
269+
<goal>test</goal>
270+
</goals>
271+
<configuration>
272+
<excludes>
273+
<exclude>**/*JenaTest.java</exclude>
274+
</excludes>
275+
<classpathDependencyExcludes>
276+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-httpclient</classpathDependencyExclude>
277+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jsonb</classpathDependencyExclude>
278+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jena</classpathDependencyExclude>
279+
</classpathDependencyExcludes>
280+
</configuration>
281+
</execution>
282+
<execution>
283+
<id>okhttp-jsonb-rdf4j-test</id>
284+
<phase>test</phase>
285+
<goals>
286+
<goal>test</goal>
287+
</goals>
288+
<configuration>
289+
<excludes>
290+
<exclude>**/*JenaTest.java</exclude>
291+
</excludes>
292+
<classpathDependencyExcludes>
293+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-httpclient</classpathDependencyExclude>
294+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jackson</classpathDependencyExclude>
295+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jena</classpathDependencyExclude>
296+
</classpathDependencyExcludes>
297+
</configuration>
298+
</execution>
299+
<execution>
300+
<id>httpclient-jsonb-rdf4j-test</id>
301+
<phase>test</phase>
302+
<goals>
303+
<goal>test</goal>
304+
</goals>
305+
<configuration>
306+
<excludes>
307+
<exclude>**/*JenaTest.java</exclude>
308+
</excludes>
309+
<classpathDependencyExcludes>
310+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-httpclient</classpathDependencyExclude>
311+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jackson</classpathDependencyExclude>
312+
<classpathDependencyExclude>com.inrupt.client:inrupt-client-jena</classpathDependencyExclude>
243313
</classpathDependencyExcludes>
244314
</configuration>
245315
</execution>
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
* Copyright Inrupt Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to use,
7+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8+
* Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
package com.inrupt.client.core;
22+
23+
import static java.nio.charset.StandardCharsets.UTF_8;
24+
import static java.util.concurrent.TimeUnit.SECONDS;
25+
import static org.awaitility.Awaitility.await;
26+
import static org.jose4j.jwx.HeaderParameterNames.TYPE;
27+
import static org.junit.jupiter.api.Assertions.*;
28+
29+
import com.inrupt.client.Client;
30+
import com.inrupt.client.ClientProvider;
31+
import com.inrupt.client.Request;
32+
import com.inrupt.client.Response;
33+
import com.inrupt.client.auth.Session;
34+
import com.inrupt.client.openid.OpenIdConfig;
35+
import com.inrupt.client.openid.OpenIdSession;
36+
37+
import java.io.IOException;
38+
import java.io.InputStream;
39+
import java.io.UncheckedIOException;
40+
import java.net.URI;
41+
import java.security.KeyPair;
42+
import java.time.Instant;
43+
import java.util.*;
44+
import java.util.concurrent.atomic.AtomicReference;
45+
46+
import org.apache.commons.io.IOUtils;
47+
import org.jose4j.jwk.PublicJsonWebKey;
48+
import org.jose4j.jws.AlgorithmIdentifiers;
49+
import org.jose4j.jws.JsonWebSignature;
50+
import org.jose4j.jwt.JwtClaims;
51+
import org.jose4j.lang.JoseException;
52+
import org.jose4j.lang.UncheckedJoseException;
53+
import org.junit.jupiter.api.AfterAll;
54+
import org.junit.jupiter.api.BeforeAll;
55+
import org.junit.jupiter.api.Test;
56+
57+
class DefaultClientNonRdfTest {
58+
59+
private static final String WEBID = "https://id.example/username";
60+
private static final String SUB = "username";
61+
private static final String ISS = "https://iss.example";
62+
private static final String AZP = "https://app.example";
63+
private static final MockHttpService mockHttpServer = new MockHttpService();
64+
private static final Client client = ClientProvider.getClient();
65+
private static final AtomicReference<String> baseUri = new AtomicReference<>();
66+
67+
@BeforeAll
68+
static void setup() {
69+
baseUri.set(mockHttpServer.start());
70+
}
71+
72+
@AfterAll
73+
static void teardown() {
74+
mockHttpServer.stop();
75+
}
76+
77+
@Test
78+
void clientLoader() {
79+
assertNotNull(client);
80+
}
81+
82+
@Test
83+
void testSendOfStringAsync() {
84+
final Request request = Request.newBuilder()
85+
.uri(URI.create(baseUri.get() + "/file"))
86+
.GET()
87+
.build();
88+
89+
final Response<String> response = client.send(request, Response.BodyHandlers.ofString())
90+
.toCompletableFuture().join();
91+
92+
assertEquals(200, response.statusCode());
93+
assertTrue(response.body().contains("Julie C. Sparks and David Widger"));
94+
}
95+
96+
@Test
97+
void testOfStringPublisherOpenidSession() {
98+
final Map<String, Object> claims = new HashMap<>();
99+
claims.put("webid", WEBID);
100+
claims.put("sub", SUB);
101+
claims.put("iss", ISS);
102+
claims.put("azp", AZP);
103+
final String token = generateIdToken(claims);
104+
105+
final Request request = Request.newBuilder()
106+
.uri(URI.create(baseUri.get() + "/postStringContainer/"))
107+
.header("Content-Type", "text/plain")
108+
.POST(Request.BodyPublishers.ofString("Test String 1"))
109+
.build();
110+
111+
final PublicJsonWebKey jwk = getDpopKey("/rsa-key.json");
112+
final OpenIdConfig config = new OpenIdConfig();
113+
config.setProofKeyPairs(Collections.singletonMap("RS256",
114+
new KeyPair(jwk.getPublicKey(), jwk.getPrivateKey())));
115+
116+
final Response<Void> response = client.session(OpenIdSession.ofIdToken(token, config))
117+
.send(request, Response.BodyHandlers.discarding())
118+
.toCompletableFuture().join();
119+
120+
assertEquals(201, response.statusCode());
121+
}
122+
123+
@Test
124+
void testOfStringPublisherUmaAnonSession() {
125+
final Request request = Request.newBuilder()
126+
.uri(URI.create(baseUri.get() + "/postStringContainer/"))
127+
.header("Content-Type", "text/plain")
128+
.POST(Request.BodyPublishers.ofString("Test String 1"))
129+
.build();
130+
131+
final Response<Void> response = client.session(Session.anonymous())
132+
.send(request, Response.BodyHandlers.discarding())
133+
.toCompletableFuture().join();
134+
135+
assertEquals(401, response.statusCode());
136+
}
137+
138+
@Test
139+
void testNullSession() {
140+
assertThrows(NullPointerException.class, () ->
141+
client.session(null));
142+
}
143+
144+
@Test
145+
void testSessionExpiredIdToken() {
146+
final Map<String, Object> claims = new HashMap<>();
147+
claims.put("webid", WEBID);
148+
claims.put("sub", SUB);
149+
claims.put("iss", ISS);
150+
claims.put("azp", AZP);
151+
claims.put("exp", Instant.now().plusSeconds(2).getEpochSecond());
152+
claims.put("iat", Instant.now().minusSeconds(61).getEpochSecond());
153+
154+
final String token = generateIdToken(claims);
155+
final OpenIdConfig config = new OpenIdConfig();
156+
config.setExpGracePeriodSecs(0);
157+
final Session s = OpenIdSession.ofIdToken(token, config);
158+
159+
// Wait for the token to expire
160+
await().atMost(5, SECONDS).until(() -> !s.getCredential(OpenIdSession.ID_TOKEN, null).isPresent());
161+
162+
final Request request = Request.newBuilder()
163+
.uri(URI.create(baseUri.get() + "/postStringContainer/"))
164+
.header("Content-Type", "text/plain")
165+
.POST(Request.BodyPublishers.ofString("Test String 1"))
166+
.build();
167+
168+
final Response<Void> response = client.session(s)
169+
.send(request, Response.BodyHandlers.discarding())
170+
.toCompletableFuture().join();
171+
172+
assertEquals(401, response.statusCode());
173+
}
174+
175+
@Test
176+
void testSendRequestImage() {
177+
final Request request = Request.newBuilder()
178+
.uri(URI.create(baseUri.get() + "/solid.png"))
179+
.GET()
180+
.build();
181+
182+
final Response<byte[]> response = client.send(request, Response.BodyHandlers.ofByteArray())
183+
.toCompletableFuture().join();
184+
185+
assertEquals(200, response.statusCode());
186+
}
187+
188+
static PublicJsonWebKey getDpopKey(final String resource) {
189+
try (final InputStream stream = DefaultClientNonRdfTest.class.getResourceAsStream(resource)) {
190+
final String jwks = IOUtils.toString(stream, UTF_8);
191+
return PublicJsonWebKey.Factory.newPublicJwk(jwks);
192+
} catch (final IOException ex) {
193+
throw new UncheckedIOException("Unable to read JWK", ex);
194+
} catch (final JoseException ex) {
195+
throw new UncheckedJoseException("Unable to generate DPoP token", ex);
196+
}
197+
}
198+
199+
static String generateIdToken(final Map<String, Object> claims) {
200+
try (final InputStream resource = DefaultClientNonRdfTest.class.getResourceAsStream("/signing-key.json")) {
201+
final String jwks = IOUtils.toString(resource, UTF_8);
202+
final PublicJsonWebKey jwk = PublicJsonWebKey.Factory
203+
.newPublicJwk(jwks);
204+
205+
final JsonWebSignature jws = new JsonWebSignature();
206+
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256);
207+
jws.setHeader(TYPE, "JWT");
208+
jws.setKey(jwk.getPrivateKey());
209+
final JwtClaims jwtClaims = new JwtClaims();
210+
jwtClaims.setJwtId(UUID.randomUUID().toString());
211+
jwtClaims.setExpirationTimeMinutesInTheFuture(5);
212+
jwtClaims.setIssuedAtToNow();
213+
// override/set claims
214+
claims.entrySet().forEach(entry -> jwtClaims.setClaim(entry.getKey(), entry.getValue()));
215+
jws.setPayload(jwtClaims.toJson());
216+
217+
return jws.getCompactSerialization();
218+
} catch (final IOException ex) {
219+
throw new UncheckedIOException("Unable to read JWK", ex);
220+
} catch (final JoseException ex) {
221+
throw new UncheckedJoseException("Unable to generate DPoP token", ex);
222+
}
223+
}
224+
}

0 commit comments

Comments
 (0)