From 95e9c96a83993282c3e2f7bfda340973e2d4bda8 Mon Sep 17 00:00:00 2001 From: Sebastian Rose Date: Thu, 8 Feb 2024 20:21:22 +0100 Subject: [PATCH 1/3] 299 - java noop --- .../template/client-ClientWriteResponse.java.mustache | 9 +++++++++ .../java/template/client-OpenFgaClient.java.mustache | 11 +++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/config/clients/java/template/client-ClientWriteResponse.java.mustache b/config/clients/java/template/client-ClientWriteResponse.java.mustache index 1883f647..ee1e707c 100644 --- a/config/clients/java/template/client-ClientWriteResponse.java.mustache +++ b/config/clients/java/template/client-ClientWriteResponse.java.mustache @@ -2,6 +2,7 @@ package {{clientPackage}}.model; import {{clientPackage}}.ApiResponse; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -10,12 +11,20 @@ public class ClientWriteResponse { private final Map> headers; private final String rawResponse; + public static ClientWriteResponse EMPTY = new ClientWriteResponse(); + public ClientWriteResponse(ApiResponse apiResponse) { this.statusCode = apiResponse.getStatusCode(); this.headers = apiResponse.getHeaders(); this.rawResponse = apiResponse.getRawResponse(); } + private ClientWriteResponse() { + this.statusCode = 0; + this.headers = Collections.emptyMap(); + this.rawResponse = ""; + } + public int getStatusCode() { return statusCode; } diff --git a/config/clients/java/template/client-OpenFgaClient.java.mustache b/config/clients/java/template/client-OpenFgaClient.java.mustache index 928f7606..d2ff0030 100644 --- a/config/clients/java/template/client-OpenFgaClient.java.mustache +++ b/config/clients/java/template/client-OpenFgaClient.java.mustache @@ -1,6 +1,7 @@ {{>licenseInfo}} package {{clientPackage}}; +import static java.util.concurrent.CompletableFuture.completedFuture; import static {{utilPackage}}.StringUtil.isNullOrWhitespace; import static java.util.UUID.randomUUID; @@ -357,15 +358,21 @@ public class OpenFgaClient { WriteRequest body = new WriteRequest(); var writeTuples = request.getWrites(); - if (writeTuples != null && !writeTuples.isEmpty()) { + var writesRequested = writeTuples != null && !writeTuples.isEmpty(); + if (writesRequested) { body.writes(ClientTupleKey.asWriteRequestWrites(writeTuples)); } var deleteTuples = request.getDeletes(); - if (deleteTuples != null && !deleteTuples.isEmpty()) { + var deletesRequested = deleteTuples != null && !deleteTuples.isEmpty(); + if (deletesRequested) { body.deletes(ClientTupleKeyWithoutCondition.asWriteRequestDeletes(deleteTuples)); } + if (!writesRequested && !deletesRequested) { + return completedFuture(ClientWriteResponse.EMPTY); + } + if (options != null && !isNullOrWhitespace(options.getAuthorizationModelId())) { body.authorizationModelId(options.getAuthorizationModelId()); } else { From 2348d08db103fc406ab263b2bdc9dbca3d81dde7 Mon Sep 17 00:00:00 2001 From: Sebastian Rose Date: Thu, 8 Feb 2024 22:15:58 +0100 Subject: [PATCH 2/3] Some adjustments --- .../java/template/client-ClientWriteResponse.java.mustache | 3 ++- .../clients/java/template/client-OpenFgaClient.java.mustache | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/config/clients/java/template/client-ClientWriteResponse.java.mustache b/config/clients/java/template/client-ClientWriteResponse.java.mustache index ee1e707c..deaa9f06 100644 --- a/config/clients/java/template/client-ClientWriteResponse.java.mustache +++ b/config/clients/java/template/client-ClientWriteResponse.java.mustache @@ -11,6 +11,7 @@ public class ClientWriteResponse { private final Map> headers; private final String rawResponse; + // how to better express no call has been made? public static ClientWriteResponse EMPTY = new ClientWriteResponse(); public ClientWriteResponse(ApiResponse apiResponse) { @@ -20,7 +21,7 @@ public class ClientWriteResponse { } private ClientWriteResponse() { - this.statusCode = 0; + this.statusCode = 200; // from openapi-spec, success status code this.headers = Collections.emptyMap(); this.rawResponse = ""; } diff --git a/config/clients/java/template/client-OpenFgaClient.java.mustache b/config/clients/java/template/client-OpenFgaClient.java.mustache index d2ff0030..1a4b362c 100644 --- a/config/clients/java/template/client-OpenFgaClient.java.mustache +++ b/config/clients/java/template/client-OpenFgaClient.java.mustache @@ -370,6 +370,7 @@ public class OpenFgaClient { } if (!writesRequested && !deletesRequested) { + // no call executed -> return empty response return completedFuture(ClientWriteResponse.EMPTY); } From 9e93eb5e66b302aa141e42de8aedc2a88b9790f0 Mon Sep 17 00:00:00 2001 From: Sebastian Rose Date: Tue, 13 Feb 2024 10:19:50 +0100 Subject: [PATCH 3/3] better fit to requirements, added test --- .../client-ClientWriteResponse.java.mustache | 10 ---------- .../client-OpenFgaClient.java.mustache | 18 ++++++++---------- .../client-OpenFgaClientTest.java.mustache | 19 ++++++++++++++++++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/config/clients/java/template/client-ClientWriteResponse.java.mustache b/config/clients/java/template/client-ClientWriteResponse.java.mustache index deaa9f06..1883f647 100644 --- a/config/clients/java/template/client-ClientWriteResponse.java.mustache +++ b/config/clients/java/template/client-ClientWriteResponse.java.mustache @@ -2,7 +2,6 @@ package {{clientPackage}}.model; import {{clientPackage}}.ApiResponse; -import java.util.Collections; import java.util.List; import java.util.Map; @@ -11,21 +10,12 @@ public class ClientWriteResponse { private final Map> headers; private final String rawResponse; - // how to better express no call has been made? - public static ClientWriteResponse EMPTY = new ClientWriteResponse(); - public ClientWriteResponse(ApiResponse apiResponse) { this.statusCode = apiResponse.getStatusCode(); this.headers = apiResponse.getHeaders(); this.rawResponse = apiResponse.getRawResponse(); } - private ClientWriteResponse() { - this.statusCode = 200; // from openapi-spec, success status code - this.headers = Collections.emptyMap(); - this.rawResponse = ""; - } - public int getStatusCode() { return statusCode; } diff --git a/config/clients/java/template/client-OpenFgaClient.java.mustache b/config/clients/java/template/client-OpenFgaClient.java.mustache index 1a4b362c..aa86a494 100644 --- a/config/clients/java/template/client-OpenFgaClient.java.mustache +++ b/config/clients/java/template/client-OpenFgaClient.java.mustache @@ -1,7 +1,6 @@ {{>licenseInfo}} package {{clientPackage}}; -import static java.util.concurrent.CompletableFuture.completedFuture; import static {{utilPackage}}.StringUtil.isNullOrWhitespace; import static java.util.UUID.randomUUID; @@ -358,22 +357,15 @@ public class OpenFgaClient { WriteRequest body = new WriteRequest(); var writeTuples = request.getWrites(); - var writesRequested = writeTuples != null && !writeTuples.isEmpty(); - if (writesRequested) { + if (writeTuples != null && !writeTuples.isEmpty()) { body.writes(ClientTupleKey.asWriteRequestWrites(writeTuples)); } var deleteTuples = request.getDeletes(); - var deletesRequested = deleteTuples != null && !deleteTuples.isEmpty(); - if (deletesRequested) { + if (deleteTuples != null && !deleteTuples.isEmpty()) { body.deletes(ClientTupleKeyWithoutCondition.asWriteRequestDeletes(deleteTuples)); } - if (!writesRequested && !deletesRequested) { - // no call executed -> return empty response - return completedFuture(ClientWriteResponse.EMPTY); - } - if (options != null && !isNullOrWhitespace(options.getAuthorizationModelId())) { body.authorizationModelId(options.getAuthorizationModelId()); } else { @@ -405,6 +397,12 @@ public class OpenFgaClient { var deleteTransactions = chunksOf(chunkSize, request.getDeletes()).map(ClientWriteRequest::ofDeletes); var transactions = Stream.concat(writeTransactions, deleteTransactions).collect(Collectors.toList()); + + if(transactions.isEmpty()) { + var emptyTransaction = new ClientWriteRequest().writes(null).deletes(null); + return this.writeNonTransaction(storeId, emptyTransaction, writeOptions); + } + var futureResponse = this.writeNonTransaction(storeId, transactions.get(0), options); for (int i = 1; i < transactions.size(); i++) { diff --git a/config/clients/java/template/client-OpenFgaClientTest.java.mustache b/config/clients/java/template/client-OpenFgaClientTest.java.mustache index 71567a7a..2f95beb5 100644 --- a/config/clients/java/template/client-OpenFgaClientTest.java.mustache +++ b/config/clients/java/template/client-OpenFgaClientTest.java.mustache @@ -6,7 +6,6 @@ import static org.hamcrest.Matchers.*; import static org.hamcrest.core.StringContains.containsString; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; - import com.fasterxml.jackson.databind.ObjectMapper; import com.pgssoft.httpclient.HttpClientMock; import {{clientPackage}}.model.*; @@ -16,6 +15,7 @@ import {{invokerPackage}}.*; import {{modelPackage}}.*; import java.net.http.HttpClient; import java.time.Duration; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.UUID; @@ -1361,6 +1361,23 @@ public class OpenFgaClientTest { assertEquals(200, response.getStatusCode()); } + @Test + public void write_nothingSentWhenWritesAndDeletesAreEmpty() throws FgaInvalidParameterException, ExecutionException, InterruptedException { + // Given + String postPath = String.format("https://localhost/stores/%s/write", DEFAULT_STORE_ID); + String expectedBody = String.format( + "{\"writes\":null,\"deletes\":null,\"authorization_model_id\":\"%s\"}", DEFAULT_AUTH_MODEL_ID); + mockHttpClient.onPost(postPath).withBody(is(expectedBody)).doReturn(200, EMPTY_RESPONSE_BODY); + + // When + var clientWriteRequest = new ClientWriteRequest().writes(Collections.emptyList()).deletes(Collections.emptyList()); + var response = fga.write(clientWriteRequest).get(); + + // Then + mockHttpClient.verify().post(postPath).called(1); + assertEquals(200, response.getStatusCode()); + } + @Test public void write_storeIdRequired() { // Given