Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add objects crud methods #34

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 68 additions & 38 deletions src/main/java/dev/warrant/WarrantBaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ public Warrant createWarrant(WarrantObject object, String relation, WarrantSubje

public Warrant createWarrant(WarrantObject object, String relation, WarrantSubject subject, String policy, RequestOptions requestOptions)
throws WarrantException {
try {
Warrant toCreate = new Warrant(object.type(), object.id(), relation, subject, policy);
return makePostRequest("/v1/warrants", toCreate, Warrant.class, requestOptions.asMap());
} catch (WarrantException e) {
throw e;
}
}

public void deleteWarrant(WarrantObject object, String relation, WarrantSubject subject) throws WarrantException {
Expand All @@ -85,12 +81,8 @@ public void deleteWarrant(WarrantObject object, String relation, WarrantSubject
}

public void deleteWarrant(WarrantObject object, String relation, WarrantSubject subject, String policy, RequestOptions requestOptions) throws WarrantException {
try {
Warrant toDelete = new Warrant(object.type(), object.id(), relation, subject, policy);
makeDeleteRequest("/v1/warrants", toDelete, requestOptions.asMap());
} catch (WarrantException e) {
throw e;
}
}

public Warrant[] listWarrants(WarrantFilters filters, ListParams listParams) throws WarrantException {
Expand Down Expand Up @@ -151,6 +143,68 @@ public WarrantCheck checkAllOf(List<WarrantSpec> warrants, RequestOptions reques
return checkWithOp(warrants, "allOf", WarrantCheck.class, new RequestOptions());
}

// public BaseWarrantObject createObject(String objectType) throws WarrantException {
// return createObject(objectType, null, Collections.emptyMap(), BaseWarrantObject.class);
// }

// public <T extends WarrantObject> T createObject(String objectType, Class<T> resultType) throws WarrantException {
// return createObject(objectType, null, Collections.emptyMap(), resultType);
// }

// public <T extends WarrantObject> T createObject(String objectType, Class<T> resultType, RequestOptions requestOptions) throws WarrantException {
// return createObject(objectType, null, Collections.emptyMap(), resultType);
// }

// public BaseWarrantObject createObject(String objectType, Map<String, Object> meta) throws WarrantException {
// return createObject(objectType, null, meta, BaseWarrantObject.class);
// }

// public <T extends WarrantObject> T createObject(String objectType, Map<String, Object> meta, Class<T> resultType) throws WarrantException {
// return createObject(objectType, null, meta, resultType);
// }

// public BaseWarrantObject createObject(String objectType, String objectId) throws WarrantException {
// return createObject(objectType, objectId, Collections.emptyMap(), BaseWarrantObject.class);
// }

// public <T extends WarrantObject> T createObject(String objectType, String objectId, Class<T> resultType) throws WarrantException {
// return createObject(objectType, objectId, Collections.emptyMap(), resultType);
// }

// public BaseWarrantObject createObject(String objectType, String objectId, Map<String, Object> meta) throws WarrantException {
// return createObject(objectType, objectId, meta, BaseWarrantObject.class);
// }

// public <T extends WarrantObject> T createObject(String objectType, String objectId, Map<String, Object> meta, Class<T> resultType) throws WarrantException {
// return createObject(objectType, objectId, meta, resultType, new RequestOptions());
// }

// public <T extends WarrantObject> T createObject(String objectType, String objectId, Map<String, Object> meta, Class<T> resultType, RequestOptions requestOptions) throws WarrantException {
// BaseWarrantObject obj = new BaseWarrantObject(objectType, objectId, meta);
// return makePostRequest("/v1/objects", obj, resultType, requestOptions.asMap());
// }

// public BaseWarrantObject getObject(String objectType, String objectId) throws WarrantException {
// return getObject(objectType, objectId, BaseWarrantObject.class);
// }

// public <T extends WarrantObject> T getObject(String objectType, String objectId, Class<T> resultType) throws WarrantException {
// return makeGetRequest("/v1/objects/" + objectType + "/" + objectId, resultType);
// }

// public BaseWarrantObject updateObject(String objectType, String objectId, Map<String, Object> meta) throws WarrantException {
// BaseWarrantObject obj = new BaseWarrantObject(objectType, objectId, meta);
// return makePutRequest("/v1/objects/" + objectType + "/" + objectId, obj, BaseWarrantObject.class);
// }

public void deleteObject(WarrantObject obj) throws WarrantException {
deleteObject(obj.type(), obj.id());
}

public void deleteObject(String objectType, String objectId) throws WarrantException {
makeDeleteRequest("/v1/objects/" + objectType + "/" + objectId);
}

public String createUserAuthzSession(String userId) throws WarrantException {
return createUserAuthzSession(userId, new RequestOptions());
}
Expand All @@ -175,11 +229,7 @@ public String createUserSelfServiceDashboardUrl(String userId, String tenantId,
}

WarrantCheck makeCheckRequest(WarrantCheckSpec toCheck) throws WarrantException {
try {
return makeCheckRequest(toCheck, Collections.emptyMap());
} catch (WarrantException e) {
throw e;
}
return makeCheckRequest(toCheck, Collections.emptyMap());
}

WarrantCheck makeCheckRequest(WarrantCheckSpec toCheck, Map<String, Object> requestOptions) throws WarrantException {
Expand Down Expand Up @@ -330,11 +380,7 @@ <T> T makePutRequest(String uri, Object reqPayload, Class<T> type, Map<String, O
}

private HttpResponse<String> makePutRequest(String uri, Object reqPayload) throws WarrantException {
try {
return makePutRequest(uri, reqPayload, Collections.emptyMap());
} catch (WarrantException e) {
throw e;
}
return makePutRequest(uri, reqPayload, Collections.emptyMap());
}

private HttpResponse<String> makePutRequest(String uri, Object reqPayload, Map<String, Object> requestOptions) throws WarrantException {
Expand Down Expand Up @@ -366,27 +412,15 @@ private HttpResponse<String> makePutRequest(String uri, Object reqPayload, Map<S
}

HttpResponse<String> makeDeleteRequest(String uri) throws WarrantException {
try {
return makeDeleteRequest(uri, null, Collections.emptyMap());
} catch (WarrantException e) {
throw e;
}
return makeDeleteRequest(uri, null, Collections.emptyMap());
}

HttpResponse<String> makeDeleteRequest(String uri, Object reqPayload) throws WarrantException {
try {
return makeDeleteRequest(uri, reqPayload, Collections.emptyMap());
} catch (WarrantException e) {
throw e;
}
return makeDeleteRequest(uri, reqPayload, Collections.emptyMap());
}

HttpResponse<String> makeDeleteRequest(String uri, Map<String, Object> requestOptions) throws WarrantException {
try {
return makeDeleteRequest(uri, null, requestOptions);
} catch (WarrantException e) {
throw e;
}
return makeDeleteRequest(uri, null, requestOptions);
}

HttpResponse<String> makeDeleteRequest(String uri, Object reqPayload, Map<String, Object> requestOptions) throws WarrantException {
Expand Down Expand Up @@ -459,11 +493,7 @@ <T> T makeGetRequest(String uri, Map<String, Object> queryParams, Class<T> type,
}

private HttpResponse<String> makeGetRequest(String uri, Map<String, Object> queryParams) throws WarrantException {
try {
return makeGetRequest(uri, queryParams, Collections.emptyMap());
} catch (WarrantException e) {
throw e;
}
return makeGetRequest(uri, queryParams, Collections.emptyMap());
}

private HttpResponse<String> makeGetRequest(String uri, Map<String, Object> queryParams, Map<String, Object> requestOptions) throws WarrantException {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/dev/warrant/model/object/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public String id() {
public String type() {
return "feature";
}

// @Override
// public Map<String, Object> meta() {
// return null;
// }
}
5 changes: 5 additions & 0 deletions src/main/java/dev/warrant/model/object/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public String id() {
public String type() {
return "permission";
}

// @Override
// public Map<String, Object> meta() {
// return null;
// }
}
5 changes: 5 additions & 0 deletions src/main/java/dev/warrant/model/object/PricingTier.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public String id() {
public String type() {
return "pricing-tier";
}

// @Override
// public Map<String, Object> meta() {
// return null;
// }
}
5 changes: 5 additions & 0 deletions src/main/java/dev/warrant/model/object/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ public String id() {
public String type() {
return "role";
}

// @Override
// public Map<String, Object> meta() {
// return null;
// }
}
5 changes: 5 additions & 0 deletions src/main/java/dev/warrant/model/object/Tenant.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public String id() {
public String type() {
return "tenant";
}

// @Override
// public Map<String, Object> meta() {
// return null;
// }
}
5 changes: 5 additions & 0 deletions src/main/java/dev/warrant/model/object/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public String id() {
public String type() {
return "user";
}

// @Override
// public Map<String, Object> meta() {
// return null;
// }
}
2 changes: 2 additions & 0 deletions src/main/java/dev/warrant/model/object/WarrantObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public interface WarrantObject {
public String id();

public String type();

// public Map<String, Object> meta();
}
36 changes: 36 additions & 0 deletions src/test/java/dev/warrant/LiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,42 @@ public void crudPricingTiers() throws WarrantException {
Assertions.assertEquals(0, tiers.length);
}

@Test
public void crudObjects() throws WarrantException {
// BaseWarrantObject roleGeneratedId = client.createObject("role");
// Assertions.assertEquals("role", roleGeneratedId.getObjectType());
// Assertions.assertNotNull(roleGeneratedId.getObjectId());
// Assertions.assertNull(roleGeneratedId.getMeta());

// Map<String, Object> meta = new HashMap<>();
// meta.put("name", "admin role");
// BaseWarrantObject roleWithMeta = client.createObject("role", "admin", meta);
// Assertions.assertEquals("role", roleWithMeta.getObjectType());
// Assertions.assertEquals("admin", roleWithMeta.getObjectId());
// Assertions.assertNotNull(roleWithMeta.getMeta());
// Assertions.assertEquals("admin role", roleWithMeta.getMeta().get("name"));

// Map<String, Object> updatedMeta = new HashMap<>();
// updatedMeta.put("name", "admin role");
// updatedMeta.put("description", "role description");
// BaseWarrantObject updatedRole = client.updateObject(roleWithMeta.getObjectType(), roleWithMeta.getObjectId(), updatedMeta);
// Assertions.assertEquals(roleWithMeta.getObjectType(), updatedRole.getObjectType());
// Assertions.assertEquals(roleWithMeta.getObjectId(), updatedRole.getObjectId());
// Assertions.assertEquals(2, updatedRole.getMeta().size());
// Assertions.assertEquals("admin role", updatedRole.getMeta().get("name"));
// Assertions.assertEquals("role description", updatedRole.getMeta().get("description"));

// BaseWarrantObject fetchedObj = client.getObject(roleGeneratedId.getObjectType(), roleGeneratedId.getObjectId());
// Assertions.assertEquals(roleGeneratedId.getObjectType(), fetchedObj.getObjectType());
// Assertions.assertEquals(roleGeneratedId.getObjectId(), fetchedObj.getObjectId());
// Assertions.assertEquals(roleGeneratedId.getMeta(), fetchedObj.getMeta());

User user = client.createUser(new User("object_test_1", "[email protected]"));
Assertions.assertEquals("object_test_1", user.getUserId());
Assertions.assertEquals("[email protected]", user.getEmail());
client.deleteObject(user);
}

@Test
public void batchCreateUsersAndTenants() throws WarrantException {
User[] newUsers = { new User("user-1"), new User("user-2") };
Expand Down