Skip to content

Commit faba82d

Browse files
authored
JCL-134: no strings in tests & improved serializers (#95)
* improved vc tests * fixed some incorrect headers * fixed deserializer bugs in jackson * added jasonb Adapter fixes & improved tests in jackson too * fixed VC tests
1 parent e30dce2 commit faba82d

20 files changed

Lines changed: 863 additions & 286 deletions

File tree

‎jackson/src/main/java/com/inrupt/client/jackson/VCDeserializer.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.fasterxml.jackson.databind.JsonNode;
2727
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
2828
import com.fasterxml.jackson.databind.node.ArrayNode;
29-
import com.fasterxml.jackson.databind.node.ObjectNode;
3029
import com.inrupt.client.spi.VerifiableCredential;
3130

3231
import java.io.IOException;
@@ -88,7 +87,7 @@ public VerifiableCredential deserialize(final JsonParser jp, final Deserializati
8887
}
8988

9089
if (node.get("credentialSubject") != null) {
91-
final ObjectNode credentialSubject = (ObjectNode) node.get("credentialSubject");
90+
final JsonNode credentialSubject = node.get("credentialSubject");
9291
final Map<String, Object> finalCredentialSubject = new HashMap<>();
9392
credentialSubject.fields().forEachRemaining(field -> {
9493
finalCredentialSubject.put(field.getKey(), field.getValue());
@@ -97,7 +96,7 @@ public VerifiableCredential deserialize(final JsonParser jp, final Deserializati
9796
}
9897

9998
if (node.get("credentialStatus") != null) {
100-
final ObjectNode credentialStatus = (ObjectNode) node.path("credentialStatus");
99+
final JsonNode credentialStatus = node.path("credentialStatus");
101100
final Map<String, Object> finalCredentialStatus = new HashMap<>();
102101
credentialStatus.fields().forEachRemaining(field -> {
103102
finalCredentialStatus.put(field.getKey(), field.getValue());
@@ -106,7 +105,7 @@ public VerifiableCredential deserialize(final JsonParser jp, final Deserializati
106105
}
107106

108107
if (node.get("proof") != null) {
109-
final ObjectNode proof = (ObjectNode) node.get("proof");
108+
final JsonNode proof = node.get("proof");
110109
final Map<String, Object> finalProof = new HashMap<>();
111110
proof.fields().forEachRemaining(field -> {
112111
finalProof.put(field.getKey(), field.getValue());

‎jackson/src/main/java/com/inrupt/client/jackson/VCSerializer.java‎

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,68 @@ public void serialize(final VerifiableCredential vc, final JsonGenerator jgen,
4343

4444
jgen.writeStartObject();
4545

46-
jgen.writeFieldName("@context");
47-
jgen.writeStartArray();
48-
for (final String context: vc.context) {
49-
jgen.writeString(context);
46+
if (vc.context != null) {
47+
jgen.writeFieldName("@context");
48+
jgen.writeStartArray();
49+
for (final var context : vc.context) {
50+
jgen.writeString(context);
51+
}
52+
jgen.writeEndArray();
5053
}
51-
jgen.writeEndArray();
5254

53-
jgen.writeStringField("id", vc.id);
55+
if (vc.id != null) {
56+
jgen.writeStringField("id", vc.id);
57+
}
5458

55-
jgen.writeFieldName("type");
56-
jgen.writeStartArray();
57-
for (final String type: vc.type) {
58-
jgen.writeString(type);
59+
if (vc.type != null) {
60+
jgen.writeFieldName("type");
61+
jgen.writeStartArray();
62+
for (final var type : vc.type) {
63+
jgen.writeString(type);
64+
}
65+
jgen.writeEndArray();
5966
}
60-
jgen.writeEndArray();
6167

62-
jgen.writeStringField("issuer", vc.issuer);
68+
if (vc.issuer != null) {
69+
jgen.writeStringField("issuer", vc.issuer);
70+
}
6371

64-
jgen.writeStringField("issuanceDate", vc.issuanceDate.toString());
72+
if (vc.issuanceDate != null) {
73+
jgen.writeStringField("issuanceDate", vc.issuanceDate.toString());
74+
}
6575

66-
jgen.writeStringField("credentialSubject", vc.credentialSubject.toString());
76+
if (vc.credentialSubject != null) {
77+
jgen.writeFieldName("credentialSubject");
78+
jgen.writeStartObject();
79+
final var itCSubject = vc.credentialSubject.entrySet().iterator();
80+
while (itCSubject.hasNext()) {
81+
final var entry = itCSubject.next();
82+
jgen.writePOJOField(entry.getKey(), entry.getValue());
83+
}
84+
jgen.writeEndObject();
85+
}
6786

68-
jgen.writeStringField("credentialStatus", vc.credentialStatus.toString());
87+
if (vc.credentialStatus != null) {
88+
jgen.writeFieldName("credentialStatus");
89+
jgen.writeStartObject();
90+
final var itCStatus = vc.credentialStatus.entrySet().iterator();
91+
while (itCStatus.hasNext()) {
92+
final var entry = itCStatus.next();
93+
jgen.writePOJOField(entry.getKey(), entry.getValue());
94+
}
95+
jgen.writeEndObject();
96+
}
6997

70-
jgen.writeStringField("proof", vc.proof.toString());
98+
if (vc.proof != null) {
99+
jgen.writeFieldName("proof");
100+
jgen.writeStartObject();
101+
final var itProof = vc.proof.entrySet().iterator();
102+
while (itProof.hasNext()) {
103+
final var entry = itProof.next();
104+
jgen.writePOJOField(entry.getKey(), entry.getValue());
105+
}
106+
jgen.writeEndObject();
107+
}
71108

72109
jgen.writeEndObject();
73110

‎jackson/src/main/java/com/inrupt/client/jackson/VPDeserializer.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.fasterxml.jackson.databind.JsonNode;
2727
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
2828
import com.fasterxml.jackson.databind.node.ArrayNode;
29-
import com.fasterxml.jackson.databind.node.ObjectNode;
3029
import com.inrupt.client.spi.VerifiableCredential;
3130
import com.inrupt.client.spi.VerifiablePresentation;
3231

@@ -89,7 +88,7 @@ public VerifiablePresentation deserialize(final JsonParser jp, final Deserializa
8988
}
9089

9190
if (node.get("proof") != null) {
92-
final ObjectNode proof = (ObjectNode) node.get("proof");
91+
final JsonNode proof = node.get("proof");
9392
final Map<String, Object> finalProof = new HashMap<>();
9493
proof.fields().forEachRemaining(field -> {
9594
finalProof.put(field.getKey(), field.getValue());

‎jackson/src/main/java/com/inrupt/client/jackson/VPSerializer.java‎

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.fasterxml.jackson.core.JsonGenerator;
2424
import com.fasterxml.jackson.databind.SerializerProvider;
2525
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
26-
import com.inrupt.client.spi.VerifiableCredential;
2726
import com.inrupt.client.spi.VerifiablePresentation;
2827

2928
import java.io.IOException;
@@ -44,32 +43,51 @@ public void serialize(final VerifiablePresentation vp, final JsonGenerator jgen,
4443

4544
jgen.writeStartObject();
4645

47-
jgen.writeFieldName("@context");
48-
jgen.writeStartArray();
49-
for (final String context: vp.context) {
50-
jgen.writeString(context);
46+
if (vp.context != null) {
47+
jgen.writeFieldName("@context");
48+
jgen.writeStartArray();
49+
for (final var context : vp.context) {
50+
jgen.writeString(context);
51+
}
52+
jgen.writeEndArray();
5153
}
52-
jgen.writeEndArray();
5354

54-
jgen.writeStringField("id", vp.id);
55+
if (vp.id != null) {
56+
jgen.writeStringField("id", vp.id);
57+
}
5558

56-
jgen.writeFieldName("type");
57-
jgen.writeStartArray();
58-
for (final String type: vp.type) {
59-
jgen.writeString(type);
59+
if (vp.type != null) {
60+
jgen.writeFieldName("type");
61+
jgen.writeStartArray();
62+
for (final var type : vp.type) {
63+
jgen.writeString(type);
64+
}
65+
jgen.writeEndArray();
6066
}
61-
jgen.writeEndArray();
6267

63-
jgen.writeStringField("holder", vp.holder);
68+
if (vp.holder != null) {
69+
jgen.writeStringField("holder", vp.holder);
70+
}
6471

65-
jgen.writeFieldName("verifiableCredential");
66-
jgen.writeStartArray();
67-
for (final VerifiableCredential vc: vp.verifiableCredential) {
68-
jgen.writeObject(vc);
72+
if (vp.verifiableCredential != null) {
73+
jgen.writeFieldName("verifiableCredential");
74+
jgen.writeStartArray();
75+
for (final var vc : vp.verifiableCredential) {
76+
jgen.writePOJO(vc);
77+
}
78+
jgen.writeEndArray();
6979
}
70-
jgen.writeEndArray();
7180

72-
jgen.writeStringField("proof", vp.proof.toString());
81+
if (vp.proof != null) {
82+
jgen.writeFieldName("proof");
83+
jgen.writeStartObject();
84+
final var itProof = vp.proof.entrySet().iterator();
85+
while (itProof.hasNext()) {
86+
final var entry = itProof.next();
87+
jgen.writeObjectField(entry.getKey(), entry.getValue());
88+
}
89+
jgen.writeEndObject();
90+
}
7391

7492
jgen.writeEndObject();
7593

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2022 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.jackson;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
import com.inrupt.client.spi.JsonProcessor;
26+
import com.inrupt.client.spi.ServiceProvider;
27+
import com.inrupt.client.spi.VerifiableCredential;
28+
29+
import java.io.File;
30+
import java.io.FileInputStream;
31+
import java.io.FileOutputStream;
32+
import java.io.IOException;
33+
import java.nio.file.Files;
34+
import java.util.UUID;
35+
36+
import org.junit.jupiter.api.Test;
37+
38+
public class JacksonVCTest {
39+
40+
private static final JsonProcessor processor = ServiceProvider.getJsonProcessor();
41+
private static VerifiableCredential vc;
42+
private static VerifiableCredential vcCopy;
43+
44+
@Test
45+
void roundtripVC() throws IOException {
46+
try (final var res = JacksonVCTest.class.getResourceAsStream("/verifiableCredential.json")) {
47+
vc = processor.fromJson(res, VerifiableCredential.class);
48+
}
49+
50+
final var targetPath = new File("target").toPath();
51+
final var testFolderName = UUID.randomUUID().toString();
52+
final var testFolderPath = Files.createTempDirectory(targetPath, testFolderName);
53+
final var testFile = Files.createTempFile(testFolderPath, UUID.randomUUID().toString(), ".json");
54+
55+
try (final var out = new FileOutputStream(testFile.toString())) {
56+
processor.toJson(vc, out);
57+
}
58+
59+
try (final var in = new FileInputStream(testFile.toString())) {
60+
vcCopy = processor.fromJson(in, VerifiableCredential.class);
61+
}
62+
63+
assertEquals(vc.context, vcCopy.context);
64+
assertEquals(vc.id, vcCopy.id);
65+
assertEquals(vc.type, vcCopy.type);
66+
assertEquals(vc.issuer, vcCopy.issuer);
67+
assertEquals(vc.issuanceDate, vcCopy.issuanceDate);
68+
assertEquals(vc.expirationDate, vcCopy.expirationDate);
69+
assertEquals(vc.credentialSubject, vcCopy.credentialSubject);
70+
assertEquals(vc.credentialStatus, vcCopy.credentialStatus);
71+
assertEquals(vc.proof, vcCopy.proof);
72+
73+
Files.deleteIfExists(testFile);
74+
Files.deleteIfExists(testFolderPath);
75+
76+
}
77+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2022 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.jackson;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
import com.inrupt.client.spi.JsonProcessor;
26+
import com.inrupt.client.spi.ServiceProvider;
27+
import com.inrupt.client.spi.VerifiablePresentation;
28+
29+
import java.io.File;
30+
import java.io.FileInputStream;
31+
import java.io.FileOutputStream;
32+
import java.io.IOException;
33+
import java.nio.file.Files;
34+
import java.util.UUID;
35+
36+
import org.junit.jupiter.api.Test;
37+
38+
public class JacksonVPTest {
39+
40+
private static final JsonProcessor processor = ServiceProvider.getJsonProcessor();
41+
private static VerifiablePresentation vp;
42+
private static VerifiablePresentation vpCopy;
43+
44+
@Test
45+
void roundtripVP() throws IOException {
46+
try (final var res = JacksonVCTest.class.getResourceAsStream("/verifiablePresentation.json")) {
47+
vp = processor.fromJson(res, VerifiablePresentation.class);
48+
}
49+
50+
final var targetPath = new File("target").toPath();
51+
final var testFolderName = UUID.randomUUID().toString();
52+
final var testFolderPath = Files.createTempDirectory(targetPath, testFolderName);
53+
final var testFile = Files.createTempFile(testFolderPath, UUID.randomUUID().toString(), ".json");
54+
55+
try (final var out = new FileOutputStream(testFile.toString())) {
56+
processor.toJson(vp, out);
57+
}
58+
59+
try (final var in = new FileInputStream(testFile.toString())) {
60+
vpCopy = processor.fromJson(in, VerifiablePresentation.class);
61+
}
62+
63+
assertEquals(vp.context, vpCopy.context);
64+
assertEquals(vp.id, vpCopy.id);
65+
assertEquals(vp.type, vpCopy.type);
66+
assertEquals(vp.holder, vpCopy.holder);
67+
assertEquals(vp.verifiableCredential.size(), vpCopy.verifiableCredential.size());
68+
assertEquals(vp.proof, vpCopy.proof);
69+
70+
Files.deleteIfExists(testFile);
71+
Files.deleteIfExists(testFolderPath);
72+
73+
}
74+
}

0 commit comments

Comments
 (0)