Skip to content

Commit

Permalink
test: add explicit non-transaction write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Nov 22, 2023
1 parent e0edb10 commit 1495f14
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,71 @@ public void writeTest_transactionsWithFailure() throws Exception {
assertEquals(400, exception.getStatusCode());
}

@Test
public void writeTest_nonTransaction() throws Exception {
// Given
String postPath = "https://localhost/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write";
String tupleBody = String.format(
"{\"object\":\"%s\",\"relation\":\"%s\",\"user\":\"%s\"}",
DEFAULT_OBJECT, DEFAULT_RELATION, DEFAULT_USER);
String expectedBody = String.format(
"{\"writes\":{\"tuple_keys\":[%s,%s,%s]},\"deletes\":{\"tuple_keys\":[%s,%s,%s]},\"authorization_model_id\":\"%s\"}",
tupleBody, tupleBody, tupleBody, tupleBody, tupleBody, tupleBody, DEFAULT_AUTH_MODEL_ID);
mockHttpClient.onPost(postPath).withBody(is(expectedBody)).doReturn(200, EMPTY_RESPONSE_BODY);
ClientTupleKey tuple = new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER);
ClientWriteRequest request =
new ClientWriteRequest().writes(List.of(tuple, tuple, tuple)).deletes(List.of(tuple, tuple, tuple));

// We expect transactionChunkSize will be ignored, and exactly one request will be sent.
ClientWriteOptions options =
new ClientWriteOptions().disableTransactions(true).transactionChunkSize(1);

// When
var response = fga.write(request, options).get();

// Then
mockHttpClient.verify().post(postPath).withBody(is(expectedBody)).called(1);
assertEquals(200, response.getStatusCode());
}

@Test
public void writeTest_nonTransactionsWithFailure() throws Exception {
// Given
String postPath = "https://localhost/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write";
String tupleBody = String.format(
"{\"object\":\"%s\",\"relation\":\"%s\",\"user\":\"%s\"}",
DEFAULT_OBJECT, DEFAULT_RELATION, DEFAULT_USER);
String expectedBody = String.format(
"{\"writes\":{\"tuple_keys\":[%s,%s,%s]},\"deletes\":{\"tuple_keys\":[%s,%s,%s]},\"authorization_model_id\":\"%s\"}",
tupleBody, tupleBody, tupleBody, tupleBody, tupleBody, tupleBody, DEFAULT_AUTH_MODEL_ID);
mockHttpClient
.onPost(postPath)
.withBody(is(expectedBody))
.doReturn(400, "{\"code\":\"validation_error\",\"message\":\"Generic validation error\"}");
ClientTupleKey tuple = new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER);
ClientWriteRequest request =
new ClientWriteRequest().writes(List.of(tuple, tuple, tuple)).deletes(List.of(tuple, tuple, tuple));

// We expect transactionChunkSize will be ignored, and exactly one request will be sent.
ClientWriteOptions options =
new ClientWriteOptions().disableTransactions(true).transactionChunkSize(1);

// When
var execException = assertThrows(
ExecutionException.class, () -> fga.write(request, options).get());

// Then
mockHttpClient.verify().post(postPath).withBody(is(expectedBody)).called(1);
var exception = assertInstanceOf(FgaApiValidationError.class, execException.getCause());
assertEquals(400, exception.getStatusCode());
}

@Test
public void writeTuplesTest() throws Exception {
// Given
Expand Down

0 comments on commit 1495f14

Please sign in to comment.