Skip to content

Commit

Permalink
ARC-1807: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrichner-oviva committed Jun 16, 2024
1 parent 8a5cae1 commit bdb217e
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ public static Builder newBuilder() {
return new Builder();
}

public ObjectRef resource() {
return resource;
}

public String permission() {
return permission;
}

public SubjectRef subject() {
return subject;
}

public Consistency consistency() {
return consistency;
}

public static final class Builder implements CheckPermission.Builder {
private ObjectRef resource;
private String permission;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ private RelationshipFilterImpl(Builder builder) {
Optional.ofNullable(builder.subjectFilter));
}

public String resourceKind() {
return resourceKind;
}

public record SubjectFilterImpl(
String subjectKind, Optional<String> subjectId, Optional<String> relation)
implements SubjectFilter {
Expand All @@ -33,10 +29,6 @@ private SubjectFilterImpl(Builder builder) {
Optional.ofNullable(builder.relation));
}

public String subjectKind() {
return subjectKind;
}

public static final class Builder implements SubjectFilter.Builder {
private String subjectKind;
private String subjectId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ public static Builder newBuilder() {
return new Builder();
}

public List<UpdateRelationship> updates() {
return updates;
}

public List<Precondition> preconditions() {
return preconditions;
}

public static final class Builder implements UpdateRelationships.Builder {
private List<Precondition> preconditions = new ArrayList<>();
private List<UpdateRelationship> updates = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import org.junit.jupiter.api.Test;

public class CheckPermissionTest {
class CheckPermissionTest {

@Test
public void test_build_success() {
void test_build_success() {

var namespace = "tenant";
var id = "9392";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void ofUser() {
var user = SubjectRef.ofObject(ObjectRef.of("user", uuid.toString()));

assertEquals(user.id(), uuid.toString());
assertEquals(user.kind(), "user");
assertEquals("user", user.kind());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void test_ofUpdate() {

var updateRelationship = UpdateRelationship.ofUpdate(resource, ADMINISTRATOR, subject);

assertEquals(updateRelationship.operation(), UpdateRelationship.Operation.UPDATE);
assertEquals(updateRelationship.relation(), ADMINISTRATOR);
assertEquals(UpdateRelationship.Operation.UPDATE, updateRelationship.operation());
assertEquals(ADMINISTRATOR, updateRelationship.relation());
assertEquals(updateRelationship.resource(), resource);
assertEquals(updateRelationship.subject(), SubjectRef.ofObject(subject));
}
Expand All @@ -38,8 +38,8 @@ void test_ofDelete() {

var updateRelationship = UpdateRelationship.ofDelete(resource, ADMINISTRATOR, subject);

assertEquals(updateRelationship.operation(), UpdateRelationship.Operation.DELETE);
assertEquals(updateRelationship.relation(), ADMINISTRATOR);
assertEquals(UpdateRelationship.Operation.DELETE, updateRelationship.operation());
assertEquals(ADMINISTRATOR, updateRelationship.relation());
assertEquals(updateRelationship.resource(), resource);
assertEquals(updateRelationship.subject(), SubjectRef.ofObject(subject));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public void test_updateRelationshipsBuilder_withAddOperations() {
.update(updateRelationship)
.build();

assertEquals(updateRelationships.updates().size(), 1);
assertEquals(1, updateRelationships.updates().size());
assertEquals(updateRelationships.updates().get(0), updateRelationship);
assertEquals(updateRelationships.preconditions().size(), 1);
assertEquals(1, updateRelationships.preconditions().size());
assertEquals(updateRelationships.preconditions().get(0), precondition);
}

Expand Down
5 changes: 5 additions & 0 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.authzed.api</groupId>
<artifactId>authzed</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.authzed.api.v1.PermissionsServiceGrpc;
import com.authzed.api.v1.SchemaServiceGrpc;
import com.authzed.api.v1.SchemaServiceOuterClass;
import com.authzed.api.v1.WriteSchemaRequest;
import com.authzed.grpcutil.BearerToken;
import com.oviva.spicegen.api.*;
import com.oviva.spicegen.permissions.refs.DocumentRef;
Expand All @@ -26,7 +26,7 @@
import org.testcontainers.utility.DockerImageName;

@Testcontainers
public class ExampleTest {
class ExampleTest {

private static final int GRPC_PORT = 50051;
private static final String TOKEN = "t0ken";
Expand Down Expand Up @@ -75,8 +75,7 @@ private void updateSchema() {
var schemaService =
SchemaServiceGrpc.newBlockingStub(channel).withCallCredentials(new BearerToken(TOKEN));

schemaService.writeSchema(
SchemaServiceOuterClass.WriteSchemaRequest.newBuilder().setSchema(loadSchema()).build());
schemaService.writeSchema(WriteSchemaRequest.newBuilder().setSchema(loadSchema()).build());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
package com.oviva.spicegen.maven;

/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.oviva.spicegen.generator.Options;
import com.oviva.spicegen.generator.internal.SpiceDbClientGeneratorImpl;
import com.oviva.spicegen.parser.SpiceDbSchemaParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ private void loadParserBinary(Path parserDestination) {

private String determineTarget() {

var target = "%s_%s".formatted(determineOs(), determineArch());
return target;
return "%s_%s".formatted(determineOs(), determineArch());
}

private String determineArch() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,62 @@
package com.oviva.spicegen.spicedbbinding.internal;

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

import com.oviva.spicegen.api.exceptions.*;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import org.junit.jupiter.api.Test;

public class GrpcExceptionMapperTest {
class GrpcExceptionMapperTest {

private final GrpcExceptionMapper grpcExceptionMapper = new GrpcExceptionMapper();

@Test
public void test_map_permissionDenied() {
void test_map_permissionDenied() {
var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.PERMISSION_DENIED));

assertTrue(exception instanceof AuthorizationException);
assertEquals(exception.getMessage(), "permission denied");
assertInstanceOf(AuthorizationException.class, exception);
assertEquals("permission denied", exception.getMessage());
}

@Test
public void test_map_unauthenticated() {
void test_map_unauthenticated() {
var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.UNAUTHENTICATED));

assertTrue(exception instanceof AuthenticationException);
assertEquals(exception.getMessage(), "unauthenticated");
assertInstanceOf(AuthenticationException.class, exception);
assertEquals("unauthenticated", exception.getMessage());
}

@Test
public void test_map_alreadyExists() {
void test_map_alreadyExists() {
var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.ALREADY_EXISTS));

assertTrue(exception instanceof ConflictException);
assertEquals(exception.getMessage(), "already exists");
assertInstanceOf(ConflictException.class, exception);
assertEquals("already exists", exception.getMessage());
}

@Test
public void test_map_invalidArgument() {
void test_map_invalidArgument() {

var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.INVALID_ARGUMENT));

assertTrue(exception instanceof ValidationException);
assertEquals(exception.getMessage(), "invalid argument");
assertInstanceOf(ValidationException.class, exception);
assertEquals("invalid argument", exception.getMessage());
}

@Test
public void test_map_failedPrecondition() {
void test_map_failedPrecondition() {
var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.FAILED_PRECONDITION));

assertTrue(exception instanceof ValidationException);
assertEquals(exception.getMessage(), "failed precondition");
assertInstanceOf(ValidationException.class, exception);
assertEquals("failed precondition", exception.getMessage());
}

@Test
public void test_map_unexpectedValue() {
void test_map_unexpectedValue() {
var exception = grpcExceptionMapper.map(new StatusRuntimeException(Status.CANCELLED));

assertTrue(exception instanceof ClientException);
assertInstanceOf(ClientException.class, exception);
assertTrue(exception.getMessage().startsWith("unexpected status:"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public static SubjectReference toRef(SubjectRef ref) {
public static WriteRelationshipsRequest updateRelationshipRequest(
ObjectRef resource, String relation, ObjectRef subject) {

logger.info("update: " + resource.toString() + "#" + relation + "@" + subject);
logger.info("update: {}#{}@{}", resource.toString(), relation, subject);
return writeRelationshipRequest(
resource, relation, subject, RelationshipUpdate.Operation.OPERATION_TOUCH);
}

public static WriteRelationshipsRequest deleteRelationshipRequest(
ObjectRef resource, String relation, ObjectRef subject) {

logger.info("delete: " + resource.toString() + "#" + relation + "@" + subject);
logger.info("delete: {}#{}@{}", resource.toString(), relation, subject);
return writeRelationshipRequest(
resource, relation, subject, RelationshipUpdate.Operation.OPERATION_DELETE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.oviva.spicegen.api.UpdateRelationship;
import org.junit.jupiter.api.Test;

public class UpdateRelationshipMapperTest {
class UpdateRelationshipMapperTest {

private static final String TENANT = "tenant";
private static final String USER = "user";
Expand All @@ -18,34 +18,34 @@ public class UpdateRelationshipMapperTest {
private final UpdateRelationshipMapper mapper = new UpdateRelationshipMapper();

@Test
public void test_mapper_withUpdateOperation() {
void test_mapper_withUpdateOperation() {

var resource = ObjectRef.of(TENANT, ID);
var subject = ObjectRef.of(USER, ID);

var updateRelationship = UpdateRelationship.ofUpdate(resource, ADMINISTRATOR, subject);
var map = mapper.map(updateRelationship);

assertEquals(map.getOperation(), RelationshipUpdate.Operation.OPERATION_TOUCH);
assertEquals(RelationshipUpdate.Operation.OPERATION_TOUCH, map.getOperation());
assertNotNull(map.getRelationship());
assertEquals(map.getRelationship().getRelation(), ADMINISTRATOR);
assertEquals(map.getRelationship().getResource().getObjectId(), ID);
assertEquals(map.getRelationship().getResource().getObjectType(), TENANT);
assertEquals(ADMINISTRATOR, map.getRelationship().getRelation());
assertEquals(ID, map.getRelationship().getResource().getObjectId());
assertEquals(TENANT, map.getRelationship().getResource().getObjectType());
}

@Test
public void test_mapper_withDeleteOperation() {
void test_mapper_withDeleteOperation() {

var resource = ObjectRef.of(TENANT, ID);
var subject = ObjectRef.of(USER, ID);

var updateRelationship = UpdateRelationship.ofDelete(resource, ADMINISTRATOR, subject);
var map = mapper.map(updateRelationship);

assertEquals(map.getOperation(), RelationshipUpdate.Operation.OPERATION_DELETE);
assertEquals(RelationshipUpdate.Operation.OPERATION_DELETE, map.getOperation());
assertNotNull(map.getRelationship());
assertEquals(map.getRelationship().getRelation(), ADMINISTRATOR);
assertEquals(map.getRelationship().getResource().getObjectId(), ID);
assertEquals(map.getRelationship().getResource().getObjectType(), TENANT);
assertEquals(ADMINISTRATOR, map.getRelationship().getRelation());
assertEquals(ID, map.getRelationship().getResource().getObjectId());
assertEquals(TENANT, map.getRelationship().getResource().getObjectType());
}
}

0 comments on commit bdb217e

Please sign in to comment.