Skip to content

Commit dbe8af9

Browse files
authored
Add tests for JSON objects (#2339)
1 parent b82b578 commit dbe8af9

File tree

6 files changed

+254
-0
lines changed

6 files changed

+254
-0
lines changed

solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ void testCustomHeaders() throws Exception {
129129
}).toCompletableFuture().join();
130130
}
131131

132+
@Test
133+
void testJsonResource() {
134+
final URI uri = URI.create(config.get("solid_resource_uri") + "/transaction");
135+
client.read(uri, Transaction.class).thenAccept(transaction -> {
136+
try (final Transaction t = transaction) {
137+
assertEquals("sample description", t.getDescription());
138+
assertEquals(TransactionType.CREDIT, t.getType());
139+
final var err = assertThrows(CompletionException.class, client.update(t).toCompletableFuture()::join);
140+
assertTrue(err.getCause() instanceof ForbiddenException);
141+
t.setType(TransactionType.DEBIT);
142+
t.setDescription("different description");
143+
assertDoesNotThrow(client.update(t).toCompletableFuture()::join);
144+
}
145+
}).toCompletableFuture().join();
146+
}
132147

133148
@Test
134149
void testGetPlaylist() throws IOException, InterruptedException {

solid/src/test/java/com/inrupt/client/solid/SolidMockHttpService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,26 @@ private void setupMocks() {
216216
.willReturn(aResponse()
217217
.withStatus(204)));
218218

219+
wireMockServer.stubFor(get(urlEqualTo("/transaction"))
220+
.withHeader("User-Agent", equalTo(USER_AGENT))
221+
.willReturn(aResponse()
222+
.withStatus(200)
223+
.withHeader("Content-Type", "application/json")
224+
.withBodyFile("transaction.json")));
225+
226+
wireMockServer.stubFor(put(urlEqualTo("/transaction"))
227+
.withHeader("User-Agent", equalTo(USER_AGENT))
228+
.withRequestBody(containing("DEBIT"))
229+
.withRequestBody(containing("different description"))
230+
.willReturn(aResponse()
231+
.withStatus(204)));
232+
233+
wireMockServer.stubFor(put(urlEqualTo("/transaction"))
234+
.withHeader("User-Agent", equalTo(USER_AGENT))
235+
.withRequestBody(containing("CREDIT"))
236+
.willReturn(aResponse()
237+
.withStatus(403)));
238+
219239
wireMockServer.stubFor(get(urlEqualTo("/binary"))
220240
.atPriority(1)
221241
.withHeader("User-Agent", equalTo(USER_AGENT))
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.solid;
22+
23+
import com.inrupt.client.spi.JsonService;
24+
import com.inrupt.client.spi.ServiceProvider;
25+
26+
import java.io.ByteArrayInputStream;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.io.UncheckedIOException;
31+
import java.net.URI;
32+
import java.time.Instant;
33+
34+
class Transaction extends SolidNonRDFSource {
35+
36+
private final JsonService jsonService;
37+
private final TransactionData data;
38+
39+
public Transaction(final URI identifier, final String contentType, final InputStream entity) {
40+
super(identifier, contentType, entity);
41+
42+
this.jsonService = ServiceProvider.getJsonService();
43+
try {
44+
this.data = jsonService.fromJson(super.getEntity(), TransactionData.class);
45+
} catch (IOException ex) {
46+
throw new UncheckedIOException("Unable to parse Transaction data", ex);
47+
}
48+
}
49+
50+
public String getId() {
51+
return data.getId();
52+
}
53+
54+
public void setId(final String id) {
55+
data.setId(id);
56+
}
57+
58+
public String getDescription() {
59+
return data.getDescription();
60+
}
61+
62+
public void setDescription(final String description) {
63+
data.setDescription(description);
64+
}
65+
66+
public Instant getDate() {
67+
return data.getDate();
68+
}
69+
70+
public void setDate(final Instant date) {
71+
data.setDate(date);
72+
}
73+
74+
public double getAmount() {
75+
return data.getAmount();
76+
}
77+
78+
public void setAmount(final double amount) {
79+
data.setAmount(amount);
80+
}
81+
82+
public String getCategory() {
83+
return data.getCategory();
84+
}
85+
86+
public void setCategory(final String category) {
87+
data.setCategory(category);
88+
}
89+
90+
public TransactionType getType() {
91+
return data.getType();
92+
}
93+
94+
public void setType(final TransactionType type) {
95+
data.setType(type);
96+
}
97+
98+
@Override
99+
public InputStream getEntity() throws IOException {
100+
try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
101+
jsonService.toJson(data, output);
102+
return new ByteArrayInputStream(output.toByteArray());
103+
}
104+
}
105+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.solid;
22+
23+
import java.time.Instant;
24+
25+
class TransactionData {
26+
27+
private String id;
28+
private String description;
29+
private Instant date;
30+
private double amount;
31+
private TransactionType type;
32+
private String category;
33+
34+
public String getId() {
35+
return id;
36+
}
37+
38+
public void setId(final String id) {
39+
this.id = id;
40+
}
41+
42+
public String getDescription() {
43+
return description;
44+
}
45+
46+
public void setDescription(final String description) {
47+
this.description = description;
48+
}
49+
50+
public Instant getDate() {
51+
return date;
52+
}
53+
54+
public void setDate(final Instant date) {
55+
this.date = date;
56+
}
57+
58+
public double getAmount() {
59+
return amount;
60+
}
61+
62+
public void setAmount(final double amount) {
63+
this.amount = amount;
64+
}
65+
66+
public TransactionType getType() {
67+
return type;
68+
}
69+
70+
public void setType(final TransactionType type) {
71+
this.type = type;
72+
}
73+
74+
public String getCategory() {
75+
return category;
76+
}
77+
78+
public void setCategory(final String category) {
79+
this.category = category;
80+
}
81+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.solid;
22+
23+
enum TransactionType {
24+
CREDIT, DEBIT
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "testid",
3+
"date": "2025-09-28T15:37:00Z",
4+
"description": "sample description",
5+
"amount": 20.12,
6+
"type": "CREDIT",
7+
"category": "groceries"
8+
}

0 commit comments

Comments
 (0)