Skip to content

Commit

Permalink
ARC-1700: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrichner-oviva committed May 23, 2024
1 parent c0ffeed commit a690802
Show file tree
Hide file tree
Showing 24 changed files with 496 additions and 80 deletions.
3 changes: 3 additions & 0 deletions api/src/main/java/com/oviva/spicegen/api/Consistency.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
27 changes: 27 additions & 0 deletions api/src/main/java/com/oviva/spicegen/api/ObjectRef.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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());
}
};
}
}
4 changes: 2 additions & 2 deletions api/src/main/java/com/oviva/spicegen/api/Precondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions api/src/main/java/com/oviva/spicegen/api/RelationshipFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
24 changes: 24 additions & 0 deletions api/src/main/java/com/oviva/spicegen/api/SubjectRef.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.oviva.spicegen.api;

import java.util.Objects;

public interface SubjectRef {
String kind();

Expand All @@ -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();
}
};
}
}
22 changes: 2 additions & 20 deletions api/src/main/java/com/oviva/spicegen/api/UpdateRelationship.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}
}
}
74 changes: 74 additions & 0 deletions api/src/test/java/com/oviva/spicegen/api/CheckPermissionTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
65 changes: 65 additions & 0 deletions api/src/test/java/com/oviva/spicegen/api/SubjectRefTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit a690802

Please sign in to comment.