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 22, 2023
1 parent af7d739 commit e0edb10
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
8 changes: 4 additions & 4 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,11 @@ 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.disableTransactions()) {
return writeNonTransaction(storeId, request, options);
}

return writeNonTransaction(storeId, request, options);
return writeTransactions(storeId, request, options);
}

private CompletableFuture<ClientWriteResponse> writeNonTransaction(
Expand All @@ -305,7 +305,7 @@ private CompletableFuture<ClientWriteResponse> writeNonTransaction(
private CompletableFuture<ClientWriteResponse> writeTransactions(
String storeId, ClientWriteRequest request, ClientWriteOptions options) {

int chunkSize = options.getTransactionChunkSize();
int chunkSize = options == null ? DEFAULT_MAX_METHOD_PARALLEL_REQS : options.getTransactionChunkSize();

var writeTransactions = chunksOf(chunkSize, request.getWrites()).map(ClientWriteRequest::ofWrites);
var deleteTransactions = chunksOf(chunkSize, request.getDeletes()).map(ClientWriteRequest::ofDeletes);
Expand Down
24 changes: 18 additions & 6 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,15 @@ 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 +1300,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 e0edb10

Please sign in to comment.