diff --git a/api/src/main/java/com/oviva/spicegen/api/Consistency.java b/api/src/main/java/com/oviva/spicegen/api/Consistency.java index 028a2de..b0aa6c5 100644 --- a/api/src/main/java/com/oviva/spicegen/api/Consistency.java +++ b/api/src/main/java/com/oviva/spicegen/api/Consistency.java @@ -45,6 +45,9 @@ public static Consistency fullyConsistent() { } public static Consistency atLeastAsFreshAs(String consistencyToken) { + if (consistencyToken == null) { + return FULLY_CONSISTENT; + } return new Consistency(Requirement.AT_LEAST_AS_FRESH, consistencyToken); } diff --git a/api/src/main/java/com/oviva/spicegen/api/ObjectRef.java b/api/src/main/java/com/oviva/spicegen/api/ObjectRef.java index d6f1fcc..0a22fb2 100644 --- a/api/src/main/java/com/oviva/spicegen/api/ObjectRef.java +++ b/api/src/main/java/com/oviva/spicegen/api/ObjectRef.java @@ -1,11 +1,19 @@ package com.oviva.spicegen.api; +import java.util.Objects; + public interface ObjectRef { String kind(); String id(); static ObjectRef of(String kind, String id) { + if (kind == null) { + throw new IllegalArgumentException("kind must not be null"); + } + if (id == null) { + throw new IllegalArgumentException("id must not be null"); + } return new ObjectRef() { @Override public String kind() { @@ -16,6 +24,25 @@ public String kind() { public String id() { return id; } + + @Override + public String toString() { + return "%s:%s".formatted(kind, id); + } + + @Override + public int hashCode() { + return Objects.hash(kind, id); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ObjectRef ref)) { + return false; + } + + return Objects.equals(this.kind(), ref.kind()) && Objects.equals(this.id(), ref.id()); + } }; } } diff --git a/api/src/main/java/com/oviva/spicegen/api/Precondition.java b/api/src/main/java/com/oviva/spicegen/api/Precondition.java index f530d4c..c3b5def 100644 --- a/api/src/main/java/com/oviva/spicegen/api/Precondition.java +++ b/api/src/main/java/com/oviva/spicegen/api/Precondition.java @@ -46,12 +46,12 @@ public static final class Builder { private Builder() {} - public Builder withCondition(Condition val) { + public Builder condition(Condition val) { condition = val; return this; } - public Builder withFilter(RelationshipFilter val) { + public Builder filter(RelationshipFilter val) { filter = val; return this; } diff --git a/api/src/main/java/com/oviva/spicegen/api/RelationshipFilter.java b/api/src/main/java/com/oviva/spicegen/api/RelationshipFilter.java index ba564d7..423cb04 100644 --- a/api/src/main/java/com/oviva/spicegen/api/RelationshipFilter.java +++ b/api/src/main/java/com/oviva/spicegen/api/RelationshipFilter.java @@ -81,17 +81,17 @@ public static final class Builder { private Builder() {} - public Builder withSubjectKind(String val) { + public Builder subjectKind(String val) { subjectKind = val; return this; } - public Builder withSubjectId(String val) { + public Builder subjectId(String val) { subjectId = val; return this; } - public Builder withRelation(String val) { + public Builder relation(String val) { relation = val; return this; } @@ -110,22 +110,22 @@ public static final class Builder { private Builder() {} - public Builder withResourceKind(String val) { + public Builder resourceKind(String val) { resourceKind = val; return this; } - public Builder withResourceId(String val) { + public Builder resourceId(String val) { resourceId = val; return this; } - public Builder withRelation(String val) { + public Builder relation(String val) { relation = val; return this; } - public Builder withSubjectFilter(SubjectFilter val) { + public Builder subjectFilter(SubjectFilter val) { subjectFilter = val; return this; } diff --git a/api/src/main/java/com/oviva/spicegen/api/SubjectRef.java b/api/src/main/java/com/oviva/spicegen/api/SubjectRef.java index aeabb45..43b1e0c 100644 --- a/api/src/main/java/com/oviva/spicegen/api/SubjectRef.java +++ b/api/src/main/java/com/oviva/spicegen/api/SubjectRef.java @@ -1,5 +1,7 @@ package com.oviva.spicegen.api; +import java.util.Objects; + public interface SubjectRef { String kind(); @@ -16,6 +18,28 @@ public String kind() { public String id() { return o.id(); } + + @Override + public int hashCode() { + return Objects.hash(o); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof SubjectRef that)) { + return false; + } + return Objects.equals(this.kind(), that.kind()) && Objects.equals(this.id(), that.id()); + } + + @Override + public String toString() { + if (o == null) { + return ""; + } + + return o.toString(); + } }; } } diff --git a/api/src/main/java/com/oviva/spicegen/api/UpdateRelationship.java b/api/src/main/java/com/oviva/spicegen/api/UpdateRelationship.java index 184592e..dee938b 100644 --- a/api/src/main/java/com/oviva/spicegen/api/UpdateRelationship.java +++ b/api/src/main/java/com/oviva/spicegen/api/UpdateRelationship.java @@ -22,13 +22,13 @@ private UpdateRelationship( public static UpdateRelationship ofUpdate( ObjectRef resource, String relation, ObjectRef subject) { return new UpdateRelationship( - resource, relation, new ObjectRefSubject(subject), Operation.UPDATE); + resource, relation, SubjectRef.ofObject(subject), Operation.UPDATE); } public static UpdateRelationship ofDelete( ObjectRef resource, String relation, ObjectRef subject) { return new UpdateRelationship( - resource, relation, new ObjectRefSubject(subject), Operation.DELETE); + resource, relation, SubjectRef.ofObject(subject), Operation.DELETE); } public SubjectRef subject() { @@ -80,22 +80,4 @@ public enum Operation { DELETE } - - private static class ObjectRefSubject implements SubjectRef { - private final ObjectRef subject; - - private ObjectRefSubject(ObjectRef subject) { - this.subject = subject; - } - - @Override - public String kind() { - return subject.kind(); - } - - @Override - public String id() { - return subject.id(); - } - } } diff --git a/api/src/test/java/com/oviva/spicegen/api/CheckPermissionTest.java b/api/src/test/java/com/oviva/spicegen/api/CheckPermissionTest.java new file mode 100644 index 0000000..3729588 --- /dev/null +++ b/api/src/test/java/com/oviva/spicegen/api/CheckPermissionTest.java @@ -0,0 +1,74 @@ +package com.oviva.spicegen.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import org.junit.jupiter.api.Test; + +public class CheckPermissionTest { + + @Test + public void test_build_success() { + + var namespace = "tenant"; + var id = "9392"; + var consistencyToken = "t0ken"; + var permission = "write"; + + var objectRef = ObjectRef.of(namespace, id); + var subjectRef = SubjectRef.ofObject(ObjectRef.of(namespace, id)); + + var checkPermission = + CheckPermission.newBuilder() + .consistency(Consistency.atLeastAsFreshAs(consistencyToken)) + .permission(permission) + .resource(objectRef) + .subject(subjectRef) + .build(); + + assertEquals(checkPermission.consistency().consistencyToken(), consistencyToken); + assertEquals(checkPermission.permission(), permission); + assertEquals(checkPermission.resource(), objectRef); + assertEquals(checkPermission.subject(), subjectRef); + } + + @Test + void of_hashCode() { + var h1 = createCheck("17").hashCode(); + var h2 = createCheck("17").hashCode(); + + assertEquals(h1, h2); + } + + private CheckPermission createCheck(String subjectId) { + + return CheckPermission.newBuilder() + .permission("test") + .resource(ObjectRef.of("tenant", "1")) + .subject(SubjectRef.ofObject(ObjectRef.of("user", subjectId))) + .build(); + } + + @Test + void of_equals_same() { + var c1 = createCheck("1"); + + assertEquals(c1, c1); + } + + @Test + void of_equals() { + var c1 = createCheck("1"); + var c2 = createCheck("1"); + + assertEquals(c1, c2); + } + + @Test + void of_equals_notEqual() { + var c1 = createCheck("3"); + var c2 = createCheck("4"); + + assertNotEquals(c1, c2); + } +} diff --git a/api/src/test/java/com/oviva/spicegen/api/SubjectRefTest.java b/api/src/test/java/com/oviva/spicegen/api/SubjectRefTest.java new file mode 100644 index 0000000..bdc37ba --- /dev/null +++ b/api/src/test/java/com/oviva/spicegen/api/SubjectRefTest.java @@ -0,0 +1,65 @@ +package com.oviva.spicegen.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.UUID; +import org.junit.jupiter.api.Test; + +class SubjectRefTest { + + @Test + void ofUser() { + var uuid = UUID.fromString("20162b05-fbc5-4567-853f-7ad90fc29d25"); + var user = SubjectRef.ofObject(ObjectRef.of("user", uuid.toString())); + + assertEquals(user.id(), uuid.toString()); + assertEquals(user.kind(), "user"); + } + + @Test + void ofUuid() { + var namespace = "tenant"; + var uuid = UUID.fromString("c0fe2b05-fbc5-4567-853f-7ad90fc29d25"); + var user = SubjectRef.ofObject(ObjectRef.of(namespace, uuid.toString())); + + assertEquals(user.id(), uuid.toString()); + assertEquals(user.kind(), namespace); + } + + @Test + void ofUuid_nullId() { + assertThrows( + IllegalArgumentException.class, () -> SubjectRef.ofObject(ObjectRef.of("anotherns", null))); + } + + @Test + void of() { + var namespace = "tenant"; + var id = "9392"; + var user = SubjectRef.ofObject(ObjectRef.of(namespace, id)); + + assertEquals(user.id(), id); + assertEquals(user.kind(), namespace); + } + + @Test + void of_nullId() { + assertThrows( + IllegalArgumentException.class, () -> SubjectRef.ofObject(ObjectRef.of("somens", null))); + } + + @Test + void of_nullNamespace() { + assertThrows( + IllegalArgumentException.class, () -> SubjectRef.ofObject(ObjectRef.of(null, "32"))); + } + + @Test + void equals() { + var a = SubjectRef.ofObject(ObjectRef.of("a", "1")); + var b = SubjectRef.ofObject(ObjectRef.of("a", "1")); + + assertEquals(a, b); + } +} diff --git a/api/src/test/java/com/oviva/spicegen/api/UpdateRelationshipTest.java b/api/src/test/java/com/oviva/spicegen/api/UpdateRelationshipTest.java new file mode 100644 index 0000000..8c9d5b5 --- /dev/null +++ b/api/src/test/java/com/oviva/spicegen/api/UpdateRelationshipTest.java @@ -0,0 +1,147 @@ +package com.oviva.spicegen.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.UUID; +import org.junit.jupiter.api.Test; + +class UpdateRelationshipTest { + + private static final String TENANT = "tenant"; + private static final String TENANT_ID = "8288"; + + private static final String DOCUMENT = "document"; + private static final String DOCUMENT_ID = "138"; + private static final String ADMINISTRATOR = "administrator"; + private static final String NAMESPACE_ID = "9392"; + + @Test + void test_ofUpdate() { + + var resource = ObjectRef.of(TENANT, NAMESPACE_ID); + var subject = ObjectRef.of(TENANT, NAMESPACE_ID); + + var updateRelationship = UpdateRelationship.ofUpdate(resource, ADMINISTRATOR, subject); + + assertEquals(updateRelationship.operation(), UpdateRelationship.Operation.UPDATE); + assertEquals(updateRelationship.relation(), ADMINISTRATOR); + assertEquals(updateRelationship.resource(), resource); + assertEquals(updateRelationship.subject(), SubjectRef.ofObject(subject)); + } + + @Test + void test_ofDelete() { + + var resource = ObjectRef.of(TENANT, NAMESPACE_ID); + var subject = ObjectRef.of(TENANT, NAMESPACE_ID); + + var updateRelationship = UpdateRelationship.ofDelete(resource, ADMINISTRATOR, subject); + + assertEquals(updateRelationship.operation(), UpdateRelationship.Operation.DELETE); + assertEquals(updateRelationship.relation(), ADMINISTRATOR); + assertEquals(updateRelationship.resource(), resource); + assertEquals(updateRelationship.subject(), SubjectRef.ofObject(subject)); + } + + @Test + void of_hashCode() { + var h1 = + UpdateRelationship.ofUpdate( + ObjectRef.of(DOCUMENT, DOCUMENT_ID), ADMINISTRATOR, ObjectRef.of(TENANT, TENANT_ID)); + + var h2 = + UpdateRelationship.ofUpdate( + ObjectRef.of(DOCUMENT, DOCUMENT_ID), ADMINISTRATOR, ObjectRef.of(TENANT, TENANT_ID)); + + assertEquals(h1.hashCode(), h2.hashCode()); + } + + @Test + void of_equals_same() { + var u1 = + UpdateRelationship.ofUpdate( + ObjectRef.of(DOCUMENT, DOCUMENT_ID), ADMINISTRATOR, ObjectRef.of(TENANT, TENANT_ID)); + + assertEquals(u1, u1); + } + + @Test + void of_equals() { + var uuid = UUID.randomUUID(); + var u1 = createUpdateDocumentOwnedByUser(uuid); + var u2 = createUpdateDocumentOwnedByUser(uuid); + + assertEquals(u1, u2); + } + + @Test + void of_equals_notEqual() { + var u1 = createUpdateDocumentOwnedByUser(UUID.randomUUID()); + var u2 = createUpdateDocumentOwnedByUser(UUID.randomUUID()); + + assertNotEquals(u1, u2); + } + + @Test + void of_equals_notEqual_subjectKind() { + var subjectUuid = UUID.randomUUID(); + + var u1 = createUpdateDocumentOwnedByUser(subjectUuid); + var u2 = + UpdateRelationship.ofUpdate( + u1.resource(), u1.relation(), ObjectRef.of("another", u1.subject().id())); + + assertNotEquals(u1, u2); + } + + @Test + void of_equals_notEqual_subjectId() { + var subjectUuid = UUID.randomUUID(); + + var u1 = createUpdateDocumentOwnedByUser(subjectUuid); + var u2 = + UpdateRelationship.ofUpdate( + u1.resource(), u1.relation(), ObjectRef.of(u1.subject().kind(), "992982")); + + assertNotEquals(u1, u2); + } + + @Test + void of_equals_notEqual_null() { + var u1 = createUpdateDocumentOwnedByUser(UUID.randomUUID()); + var u2 = (UpdateRelationship) null; + + assertNotEquals(u1, u2); + } + + @Test + void of_equals_notEqualOtherClass() { + var u1 = createUpdateDocumentOwnedByUser(UUID.randomUUID()); + var u2 = new Object(); + + assertNotEquals(u1, u2); + } + + private UpdateRelationship createUpdateDocumentOwnedByUser(UUID subjectUuid) { + + return UpdateRelationship.ofUpdate( + ObjectRef.of("document", "1"), "owner", ObjectRef.of("user", subjectUuid.toString())); + } + + @Test + void of_toString() { + var obj = ObjectRef.of("document", "4"); + var sub = ObjectRef.of("user", "18"); + var relation = "owner"; + + var u = UpdateRelationship.ofUpdate(obj, relation, sub); + assertEquals("UPDATE(document:4#owner@user:18)", u.toString()); + } + + @Test + void of_toString_null() { + var u = UpdateRelationship.ofUpdate(null, null, null); + assertEquals("UPDATE(#@)", u.toString()); + } +} diff --git a/api/src/test/java/com/oviva/spicegen/api/UpdateRelationshipsTest.java b/api/src/test/java/com/oviva/spicegen/api/UpdateRelationshipsTest.java new file mode 100644 index 0000000..4633521 --- /dev/null +++ b/api/src/test/java/com/oviva/spicegen/api/UpdateRelationshipsTest.java @@ -0,0 +1,114 @@ +package com.oviva.spicegen.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; +import java.util.UUID; +import org.junit.jupiter.api.Test; + +public class UpdateRelationshipsTest { + + private static final String PATIENT_ID = UUID.randomUUID().toString().replaceAll("-", ""); + private static final String TENANT_ID = UUID.randomUUID().toString().replaceAll("-", ""); + private static final String PATIENT = "patient"; + private static final String TENANT = "tenant"; + private static final String USER = "user"; + private static final String ADMINISTRATOR = "administrator"; + + @Test + public void test_updateRelationships_ownBuilder() { + + var updates = getUpdateRelationshipList(); + var preconditions = getPreconditionList(); + + var updateRelationships = + UpdateRelationships.newBuilder().updates(updates).preconditions(preconditions).build(); + + assertEquals(updateRelationships.updates(), updates); + assertEquals(updateRelationships.preconditions(), preconditions); + } + + @Test + public void test_updateRelationshipsBuilder() { + var updates = getUpdateRelationshipList(); + var preconditions = getPreconditionList(); + + var updateRelationships = + UpdateRelationships.UpdateRelationshipsBuilder.newBuilder() + .updates(updates) + .preconditions(preconditions) + .build(); + + assertEquals(updateRelationships.updates(), updates); + assertEquals(updateRelationships.preconditions(), preconditions); + } + + @Test + public void test_updateRelationshipsBuilder_withAddOperations() { + + var userId = UUID.randomUUID(); + var subject = ObjectRef.of("user", userId.toString()); + var resource = ObjectRef.of(TENANT, TENANT_ID); + + var precondition = + Precondition.mustNotMatch( + RelationshipFilter.newBuilder() + .resourceKind(TENANT) + .relation(PATIENT) + .subjectFilter( + RelationshipFilter.SubjectFilter.newBuilder() + .subjectKind(USER) + .subjectId(userId.toString()) + .build()) + .build()); + + var updateRelationship = UpdateRelationship.ofUpdate(resource, ADMINISTRATOR, subject); + + var updateRelationships = + UpdateRelationships.UpdateRelationshipsBuilder.newBuilder() + .precondition(precondition) + .update(updateRelationship) + .build(); + + assertEquals(updateRelationships.updates().size(), 1); + assertEquals(updateRelationships.updates().get(0), updateRelationship); + assertEquals(updateRelationships.preconditions().size(), 1); + assertEquals(updateRelationships.preconditions().get(0), precondition); + } + + private List getPreconditionList() { + + var subjectFilter = + RelationshipFilter.SubjectFilter.newBuilder() + .subjectKind(TENANT) + .subjectId(TENANT_ID) + .build(); + + var relationshipFilter = + RelationshipFilter.newBuilder() + .resourceKind(PATIENT) + .resourceId(PATIENT_ID) + .relation(TENANT) + .subjectFilter(subjectFilter) + .build(); + + var precondition = + Precondition.newBuilder() + .condition(Precondition.Condition.MUST_MATCH) + .filter(relationshipFilter) + .build(); + + return List.of(precondition); + } + + private List getUpdateRelationshipList() { + var id = "9392"; + + var resource = ObjectRef.of(TENANT, id); + var subject = ObjectRef.of(TENANT, id); + + var updateRelationship = UpdateRelationship.ofUpdate(resource, ADMINISTRATOR, subject); + + return List.of(updateRelationship); + } +} diff --git a/example/src/test/java/com/oviva/spicegen/example/ExampleTest.java b/example/src/test/java/com/oviva/spicegen/example/ExampleTest.java index 1cf8695..c4b6a7a 100644 --- a/example/src/test/java/com/oviva/spicegen/example/ExampleTest.java +++ b/example/src/test/java/com/oviva/spicegen/example/ExampleTest.java @@ -102,7 +102,8 @@ void example() { // EXAMPLE: checking permission assertTrue( permissionService.checkPermission( - document.checkRead(SubjectRef.ofObject(user), Consistency.atLeastAsFreshAs(consistencyToken)))); + document.checkRead( + SubjectRef.ofObject(user), Consistency.atLeastAsFreshAs(consistencyToken)))); } private String loadSchema() { diff --git a/model/pom.xml b/model/pom.xml index 69204bf..43e7a1a 100644 --- a/model/pom.xml +++ b/model/pom.xml @@ -40,11 +40,6 @@ junit-jupiter test - - org.hamcrest - hamcrest - test - diff --git a/model/src/main/resources/spicegen_darwin_amd64_v1 b/model/src/main/resources/spicegen_darwin_amd64_v1 index 07659b5..7640d83 100755 Binary files a/model/src/main/resources/spicegen_darwin_amd64_v1 and b/model/src/main/resources/spicegen_darwin_amd64_v1 differ diff --git a/model/src/main/resources/spicegen_darwin_arm64 b/model/src/main/resources/spicegen_darwin_arm64 index 7e3d3d0..1838cfc 100755 Binary files a/model/src/main/resources/spicegen_darwin_arm64 and b/model/src/main/resources/spicegen_darwin_arm64 differ diff --git a/model/src/main/resources/spicegen_linux_386 b/model/src/main/resources/spicegen_linux_386 index d1409c3..bcbbac1 100755 Binary files a/model/src/main/resources/spicegen_linux_386 and b/model/src/main/resources/spicegen_linux_386 differ diff --git a/model/src/main/resources/spicegen_linux_amd64_v1 b/model/src/main/resources/spicegen_linux_amd64_v1 index 858b484..97036f9 100755 Binary files a/model/src/main/resources/spicegen_linux_amd64_v1 and b/model/src/main/resources/spicegen_linux_amd64_v1 differ diff --git a/model/src/main/resources/spicegen_linux_arm64 b/model/src/main/resources/spicegen_linux_arm64 index aa41a01..5a06506 100755 Binary files a/model/src/main/resources/spicegen_linux_arm64 and b/model/src/main/resources/spicegen_linux_arm64 differ diff --git a/model/src/test/java/com/oviva/spicegen/parser/SpiceDbSchemaParserTest.java b/model/src/test/java/com/oviva/spicegen/parser/SpiceDbSchemaParserTest.java index ea001de..ee90be4 100644 --- a/model/src/test/java/com/oviva/spicegen/parser/SpiceDbSchemaParserTest.java +++ b/model/src/test/java/com/oviva/spicegen/parser/SpiceDbSchemaParserTest.java @@ -1,7 +1,5 @@ package com.oviva.spicegen.parser; -import static org.hamcrest.Matchers.*; - import java.nio.file.Path; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; diff --git a/parser/main.go b/parser/main.go index c1fbbeb..65e1229 100644 --- a/parser/main.go +++ b/parser/main.go @@ -36,7 +36,7 @@ func main() { out := os.Stdout if outPath != "-" { - f, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + f, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644) if err != nil { log.Fatal(err) } diff --git a/pom.xml b/pom.xml index dbce9bb..0492e90 100644 --- a/pom.xml +++ b/pom.xml @@ -69,7 +69,6 @@ 1.7.36 - 2.2 2.17.0 5.10.2 ${mockito.version} @@ -150,12 +149,6 @@ ${jupiter.version} test - - org.hamcrest - hamcrest - ${hamcrest.version} - test - org.mockito mockito-core diff --git a/spicedb-binding/pom.xml b/spicedb-binding/pom.xml index d9e808c..6ba783a 100644 --- a/spicedb-binding/pom.xml +++ b/spicedb-binding/pom.xml @@ -43,11 +43,6 @@ junit-jupiter test - - org.hamcrest - hamcrest - test - org.mockito mockito-core diff --git a/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/SpiceDbPermissionServiceBuilderTest.java b/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/SpiceDbPermissionServiceBuilderTest.java index 8c8624d..15fb96d 100644 --- a/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/SpiceDbPermissionServiceBuilderTest.java +++ b/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/SpiceDbPermissionServiceBuilderTest.java @@ -1,7 +1,6 @@ package com.oviva.spicegen.spicedbbinding; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertTrue; import com.authzed.api.v1.PermissionsServiceGrpc; import com.authzed.grpcutil.BearerToken; @@ -28,6 +27,6 @@ public void test_spiceDbPermissionServiceBuilder() { .permissionsBlockingStub(permissionsService) .build(); - assertThat(svc, instanceOf(PermissionService.class)); + assertTrue(svc instanceof PermissionService); } } diff --git a/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/internal/CreateRelationshipUpdateToSpiceDBUpdateRelationshipMapperTest.java b/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/internal/CreateRelationshipUpdateToSpiceDBUpdateRelationshipMapperTest.java index 2c7787f..2a8889d 100644 --- a/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/internal/CreateRelationshipUpdateToSpiceDBUpdateRelationshipMapperTest.java +++ b/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/internal/CreateRelationshipUpdateToSpiceDBUpdateRelationshipMapperTest.java @@ -1,8 +1,7 @@ package com.oviva.spicegen.spicedbbinding.internal; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import com.authzed.api.v1.Core; import com.oviva.spicegen.api.ObjectRef; @@ -28,11 +27,11 @@ public void test_mapper_withUpdateOperation() { var updateRelationship = UpdateRelationship.ofUpdate(resource, ADMINISTRATOR, subject); var map = mapper.map(updateRelationship); - assertThat(map.getOperation(), equalTo(Core.RelationshipUpdate.Operation.OPERATION_TOUCH)); - assertThat(map.getRelationship(), notNullValue()); - assertThat(map.getRelationship().getRelation(), equalTo(ADMINISTRATOR)); - assertThat(map.getRelationship().getResource().getObjectId(), equalTo(ID)); - assertThat(map.getRelationship().getResource().getObjectType(), equalTo(TENANT)); + assertEquals(map.getOperation(), Core.RelationshipUpdate.Operation.OPERATION_TOUCH); + assertNotNull(map.getRelationship()); + assertEquals(map.getRelationship().getRelation(), ADMINISTRATOR); + assertEquals(map.getRelationship().getResource().getObjectId(), ID); + assertEquals(map.getRelationship().getResource().getObjectType(), TENANT); } @Test @@ -44,10 +43,10 @@ public void test_mapper_withDeleteOperation() { var updateRelationship = UpdateRelationship.ofDelete(resource, ADMINISTRATOR, subject); var map = mapper.map(updateRelationship); - assertThat(map.getOperation(), equalTo(Core.RelationshipUpdate.Operation.OPERATION_DELETE)); - assertThat(map.getRelationship(), notNullValue()); - assertThat(map.getRelationship().getRelation(), equalTo(ADMINISTRATOR)); - assertThat(map.getRelationship().getResource().getObjectId(), equalTo(ID)); - assertThat(map.getRelationship().getResource().getObjectType(), equalTo(TENANT)); + assertEquals(map.getOperation(), Core.RelationshipUpdate.Operation.OPERATION_DELETE); + assertNotNull(map.getRelationship()); + assertEquals(map.getRelationship().getRelation(), ADMINISTRATOR); + assertEquals(map.getRelationship().getResource().getObjectId(), ID); + assertEquals(map.getRelationship().getResource().getObjectType(), TENANT); } } diff --git a/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/internal/GrpcExceptionMapperTest.java b/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/internal/GrpcExceptionMapperTest.java index 3c4496f..e0c8cc0 100644 --- a/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/internal/GrpcExceptionMapperTest.java +++ b/spicedb-binding/src/test/java/com/oviva/spicegen/spicedbbinding/internal/GrpcExceptionMapperTest.java @@ -1,7 +1,7 @@ package com.oviva.spicegen.spicedbbinding.internal; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import com.oviva.spicegen.api.exceptions.*; import io.grpc.Status; @@ -16,24 +16,24 @@ public class GrpcExceptionMapperTest { public void test_map_permissionDenied() { var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.PERMISSION_DENIED)); - assertThat(exception, instanceOf(AuthorizationException.class)); - assertThat(exception.getMessage(), equalTo("permission denied")); + assertTrue(exception instanceof AuthorizationException); + assertEquals(exception.getMessage(), "permission denied"); } @Test public void test_map_unauthenticated() { var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.UNAUTHENTICATED)); - assertThat(exception, instanceOf(AuthenticationException.class)); - assertThat(exception.getMessage(), equalTo("unauthenticated")); + assertTrue(exception instanceof AuthenticationException); + assertEquals(exception.getMessage(), "unauthenticated"); } @Test public void test_map_alreadyExists() { var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.ALREADY_EXISTS)); - assertThat(exception, instanceOf(ConflictException.class)); - assertThat(exception.getMessage(), equalTo("already exists")); + assertTrue(exception instanceof ConflictException); + assertEquals(exception.getMessage(), "already exists"); } @Test @@ -41,23 +41,23 @@ public void test_map_invalidArgument() { var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.INVALID_ARGUMENT)); - assertThat(exception, instanceOf(ValidationException.class)); - assertThat(exception.getMessage(), equalTo("invalid argument")); + assertTrue(exception instanceof ValidationException); + assertEquals(exception.getMessage(), "invalid argument"); } @Test public void test_map_failedPrecondition() { var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.FAILED_PRECONDITION)); - assertThat(exception, instanceOf(ValidationException.class)); - assertThat(exception.getMessage(), equalTo("failed precondition")); + assertTrue(exception instanceof ValidationException); + assertEquals(exception.getMessage(), "failed precondition"); } @Test public void test_map_unexpectedValue() { var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.CANCELLED)); - assertThat(exception, instanceOf(ClientException.class)); - assertThat(exception.getMessage(), containsString("unexpected status:")); + assertTrue(exception instanceof ClientException); + assertTrue(exception.getMessage().startsWith("unexpected status:")); } }