Skip to content

Commit

Permalink
chore: Various code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Oct 22, 2023
1 parent 80ffbcb commit a264694
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 44 deletions.
5 changes: 3 additions & 2 deletions src/main/java/foundation/identity/did/DID.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -71,7 +72,7 @@ public void postBranch(int offset, int length) {
if (keepParseTree) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ast.display(new PrintStream(byteArrayOutputStream));
parseTree = new String(byteArrayOutputStream.toByteArray());
parseTree = byteArrayOutputStream.toString(StandardCharsets.UTF_8);
}

String methodName = parsedStrings[0] == null ? null : parsedStrings[0];
Expand Down Expand Up @@ -198,7 +199,7 @@ public int hashCode() {
@Override
public boolean equals(Object obj) {

if (obj == null || ! (obj instanceof DID)) return false;
if (! (obj instanceof DID)) return false;
if (obj == this) return true;

return this.didString.equals(((DID) obj).didString);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/foundation/identity/did/DIDDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public B service(Service service) {
}

public static Builder<? extends Builder<?>> builder() {
return new Builder(new DIDDocument());
return new Builder<>(new DIDDocument());
}

public static DIDDocument fromJsonObject(Map<String, Object> jsonObject) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/foundation/identity/did/DIDURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DIDURL {
private String path;
private String query;
private String fragment;
private Map<String, String> parameters = new HashMap<String, String> ();
private Map<String, String> parameters;
private String parseTree;

DIDURL(String didUrlString, DID did, String path, String query, Map<String, String> parameters, String fragment, String parseTree) {
Expand Down Expand Up @@ -105,7 +105,7 @@ public void postBranch(int offset, int length) {
if (keepParseTree) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ast.display(new PrintStream(byteArrayOutputStream));
parseTree = new String(byteArrayOutputStream.toByteArray());
parseTree = byteArrayOutputStream.toString(StandardCharsets.UTF_8);
}

String methodName = parsedStrings[0] == null ? null : parsedStrings[0];
Expand Down Expand Up @@ -171,7 +171,7 @@ public JsonObject toJsonObject(boolean addParseTree) {
jsonObjectBuilder = jsonObjectBuilder
.add("didUrlString", this.getDidUrlString() == null ? JsonValue.NULL : Json.createValue(this.getDidUrlString()))
.add("did", this.getDid() == null ? JsonValue.NULL : this.getDid().toJsonObject(addParseTree))
.add("parameters", this.getParameters() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap<String, Object>(this.getParameters())).build())
.add("parameters", this.getParameters() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap<>(this.getParameters())).build())
.add("path", this.getPath() == null ? JsonValue.NULL : Json.createValue(this.getPath()))
.add("query", this.getQuery() == null ? JsonValue.NULL : Json.createValue(this.getQuery()))
.add("fragment", this.getFragment() == null ? JsonValue.NULL : Json.createValue(this.getFragment()));
Expand Down Expand Up @@ -283,7 +283,7 @@ public int hashCode() {
@Override
public boolean equals(Object obj) {

if (obj == null || ! (obj instanceof DIDURL)) return false;
if (! (obj instanceof DIDURL)) return false;
if (obj == this) return true;

return this.didUrlString.equals(((DIDURL) obj).didUrlString);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/foundation/identity/did/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public B serviceEndpoint(Object serviceEndpoint) {
}

public static Builder<? extends Builder<?>> builder() {
return new Builder(new Service());
return new Builder<>(new Service());
}

public static Service fromJsonObject(Map<String, Object> jsonObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public B publicKeyJwk(Map<String, Object> publicKeyJwk) {
}

public static Builder<? extends Builder<?>> builder() {
return new Builder(new VerificationMethod());
return new Builder<>(new VerificationMethod());
}

public static VerificationMethod fromJsonObject(Map<String, Object> jsonObject) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/foundation/identity/did/jsonld/DIDContexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class DIDContexts {

Expand All @@ -28,15 +29,15 @@ public class DIDContexts {
CONTEXTS = new HashMap<>();

CONTEXTS.put(JSONLD_CONTEXT_W3_NS_DID_V1,
JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3-ns-did-v1.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3-ns-did-v1.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3_2019_DID_V1,
JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3-2019-did-v1.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3-2019-did-v1.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_DID_V1,
JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3id-did-v1.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3id-did-v1.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_DID_V011,
JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3id-did-v011.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3id-did-v011.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_VERESONE_V1,
JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3id-veresone-v1.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3id-veresone-v1.jsonld"))));

for (Map.Entry<URI, JsonDocument> context : CONTEXTS.entrySet()) {
context.getValue().setDocumentUrl(context.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package foundation.identity.did.parser;

import apg.Grammar;

import java.io.PrintStream;

public class DIDGrammar extends Grammar{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package foundation.identity.did.representations;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RepresentationSpecificEntries {

public static final List<String> JSONLD_REPRESENTATION_SPECIFIC_ENTRY_NAMES = Arrays.asList(
public static final List<String> JSONLD_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of(
"@context"
);

public static final List<String> JSON_REPRESENTATION_SPECIFIC_ENTRY_NAMES = Arrays.asList();
public static final List<String> JSON_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of();

public static final List<String> CBOR_REPRESENTATION_SPECIFIC_ENTRY_NAMES = Arrays.asList();
public static final List<String> CBOR_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of();

public static final Map<String, List<String>> REPRESENTATION_SPECIFIC_ENTRY_NAMES = new HashMap<>();
public static final List<String> ALL_REPRESENTATION_SPECIFIC_ENTRY_NAMES = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

public interface RepresentationConsumer {

public static class Result {
public record Result(Map<String, Object> didDocument,
Map<String, Map<String, Object>> representationSpecificEntries) {

public Result(Map<String, Object> didDocument, Map<String, Map<String, Object>> representationSpecificEntries) { this.didDocument = didDocument; this.representationSpecificEntries = representationSpecificEntries; }
public Map<String, Object> didDocument;
public Map<String, Map<String, Object>> representationSpecificEntries;
}

public static Result consume(byte[] representation, String mediaType) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private RepresentationConsumerCBOR() {
@Override
public RepresentationConsumer.Result consume(byte[] representation) throws IOException {
CBORObject cborObject = CBORObject.DecodeFromBytes(representation);
Map<String, Object> map = (Map<String, Object>) cborObject.ToObject(LinkedHashMap.class);
Map<String, Object> map = cborObject.ToObject(LinkedHashMap.class);
return this.detectRepresentationSpecificEntries(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

public interface RepresentationProducer {

public static class Result {
public record Result(String mediaType, byte[] representation) {

public Result(String mediaType, byte[] representation) { this.mediaType = mediaType; this.representation = representation; }
public String mediaType;
public byte[] representation;
}

public static RepresentationProducer.Result produce(Map<String, Object> didDocument, Map<String, Object> representationSpecificEntries, String mediaType) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private RepresentationProducerCBOR() {
public RepresentationProducer.Result produce(Map<String, Object> didDocument, Map<String, Object> representationSpecificEntries) throws IOException {

RepresentationProducer.Result jsonResult = RepresentationProducerJSON.getInstance().produce(didDocument, representationSpecificEntries);
CBORObject cborObject = CBORObject.FromJSONBytes(jsonResult.representation);
CBORObject cborObject = CBORObject.FromJSONBytes(jsonResult.representation());
byte[] representation = cborObject.EncodeToBytes();
return new RepresentationProducer.Result(MEDIA_TYPE, representation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ private static void validateRun(Runnable runnable, String message) throws Illega

public static void validate(DIDDocument didDocument) throws IllegalStateException {

validateRun(() -> { validateTrue(didDocument.getJsonObject() != null); }, "Bad or missing JSON object.");
validateRun(() -> { validateTrue(didDocument.getContexts().size() > 0); }, "Bad or missing '@context'.");
validateRun(() -> { validateUrl(didDocument.getContexts().get(0)); }, "@context must be a valid URI: " + didDocument.getContexts().get(0));
validateRun(() -> { validateTrue(DIDDocument.DEFAULT_JSONLD_CONTEXTS[0].equals(didDocument.getContexts().get(0))); }, "First value of @context must be " + DIDDocument.DEFAULT_JSONLD_CONTEXTS[0] + ": " + didDocument.getContexts().get(0));
validateRun(() -> validateTrue(didDocument.getJsonObject() != null), "Bad or missing JSON object.");
validateRun(() -> validateTrue(! didDocument.getContexts().isEmpty()), "Bad or missing '@context'.");
validateRun(() -> validateUrl(didDocument.getContexts().get(0)), "@context must be a valid URI: " + didDocument.getContexts().get(0));
validateRun(() -> validateTrue(DIDDocument.DEFAULT_JSONLD_CONTEXTS[0].equals(didDocument.getContexts().get(0))), "First value of @context must be " + DIDDocument.DEFAULT_JSONLD_CONTEXTS[0] + ": " + didDocument.getContexts().get(0));
validateRun(() -> { if (didDocument.getId() != null) validateUrl(didDocument.getId()); }, "'id' must be a valid URI.");
}
}
9 changes: 6 additions & 3 deletions src/test/java/foundation/identity/did/DIDDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -39,15 +42,15 @@ private DIDDocument create() {

VerificationMethod verificationMethod = VerificationMethod.builder()
.id(URI.create("did:ex:123#key-1"))
.types(Arrays.asList("Ed25519VerificationKey2018"))
.types(List.of("Ed25519VerificationKey2018"))
.publicKeyBase58("000000000")
.build();

verificationMethods.add(verificationMethod);

// DID DOCUMENT services

List<Service> services = new ArrayList<Service>();
List<Service> services = new ArrayList<>();

Service service = Service.builder()
.type("DIDCommService")
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/foundation/identity/did/DIDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DIDTest {

Expand All @@ -21,8 +21,6 @@ public void testDID() throws Exception {
@Test
public void testInvalidDID() throws Exception {

Assertions.assertThrows(ParserException.class, () -> {
DID.fromString("did:ex");
});
Assertions.assertThrows(ParserException.class, () -> DID.fromString("did:ex"));
}
}
4 changes: 1 addition & 3 deletions src/test/java/foundation/identity/did/DIDURLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public void testBareDIDURL() throws Exception {
@Test
public void testInvalidDIDURL() throws Exception {

Assertions.assertThrows(ParserException.class, () -> {
DIDURL.fromString("did:ex/my/path?ab=1&cd=2#frag");
});
Assertions.assertThrows(ParserException.class, () -> DIDURL.fromString("did:ex/my/path?ab=1&cd=2#frag"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -34,7 +34,7 @@ private VerificationMethod create() {

return VerificationMethod.builder()
.id(URI.create("did:ex:123#key-1"))
.types(Arrays.asList("Ed25519VerificationKey2018"))
.types(List.of("Ed25519VerificationKey2018"))
.publicKeyBase58("000000000")
.build();
}
Expand Down

0 comments on commit a264694

Please sign in to comment.