Skip to content

Commit

Permalink
Added Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tanya732 committed Dec 20, 2024
1 parent bd020b0 commit 3ab7a58
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/test/java/com/auth0/client/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public class MockServer {
public static final String ENCRYPTION_KEYS_LIST = "src/test/resources/mgmt/encryption_keys_list.json";
public static final String RATE_LIMIT_ERROR = "src/test/resources/mgmt/rate_limit_error.json";
public static final String SELF_SERVICE_PROFILES_LIST = "src/test/resources/mgmt/self_service_profiles_list.json";
public static final String SELF_SERVICE_PROFILE_RESPONSE = "src/test/resources/mgmt/self_service_profile_response.json";
public static final String SELF_SERVICE_PROFILE = "src/test/resources/mgmt/self_service_profile.json";

private final MockWebServer server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void shouldGetSelfServiceProfilesWithoutFilter() throws Exception{
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));

assertThat(response, is(notNullValue()));
assertThat(response.getItems(), hasSize(2));
assertThat(response.getItems(), hasSize(3));
}

@Test
Expand All @@ -53,7 +53,7 @@ public void shouldGetSelfServiceProfilesWithPage() throws Exception {
assertThat(recordedRequest, hasQueryParameter("per_page", "5"));

assertThat(response, is(notNullValue()));
assertThat(response.getItems(), hasSize(2));
assertThat(response.getItems(), hasSize(3));
}

@Test
Expand All @@ -72,7 +72,7 @@ public void shouldGetSelfServiceProfilesWithTotals() throws Exception {
assertThat(recordedRequest, hasQueryParameter("include_totals", "true"));

assertThat(response, is(notNullValue()));
assertThat(response.getItems(), hasSize(2));
assertThat(response.getItems(), hasSize(3));
}

@Test
Expand All @@ -95,7 +95,7 @@ public void shouldCreateSelfServiceProfile() throws Exception {
Request<SelfServiceProfileResponse> request = api.selfServiceProfiles().create(profile);
assertThat(request, is(notNullValue()));

server.jsonResponse(SELF_SERVICE_PROFILE, 201);
server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 201);
SelfServiceProfileResponse response = request.execute().getBody();
RecordedRequest recordedRequest = server.takeRequest();

Expand All @@ -122,7 +122,7 @@ public void shouldGetSelfServiceProfileById() throws Exception {
Request<SelfServiceProfileResponse> request = api.selfServiceProfiles().getById("id");
assertThat(request, is(notNullValue()));

server.jsonResponse(SELF_SERVICE_PROFILE, 200);
server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 200);
SelfServiceProfileResponse response = request.execute().getBody();
RecordedRequest recordedRequest = server.takeRequest();

Expand All @@ -144,7 +144,7 @@ public void shouldDeleteSelfServiceProfile() throws Exception {
Request<Void> request = api.selfServiceProfiles().delete("id");
assertThat(request, is(notNullValue()));

server.jsonResponse(SELF_SERVICE_PROFILE, 200);
server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 200);
request.execute().getBody();
RecordedRequest recordedRequest = server.takeRequest();

