Skip to content

Commit

Permalink
test(client): add tests on non-transactional write
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Nov 18, 2023
1 parent 1db23c9 commit e33b91d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private CompletableFuture<ClientWriteResponse> writeTransactions(
}

private <T> List<List<T>> chunksOf(int chunkSize, List<T> list) {
int nChunks = list.size() / chunkSize;
int nChunks = (int) Math.ceil(list.size() / (double) chunkSize);

int finalEndExclusive = list.size();
List<List<T>> chunks = new ArrayList<>();
Expand Down
52 changes: 47 additions & 5 deletions src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ public class OpenFgaClientTest {
private OpenFgaClient fga;
private ClientConfiguration clientConfiguration;
private HttpClientMock mockHttpClient;
private HttpClient.Builder mockHttpClientBuilder;

@BeforeEach
public void beforeEachTest() throws Exception {
mockHttpClient = new HttpClientMock();

mockHttpClientBuilder = mock(HttpClient.Builder.class);
HttpClient.Builder mockHttpClientBuilder = mock(HttpClient.Builder.class);
when(mockHttpClientBuilder.executor(any())).thenReturn(mockHttpClientBuilder);
when(mockHttpClientBuilder.build()).thenReturn(mockHttpClient);

Expand Down Expand Up @@ -955,7 +954,7 @@ public void read_emptyRequestSendsNoTupleKey() throws Exception {
ClientReadRequest request = new ClientReadRequest();

// When
ClientReadResponse response = fga.read(request).get();
fga.read(request).get();

// Then
mockHttpClient.verify().post(postUrl).withBody(is(expectedBody)).called(1);
Expand Down Expand Up @@ -1088,6 +1087,50 @@ public void writeTest_deletes() throws Exception {
mockHttpClient.verify().post(postPath).withBody(is(expectedBody)).called(1);
}

@Test
public void writeTest_transactions() 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);
ClientTupleKey tuple = new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER);
String write2Body = String.format(
"{\"writes\":{\"tuple_keys\":[%s,%s]},\"deletes\":null,\"authorization_model_id\":\"%s\"}",
tupleBody, tupleBody, DEFAULT_AUTH_MODEL_ID);
String write1Body = String.format(
"{\"writes\":{\"tuple_keys\":[%s]},\"deletes\":null,\"authorization_model_id\":\"%s\"}",
tupleBody, DEFAULT_AUTH_MODEL_ID);
String delete2Body = String.format(
"{\"writes\":null,\"deletes\":{\"tuple_keys\":[%s,%s]},\"authorization_model_id\":\"%s\"}",
tupleBody, tupleBody, DEFAULT_AUTH_MODEL_ID);
String delete1Body = String.format(
"{\"writes\":null,\"deletes\":{\"tuple_keys\":[%s]},\"authorization_model_id\":\"%s\"}",
tupleBody, DEFAULT_AUTH_MODEL_ID);
mockHttpClient
.onPost(postPath)
.withBody(isOneOf(write2Body, write1Body, delete2Body, delete1Body))
.doReturn(200, EMPTY_RESPONSE_BODY);
ClientWriteRequest request = new ClientWriteRequest()
.writes(List.of(tuple, tuple, tuple, tuple, tuple))
.deletes(List.of(tuple, tuple, tuple, tuple, tuple));
ClientWriteOptions options =
new ClientWriteOptions().enableTransactions(true).transactionChunkSize(2);

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

// Then
mockHttpClient.verify().post(postPath).withBody(is(write2Body)).called(2);
mockHttpClient.verify().post(postPath).withBody(is(write1Body)).called(1);
mockHttpClient.verify().post(postPath).withBody(is(delete2Body)).called(2);
mockHttpClient.verify().post(postPath).withBody(is(delete1Body)).called(1);
assertEquals(200, response.getStatusCode());
}

@Test
public void writeTuplesTest() throws Exception {
// Given
Expand Down Expand Up @@ -1354,8 +1397,7 @@ public void batchCheck_twentyTimes() throws Exception {
ClientBatchCheckOptions options = new ClientBatchCheckOptions().authorizationModelId(DEFAULT_AUTH_MODEL_ID);

// When
List<ClientBatchCheckResponse> responses =
fga.batchCheck(requests, options).get();
fga.batchCheck(requests, options).get();

// Then
mockHttpClient.verify().post(postUrl).withBody(is(expectedBody)).called(20);
Expand Down

0 comments on commit e33b91d

Please sign in to comment.