From af3ada6cc47f9f2c074749dedffbd935dcb2f781 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Wed, 4 Sep 2024 18:17:13 +0530 Subject: [PATCH 1/7] Add success tests for orgnaization meta-attributes GET --- .../v1/OrganizationManagementBaseTest.java | 5 + .../v1/OrganizationManagementSuccessTest.java | 223 ++++++++++++++++++ 2 files changed, 228 insertions(+) diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java index 10d9b8beb25..3cd92566c43 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java @@ -42,6 +42,7 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { static final String ORGANIZATION_MANAGEMENT_API_BASE_PATH = "/organizations"; static final String ORGANIZATION_CONFIGS_API_BASE_PATH = "/organization-configs"; static final String ORGANIZATION_DISCOVERY_API_PATH = "/discovery"; + static final String ORGANIZATION_META_ATTRIBUTES_API_PATH = "/meta-attributes"; static final String PATH_SEPARATOR = "/"; protected static final String ORGANIZATION_ID = "id"; @@ -65,6 +66,9 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { protected static final String START_INDEX_PATH_PARAM = "startIndex"; protected static final String ORGANIZATION_NAME_ATTRIBUTE = "organizationName"; + protected static final String ORGANIZATION_ID_ATTRIBUTE = "organizationId"; + protected static final String ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE = "attribute"; + protected static final String ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES = "attributes"; protected static final String LINK_REL_PREVIOUS = "previous"; protected static final String LINK_REL_NEXT = "next"; @@ -85,6 +89,7 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { protected static final int NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS = 20; protected static final int DEFAULT_ORG_LIMIT = 15; + protected static final int DEFAULT_META_ATTRIBUTES_LIMIT = 15; protected static String swaggerDefinition; protected OAuth2RestClient oAuth2RestClient; diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java index 947d2508d37..646c2919ed9 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java @@ -81,6 +81,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.equalTo; @@ -110,6 +111,7 @@ public class OrganizationManagementSuccessTest extends OrganizationManagementBas private String b2bApplicationID; private HttpClient client; private List> organizations; + private List> metaAttributes; protected OAuth2RestClient restClient; @@ -1135,6 +1137,193 @@ public void testDeleteOrganizationsForPagination() { Assert.assertTrue(organizations.isEmpty(), "All organizations should be deleted, but the list is not empty."); } + @Test(groups = "organizationMetaAttributesPaginationTests", dependsOnMethods = + "testDeleteOrganizationsForPagination") + public void createOrganizationsForMetaAttributesPaginationTests() throws JSONException { + + organizations = createOrganizations(1); + assertEquals(organizations.size(), 1); + } + + @Test(groups = "organizationMetaAttributesPaginationTests", + dependsOnMethods = "createOrganizationsForMetaAttributesPaginationTests") + public void testAddMetaAttributesToOrganizations() { + + // Sorted in the order. + String[] attributes = + {"1", "2", "3", ":", "@", "A", "B", "C", "LMN", "PQR", "STU", "a", "b", "c", "fg", "jKL", "mNo", "x", + "y", "z"}; + metaAttributes = new ArrayList<>(); + + for (Map org : organizations) { + String organizationId = org.get(ORGANIZATION_ID); + for (String attribute : attributes) { + String requestBody = + "[{\"operation\":\"ADD\",\"path\":\"/attributes/" + attribute + "\",\"value\":\"value-" + + attribute + "\"}]"; + + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/" + organizationId; + Response response = getResponseOfPatch(endpointURL, requestBody); + + validateHttpStatusCode(response, HttpStatus.SC_OK); + + // Save the attributes to the instance variable. + Map attrMap = new HashMap<>(); + attrMap.put(ORGANIZATION_ID_ATTRIBUTE, organizationId); + attrMap.put(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE, attribute); + metaAttributes.add(attrMap); + } + } + int expectedAttributesSize = organizations.size() * attributes.length; + Assert.assertEquals(metaAttributes.size(), expectedAttributesSize, + "Meta attributes were not added successfully."); + } + + @DataProvider(name = "metaAttributesLimitValidationDataProvider") + public Object[][] metaAttributesLimitValidationDataProvider() { + + return new Object[][]{ + {1}, {2}, {3}, {5}, {10}, {13} + }; + } + + @Test(groups = "organizationMetaAttributesPaginationTests", + dependsOnMethods = "testAddMetaAttributesToOrganizations", + dataProvider = "metaAttributesLimitValidationDataProvider") + public void testGetPaginatedMetaAttributesWithLimit(int limit) { + + String endpointURL = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + + LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false; + Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); + + validateHttpStatusCode(response, HttpStatus.SC_OK); + List attributes = response.jsonPath().getList(ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES); + Assert.assertEquals(attributes.size(), limit); + + // Validate the order of the returned attributes. + for (int i = 0; i < limit; i++) { + Assert.assertEquals(attributes.get(i), metaAttributes.get(i).get(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE)); + } + } + + @Test(groups = "organizationMetaAttributesPaginationTests", + dependsOnMethods = "testGetMetaAttributesPaginationForNumericEdgeCasesOfLimit", + dataProvider = "metaAttributesLimitValidationDataProvider") + public void testGetPaginatedMetaAttributes(int limit) { + + String after = null; + String before = null; + + // Prepare a sorted list of expected attributes (in ascending order). + List expectedAttributes = + metaAttributes.stream().map(attr -> attr.get(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE)).sorted() + .collect(Collectors.toList()); + + // Forward Pagination. + int startIndex = 0; + do { + String endpointURL = getMetaAttributesEndpoint(true, limit, after, before); + Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); + validateHttpStatusCode(response, HttpStatus.SC_OK); + + List returnedMetaAttributes = + response.jsonPath().getList(ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES); + List expectedMetaAttributes = + expectedAttributes.subList(startIndex, startIndex + returnedMetaAttributes.size()); + + after = getLink(response.jsonPath().getList(LINKS_PATH_PARAM), LINK_REL_NEXT); + before = getLink(response.jsonPath().getList(LINKS_PATH_PARAM), LINK_REL_PREVIOUS); + + Assert.assertEquals(returnedMetaAttributes, expectedMetaAttributes, + "Attributes in the response do not match the expected order."); + validatePaginationLinksForOrganizationDiscovery(before == null, after == null, after, before); + validateReturnedMetaAttributesOrder(startIndex, returnedMetaAttributes); + + startIndex += limit; + } while (after != null); + + // Reset the start index for reverse validation. + startIndex -= 2 * limit; + + // Backward Pagination. + do { + String endpointURL = getMetaAttributesEndpoint(false, limit, after, before); + Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); + validateHttpStatusCode(response, HttpStatus.SC_OK); + + List returnedMetaAttributes = + response.jsonPath().getList(ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES); + List expectedMetaAttributes = + expectedAttributes.subList(startIndex, startIndex + returnedMetaAttributes.size()); + + after = getLink(response.jsonPath().getList(LINKS_PATH_PARAM), LINK_REL_NEXT); + before = getLink(response.jsonPath().getList(LINKS_PATH_PARAM), LINK_REL_PREVIOUS); + + Assert.assertEquals(returnedMetaAttributes, expectedMetaAttributes, + "Attributes in the response do not match the expected order."); + validatePaginationLinksForOrganizationDiscovery(before == null, after == null, after, before); + validateReturnedMetaAttributesOrder(startIndex, returnedMetaAttributes); + + startIndex -= limit; + } while (before != null); + } + + @DataProvider(name = "metaAttributesPaginationNumericEdgeCasesOfLimitDataProvider") + public Object[][] metaAttributesPaginationNumericEdgeCasesOfLimitDataProvider() { + + return new Object[][]{ + {0}, {20}, {25} + }; + } + + @Test(groups = "organizationMetaAttributesPaginationTests", dependsOnMethods = "testGetPaginatedMetaAttributesWithLimit", + dataProvider = "metaAttributesPaginationNumericEdgeCasesOfLimitDataProvider") + public void testGetMetaAttributesPaginationForNumericEdgeCasesOfLimit(int limit) { + + String endpointURL = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + + LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false; + Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); + validateHttpStatusCode(response, HttpStatus.SC_OK); + + if (limit == 0) { + Assert.assertNull(response.jsonPath().getList(ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES)); + } else { + List attributes = response.jsonPath().getList(ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES); + Assert.assertEquals(attributes.size(), metaAttributes.size()); + } + } + + @DataProvider(name = "metaAttributesPaginationNonNumericEdgeCasesOfLimitProvider") + public Object[][] metaAttributesPaginationNonNumericEdgeCasesOfLimitProvider() { + + return new Object[][]{ + {LIMIT_QUERY_PARAM + EQUAL}, // Case with limit= (no value), default limit is 15. + {""} // Case with no limit parameter, default limit is 15. + }; + } + + @Test(groups = "organizationMetaAttributesPaginationTests", dependsOnMethods = "testGetPaginatedMetaAttributesWithLimit", + dataProvider = "metaAttributesPaginationNonNumericEdgeCasesOfLimitProvider") + public void testGetMetaAttributesForNonNumericEdgeCasesOfLimit(String limitQueryParam) { + + String endpointURL = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + + RECURSIVE_QUERY_PARAM + EQUAL + false + AMPERSAND + limitQueryParam; + Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); + validateHttpStatusCode(response, HttpStatus.SC_OK); + + List attributes = response.jsonPath().getList(ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES); + + if (metaAttributes.size() <= DEFAULT_META_ATTRIBUTES_LIMIT) { + Assert.assertEquals(attributes.size(), metaAttributes.size()); + } else { + Assert.assertEquals(attributes.size(), DEFAULT_META_ATTRIBUTES_LIMIT); + validateNextLinkBasedOnMetaAttributeCount(response); + } + } + private void validateNextLink(Response response, boolean expectNextLink) { List> links = response.jsonPath().getList(LINKS_PATH_PARAM); @@ -1413,6 +1602,27 @@ private void validateCommonAssertions(Response response, int expectedTotalResult COUNT_MISMATCH_ERROR); } + private void validateNextLinkBasedOnMetaAttributeCount(Response response) { + + List> links = response.jsonPath().getList(LINKS_PATH_PARAM); + if (metaAttributes.size() > DEFAULT_META_ATTRIBUTES_LIMIT) { + Assert.assertNotNull(getLink(links, LINK_REL_NEXT), + "'next' link should be present when meta attributes exceed the default limit of 15."); + } else { + Assert.assertNull(links, + "'links' should not be present when meta attributes are within the default limit."); + } + } + + private void validateReturnedMetaAttributesOrder(int startIndex, List attributes) { + + for (int i = 0; i < attributes.size(); i++) { + String expectedAttribute = metaAttributes.get(startIndex + i).get(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE); + Assert.assertEquals(attributes.get(i), expectedAttribute, + "The attribute at index " + i + " does not match the expected value."); + } + } + private String getLink(List> links, String rel) { for (Map link : links) { @@ -1482,6 +1692,19 @@ private void addEmailDomainsToOrganization(String organizationId, String... doma validateHttpStatusCode(response, HttpStatus.SC_CREATED); } + private String getMetaAttributesEndpoint(boolean isForward, int limit, String after, String before) { + + if (isForward) { + return ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + + LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false + + (after != null ? AMPERSAND + AFTER_QUERY_PARAM + EQUAL + after : ""); + } else { + return ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + + LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false + + (before != null ? AMPERSAND + BEFORE_QUERY_PARAM + EQUAL + before : ""); + } + } + private void addReturnedOrganizationsToList(List> returnedOrganizations, List accumulatedOrganizationNames) { From 311546c24d7116e8555005363507bccb3b7f4606 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Wed, 4 Sep 2024 18:35:21 +0530 Subject: [PATCH 2/7] Add faliure tests for orgnaization meta-attributes GET --- .../v1/OrganizationManagementFailureTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementFailureTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementFailureTest.java index b9757a4066a..3b3c6a11634 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementFailureTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementFailureTest.java @@ -454,4 +454,33 @@ public void testGetPaginatedOrganizationsDiscoveryWithInvalidOffsetAndLimitZero( validateErrorResponse(response, HttpStatus.SC_INTERNAL_SERVER_ERROR, ERROR_CODE_SERVER_ERROR); } + @Test(dependsOnMethods = "testGetPaginatedOrganizationsDiscoveryWithInvalidLimitAndOffset") + public void testGetPaginatedMetaAttributesWithInvalidLimit() { + + String invalidLimitUrl = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + + LIMIT_QUERY_PARAM + EQUAL + "-1"; + Response response = getResponseOfGetWithOAuth2(invalidLimitUrl, m2mToken); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, ERROR_CODE_BAD_REQUEST); + } + + @Test(dependsOnMethods = "testGetPaginatedMetaAttributesWithInvalidLimit") + public void testGetPaginatedMetaAttributesWithInvalidAfterCursor() { + + String invalidAfterCursorUrl = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + + LIMIT_QUERY_PARAM + EQUAL + "10" + AMPERSAND + AFTER_QUERY_PARAM + EQUAL + INVALID_CURSOR; + Response response = getResponseOfGetWithOAuth2(invalidAfterCursorUrl, m2mToken); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, ERROR_CODE_INVALID_PAGINATION_CURSOR); + } + + @Test(dependsOnMethods = "testGetPaginatedMetaAttributesWithInvalidAfterCursor") + public void testGetPaginatedMetaAttributesWithInvalidBeforeCursor() { + + String invalidBeforeCursorUrl = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + + LIMIT_QUERY_PARAM + EQUAL + "10" + AMPERSAND + BEFORE_QUERY_PARAM + EQUAL + INVALID_CURSOR; + Response response = getResponseOfGetWithOAuth2(invalidBeforeCursorUrl, m2mToken); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, ERROR_CODE_INVALID_PAGINATION_CURSOR); + } } From 4bfe3e9891e268fae589c7a40ab5d741c81965da Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Wed, 4 Sep 2024 19:19:31 +0530 Subject: [PATCH 3/7] Complete success tests for orgnaization meta-attributes GET --- .../v1/OrganizationManagementSuccessTest.java | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java index 646c2919ed9..a374e6f55fb 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java @@ -90,6 +90,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; import static org.wso2.identity.integration.test.restclients.RestBaseClient.API_SERVER_PATH; import static org.wso2.identity.integration.test.restclients.RestBaseClient.CONTENT_TYPE_ATTRIBUTE; import static org.wso2.identity.integration.test.restclients.RestBaseClient.ORGANIZATION_PATH; @@ -110,6 +111,7 @@ public class OrganizationManagementSuccessTest extends OrganizationManagementBas private String switchedM2MToken; private String b2bApplicationID; private HttpClient client; + private Map organizationForMetaAttributes; private List> organizations; private List> metaAttributes; @@ -1143,6 +1145,11 @@ public void createOrganizationsForMetaAttributesPaginationTests() throws JSONExc organizations = createOrganizations(1); assertEquals(organizations.size(), 1); + + organizationForMetaAttributes = organizations.get(0); + assertNotNull(organizationForMetaAttributes); + + organizations.clear(); } @Test(groups = "organizationMetaAttributesPaginationTests", @@ -1155,30 +1162,33 @@ public void testAddMetaAttributesToOrganizations() { "y", "z"}; metaAttributes = new ArrayList<>(); - for (Map org : organizations) { - String organizationId = org.get(ORGANIZATION_ID); - for (String attribute : attributes) { - String requestBody = - "[{\"operation\":\"ADD\",\"path\":\"/attributes/" + attribute + "\",\"value\":\"value-" + - attribute + "\"}]"; - - String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/" + organizationId; - Response response = getResponseOfPatch(endpointURL, requestBody); - - validateHttpStatusCode(response, HttpStatus.SC_OK); - - // Save the attributes to the instance variable. - Map attrMap = new HashMap<>(); - attrMap.put(ORGANIZATION_ID_ATTRIBUTE, organizationId); - attrMap.put(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE, attribute); - metaAttributes.add(attrMap); - } + String organizationId = organizationForMetaAttributes.get(ORGANIZATION_ID); + assertNotNull(organizationId, "Organization ID should not be null"); + + for (String attribute : attributes) { + String requestBody = + "[{\"operation\":\"ADD\",\"path\":\"/attributes/" + attribute + "\",\"value\":\"value-" + + attribute + "\"}]"; + + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/" + organizationId; + Response response = getResponseOfPatch(endpointURL, requestBody); + + validateHttpStatusCode(response, HttpStatus.SC_OK); + + // Save the attributes to the instance variable. + Map attrMap = new HashMap<>(); + attrMap.put(ORGANIZATION_ID_ATTRIBUTE, organizationId); + attrMap.put(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE, attribute); + metaAttributes.add(attrMap); } - int expectedAttributesSize = organizations.size() * attributes.length; + + int expectedAttributesSize = attributes.length; Assert.assertEquals(metaAttributes.size(), expectedAttributesSize, - "Meta attributes were not added successfully."); + String.format("Expected %d meta attributes, but found %d.", expectedAttributesSize, + metaAttributes.size())); } + @DataProvider(name = "metaAttributesLimitValidationDataProvider") public Object[][] metaAttributesLimitValidationDataProvider() { @@ -1324,6 +1334,14 @@ public void testGetMetaAttributesForNonNumericEdgeCasesOfLimit(String limitQuery } } + @Test(dependsOnGroups = "organizationMetaAttributesPaginationTests") + public void testDeleteOrganizationsForMetaAttributesPagination() { + + deleteSingleOrganization(organizationForMetaAttributes); + + Assert.assertTrue(organizations.isEmpty(), "All organizations should be deleted, but the list is not empty."); + } + private void validateNextLink(Response response, boolean expectNextLink) { List> links = response.jsonPath().getList(LINKS_PATH_PARAM); From 06d2644fb2832c226fc8063fc44d886a9dfb2e6c Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Wed, 4 Sep 2024 19:26:10 +0530 Subject: [PATCH 4/7] Formt success tests for orgnaization meta-attributes GET --- .../management/v1/OrganizationManagementSuccessTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java index a374e6f55fb..f7ab7c9ab12 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java @@ -1188,7 +1188,6 @@ public void testAddMetaAttributesToOrganizations() { metaAttributes.size())); } - @DataProvider(name = "metaAttributesLimitValidationDataProvider") public Object[][] metaAttributesLimitValidationDataProvider() { From d1921f987aae0800b4ba25ddc21c7274bca95774 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Thu, 5 Sep 2024 00:28:29 +0530 Subject: [PATCH 5/7] Refactor getMetaAttributesEndpoint method to reuse base endpoint logic, reducing code duplication. --- .../v1/OrganizationManagementSuccessTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java index f7ab7c9ab12..f18b774de55 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java @@ -1711,14 +1711,14 @@ private void addEmailDomainsToOrganization(String organizationId, String... doma private String getMetaAttributesEndpoint(boolean isForward, int limit, String after, String before) { + String baseEndpoint = ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit + + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false; + if (isForward) { - return ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + - LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false + - (after != null ? AMPERSAND + AFTER_QUERY_PARAM + EQUAL + after : ""); + return baseEndpoint + (after != null ? AMPERSAND + AFTER_QUERY_PARAM + EQUAL + after : ""); } else { - return ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + - LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false + - (before != null ? AMPERSAND + BEFORE_QUERY_PARAM + EQUAL + before : ""); + return baseEndpoint + (before != null ? AMPERSAND + BEFORE_QUERY_PARAM + EQUAL + before : ""); } } From 7f95c8efe6686a47acfec90aa0973af6e6e3bcc7 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Fri, 6 Sep 2024 13:18:58 +0530 Subject: [PATCH 6/7] Use the same organizations which used for organization GET and organization discovery GET in the organization meta attributes GET --- .../v1/OrganizationManagementBaseTest.java | 3 +- .../v1/OrganizationManagementSuccessTest.java | 173 +++++++++--------- 2 files changed, 92 insertions(+), 84 deletions(-) diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java index 3cd92566c43..a5fd314ce90 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java @@ -66,8 +66,6 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { protected static final String START_INDEX_PATH_PARAM = "startIndex"; protected static final String ORGANIZATION_NAME_ATTRIBUTE = "organizationName"; - protected static final String ORGANIZATION_ID_ATTRIBUTE = "organizationId"; - protected static final String ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE = "attribute"; protected static final String ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES = "attributes"; protected static final String LINK_REL_PREVIOUS = "previous"; @@ -89,6 +87,7 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { protected static final int NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS = 20; protected static final int DEFAULT_ORG_LIMIT = 15; + protected static final int NUM_OF_ORGANIZATIONS_WITH_META_ATTRIBUTES = 3; protected static final int DEFAULT_META_ATTRIBUTES_LIMIT = 15; protected static String swaggerDefinition; diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java index f18b774de55..8fc2a7ec91f 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java @@ -74,13 +74,17 @@ import java.io.IOException; import java.net.URI; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Random; +import java.util.Set; import java.util.stream.Collectors; import static io.restassured.RestAssured.given; @@ -111,9 +115,8 @@ public class OrganizationManagementSuccessTest extends OrganizationManagementBas private String switchedM2MToken; private String b2bApplicationID; private HttpClient client; - private Map organizationForMetaAttributes; private List> organizations; - private List> metaAttributes; + private List metaAttributes; protected OAuth2RestClient restClient; @@ -210,7 +213,7 @@ public void createApplicationForSelfOrganizationOnboardService() throws IOExcept public void getM2MAccessToken() throws Exception { OpenIDConnectConfiguration openIDConnectConfiguration = oAuth2RestClient - .getOIDCInboundDetails(selfServiceAppId); + .getOIDCInboundDetails(selfServiceAppId); selfServiceAppClientId = openIDConnectConfiguration.getClientId(); selfServiceAppClientSecret = openIDConnectConfiguration.getClientSecret(); AuthorizationGrant clientCredentialsGrant = new ClientCredentialsGrant(); @@ -220,7 +223,7 @@ public void getM2MAccessToken() throws Exception { Scope scope = new Scope("SYSTEM"); URI tokenEndpoint = new URI(getTenantQualifiedURL(OAuth2Constant.ACCESS_TOKEN_ENDPOINT, - tenantInfo.getDomain())); + tenantInfo.getDomain())); TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, clientCredentialsGrant, scope); HTTPResponse tokenHTTPResp = request.toHTTPRequest().send(); Assert.assertNotNull(tokenHTTPResp, "Access token http response is null."); @@ -256,7 +259,7 @@ public void testSelfOnboardOrganization() throws IOException { public void testGetOrganization() { Response response = getResponseOfGet(ORGANIZATION_MANAGEMENT_API_BASE_PATH + PATH_SEPARATOR - + organizationID); + + organizationID); validateHttpStatusCode(response, HttpStatus.SC_OK); Assert.assertNotNull(response.asString()); response.then() @@ -269,7 +272,7 @@ public void testGetOrganization() { @DataProvider(name = "dataProviderForFilterOrganizations") public Object[][] dataProviderForFilterOrganizations() { - return new Object[][] { + return new Object[][]{ {"name co G", false, false}, {"attributes.Country co S", true, false}, {"attributes.Country eq Sri Lanka and name co Greater", true, false}, @@ -325,7 +328,7 @@ public void switchM2MToken() throws IOException, ParseException, InterruptedExce HttpPost httpPost = new HttpPost(getTenantQualifiedURL(OAuth2Constant.ACCESS_TOKEN_ENDPOINT, tenant)); httpPost.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64( - (selfServiceAppClientId + ":" + selfServiceAppClientSecret).getBytes()))); + (selfServiceAppClientId + ":" + selfServiceAppClientSecret).getBytes()))); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); httpPost.setEntity(new UrlEncodedFormEntity(urlParameters)); @@ -345,7 +348,7 @@ public void createUserInOrganization() throws IOException { String body = readResource("add-admin-user-in-organization-request-body.json"); HttpPost request = new HttpPost(serverURL + TENANT_PATH + tenant + PATH_SEPARATOR + ORGANIZATION_PATH - + SCIM2_USERS_ENDPOINT); + + SCIM2_USERS_ENDPOINT); Header[] headerList = new Header[2]; headerList[0] = new BasicHeader("Authorization", "Bearer " + switchedM2MToken); headerList[1] = new BasicHeader(CONTENT_TYPE_ATTRIBUTE, "application/scim+json"); @@ -379,7 +382,7 @@ public void shareB2BApplication() throws JSONException { return; } String shareApplicationUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/" + SUPER_ORGANIZATION_ID - + "/applications/" + b2bApplicationID + "/share"; + + "/applications/" + b2bApplicationID + "/share"; org.json.JSONObject shareAppObject = new org.json.JSONObject(); shareAppObject.put("shareWithAllChildren", true); getResponseOfPost(shareApplicationUrl, shareAppObject.toString()); @@ -392,7 +395,7 @@ public void unShareB2BApplication() throws JSONException { return; } String shareApplicationUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/" + SUPER_ORGANIZATION_ID - + "/applications/" + b2bApplicationID + "/share"; + + "/applications/" + b2bApplicationID + "/share"; org.json.JSONObject shareAppObject = new org.json.JSONObject(); shareAppObject.put("shareWithAllChildren", false); getResponseOfPost(shareApplicationUrl, shareAppObject.toString()); @@ -404,7 +407,7 @@ public void testOnboardChildOrganization() throws IOException { String body = readResource("add-smaller-hospital-organization-request-body.json"); body = body.replace("${parentId}", organizationID); HttpPost request = new HttpPost(serverURL + TENANT_PATH + tenant + PATH_SEPARATOR + ORGANIZATION_PATH - + API_SERVER_PATH + ORGANIZATION_MANAGEMENT_API_BASE_PATH); + + API_SERVER_PATH + ORGANIZATION_MANAGEMENT_API_BASE_PATH); Header[] headerList = new Header[3]; headerList[0] = new BasicHeader("Authorization", "Bearer " + switchedM2MToken); headerList[1] = new BasicHeader(CONTENT_TYPE_ATTRIBUTE, "application/json"); @@ -423,7 +426,7 @@ public void testOnboardChildOrganization() throws IOException { @DataProvider(name = "dataProviderForGetOrganizationsMetaAttributes") public Object[][] dataProviderForGetOrganizationsMetaAttributes() { - return new Object[][] { + return new Object[][]{ {"attributes eq Country", false, false}, {"attributes sw C and attributes ew try", false, false}, {"attributes eq Region", true, false}, @@ -431,8 +434,8 @@ public Object[][] dataProviderForGetOrganizationsMetaAttributes() { }; } - @Test(dependsOnMethods = "testOnboardChildOrganization", - dataProvider = "dataProviderForGetOrganizationsMetaAttributes") + @Test(dependsOnMethods = "testOnboardChildOrganization", + dataProvider = "dataProviderForGetOrganizationsMetaAttributes") public void testGetOrganizationsMetaAttributes(String filter, boolean isRecursive, boolean expectEmptyList) { String query = "?filter=" + filter + "&limit=1&recursive=" + isRecursive; @@ -516,7 +519,7 @@ public void testGetDiscoveryAttributesOfOrganizations() { public void testGetDiscoveryAttributesOfOrganization() { String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + PATH_SEPARATOR + organizationID - + ORGANIZATION_DISCOVERY_API_PATH; + + ORGANIZATION_DISCOVERY_API_PATH; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); response.then() .log().ifValidationFails() @@ -530,7 +533,7 @@ public void testGetDiscoveryAttributesOfOrganization() { public void testUpdateDiscoveryAttributesOfOrganization() throws IOException { String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + PATH_SEPARATOR + organizationID - + ORGANIZATION_DISCOVERY_API_PATH; + + ORGANIZATION_DISCOVERY_API_PATH; String requestBody = readResource("update-discovery-attributes-request-body.json"); Response response = getResponseOfPutWithOAuth2(endpointURL, requestBody, m2mToken); response.then() @@ -568,7 +571,7 @@ public void testCheckDiscoveryAttributeExists(String requestBodyFileName, boolea public void testDeleteDiscoveryAttributesOfOrganization() { String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + PATH_SEPARATOR + organizationID - + ORGANIZATION_DISCOVERY_API_PATH; + + ORGANIZATION_DISCOVERY_API_PATH; Response response = getResponseOfDeleteWithOAuth2(endpointURL, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_NO_CONTENT); } @@ -585,8 +588,8 @@ public void testDeleteDiscoveryConfig() { public void testDeleteChildOrganization() throws IOException { HttpDelete request = new HttpDelete(serverURL + TENANT_PATH + tenant + PATH_SEPARATOR + ORGANIZATION_PATH - + API_SERVER_PATH + ORGANIZATION_MANAGEMENT_API_BASE_PATH + PATH_SEPARATOR - + childOrganizationID); + + API_SERVER_PATH + ORGANIZATION_MANAGEMENT_API_BASE_PATH + PATH_SEPARATOR + + childOrganizationID); Header[] headerList = new Header[1]; headerList[0] = new BasicHeader("Authorization", "Bearer " + switchedM2MToken); request.setHeaders(headerList); @@ -767,7 +770,8 @@ public void testGetPaginatedOrganizationsForNumericEdgeCasesOfLimit(int limit) { @DataProvider(name = "organizationPaginationNonNumericEdgeCasesOfLimitDataProvider") public Object[][] organizationPaginationNonNumericEdgeCasesOfLimitProvider() { - return new Object[][] { + + return new Object[][]{ {AMPERSAND + LIMIT_QUERY_PARAM + EQUAL}, // Test case 1: URL with LIMIT_QUERY_PARAM but no value. {""} // Test case 2: URL without LIMIT_QUERY_PARAM. }; @@ -777,8 +781,9 @@ public Object[][] organizationPaginationNonNumericEdgeCasesOfLimitProvider() { dataProvider = "organizationPaginationNonNumericEdgeCasesOfLimitDataProvider") public void testGetPaginatedOrganizationsForNonNumericEdgeCasesOfLimit(String limitQueryParam) { - String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + RECURSIVE_QUERY_PARAM + EQUAL + FALSE + - limitQueryParam; + String endpointURL = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + RECURSIVE_QUERY_PARAM + EQUAL + FALSE + + limitQueryParam; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); @@ -967,7 +972,8 @@ public void testGetPaginatedOrganizationsDiscoveryForNumericEdgeCasesOfLimit(int @DataProvider(name = "organizationDiscoveryPaginationNonNumericEdgeCasesOfLimitDataProvider") public Object[][] organizationDiscoveryPaginationNonNumericEdgeCasesOfLimitProvider() { - return new Object[][] { + + return new Object[][]{ {AMPERSAND + LIMIT_QUERY_PARAM + EQUAL}, // Test case 1: URL with LIMIT_QUERY_PARAM but no value. {""} // Test case 2: URL without LIMIT_QUERY_PARAM. }; @@ -1069,7 +1075,7 @@ public void testGetPaginatedOrganizationsDiscoveryForNumericEdgeCasesOfOffsetAnd validateOrganizationDiscoveryOffsetIsInLinks(previousLink, expectedOffset); } - + @Test(groups = "organizationDiscoveryPaginationTests", dependsOnMethods = "testAddEmailDomainsToOrganization") public void testGetPaginatedOrganizationsDiscoveryForNonNumericEdgeCasesOfOffsetAndOffsetWithLimit() { @@ -1129,63 +1135,39 @@ public void testDisableEmailDomainDiscovery() { Assert.assertFalse(isEnabled, "Email domain discovery was not successfully disabled."); } - @Test(dependsOnGroups = "organizationDiscoveryPaginationTests") - public void testDeleteOrganizationsForPagination() { - - for (Map org : new ArrayList<>(organizations)) { - deleteSingleOrganization(org); - } - - Assert.assertTrue(organizations.isEmpty(), "All organizations should be deleted, but the list is not empty."); - } - - @Test(groups = "organizationMetaAttributesPaginationTests", dependsOnMethods = - "testDeleteOrganizationsForPagination") - public void createOrganizationsForMetaAttributesPaginationTests() throws JSONException { - - organizations = createOrganizations(1); - assertEquals(organizations.size(), 1); - - organizationForMetaAttributes = organizations.get(0); - assertNotNull(organizationForMetaAttributes); - - organizations.clear(); - } - @Test(groups = "organizationMetaAttributesPaginationTests", - dependsOnMethods = "createOrganizationsForMetaAttributesPaginationTests") + dependsOnGroups = "organizationDiscoveryPaginationTests") public void testAddMetaAttributesToOrganizations() { + // Initialize meta attributes in sorted order. + String[] attributes = {"1", "2", "3", ":", "@", "A", "B", "C", "LMN", "PQR", "STU", "a", "b", "c", "fg", "jKL", + "mNo", "x", "y", "z"}; - // Sorted in the order. - String[] attributes = - {"1", "2", "3", ":", "@", "A", "B", "C", "LMN", "PQR", "STU", "a", "b", "c", "fg", "jKL", "mNo", "x", - "y", "z"}; - metaAttributes = new ArrayList<>(); + metaAttributes = new ArrayList<>(Arrays.asList(attributes)); + List> addedAttributes = new ArrayList<>(); + List shuffledAttributes = new ArrayList<>(Arrays.asList(attributes)); + Collections.shuffle(shuffledAttributes); // Randomize attribute distribution. - String organizationId = organizationForMetaAttributes.get(ORGANIZATION_ID); - assertNotNull(organizationId, "Organization ID should not be null"); + int remainingAttributes = attributes.length; - for (String attribute : attributes) { - String requestBody = - "[{\"operation\":\"ADD\",\"path\":\"/attributes/" + attribute + "\",\"value\":\"value-" + - attribute + "\"}]"; + for (int i = 0; i < NUM_OF_ORGANIZATIONS_WITH_META_ATTRIBUTES; i++) { + String organizationId = organizations.get(i).get(ORGANIZATION_ID); - String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/" + organizationId; - Response response = getResponseOfPatch(endpointURL, requestBody); + int attributesToAssign = calculateAttributesToAssign(remainingAttributes, i); - validateHttpStatusCode(response, HttpStatus.SC_OK); + for (int j = 0; j < attributesToAssign; j++) { + String attribute = shuffledAttributes.remove(0); + String requestBody = createMetaAttributeCreationPatchRequestBody(attribute); + String endpointURL = buildOrganizationApiEndpoint(organizationId); + + Response response = getResponseOfPatch(endpointURL, requestBody); + validateHttpStatusCode(response, HttpStatus.SC_OK); - // Save the attributes to the instance variable. - Map attrMap = new HashMap<>(); - attrMap.put(ORGANIZATION_ID_ATTRIBUTE, organizationId); - attrMap.put(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE, attribute); - metaAttributes.add(attrMap); + addedAttributes.add(createAttributeMap(attribute)); + remainingAttributes--; + } } - int expectedAttributesSize = attributes.length; - Assert.assertEquals(metaAttributes.size(), expectedAttributesSize, - String.format("Expected %d meta attributes, but found %d.", expectedAttributesSize, - metaAttributes.size())); + validateAttributesAddedSuccessfully(addedAttributes); } @DataProvider(name = "metaAttributesLimitValidationDataProvider") @@ -1212,7 +1194,7 @@ public void testGetPaginatedMetaAttributesWithLimit(int limit) { // Validate the order of the returned attributes. for (int i = 0; i < limit; i++) { - Assert.assertEquals(attributes.get(i), metaAttributes.get(i).get(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE)); + Assert.assertEquals(attributes.get(i), metaAttributes.get(i)); } } @@ -1224,11 +1206,6 @@ public void testGetPaginatedMetaAttributes(int limit) { String after = null; String before = null; - // Prepare a sorted list of expected attributes (in ascending order). - List expectedAttributes = - metaAttributes.stream().map(attr -> attr.get(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE)).sorted() - .collect(Collectors.toList()); - // Forward Pagination. int startIndex = 0; do { @@ -1239,7 +1216,7 @@ public void testGetPaginatedMetaAttributes(int limit) { List returnedMetaAttributes = response.jsonPath().getList(ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES); List expectedMetaAttributes = - expectedAttributes.subList(startIndex, startIndex + returnedMetaAttributes.size()); + metaAttributes.subList(startIndex, startIndex + returnedMetaAttributes.size()); after = getLink(response.jsonPath().getList(LINKS_PATH_PARAM), LINK_REL_NEXT); before = getLink(response.jsonPath().getList(LINKS_PATH_PARAM), LINK_REL_PREVIOUS); @@ -1264,7 +1241,7 @@ public void testGetPaginatedMetaAttributes(int limit) { List returnedMetaAttributes = response.jsonPath().getList(ORGANIZATION_MULTIPLE_META_ATTRIBUTE_ATTRIBUTES); List expectedMetaAttributes = - expectedAttributes.subList(startIndex, startIndex + returnedMetaAttributes.size()); + metaAttributes.subList(startIndex, startIndex + returnedMetaAttributes.size()); after = getLink(response.jsonPath().getList(LINKS_PATH_PARAM), LINK_REL_NEXT); before = getLink(response.jsonPath().getList(LINKS_PATH_PARAM), LINK_REL_PREVIOUS); @@ -1334,9 +1311,11 @@ public void testGetMetaAttributesForNonNumericEdgeCasesOfLimit(String limitQuery } @Test(dependsOnGroups = "organizationMetaAttributesPaginationTests") - public void testDeleteOrganizationsForMetaAttributesPagination() { + public void testDeleteOrganizationsForPagination() { - deleteSingleOrganization(organizationForMetaAttributes); + for (Map org : new ArrayList<>(organizations)) { + deleteSingleOrganization(org); + } Assert.assertTrue(organizations.isEmpty(), "All organizations should be deleted, but the list is not empty."); } @@ -1619,6 +1598,12 @@ private void validateCommonAssertions(Response response, int expectedTotalResult COUNT_MISMATCH_ERROR); } + private void validateAttributesAddedSuccessfully(List> addedAttributes) { + + Assert.assertEquals(metaAttributes.size(), addedAttributes.size(), + "Meta attributes were not added successfully."); + } + private void validateNextLinkBasedOnMetaAttributeCount(Response response) { List> links = response.jsonPath().getList(LINKS_PATH_PARAM); @@ -1634,7 +1619,7 @@ private void validateNextLinkBasedOnMetaAttributeCount(Response response) { private void validateReturnedMetaAttributesOrder(int startIndex, List attributes) { for (int i = 0; i < attributes.size(); i++) { - String expectedAttribute = metaAttributes.get(startIndex + i).get(ORGANIZATION_META_ATTRIBUTE_ATTRIBUTE); + String expectedAttribute = metaAttributes.get(startIndex + i); Assert.assertEquals(attributes.get(i), expectedAttribute, "The attribute at index " + i + " does not match the expected value."); } @@ -1759,6 +1744,19 @@ private List> createOrganizations(int numberOfOrganizations) return newOrganizations; } + private String createMetaAttributeCreationPatchRequestBody(String attribute) { + + return String.format("[{\"operation\":\"ADD\",\"path\":\"/attributes/%s\",\"value\":\"value-%s\"}]", + attribute, attribute); + } + + private Map createAttributeMap(String attribute) { + + Map attrMap = new HashMap<>(); + attrMap.put(attribute, "value-" + attribute); + return attrMap; + } + private void deleteSingleOrganization(Map org) { String organizationId = org.get(ORGANIZATION_ID); @@ -1772,6 +1770,12 @@ private void deleteSingleOrganization(Map org) { organizations.remove(org); } + private int calculateAttributesToAssign(int remainingAttributes, int organizationIndex) { + + return Math.min(remainingAttributes, + remainingAttributes / (NUM_OF_ORGANIZATIONS_WITH_META_ATTRIBUTES - organizationIndex)); + } + private String buildQueryUrl(int offset, int limit) { return ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_DISCOVERY_API_PATH + QUESTION_MARK + @@ -1808,4 +1812,9 @@ private String buildQueryUrlWithNoOffsetAndLimit() { return ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_DISCOVERY_API_PATH + QUESTION_MARK + FILTER_QUERY_PARAM + EQUAL; } + + private String buildOrganizationApiEndpoint(String organizationId) { + + return String.format("%s/%s", ORGANIZATION_MANAGEMENT_API_BASE_PATH, organizationId); + } } From 2122b959d805379690765472e0f1c6ae56087016 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Sat, 7 Sep 2024 12:53:32 +0530 Subject: [PATCH 7/7] Format suggestions --- .../v1/OrganizationManagementSuccessTest.java | 73 +++++++++---------- 1 file changed, 33 insertions(+), 40 deletions(-) diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java index 8fc2a7ec91f..1c89ee36195 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementSuccessTest.java @@ -162,7 +162,7 @@ public void testFinish() { @DataProvider(name = "restAPIUserConfigProvider") public static Object[][] restAPIUserConfigProvider() { - return new Object[][]{ + return new Object[][] { {TestUserMode.SUPER_TENANT_ADMIN}, {TestUserMode.TENANT_ADMIN} }; @@ -272,7 +272,7 @@ public void testGetOrganization() { @DataProvider(name = "dataProviderForFilterOrganizations") public Object[][] dataProviderForFilterOrganizations() { - return new Object[][]{ + return new Object[][] { {"name co G", false, false}, {"attributes.Country co S", true, false}, {"attributes.Country eq Sri Lanka and name co Greater", true, false}, @@ -426,7 +426,7 @@ public void testOnboardChildOrganization() throws IOException { @DataProvider(name = "dataProviderForGetOrganizationsMetaAttributes") public Object[][] dataProviderForGetOrganizationsMetaAttributes() { - return new Object[][]{ + return new Object[][] { {"attributes eq Country", false, false}, {"attributes sw C and attributes ew try", false, false}, {"attributes eq Region", true, false}, @@ -547,7 +547,7 @@ public void testUpdateDiscoveryAttributesOfOrganization() throws IOException { @DataProvider(name = "checkDiscoveryAttributes") public Object[][] checkDiscoveryAttributeFilePaths() { - return new Object[][]{ + return new Object[][] { {"check-discovery-attributes-available-request-body.json", true}, {"check-discovery-attributes-unavailable-request-body.json", false} }; @@ -629,7 +629,7 @@ public void createOrganizationsForPaginationTests() throws JSONException { @DataProvider(name = "organizationLimitValidationDataProvider") public Object[][] organizationLimitValidationDataProvider() { - return new Object[][]{ + return new Object[][] { {10}, {20}, {25}, @@ -670,7 +670,7 @@ public void testGetPaginatedOrganizationsWithLimit(int limit) { @DataProvider(name = "organizationPaginationValidationDataProvider") public Object[][] organizationPaginationValidationProvider() { - return new Object[][]{ + return new Object[][] { {1}, {2}, {5}, {6}, {10}, {17} }; } @@ -722,10 +722,9 @@ public void testGetPaginatedOrganizations(int limit) { validateOrganizationsOnPage(secondPageResponse, 2, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); // Step 3: Call the previous page using the 'before' value. - String previousPageUrl = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit - + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + FALSE + AMPERSAND + BEFORE_QUERY_PARAM + EQUAL + - before; + String previousPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + FALSE + AMPERSAND + BEFORE_QUERY_PARAM + EQUAL + + before; Response previousPageResponse = getResponseOfGetWithOAuth2(previousPageUrl, m2mToken); validateHttpStatusCode(previousPageResponse, HttpStatus.SC_OK); @@ -744,7 +743,7 @@ public void testGetPaginatedOrganizations(int limit) { @DataProvider(name = "organizationPaginationNumericEdgeCasesOfLimitDataProvider") public Object[][] organizationPaginationNumericEdgeCasesOfLimitDataProvider() { - return new Object[][]{ + return new Object[][] { {0}, {20}, {25} }; } @@ -753,9 +752,8 @@ public Object[][] organizationPaginationNumericEdgeCasesOfLimitDataProvider() { dataProvider = "organizationPaginationNumericEdgeCasesOfLimitDataProvider") public void testGetPaginatedOrganizationsForNumericEdgeCasesOfLimit(int limit) { - String limitUrl = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + - RECURSIVE_QUERY_PARAM + EQUAL + FALSE; + String limitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit + + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + FALSE; Response response = getResponseOfGetWithOAuth2(limitUrl, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); @@ -771,7 +769,7 @@ public void testGetPaginatedOrganizationsForNumericEdgeCasesOfLimit(int limit) { @DataProvider(name = "organizationPaginationNonNumericEdgeCasesOfLimitDataProvider") public Object[][] organizationPaginationNonNumericEdgeCasesOfLimitProvider() { - return new Object[][]{ + return new Object[][] { {AMPERSAND + LIMIT_QUERY_PARAM + EQUAL}, // Test case 1: URL with LIMIT_QUERY_PARAM but no value. {""} // Test case 2: URL without LIMIT_QUERY_PARAM. }; @@ -781,9 +779,8 @@ public Object[][] organizationPaginationNonNumericEdgeCasesOfLimitProvider() { dataProvider = "organizationPaginationNonNumericEdgeCasesOfLimitDataProvider") public void testGetPaginatedOrganizationsForNonNumericEdgeCasesOfLimit(String limitQueryParam) { - String endpointURL = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + RECURSIVE_QUERY_PARAM + EQUAL + FALSE + - limitQueryParam; + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + RECURSIVE_QUERY_PARAM + EQUAL + + FALSE + limitQueryParam; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); @@ -826,7 +823,7 @@ public void testAddEmailDomainsToOrganization() { @DataProvider(name = "organizationDiscoveryLimitValidationDataProvider") public Object[][] organizationDiscoveryLimitValidationDataProvider() { - return new Object[][]{ + return new Object[][] { {3}, {5}, {10}, {15}, {17}, {20}, {25} }; } @@ -840,10 +837,8 @@ public void testGetPaginatedOrganizationsDiscoveryWithLimit(int limit) { // Loop through each page to test the organization discovery GET API limit. while (offset < NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS) { - String queryUrl = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_DISCOVERY_API_PATH + QUESTION_MARK + - OFFSET_QUERY_PARAM + EQUAL + offset + - AMPERSAND + LIMIT_QUERY_PARAM + EQUAL + limit; + String queryUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_DISCOVERY_API_PATH + QUESTION_MARK + + OFFSET_QUERY_PARAM + EQUAL + offset + AMPERSAND + LIMIT_QUERY_PARAM + EQUAL + limit; Response response = getResponseOfGetWithOAuth2(queryUrl, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); @@ -874,7 +869,7 @@ public void testGetPaginatedOrganizationsDiscoveryWithLimit(int limit) { @DataProvider(name = "organizationDiscoveryPaginationValidationDataProvider") public Object[][] organizationDiscoveryPaginationValidationProvider() { - return new Object[][]{ + return new Object[][] { {1}, {2}, {5}, {6}, {10}, {17} }; } @@ -934,7 +929,7 @@ public void testGetPaginatedOrganizationsDiscovery(int limit) { @DataProvider(name = "organizationDiscoveryPaginationNumericEdgeCasesOfLimitDataProvider") public Object[][] organizationDiscoveryPaginationNumericEdgeCasesOfLimitDataProvider() { - return new Object[][]{ + return new Object[][] { {0, 0}, {0, 20}, {0, 25}, {2, 0}, {2, 20}, {2, 25} }; @@ -973,7 +968,7 @@ public void testGetPaginatedOrganizationsDiscoveryForNumericEdgeCasesOfLimit(int @DataProvider(name = "organizationDiscoveryPaginationNonNumericEdgeCasesOfLimitDataProvider") public Object[][] organizationDiscoveryPaginationNonNumericEdgeCasesOfLimitProvider() { - return new Object[][]{ + return new Object[][] { {AMPERSAND + LIMIT_QUERY_PARAM + EQUAL}, // Test case 1: URL with LIMIT_QUERY_PARAM but no value. {""} // Test case 2: URL without LIMIT_QUERY_PARAM. }; @@ -996,7 +991,7 @@ public void testGetPaginatedOrganizationsDiscoveryForNonNumericEdgeCasesOfLimit( @DataProvider(name = "organizationDiscoveryOffsetValidationDataProvider") public Object[][] organizationDiscoveryOffsetValidationDataProvider() { - return new Object[][]{ + return new Object[][] { {0, 1}, {0, 5}, {0, 10}, {5, 1}, {5, 5}, {5, 10}, {10, 1}, {10, 5}, {10, 10} @@ -1031,7 +1026,7 @@ public void testGetPaginatedOrganizationsDiscoveryWithOffset(int offset, int lim @DataProvider(name = "numericEdgeCasesOfOffsetAndOffsetWithLimitDataProvider") public Object[][] numericEdgeCasesOfOffsetAndOffsetWithLimitDataProvider() { - return new Object[][]{ + return new Object[][] { {20, 5}, {20, 17}, {20, 20}, @@ -1138,6 +1133,7 @@ public void testDisableEmailDomainDiscovery() { @Test(groups = "organizationMetaAttributesPaginationTests", dependsOnGroups = "organizationDiscoveryPaginationTests") public void testAddMetaAttributesToOrganizations() { + // Initialize meta attributes in sorted order. String[] attributes = {"1", "2", "3", ":", "@", "A", "B", "C", "LMN", "PQR", "STU", "a", "b", "c", "fg", "jKL", "mNo", "x", "y", "z"}; @@ -1173,7 +1169,7 @@ public void testAddMetaAttributesToOrganizations() { @DataProvider(name = "metaAttributesLimitValidationDataProvider") public Object[][] metaAttributesLimitValidationDataProvider() { - return new Object[][]{ + return new Object[][] { {1}, {2}, {3}, {5}, {10}, {13} }; } @@ -1183,9 +1179,8 @@ public Object[][] metaAttributesLimitValidationDataProvider() { dataProvider = "metaAttributesLimitValidationDataProvider") public void testGetPaginatedMetaAttributesWithLimit(int limit) { - String endpointURL = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + - LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false; + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); @@ -1258,7 +1253,7 @@ public void testGetPaginatedMetaAttributes(int limit) { @DataProvider(name = "metaAttributesPaginationNumericEdgeCasesOfLimitDataProvider") public Object[][] metaAttributesPaginationNumericEdgeCasesOfLimitDataProvider() { - return new Object[][]{ + return new Object[][] { {0}, {20}, {25} }; } @@ -1267,9 +1262,8 @@ public Object[][] metaAttributesPaginationNumericEdgeCasesOfLimitDataProvider() dataProvider = "metaAttributesPaginationNumericEdgeCasesOfLimitDataProvider") public void testGetMetaAttributesPaginationForNumericEdgeCasesOfLimit(int limit) { - String endpointURL = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + - LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false; + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + false; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); @@ -1284,7 +1278,7 @@ public void testGetMetaAttributesPaginationForNumericEdgeCasesOfLimit(int limit) @DataProvider(name = "metaAttributesPaginationNonNumericEdgeCasesOfLimitProvider") public Object[][] metaAttributesPaginationNonNumericEdgeCasesOfLimitProvider() { - return new Object[][]{ + return new Object[][] { {LIMIT_QUERY_PARAM + EQUAL}, // Case with limit= (no value), default limit is 15. {""} // Case with no limit parameter, default limit is 15. }; @@ -1294,9 +1288,8 @@ public Object[][] metaAttributesPaginationNonNumericEdgeCasesOfLimitProvider() { dataProvider = "metaAttributesPaginationNonNumericEdgeCasesOfLimitProvider") public void testGetMetaAttributesForNonNumericEdgeCasesOfLimit(String limitQueryParam) { - String endpointURL = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + QUESTION_MARK + - RECURSIVE_QUERY_PARAM + EQUAL + false + AMPERSAND + limitQueryParam; + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + ORGANIZATION_META_ATTRIBUTES_API_PATH + + QUESTION_MARK + RECURSIVE_QUERY_PARAM + EQUAL + false + AMPERSAND + limitQueryParam; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK);