Skip to content

Commit

Permalink
test(java): Add unhappy case tests
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Aug 3, 2023
1 parent 8b97767 commit 777132d
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,4 @@ test-integration-client-java: build-client-java

.PHONY: test-client-java
test-client-java: build-client-java
# ... any custom test code ...
make run-in-docker sdk_language=java image=gradle:${GRADLE_DOCKER_TAG} command="/bin/sh -c 'gradle test'"
220 changes: 213 additions & 7 deletions config/clients/java/template/OpenFgaApiTest.java.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import java.util.HashMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

/**
* API tests for OpenFgaApi
Expand Down Expand Up @@ -63,7 +64,7 @@ public class OpenFgaApiTest {
.authorizationModelId("anything");
// When
CheckResponse response = fga.check(storeId, new CheckRequest());
CheckResponse response = fga.check(storeId, request);
// Then
verify(mockApiClient).getBaseUri();
Expand All @@ -81,6 +82,15 @@ public class OpenFgaApiTest {
assertEquals("Missing the required parameter 'storeId' when calling check", exception.getMessage());
}

@Test
public void check_bodyRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.check("whatever", null));
// Then
assertEquals("Missing the required parameter 'body' when calling check", exception.getMessage());
}

/**
* Create a store.
* <p>
Expand All @@ -92,10 +102,18 @@ public class OpenFgaApiTest {
@Test
@Disabled
public void createStoreTest() throws ApiException {
CreateStoreRequest body = null;
CreateStoreResponse response = fga.createStore(body);
CreateStoreResponse response = fga.createStore(null);
// TODO: test validations
// TODO: Write this
}

@Test
public void createStore_bodyRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.createStore(null));
// Then
assertEquals("Missing the required parameter 'body' when calling createStore", exception.getMessage());
}

/**
Expand All @@ -116,6 +134,15 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void deleteStore_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.deleteStore(null));
// Then
assertEquals("Missing the required parameter 'storeId' when calling deleteStore", exception.getMessage());
}

/**
* Expand all relationships in userset tree format, and following userset rewrite rules. Useful to reason about and debug a certain relationship.
* <p>
Expand All @@ -134,6 +161,24 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void expand_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.expand(null, new ExpandRequest()));
// Then
assertEquals("Missing the required parameter 'storeId' when calling expand", exception.getMessage());
}

@Test
public void expand_bodyRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.expand("whatever", null));
// Then
assertEquals("Missing the required parameter 'body' when calling expand", exception.getMessage());
}

/**
* Get a store.
* <p>
Expand All @@ -151,6 +196,15 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void getStore_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.getStore(null));
// Then
assertEquals("Missing the required parameter 'storeId' when calling getStore", exception.getMessage());
}

/**
* List all objects of the given type that the user has a relation with.
* <p>
Expand All @@ -169,6 +223,24 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void listObjects_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.listObjects(null, new ListObjectsRequest()));
// Then
assertEquals("Missing the required parameter 'storeId' when calling listObjects", exception.getMessage());
}

@Test
public void listObjects_bodyRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.listObjects("whatever", null));
// Then
assertEquals("Missing the required parameter 'body' when calling listObjects", exception.getMessage());
}

/**
* List all stores.
* <p>
Expand All @@ -180,9 +252,7 @@ public class OpenFgaApiTest {
@Test
@Disabled
public void listStoresTest() throws ApiException {
Integer pageSize = null;
String continuationToken = null;
ListStoresResponse response = fga.listStores(pageSize, continuationToken);
ListStoresResponse response = fga.listStores(null, null);
// TODO: test validations
}
Expand All @@ -205,6 +275,24 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void read_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.read(null, new ReadRequest()));
// Then
assertEquals("Missing the required parameter 'storeId' when calling read", exception.getMessage());
}

@Test
public void read_bodyRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.read("whatever", null));
// Then
assertEquals("Missing the required parameter 'body' when calling read", exception.getMessage());
}

/**
* Read assertions for an authorization model ID.
* <p>
Expand All @@ -223,6 +311,24 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void readAssertions_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.readAssertions(null, "whatever"));
// Then
assertEquals("Missing the required parameter 'storeId' when calling readAssertions", exception.getMessage());
}

@Test
public void readAssertions_authModelIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.readAssertions("whatever", null));
// Then
assertEquals("Missing the required parameter 'authorizationModelId' when calling readAssertions", exception.getMessage());
}

/**
* Return a particular version of an authorization model.
* <p>
Expand All @@ -241,6 +347,24 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void readAuthorizationModel_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.readAuthorizationModel(null, "whatever"));
// Then
assertEquals("Missing the required parameter 'storeId' when calling readAuthorizationModel", exception.getMessage());
}

@Test
public void readAuthorizationModel_idRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.readAuthorizationModel("whatever", null));
// Then
assertEquals("Missing the required parameter 'id' when calling readAuthorizationModel", exception.getMessage());
}

/**
* Return all the authorization models for a particular store.
* <p>
Expand All @@ -260,6 +384,15 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void readAuthorizationModels_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.readAuthorizationModels(null, null, null));
// Then
assertEquals("Missing the required parameter 'storeId' when calling readAuthorizationModels", exception.getMessage());
}

/**
* Return a list of all the tuple changes.
* <p>
Expand All @@ -280,6 +413,15 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void readChanges_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.readChanges(null, null, null, null));
// Then
assertEquals("Missing the required parameter 'storeId' when calling readChanges", exception.getMessage());
}

/**
* Add or delete tuples from the store.
* <p>
Expand All @@ -298,6 +440,24 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void write_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.write(null, new WriteRequest()));
// Then
assertEquals("Missing the required parameter 'storeId' when calling write", exception.getMessage());
}

@Test
public void write_bodyRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.write("whatever", null));
// Then
assertEquals("Missing the required parameter 'body' when calling write", exception.getMessage());
}

/**
* Upsert assertions for an authorization model ID.
* <p>
Expand All @@ -318,6 +478,33 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void writeAssertions_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.writeAssertions(null, "whatever", new WriteAssertionsRequest()));
// Then
assertEquals("Missing the required parameter 'storeId' when calling writeAssertions", exception.getMessage());
}

@Test
public void writeAssertions_authModelIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.writeAssertions("whatever", null, new WriteAssertionsRequest()));
// Then
assertEquals("Missing the required parameter 'authorizationModelId' when calling writeAssertions", exception.getMessage());
}

@Test
public void writeAssertions_bodyRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.writeAssertions("whatever", "whatever", null));
// Then
assertEquals("Missing the required parameter 'body' when calling writeAssertions", exception.getMessage());
}

/**
* Create a new authorization model.
* <p>
Expand All @@ -336,7 +523,26 @@ public class OpenFgaApiTest {
// TODO: test validations
}

@Test
public void writeAuthorizationModel_storeIdRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.writeAuthorizationModel(null, new WriteAuthorizationModelRequest()));
// Then
assertEquals("Missing the required parameter 'storeId' when calling writeAuthorizationModel", exception.getMessage());
}

@Test
public void writeAuthorizationModel_bodyRequired() throws ApiException {
// When
ApiException exception = assertThrows(ApiException.class, () -> fga.writeAuthorizationModel("whatever", null));
// Then
assertEquals("Missing the required parameter 'body' when calling writeAuthorizationModel", exception.getMessage());
}

private HttpResponse<InputStream> basicHttpOkResponse() {
@SuppressWarnings("unchecked")
HttpResponse<InputStream> response = mock(HttpResponse.class);
when(response.headers()).thenReturn(HttpHeaders.of(new HashMap<>(), (_a, _b) -> true));
InputStream b = new ByteArrayInputStream("{}".getBytes());
Expand Down

0 comments on commit 777132d

Please sign in to comment.