Skip to content

Commit

Permalink
fix: default write to transaction mode
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Nov 21, 2023
1 parent af7d739 commit f254328
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,15 @@ public CompletableFuture<ClientWriteResponse> write(ClientWriteRequest request,
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();

if (options != null && !options.disableTransactions()) {
return writeTransactions(storeId, request, options);
if (options == null) {
options = new ClientWriteOptions();
}

return writeNonTransaction(storeId, request, options);
if (options.disableTransactions()) {
return writeNonTransaction(storeId, request, options);
}

return writeTransactions(storeId, request, options);
}

private CompletableFuture<ClientWriteResponse> writeNonTransaction(
Expand Down
25 changes: 18 additions & 7 deletions src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1249,11 +1249,15 @@ public void write_400() throws Exception {
mockHttpClient
.onPost(postUrl)
.doReturn(400, "{\"code\":\"validation_error\",\"message\":\"Generic validation error\"}");
ClientWriteRequest request = new ClientWriteRequest()
.writes(List.of(new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER)));

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

// Then
mockHttpClient.verify().post(postUrl).called(1);
Expand All @@ -1271,11 +1275,14 @@ public void write_404() throws Exception {
mockHttpClient
.onPost(postUrl)
.doReturn(404, "{\"code\":\"undefined_endpoint\",\"message\":\"Endpoint not enabled\"}");

ClientWriteRequest request = new ClientWriteRequest()
.writes(List.of(new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER)));
// When
ExecutionException execException =
assertThrows(ExecutionException.class, () -> fga.write(new ClientWriteRequest())
.get());
assertThrows(ExecutionException.class, () -> fga.write(request).get());

// Then
mockHttpClient.verify().post(postUrl).called(1);
Expand All @@ -1292,11 +1299,15 @@ public void write_500() throws Exception {
mockHttpClient
.onPost(postUrl)
.doReturn(500, "{\"code\":\"internal_error\",\"message\":\"Internal Server Error\"}");
ClientWriteRequest request = new ClientWriteRequest()
.writes(List.of(new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER)));

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

// Then
mockHttpClient.verify().post(postUrl).called(1 + DEFAULT_MAX_RETRIES);
Expand Down

0 comments on commit f254328

Please sign in to comment.