Expand All @@ -170,7 +170,7 @@ public void shouldUpdateSelfServiceProfile() throws Exception {
Request<SelfServiceProfileResponse> request = api.selfServiceProfiles().update(new SelfServiceProfile(), "id");
assertThat(request, is(notNullValue()));

server.jsonResponse(SELF_SERVICE_PROFILE, 200);
server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 200);
SelfServiceProfileResponse response = request.execute().getBody();
RecordedRequest recordedRequest = server.takeRequest();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.auth0.client.mgmt.filter;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class PageBasedPaginationFilterTest {

private PageBasedPaginationFilter filter;

@BeforeEach
public void setUp() {
filter = new PageBasedPaginationFilter();
}

@Test
public void shouldFilterByPage() {
PageBasedPaginationFilter instance = filter.withPage(5, 10);

assertThat(filter, is(instance));
assertThat(filter.getAsMap(), is(notNullValue()));
assertThat(filter.getAsMap(), Matchers.hasEntry("per_page", 10));
assertThat(filter.getAsMap(), Matchers.hasEntry("page", 5));
}

@Test
public void shouldIncludeTotals() {
PageBasedPaginationFilter instance = filter.withTotals(true);

assertThat(filter, is(instance));
assertThat(filter.getAsMap(), is(notNullValue()));
assertThat(filter.getAsMap(), Matchers.hasEntry("include_totals", true));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.auth0.json.mgmt.selfserviceprofiles;

import com.auth0.json.JsonTest;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class SelfServiceProfileResponsePageTest extends JsonTest<SelfServiceProfileResponsePage> {

private static final String jsonWithoutTotals =
"[\n" +
" {\n" +
" \"id\": \"id1\",\n" +
" \"name\": \"test1\",\n" +
" \"description\": \"This is for testing\",\n" +
" \"user_attributes\": [\n" +
" {\n" +
" \"name\": \"Phone\",\n" +
" \"description\": \"This is Phone Number\",\n" +
" \"is_optional\": true\n" +
" }\n" +
" ],\n" +
" \"allowed_strategies\": [\n" +
" \"google-apps\"\n" +
" ],\n" +
" \"created_at\": \"2024-12-16T15:26:39.015Z\",\n" +
" \"updated_at\": \"2024-12-16T15:28:04.933Z\"\n" +
" },\n" +
" {\n" +
" \"id\": \"id2\",\n" +
" \"name\": \"Test2\",\n" +
" \"description\": \"This is for Test2\",\n" +
" \"user_attributes\": [\n" +
" {\n" +
" \"name\": \"Phone\",\n" +
" \"description\": \"This is Phone Number\",\n" +
" \"is_optional\": true\n" +
" },\n" +
" {\n" +
" \"name\": \"UserName\",\n" +
" \"description\": \"This is User Name\",\n" +
" \"is_optional\": true\n" +
" }\n" +
" ],\n" +
" \"allowed_strategies\": [\n" +
" \"oidc\"\n" +
" ],\n" +
" \"created_at\": \"2024-12-16T15:29:06.119Z\",\n" +
" \"updated_at\": \"2024-12-16T15:29:06.119Z\"\n" +
" },\n" +
" {\n" +
" \"id\": \"id3\",\n" +
" \"name\": \"Test3\",\n" +
" \"description\": \"This is a Test3\",\n" +
" \"user_attributes\": [\n" +
" {\n" +
" \"name\": \"Name\",\n" +
" \"description\": \"Name Field\",\n" +
" \"is_optional\": true\n" +
" }\n" +
" ],\n" +
" \"allowed_strategies\": [\n" +
" \"oidc\"\n" +
" ],\n" +
" \"created_at\": \"2024-12-20T09:32:13.885Z\",\n" +
" \"updated_at\": \"2024-12-20T09:32:13.885Z\",\n" +
" \"branding\": {\n" +
" \"logo_url\": \"https://www.google.com\",\n" +
" \"colors\": {\n" +
" \"primary\": \"#ffffff\"\n" +
" }\n" +
" }\n" +
" }\n" +
"]\n";

private static final String jsonWithTotals =
"{\n" +
" \"self_service_profiles\": [\n" +
" {\n" +
" \"id\": \"id1\",\n" +
" \"name\": \"test1\",\n" +
" \"description\": \"This is for testing\",\n" +
" \"user_attributes\": [\n" +
" {\n" +
" \"name\": \"Phone\",\n" +
" \"description\": \"This is Phone Number\",\n" +
" \"is_optional\": true\n" +
" }\n" +
" ],\n" +
" \"allowed_strategies\": [\n" +
" \"google-apps\"\n" +
" ],\n" +
" \"created_at\": \"2024-12-16T15:26:39.015Z\",\n" +
" \"updated_at\": \"2024-12-16T15:28:04.933Z\"\n" +
" },\n" +
" {\n" +
" \"id\": \"id2\",\n" +
" \"name\": \"Test2\",\n" +
" \"description\": \"This is for Test2\",\n" +
" \"user_attributes\": [\n" +
" {\n" +
" \"name\": \"Phone\",\n" +
" \"description\": \"This is Phone Number\",\n" +
" \"is_optional\": true\n" +
" },\n" +
" {\n" +
" \"name\": \"UserName\",\n" +
" \"description\": \"This is User Name\",\n" +
" \"is_optional\": true\n" +
" }\n" +
" ],\n" +
" \"allowed_strategies\": [\n" +
" \"oidc\"\n" +
" ],\n" +
" \"created_at\": \"2024-12-16T15:29:06.119Z\",\n" +
" \"updated_at\": \"2024-12-16T15:29:06.119Z\"\n" +
" },\n" +
" {\n" +
" \"id\": \"id3\",\n" +
" \"name\": \"Test3\",\n" +
" \"description\": \"This is a Test3\",\n" +
" \"user_attributes\": [\n" +
" {\n" +
" \"name\": \"Name\",\n" +
" \"description\": \"Name Field\",\n" +
" \"is_optional\": true\n" +
" }\n" +
" ],\n" +
" \"allowed_strategies\": [\n" +
" \"oidc\"\n" +
" ],\n" +
" \"created_at\": \"2024-12-20T09:32:13.885Z\",\n" +
" \"updated_at\": \"2024-12-20T09:32:13.885Z\",\n" +
" \"branding\": {\n" +
" \"logo_url\": \"https://www.google.com\",\n" +
" \"colors\": {\n" +
" \"primary\": \"#ffffff\"\n" +
" }\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"start\": 0,\n" +
" \"limit\": 10,\n" +
" \"total\": 3\n" +
"}";

@Test
public void shouldDeserializeWithoutTotals() throws Exception {
SelfServiceProfileResponsePage page = fromJSON(jsonWithoutTotals, SelfServiceProfileResponsePage.class);

assertThat(page, is(notNullValue()));
assertThat(page.getStart(), is(nullValue()));
assertThat(page.getTotal(), is(nullValue()));
assertThat(page.getLimit(), is(nullValue()));
assertThat(page.getItems(), is(notNullValue()));
assertThat(page.getItems().size(), is(3));
assertThat(page.getNext(), is(nullValue()));
}

@Test
public void shouldDeserializeWithTotals() throws Exception {
SelfServiceProfileResponsePage page = fromJSON(jsonWithTotals, SelfServiceProfileResponsePage.class);

assertThat(page, is(notNullValue()));
assertThat(page.getStart(), is(0));
assertThat(page.getTotal(), is(3));
assertThat(page.getLimit(), is(10));
assertThat(page.getItems(), is(notNullValue()));
assertThat(page.getItems().size(), is(3));
assertThat(page.getNext(), is(nullValue()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.auth0.json.mgmt.selfserviceprofiles;

import com.auth0.json.JsonTest;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static com.auth0.json.JsonMatcher.hasEntry;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class SelfServiceProfileResponseTest extends JsonTest<SelfServiceProfileResponse> {

private final static String SELF_SERVICE_PROFILE_RESPONSE_JSON = "src/test/resources/mgmt/self_service_profile_response.json";

@Test
public void deserialize() throws Exception {
SelfServiceProfileResponse deserialized = fromJSON(readTextFile(SELF_SERVICE_PROFILE_RESPONSE_JSON), SelfServiceProfileResponse.class);

assertThat(deserialized.getId(), is("id"));
assertThat(deserialized.getName(), is("Test"));
assertThat(deserialized.getDescription(), is("This is a Test"));
assertThat(deserialized.getUserAttributes().get(0).getName(), is("Phone"));
assertThat(deserialized.getUserAttributes().get(0).getDescription(), is("This is Phone Number"));
assertThat(deserialized.getUserAttributes().get(0).getIsOptional(), is(true));
assertThat(deserialized.getBranding().getColors().getPrimary(), is("#ffffff"));
assertThat(deserialized.getBranding().getLogoUrl(), is("https://www.google.com"));
assertThat(deserialized.getAllowedStrategies().get(0), is("oidc"));
assertThat(deserialized.getCreatedAt(), is("2024-12-20T09:32:13.885Z"));
assertThat(deserialized.getCreatedAt(), is("2024-12-20T09:32:13.885Z"));
}

@Test
public void serialize() throws Exception {
SelfServiceProfileResponse selfServiceProfileResponse = new SelfServiceProfileResponse();
selfServiceProfileResponse.setId("id");
selfServiceProfileResponse.setName("Test");
selfServiceProfileResponse.setDescription("This is for Test");

UserAttribute userAttribute = new UserAttribute("Phone", "This is Phone Number", true);
List<UserAttribute> userAttributes = new ArrayList<>();
userAttributes.add(userAttribute);
selfServiceProfileResponse.setUserAttributes(userAttributes);

Branding branding = new Branding();
branding.setColors(new Color("#ffffff"));
branding.setLogoUrl("https://www.google.com");
selfServiceProfileResponse.setBranding(branding);

List<String> allowedStrategies = new ArrayList<>();
allowedStrategies.add("oidc");
selfServiceProfileResponse.setAllowedStrategies(allowedStrategies);

selfServiceProfileResponse.setCreatedAt("2024-12-20T09:32:13.885Z");
selfServiceProfileResponse.setUpdatedAt("2024-12-20T09:32:13.885Z");

String serialized = toJSON(selfServiceProfileResponse);
assertThat(serialized, is(notNullValue()));

assertThat(serialized, hasEntry("id", "id"));
assertThat(serialized, hasEntry("name", "Test"));
assertThat(serialized, hasEntry("description", "This is for Test"));
assertThat(serialized, hasEntry("user_attributes", notNullValue()));
assertThat(serialized, containsString("\"user_attributes\":[{\"name\":\"Phone\",\"description\":\"This is Phone Number\",\"is_optional\":true}]"));
assertThat(serialized, hasEntry("branding", notNullValue()));
assertThat(serialized, containsString("\"branding\":{\"logo_url\":\"https://www.google.com\",\"colors\":{\"primary\":\"#ffffff\"}}"));
assertThat(serialized, hasEntry("allowed_strategies", notNullValue()));
assertThat(serialized, containsString("\"allowed_strategies\":[\"oidc\"]"));
assertThat(serialized, hasEntry("created_at", "2024-12-20T09:32:13.885Z"));
assertThat(serialized, hasEntry("updated_at", "2024-12-20T09:32:13.885Z"));
}
}
Loading

0 comments on commit 3ab7a58

Please sign in to comment.