From 2d11ad6e5fca2c50238fa007081a6b14b1f99411 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Mon, 19 Aug 2024 00:56:55 +0530 Subject: [PATCH 01/12] Add integration tests for Organizations GET API --- .../v1/OrganizationManagementFailureTest.java | 25 +++ .../v1/OrganizationManagementSuccessTest.java | 197 ++++++++++++++++++ 2 files changed, 222 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 934063e3f32..4506c61396a 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 @@ -372,4 +372,29 @@ public void deleteOrganizations() { .statusCode(HttpStatus.SC_NO_CONTENT); } } + + @Test(dependsOnMethods = "testUpdateDiscoveryAttributesUnauthorized") + public void testGetPaginatedOrganizationsWithInvalidLimit() { + + String invalidLimitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=-1"; + Response response = getResponseOfGetWithOAuth2(invalidLimitUrl, m2mToken); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "UE-10000"); + } + + @Test(dependsOnMethods = "testGetPaginatedOrganizationsWithInvalidLimit") + public void testGetPaginatedOrganizationsWithInvalidAfterCursor() { + + String invalidAfterCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=10&after=invalid-cursor"; + Response response = getResponseOfGetWithOAuth2(invalidAfterCursorUrl, m2mToken); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "ORG-60026"); + } + + @Test(dependsOnMethods = "testGetPaginatedOrganizationsWithInvalidAfterCursor") + public void testGetPaginatedOrganizationsWithInvalidBeforeCursor() { + + String invalidBeforeCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=10&before=invalid-cursor"; + Response response = getResponseOfGetWithOAuth2(invalidBeforeCursorUrl, m2mToken); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "ORG-60026"); + } + } 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 003be89a005..a14b7cc4dee 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 @@ -75,8 +75,10 @@ import java.net.URI; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Optional; import static io.restassured.RestAssured.given; @@ -105,6 +107,8 @@ public class OrganizationManagementSuccessTest extends OrganizationManagementBas private String b2bApplicationID; private HttpClient client; protected OAuth2RestClient restClient; + private final int totalCreatedOrganizations = 20; + private List> createdOrganizations; @Factory(dataProvider = "restAPIUserConfigProvider") public OrganizationManagementSuccessTest(TestUserMode userMode) throws Exception { @@ -604,4 +608,197 @@ private void deleteApplication(String applicationId) throws Exception { oAuth2RestClient.deleteApplication(applicationId); } + + @Test(dependsOnMethods = "testDeleteOrganization") + public void createOrganizationsForPaginationTests() { + + int createdOrganizationsCount = createOrganizations(totalCreatedOrganizations); + + if (createdOrganizationsCount != totalCreatedOrganizations) { + throw new RuntimeException("Failed to create the expected number of organizations for testing."); + } + } + + @DataProvider(name = "paginationLimitsDataProvider") + public Object[][] paginationLimitsDataProvider() { + return new Object[][]{ + {10}, + {20}, + {25}, + }; + } + + @Test(dataProvider = "paginationLimitsDataProvider", dependsOnMethods = "createOrganizationsForPaginationTests") + public void testGetPaginatedOrganizationsWithLimit(int limit) { + + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit; + Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); + + validateHttpStatusCode(response, HttpStatus.SC_OK); + + int actualOrganizationCount = response.jsonPath().getList("organizations").size(); + int expectedOrganizationCount = Math.min(limit, totalCreatedOrganizations); + Assert.assertEquals(actualOrganizationCount, expectedOrganizationCount); + + String nextLink = response.jsonPath().getString("links.find { it.rel == 'next' }.href"); + String afterValue = null; + + if (nextLink != null && nextLink.contains("after=")) { + afterValue = nextLink.substring(nextLink.indexOf("after=") + 6); + } + + String storedAfterValue = afterValue; + + if (totalCreatedOrganizations > limit) { + Assert.assertNotNull(storedAfterValue); + } else { + Assert.assertNull(storedAfterValue); + } + } + + @Test(dependsOnMethods = "createOrganizationsForPaginationTests") + public void testGetPaginatedOrganizationsWithCursor() { + + String after; + String before; + int limit = 2; + int orgLimit = 20; + int largeLimit = 30; + + // Step 1: Call the first page + String firstPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit + "&recursive=false"; + Response firstPageResponse = getResponseOfGetWithOAuth2(firstPageUrl, m2mToken); + + validateHttpStatusCode(firstPageResponse, HttpStatus.SC_OK); + + int firstPageOrgCount = firstPageResponse.jsonPath().getList("organizations").size(); + Assert.assertEquals(firstPageOrgCount, Math.min(limit, totalCreatedOrganizations)); + + List> firstPageLinks = firstPageResponse.jsonPath().getList("links"); + after = getLink(firstPageLinks, "next"); + before = getLink(firstPageLinks, "previous"); + + Assert.assertNotNull(after, "After value should not be null on the first page."); + Assert.assertNull(before, "Before value should be null on the first page."); + + // Step 2: Call the second page using the 'after' value + String secondPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit + + "&recursive=false&after=" + after; + Response secondPageResponse = getResponseOfGetWithOAuth2(secondPageUrl, m2mToken); + + validateHttpStatusCode(secondPageResponse, HttpStatus.SC_OK); + + int secondPageOrgCount = secondPageResponse.jsonPath().getList("organizations").size(); + Assert.assertEquals(secondPageOrgCount, Math.min(limit, totalCreatedOrganizations - firstPageOrgCount)); + + List> secondPageLinks = secondPageResponse.jsonPath().getList("links"); + before = getLink(secondPageLinks, "previous"); + after = getLink(secondPageLinks, "next"); + + Assert.assertNotNull(before, "Before value should not be null on the second page."); + if (totalCreatedOrganizations > limit * 2) { + Assert.assertNotNull(after, "After value should not be null if there are more pages."); + } else { + Assert.assertNull(after, "After value should be null if this is the last page."); + } + + // Step 3: Call the previous page using the 'before' value + String previousPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit + + "&recursive=false&before=" + before; + Response previousPageResponse = getResponseOfGetWithOAuth2(previousPageUrl, m2mToken); + + validateHttpStatusCode(previousPageResponse, HttpStatus.SC_OK); + + int previousPageOrgCount = previousPageResponse.jsonPath().getList("organizations").size(); + Assert.assertEquals(previousPageOrgCount, firstPageOrgCount); + + // Ensure that the organizations match the first page organizations + Assert.assertEquals(previousPageResponse.jsonPath().getList("organizations"), + firstPageResponse.jsonPath().getList("organizations"), + "The previous page should return the same organizations as the first page."); + + // Extract only the name and description from the previousPageResponse for comparison + List> actualOrganizations = previousPageResponse.jsonPath().getList("organizations"); + + List> actualOrganizationsSimplified = new ArrayList<>(); + for (Map org : actualOrganizations) { + Map simplifiedOrg = new HashMap<>(); + simplifiedOrg.put("name", org.get("name")); + simplifiedOrg.put("description", "Test " + org.get("name")); + actualOrganizationsSimplified.add(simplifiedOrg); + } + + // Additional validation: Check that the previousPageResponse contains the last 'limit' organizations + List> expectedLastOrganizations = + createdOrganizations.subList(totalCreatedOrganizations - limit, totalCreatedOrganizations); + Collections.reverse(expectedLastOrganizations); // Reverse the order to match the expected response + Assert.assertEquals(actualOrganizationsSimplified, expectedLastOrganizations, + "The previous page should return the last created organizations."); + + + // Step 4: Test with orgLimit (equal to total org count) + validatePaginationForNonPaginatedLimit(orgLimit); + + // Step 5: Test with largeLimit (greater than total org count) + validatePaginationForNonPaginatedLimit(largeLimit); + + } + + private int createOrganizations(int numberOfOrganizations) { + createdOrganizations = new ArrayList<>(); + int createdOrganizationsCount = 0; + + for (int i = 0; i < numberOfOrganizations; i++) { + String body = String.format("{\"name\": \"Org-%d\", \"description\": \"Test Organization %d\"}", i, i); + Response response = getResponseOfPostWithOAuth2(ORGANIZATION_MANAGEMENT_API_BASE_PATH, body, m2mToken); + + if (response.getStatusCode() == HttpStatus.SC_CREATED) { + // Store the created organization details + Map org = new HashMap<>(); + org.put("name", String.format("Org-%d", i)); + org.put("description", String.format("Test Org-%d", i)); + createdOrganizations.add(org); + + createdOrganizationsCount++; + } else { + throw new RuntimeException("Failed to create organization " + i); + } + } + + return createdOrganizationsCount; + } + + private void validatePaginationForNonPaginatedLimit(int limit) { + + String limitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit + "&recursive=false"; + Response response = getResponseOfGetWithOAuth2(limitUrl, m2mToken); + + validateHttpStatusCode(response, HttpStatus.SC_OK); + + List> links = response.jsonPath().getList("links"); + + Assert.assertNull(links, "Links should be null when all organizations are returned in one page."); + + int returnedOrgCount = response.jsonPath().getList("organizations").size(); + Assert.assertEquals(returnedOrgCount, totalCreatedOrganizations, + "The number of returned organizations should match the total created organizations."); + + } + + private String getLink(List> links, String rel) { + + for (Map link : links) { + if (rel.equals(link.get("rel"))) { + String href = link.get("href"); + if (href.contains("after=")) { + return href.substring(href.indexOf("after=") + 6); + } else if (href.contains("before=")) { + return href.substring(href.indexOf("before=") + 7); + } + } + } + return null; + + } + } From e5ad362ba1e852ce60b849f8d826d9b25303cf9e Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Tue, 20 Aug 2024 00:53:00 +0530 Subject: [PATCH 02/12] Refactor OrganizationManagementSuccessTest: use JSON builder, add constants, improve readability --- .../v1/OrganizationManagementBaseTest.java | 6 +++ .../v1/OrganizationManagementSuccessTest.java | 47 ++++++++++--------- 2 files changed, 31 insertions(+), 22 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 d81b8be1523..2c4b0a0e70a 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 @@ -44,6 +44,12 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { static final String ORGANIZATION_DISCOVERY_API_PATH = "/discovery"; static final String PATH_SEPARATOR = "/"; + static final String ORGANIZATION_NAME = "name"; + static final String ORGANIZATION_DESCRIPTION = "description"; + static final String LIMIT_ARG = "?limit="; + static final String AFTER_CURSOR_ARG = "after"; + static final String BEFORE_CURSOR_ARG = "before"; + 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 a14b7cc4dee..d4f1e42f3ac 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 @@ -610,7 +610,7 @@ private void deleteApplication(String applicationId) throws Exception { } @Test(dependsOnMethods = "testDeleteOrganization") - public void createOrganizationsForPaginationTests() { + public void createOrganizationsForPaginationTests() throws JSONException { int createdOrganizationsCount = createOrganizations(totalCreatedOrganizations); @@ -621,6 +621,7 @@ public void createOrganizationsForPaginationTests() { @DataProvider(name = "paginationLimitsDataProvider") public Object[][] paginationLimitsDataProvider() { + return new Object[][]{ {10}, {20}, @@ -631,7 +632,7 @@ public Object[][] paginationLimitsDataProvider() { @Test(dataProvider = "paginationLimitsDataProvider", dependsOnMethods = "createOrganizationsForPaginationTests") public void testGetPaginatedOrganizationsWithLimit(int limit) { - String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit; + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); @@ -643,8 +644,8 @@ public void testGetPaginatedOrganizationsWithLimit(int limit) { String nextLink = response.jsonPath().getString("links.find { it.rel == 'next' }.href"); String afterValue = null; - if (nextLink != null && nextLink.contains("after=")) { - afterValue = nextLink.substring(nextLink.indexOf("after=") + 6); + if (nextLink != null && nextLink.contains(AFTER_CURSOR_ARG)) { + afterValue = nextLink.substring(nextLink.indexOf(AFTER_CURSOR_ARG) + 6); } String storedAfterValue = afterValue; @@ -666,7 +667,7 @@ public void testGetPaginatedOrganizationsWithCursor() { int largeLimit = 30; // Step 1: Call the first page - String firstPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit + "&recursive=false"; + String firstPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit + "&recursive=false"; Response firstPageResponse = getResponseOfGetWithOAuth2(firstPageUrl, m2mToken); validateHttpStatusCode(firstPageResponse, HttpStatus.SC_OK); @@ -682,7 +683,7 @@ public void testGetPaginatedOrganizationsWithCursor() { Assert.assertNull(before, "Before value should be null on the first page."); // Step 2: Call the second page using the 'after' value - String secondPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit + String secondPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit + "&recursive=false&after=" + after; Response secondPageResponse = getResponseOfGetWithOAuth2(secondPageUrl, m2mToken); @@ -703,7 +704,7 @@ public void testGetPaginatedOrganizationsWithCursor() { } // Step 3: Call the previous page using the 'before' value - String previousPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit + String previousPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit + "&recursive=false&before=" + before; Response previousPageResponse = getResponseOfGetWithOAuth2(previousPageUrl, m2mToken); @@ -723,8 +724,8 @@ public void testGetPaginatedOrganizationsWithCursor() { List> actualOrganizationsSimplified = new ArrayList<>(); for (Map org : actualOrganizations) { Map simplifiedOrg = new HashMap<>(); - simplifiedOrg.put("name", org.get("name")); - simplifiedOrg.put("description", "Test " + org.get("name")); + simplifiedOrg.put(ORGANIZATION_NAME, org.get(ORGANIZATION_NAME)); + simplifiedOrg.put(ORGANIZATION_DESCRIPTION, "Test " + org.get(ORGANIZATION_NAME)); actualOrganizationsSimplified.add(simplifiedOrg); } @@ -741,22 +742,26 @@ public void testGetPaginatedOrganizationsWithCursor() { // Step 5: Test with largeLimit (greater than total org count) validatePaginationForNonPaginatedLimit(largeLimit); - } - private int createOrganizations(int numberOfOrganizations) { + private int createOrganizations(int numberOfOrganizations) throws JSONException { + createdOrganizations = new ArrayList<>(); int createdOrganizationsCount = 0; for (int i = 0; i < numberOfOrganizations; i++) { - String body = String.format("{\"name\": \"Org-%d\", \"description\": \"Test Organization %d\"}", i, i); - Response response = getResponseOfPostWithOAuth2(ORGANIZATION_MANAGEMENT_API_BASE_PATH, body, m2mToken); + JSONObject body = new JSONObject() + .put(ORGANIZATION_NAME, String.format("Org-%d", i)) + .put(ORGANIZATION_DESCRIPTION, String.format("Test Organization %d", i)); + + Response response = + getResponseOfPostWithOAuth2(ORGANIZATION_MANAGEMENT_API_BASE_PATH, body.toString(), m2mToken); if (response.getStatusCode() == HttpStatus.SC_CREATED) { // Store the created organization details Map org = new HashMap<>(); - org.put("name", String.format("Org-%d", i)); - org.put("description", String.format("Test Org-%d", i)); + org.put(ORGANIZATION_NAME, String.format("Org-%d", i)); + org.put(ORGANIZATION_DESCRIPTION, String.format("Test Org-%d", i)); createdOrganizations.add(org); createdOrganizationsCount++; @@ -770,7 +775,7 @@ private int createOrganizations(int numberOfOrganizations) { private void validatePaginationForNonPaginatedLimit(int limit) { - String limitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=" + limit + "&recursive=false"; + String limitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit + "&recursive=false"; Response response = getResponseOfGetWithOAuth2(limitUrl, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); @@ -782,7 +787,6 @@ private void validatePaginationForNonPaginatedLimit(int limit) { int returnedOrgCount = response.jsonPath().getList("organizations").size(); Assert.assertEquals(returnedOrgCount, totalCreatedOrganizations, "The number of returned organizations should match the total created organizations."); - } private String getLink(List> links, String rel) { @@ -790,15 +794,14 @@ private String getLink(List> links, String rel) { for (Map link : links) { if (rel.equals(link.get("rel"))) { String href = link.get("href"); - if (href.contains("after=")) { - return href.substring(href.indexOf("after=") + 6); - } else if (href.contains("before=")) { - return href.substring(href.indexOf("before=") + 7); + if (href.contains(AFTER_CURSOR_ARG)) { + return href.substring(href.indexOf(AFTER_CURSOR_ARG) + 6); + } else if (href.contains(BEFORE_CURSOR_ARG)) { + return href.substring(href.indexOf(BEFORE_CURSOR_ARG) + 7); } } } return null; - } } From a3583a2d24d01af54a13be8c07005704b43c0664 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Tue, 20 Aug 2024 14:54:32 +0530 Subject: [PATCH 03/12] Refactor OrganizationManagementTests Format --- .../v1/OrganizationManagementBaseTest.java | 24 +++- .../v1/OrganizationManagementSuccessTest.java | 130 +++++++++--------- 2 files changed, 88 insertions(+), 66 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 2c4b0a0e70a..9e34f391491 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 @@ -46,9 +46,27 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { static final String ORGANIZATION_NAME = "name"; static final String ORGANIZATION_DESCRIPTION = "description"; - static final String LIMIT_ARG = "?limit="; - static final String AFTER_CURSOR_ARG = "after"; - static final String BEFORE_CURSOR_ARG = "before"; + static final String ORGANIZATION_NAME_FORMAT = "Org-%d"; + static final String ORGANIZATION_DESCRIPTION_FORMAT = "Org-Description-%d"; + + static final String LIMIT_QUERY_PARAM = "limit="; + static final String AFTER_QUERY_PARAM = "after="; + static final String BEFORE_QUERY_PARAM = "before="; + static final String RECURSIVE_QUERY_PARAM = "recursive="; + + static final String ORGANIZATIONS_PATH_PARAM = "organizations"; + static final String LINKS_PATH_PARAM = "links"; + + static final String LINK_REL_PREVIOUS = "previous"; + static final String LINK_REL_NEXT = "next"; + static final String REL = "rel"; + static final String HREF = "href"; + + static final String AMPERSAND = "&"; + static final String QUESTION_MARK = "?"; + static final String EQUAL = "="; + + static final String FALSE = "false"; 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 d4f1e42f3ac..005eda5a0c3 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 @@ -106,9 +106,10 @@ public class OrganizationManagementSuccessTest extends OrganizationManagementBas private String switchedM2MToken; private String b2bApplicationID; private HttpClient client; + private final int NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS = 20; + private List> organizations; + protected OAuth2RestClient restClient; - private final int totalCreatedOrganizations = 20; - private List> createdOrganizations; @Factory(dataProvider = "restAPIUserConfigProvider") public OrganizationManagementSuccessTest(TestUserMode userMode) throws Exception { @@ -612,10 +613,10 @@ private void deleteApplication(String applicationId) throws Exception { @Test(dependsOnMethods = "testDeleteOrganization") public void createOrganizationsForPaginationTests() throws JSONException { - int createdOrganizationsCount = createOrganizations(totalCreatedOrganizations); + int organizationsCount = createOrganizations(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); - if (createdOrganizationsCount != totalCreatedOrganizations) { - throw new RuntimeException("Failed to create the expected number of organizations for testing."); + if (organizationsCount != NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS) { + throw new RuntimeException("Failed to create the expected number of organizations for testing pagination."); } } @@ -632,25 +633,25 @@ public Object[][] paginationLimitsDataProvider() { @Test(dataProvider = "paginationLimitsDataProvider", dependsOnMethods = "createOrganizationsForPaginationTests") public void testGetPaginatedOrganizationsWithLimit(int limit) { - String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit; + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); - int actualOrganizationCount = response.jsonPath().getList("organizations").size(); - int expectedOrganizationCount = Math.min(limit, totalCreatedOrganizations); + int actualOrganizationCount = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); + int expectedOrganizationCount = Math.min(limit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); Assert.assertEquals(actualOrganizationCount, expectedOrganizationCount); String nextLink = response.jsonPath().getString("links.find { it.rel == 'next' }.href"); String afterValue = null; - if (nextLink != null && nextLink.contains(AFTER_CURSOR_ARG)) { - afterValue = nextLink.substring(nextLink.indexOf(AFTER_CURSOR_ARG) + 6); + if (nextLink != null && nextLink.contains(AFTER_QUERY_PARAM)) { + afterValue = nextLink.substring(nextLink.indexOf(AFTER_QUERY_PARAM) + 6); } String storedAfterValue = afterValue; - if (totalCreatedOrganizations > limit) { + if (NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS > limit) { Assert.assertNotNull(storedAfterValue); } else { Assert.assertNull(storedAfterValue); @@ -666,142 +667,145 @@ public void testGetPaginatedOrganizationsWithCursor() { int orgLimit = 20; int largeLimit = 30; - // Step 1: Call the first page - String firstPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit + "&recursive=false"; + // Step 1: Call the first page. + String firstPageUrl = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + AMPERSAND + + RECURSIVE_QUERY_PARAM + FALSE; Response firstPageResponse = getResponseOfGetWithOAuth2(firstPageUrl, m2mToken); validateHttpStatusCode(firstPageResponse, HttpStatus.SC_OK); - int firstPageOrgCount = firstPageResponse.jsonPath().getList("organizations").size(); - Assert.assertEquals(firstPageOrgCount, Math.min(limit, totalCreatedOrganizations)); + int firstPageOrgCount = firstPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); + Assert.assertEquals(firstPageOrgCount, Math.min(limit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS)); - List> firstPageLinks = firstPageResponse.jsonPath().getList("links"); - after = getLink(firstPageLinks, "next"); - before = getLink(firstPageLinks, "previous"); + List> firstPageLinks = firstPageResponse.jsonPath().getList(LINKS_PATH_PARAM); + after = getLink(firstPageLinks, LINK_REL_NEXT); + before = getLink(firstPageLinks, LINK_REL_PREVIOUS); Assert.assertNotNull(after, "After value should not be null on the first page."); Assert.assertNull(before, "Before value should be null on the first page."); - // Step 2: Call the second page using the 'after' value - String secondPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit - + "&recursive=false&after=" + after; + // Step 2: Call the second page using the 'after' value. + String secondPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + + AMPERSAND + RECURSIVE_QUERY_PARAM + FALSE + AMPERSAND + AFTER_QUERY_PARAM + after; Response secondPageResponse = getResponseOfGetWithOAuth2(secondPageUrl, m2mToken); validateHttpStatusCode(secondPageResponse, HttpStatus.SC_OK); - int secondPageOrgCount = secondPageResponse.jsonPath().getList("organizations").size(); - Assert.assertEquals(secondPageOrgCount, Math.min(limit, totalCreatedOrganizations - firstPageOrgCount)); + int secondPageOrgCount = secondPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); + Assert.assertEquals(secondPageOrgCount, Math.min(limit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS - firstPageOrgCount)); - List> secondPageLinks = secondPageResponse.jsonPath().getList("links"); - before = getLink(secondPageLinks, "previous"); - after = getLink(secondPageLinks, "next"); + List> secondPageLinks = secondPageResponse.jsonPath().getList(LINKS_PATH_PARAM); + before = getLink(secondPageLinks, LINK_REL_PREVIOUS); + after = getLink(secondPageLinks, LINK_REL_NEXT); Assert.assertNotNull(before, "Before value should not be null on the second page."); - if (totalCreatedOrganizations > limit * 2) { + if (NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS > limit * 2) { Assert.assertNotNull(after, "After value should not be null if there are more pages."); } else { Assert.assertNull(after, "After value should be null if this is the last page."); } - // Step 3: Call the previous page using the 'before' value - String previousPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit - + "&recursive=false&before=" + before; + // Step 3: Call the previous page using the 'before' value. + String previousPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + + AMPERSAND + RECURSIVE_QUERY_PARAM + FALSE + AMPERSAND + BEFORE_QUERY_PARAM + before; Response previousPageResponse = getResponseOfGetWithOAuth2(previousPageUrl, m2mToken); validateHttpStatusCode(previousPageResponse, HttpStatus.SC_OK); - int previousPageOrgCount = previousPageResponse.jsonPath().getList("organizations").size(); + int previousPageOrgCount = previousPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); Assert.assertEquals(previousPageOrgCount, firstPageOrgCount); - // Ensure that the organizations match the first page organizations - Assert.assertEquals(previousPageResponse.jsonPath().getList("organizations"), - firstPageResponse.jsonPath().getList("organizations"), + // Ensure that the organizations match the first page organizations. + Assert.assertEquals(previousPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM), + firstPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM), "The previous page should return the same organizations as the first page."); - // Extract only the name and description from the previousPageResponse for comparison - List> actualOrganizations = previousPageResponse.jsonPath().getList("organizations"); + // Extract only the name and description from the previousPageResponse for comparison. + List> actualOrganizations = + previousPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM); List> actualOrganizationsSimplified = new ArrayList<>(); for (Map org : actualOrganizations) { Map simplifiedOrg = new HashMap<>(); simplifiedOrg.put(ORGANIZATION_NAME, org.get(ORGANIZATION_NAME)); - simplifiedOrg.put(ORGANIZATION_DESCRIPTION, "Test " + org.get(ORGANIZATION_NAME)); + simplifiedOrg.put(ORGANIZATION_DESCRIPTION, org.get(ORGANIZATION_DESCRIPTION)); actualOrganizationsSimplified.add(simplifiedOrg); } - // Additional validation: Check that the previousPageResponse contains the last 'limit' organizations + // Additional validation: Check that the previousPageResponse contains the last 'limit' organizations. List> expectedLastOrganizations = - createdOrganizations.subList(totalCreatedOrganizations - limit, totalCreatedOrganizations); + organizations.subList(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS - limit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); Collections.reverse(expectedLastOrganizations); // Reverse the order to match the expected response Assert.assertEquals(actualOrganizationsSimplified, expectedLastOrganizations, "The previous page should return the last created organizations."); - - // Step 4: Test with orgLimit (equal to total org count) + // Step 4: Test with orgLimit. (equal to total org count) validatePaginationForNonPaginatedLimit(orgLimit); - // Step 5: Test with largeLimit (greater than total org count) + // Step 5: Test with largeLimit. (greater than total org count) validatePaginationForNonPaginatedLimit(largeLimit); } private int createOrganizations(int numberOfOrganizations) throws JSONException { - createdOrganizations = new ArrayList<>(); - int createdOrganizationsCount = 0; + organizations = new ArrayList<>(); + int organizationsCount = 0; for (int i = 0; i < numberOfOrganizations; i++) { JSONObject body = new JSONObject() - .put(ORGANIZATION_NAME, String.format("Org-%d", i)) - .put(ORGANIZATION_DESCRIPTION, String.format("Test Organization %d", i)); + .put(ORGANIZATION_NAME, String.format(ORGANIZATION_NAME_FORMAT, i)) + .put(ORGANIZATION_DESCRIPTION, String.format(ORGANIZATION_DESCRIPTION_FORMAT, i)); Response response = getResponseOfPostWithOAuth2(ORGANIZATION_MANAGEMENT_API_BASE_PATH, body.toString(), m2mToken); if (response.getStatusCode() == HttpStatus.SC_CREATED) { - // Store the created organization details + // Store the created organization details. Map org = new HashMap<>(); - org.put(ORGANIZATION_NAME, String.format("Org-%d", i)); - org.put(ORGANIZATION_DESCRIPTION, String.format("Test Org-%d", i)); - createdOrganizations.add(org); + org.put(ORGANIZATION_NAME, String.format(ORGANIZATION_NAME_FORMAT, i)); + org.put(ORGANIZATION_DESCRIPTION, String.format(ORGANIZATION_DESCRIPTION_FORMAT, i)); + organizations.add(org); - createdOrganizationsCount++; + organizationsCount++; } else { throw new RuntimeException("Failed to create organization " + i); } } - return createdOrganizationsCount; + return organizationsCount; } private void validatePaginationForNonPaginatedLimit(int limit) { - String limitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + LIMIT_ARG + limit + "&recursive=false"; + String limitUrl = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + AMPERSAND + + RECURSIVE_QUERY_PARAM + FALSE; Response response = getResponseOfGetWithOAuth2(limitUrl, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); - List> links = response.jsonPath().getList("links"); + List> links = response.jsonPath().getList(LINKS_PATH_PARAM); Assert.assertNull(links, "Links should be null when all organizations are returned in one page."); - int returnedOrgCount = response.jsonPath().getList("organizations").size(); - Assert.assertEquals(returnedOrgCount, totalCreatedOrganizations, + int returnedOrgCount = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); + Assert.assertEquals(returnedOrgCount, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, "The number of returned organizations should match the total created organizations."); } private String getLink(List> links, String rel) { for (Map link : links) { - if (rel.equals(link.get("rel"))) { - String href = link.get("href"); - if (href.contains(AFTER_CURSOR_ARG)) { - return href.substring(href.indexOf(AFTER_CURSOR_ARG) + 6); - } else if (href.contains(BEFORE_CURSOR_ARG)) { - return href.substring(href.indexOf(BEFORE_CURSOR_ARG) + 7); + if (rel.equals(link.get(REL))) { + String href = link.get(HREF); + if (href.contains(AFTER_QUERY_PARAM)) { + return href.substring(href.indexOf(AFTER_QUERY_PARAM) + 6); + } else if (href.contains(BEFORE_QUERY_PARAM)) { + return href.substring(href.indexOf(BEFORE_QUERY_PARAM) + 7); } } } return null; } - } From b3e352d1a2aa777df51d1c8073ade1ad2b2dd605 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Tue, 20 Aug 2024 14:55:09 +0530 Subject: [PATCH 04/12] validateOrganizationsOnPage --- .../v1/OrganizationManagementSuccessTest.java | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 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 005eda5a0c3..286f8eb451f 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 @@ -613,9 +613,9 @@ private void deleteApplication(String applicationId) throws Exception { @Test(dependsOnMethods = "testDeleteOrganization") public void createOrganizationsForPaginationTests() throws JSONException { - int organizationsCount = createOrganizations(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); + organizations = createOrganizations(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); - if (organizationsCount != NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS) { + if (organizations.size() != NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS) { throw new RuntimeException("Failed to create the expected number of organizations for testing pagination."); } } @@ -675,9 +675,6 @@ public void testGetPaginatedOrganizationsWithCursor() { validateHttpStatusCode(firstPageResponse, HttpStatus.SC_OK); - int firstPageOrgCount = firstPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); - Assert.assertEquals(firstPageOrgCount, Math.min(limit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS)); - List> firstPageLinks = firstPageResponse.jsonPath().getList(LINKS_PATH_PARAM); after = getLink(firstPageLinks, LINK_REL_NEXT); before = getLink(firstPageLinks, LINK_REL_PREVIOUS); @@ -685,6 +682,9 @@ public void testGetPaginatedOrganizationsWithCursor() { Assert.assertNotNull(after, "After value should not be null on the first page."); Assert.assertNull(before, "Before value should be null on the first page."); + // Validate the first page organizations + validateOrganizationsOnPage(firstPageResponse, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); + // Step 2: Call the second page using the 'after' value. String secondPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + FALSE + AMPERSAND + AFTER_QUERY_PARAM + after; @@ -692,9 +692,6 @@ public void testGetPaginatedOrganizationsWithCursor() { validateHttpStatusCode(secondPageResponse, HttpStatus.SC_OK); - int secondPageOrgCount = secondPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); - Assert.assertEquals(secondPageOrgCount, Math.min(limit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS - firstPageOrgCount)); - List> secondPageLinks = secondPageResponse.jsonPath().getList(LINKS_PATH_PARAM); before = getLink(secondPageLinks, LINK_REL_PREVIOUS); after = getLink(secondPageLinks, LINK_REL_NEXT); @@ -706,6 +703,9 @@ public void testGetPaginatedOrganizationsWithCursor() { Assert.assertNull(after, "After value should be null if this is the last page."); } + // Validate the second page organizations + 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 + limit + AMPERSAND + RECURSIVE_QUERY_PARAM + FALSE + AMPERSAND + BEFORE_QUERY_PARAM + before; @@ -713,32 +713,15 @@ public void testGetPaginatedOrganizationsWithCursor() { validateHttpStatusCode(previousPageResponse, HttpStatus.SC_OK); - int previousPageOrgCount = previousPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); - Assert.assertEquals(previousPageOrgCount, firstPageOrgCount); + List> previousPageLinks = previousPageResponse.jsonPath().getList(LINKS_PATH_PARAM); + after = getLink(previousPageLinks, LINK_REL_NEXT); + before = getLink(previousPageLinks, LINK_REL_PREVIOUS); - // Ensure that the organizations match the first page organizations. - Assert.assertEquals(previousPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM), - firstPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM), - "The previous page should return the same organizations as the first page."); + Assert.assertNotNull(after, "After value should not be null on the previous (first) page."); + Assert.assertNull(before, "Before value should be null on the previous (first) page."); - // Extract only the name and description from the previousPageResponse for comparison. - List> actualOrganizations = - previousPageResponse.jsonPath().getList(ORGANIZATIONS_PATH_PARAM); - - List> actualOrganizationsSimplified = new ArrayList<>(); - for (Map org : actualOrganizations) { - Map simplifiedOrg = new HashMap<>(); - simplifiedOrg.put(ORGANIZATION_NAME, org.get(ORGANIZATION_NAME)); - simplifiedOrg.put(ORGANIZATION_DESCRIPTION, org.get(ORGANIZATION_DESCRIPTION)); - actualOrganizationsSimplified.add(simplifiedOrg); - } - - // Additional validation: Check that the previousPageResponse contains the last 'limit' organizations. - List> expectedLastOrganizations = - organizations.subList(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS - limit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); - Collections.reverse(expectedLastOrganizations); // Reverse the order to match the expected response - Assert.assertEquals(actualOrganizationsSimplified, expectedLastOrganizations, - "The previous page should return the last created organizations."); + // Validate the previous page organizations + validateOrganizationsOnPage(previousPageResponse, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); // Step 4: Test with orgLimit. (equal to total org count) validatePaginationForNonPaginatedLimit(orgLimit); @@ -747,10 +730,9 @@ public void testGetPaginatedOrganizationsWithCursor() { validatePaginationForNonPaginatedLimit(largeLimit); } - private int createOrganizations(int numberOfOrganizations) throws JSONException { + private List> createOrganizations(int numberOfOrganizations) throws JSONException { - organizations = new ArrayList<>(); - int organizationsCount = 0; + List> newOrganizations = new ArrayList<>(); for (int i = 0; i < numberOfOrganizations; i++) { JSONObject body = new JSONObject() @@ -765,15 +747,14 @@ private int createOrganizations(int numberOfOrganizations) throws JSONException Map org = new HashMap<>(); org.put(ORGANIZATION_NAME, String.format(ORGANIZATION_NAME_FORMAT, i)); org.put(ORGANIZATION_DESCRIPTION, String.format(ORGANIZATION_DESCRIPTION_FORMAT, i)); - organizations.add(org); + newOrganizations.add(org); - organizationsCount++; } else { throw new RuntimeException("Failed to create organization " + i); } } - return organizationsCount; + return newOrganizations; } private void validatePaginationForNonPaginatedLimit(int limit) { @@ -808,4 +789,25 @@ private String getLink(List> links, String rel) { } return null; } + + private void validateOrganizationsOnPage(Response response, int pageNum, int totalOrganizations, int limit) { + + // Validate the organization count + int expectedOrgCount = Math.min(limit, totalOrganizations - (pageNum - 1) * limit); + int actualOrgCount = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); + Assert.assertEquals(actualOrgCount, expectedOrgCount, "Organization count mismatch on page " + pageNum); + + // Validate the organization names and descriptions + List actualOrgNames = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM + ".name"); + + for (int i = 0; i < expectedOrgCount; i++) { + int orgIndex = totalOrganizations - ((pageNum - 1) * limit) - i - 1; + + String expectedOrgName = String.format(ORGANIZATION_NAME_FORMAT, orgIndex); + Assert.assertEquals(actualOrgNames.get(i), expectedOrgName, + "Organization name mismatch on page " + pageNum + " at index " + i); + } + } + + } From 1a9d01566343204553b4d7eb6c618dff6a5d6c2b Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Tue, 20 Aug 2024 22:20:03 +0530 Subject: [PATCH 05/12] Refactor OrganizationManagementSuccessTest --- .../v1/OrganizationManagementBaseTest.java | 33 +++++++++---------- .../v1/OrganizationManagementSuccessTest.java | 14 ++++---- 2 files changed, 22 insertions(+), 25 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 9e34f391491..d1d0ec34b09 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 @@ -44,29 +44,26 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { static final String ORGANIZATION_DISCOVERY_API_PATH = "/discovery"; static final String PATH_SEPARATOR = "/"; - static final String ORGANIZATION_NAME = "name"; - static final String ORGANIZATION_DESCRIPTION = "description"; - static final String ORGANIZATION_NAME_FORMAT = "Org-%d"; - static final String ORGANIZATION_DESCRIPTION_FORMAT = "Org-Description-%d"; + protected static final String ORGANIZATION_NAME = "name"; + protected static final String ORGANIZATION_NAME_FORMAT = "Org-%d"; - static final String LIMIT_QUERY_PARAM = "limit="; - static final String AFTER_QUERY_PARAM = "after="; - static final String BEFORE_QUERY_PARAM = "before="; - static final String RECURSIVE_QUERY_PARAM = "recursive="; + protected static final String LIMIT_QUERY_PARAM = "limit="; + protected static final String AFTER_QUERY_PARAM = "after="; + protected static final String BEFORE_QUERY_PARAM = "before="; + protected static final String RECURSIVE_QUERY_PARAM = "recursive="; - static final String ORGANIZATIONS_PATH_PARAM = "organizations"; - static final String LINKS_PATH_PARAM = "links"; + protected static final String ORGANIZATIONS_PATH_PARAM = "organizations"; + protected static final String LINKS_PATH_PARAM = "links"; - static final String LINK_REL_PREVIOUS = "previous"; - static final String LINK_REL_NEXT = "next"; - static final String REL = "rel"; - static final String HREF = "href"; + protected static final String LINK_REL_PREVIOUS = "previous"; + protected static final String LINK_REL_NEXT = "next"; + protected static final String REL = "rel"; + protected static final String HREF = "href"; - static final String AMPERSAND = "&"; - static final String QUESTION_MARK = "?"; - static final String EQUAL = "="; + protected static final String AMPERSAND = "&"; + protected static final String QUESTION_MARK = "?"; - static final String FALSE = "false"; + protected static final String FALSE = "false"; 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 286f8eb451f..7450be78895 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 @@ -106,11 +106,11 @@ public class OrganizationManagementSuccessTest extends OrganizationManagementBas private String switchedM2MToken; private String b2bApplicationID; private HttpClient client; - private final int NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS = 20; - private List> organizations; protected OAuth2RestClient restClient; + private static final int NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS = 20; + @Factory(dataProvider = "restAPIUserConfigProvider") public OrganizationManagementSuccessTest(TestUserMode userMode) throws Exception { @@ -613,7 +613,7 @@ private void deleteApplication(String applicationId) throws Exception { @Test(dependsOnMethods = "testDeleteOrganization") public void createOrganizationsForPaginationTests() throws JSONException { - organizations = createOrganizations(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); + List> organizations = createOrganizations(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); if (organizations.size() != NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS) { throw new RuntimeException("Failed to create the expected number of organizations for testing pagination."); @@ -642,7 +642,9 @@ public void testGetPaginatedOrganizationsWithLimit(int limit) { int expectedOrganizationCount = Math.min(limit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); Assert.assertEquals(actualOrganizationCount, expectedOrganizationCount); - String nextLink = response.jsonPath().getString("links.find { it.rel == 'next' }.href"); + String nextLink = response.jsonPath().getString( + String.format("links.find { it.%s == '%s' }.%s", REL, LINK_REL_NEXT, HREF)); + String afterValue = null; if (nextLink != null && nextLink.contains(AFTER_QUERY_PARAM)) { @@ -736,8 +738,7 @@ private List> createOrganizations(int numberOfOrganizations) for (int i = 0; i < numberOfOrganizations; i++) { JSONObject body = new JSONObject() - .put(ORGANIZATION_NAME, String.format(ORGANIZATION_NAME_FORMAT, i)) - .put(ORGANIZATION_DESCRIPTION, String.format(ORGANIZATION_DESCRIPTION_FORMAT, i)); + .put(ORGANIZATION_NAME, String.format(ORGANIZATION_NAME_FORMAT, i)); Response response = getResponseOfPostWithOAuth2(ORGANIZATION_MANAGEMENT_API_BASE_PATH, body.toString(), m2mToken); @@ -746,7 +747,6 @@ private List> createOrganizations(int numberOfOrganizations) // Store the created organization details. Map org = new HashMap<>(); org.put(ORGANIZATION_NAME, String.format(ORGANIZATION_NAME_FORMAT, i)); - org.put(ORGANIZATION_DESCRIPTION, String.format(ORGANIZATION_DESCRIPTION_FORMAT, i)); newOrganizations.add(org); } else { From eef28425f467b6fd983b35d922c146f2837f082c Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Tue, 20 Aug 2024 23:30:19 +0530 Subject: [PATCH 06/12] Test Pagination for edge cases and format the faliure cases --- .../v1/OrganizationManagementFailureTest.java | 21 ++++-- .../v1/OrganizationManagementSuccessTest.java | 68 ++++++++++--------- 2 files changed, 51 insertions(+), 38 deletions(-) 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 4506c61396a..3dfe54238e9 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 @@ -49,6 +49,12 @@ public class OrganizationManagementFailureTest extends OrganizationManagementBas private final String invalidM2MToken = "06c1f4e2-3339-44e4-a825-96585e3653b1"; private final String invalidOrganizationID = "06c1f4e2-3339-44e4-a825-96585e3653b1"; + protected static final String INVALID_CURSOR = "invalid-cursor"; + + private static final String ERROR_CODE_BAD_REQUEST = "UE-10000"; + private static final String ERROR_CODE_INVALID_PAGINATION_CURSOR = "ORG-60026"; + + private List organizationIDs = new ArrayList<>(); private String applicationID; @@ -376,25 +382,26 @@ public void deleteOrganizations() { @Test(dependsOnMethods = "testUpdateDiscoveryAttributesUnauthorized") public void testGetPaginatedOrganizationsWithInvalidLimit() { - String invalidLimitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=-1"; + String invalidLimitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + "-1"; Response response = getResponseOfGetWithOAuth2(invalidLimitUrl, m2mToken); - validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "UE-10000"); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, ERROR_CODE_BAD_REQUEST); } @Test(dependsOnMethods = "testGetPaginatedOrganizationsWithInvalidLimit") public void testGetPaginatedOrganizationsWithInvalidAfterCursor() { - String invalidAfterCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=10&after=invalid-cursor"; + String invalidAfterCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + "10" + + AMPERSAND + AFTER_QUERY_PARAM + INVALID_CURSOR; Response response = getResponseOfGetWithOAuth2(invalidAfterCursorUrl, m2mToken); - validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "ORG-60026"); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, ERROR_CODE_INVALID_PAGINATION_CURSOR); } @Test(dependsOnMethods = "testGetPaginatedOrganizationsWithInvalidAfterCursor") public void testGetPaginatedOrganizationsWithInvalidBeforeCursor() { - String invalidBeforeCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "?limit=10&before=invalid-cursor"; + String invalidBeforeCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + "10" + + AMPERSAND + BEFORE_QUERY_PARAM + INVALID_CURSOR; Response response = getResponseOfGetWithOAuth2(invalidBeforeCursorUrl, m2mToken); - validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, "ORG-60026"); + validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, ERROR_CODE_INVALID_PAGINATION_CURSOR); } - } 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 7450be78895..1318936fa1e 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 @@ -660,14 +660,19 @@ public void testGetPaginatedOrganizationsWithLimit(int limit) { } } - @Test(dependsOnMethods = "createOrganizationsForPaginationTests") - public void testGetPaginatedOrganizationsWithCursor() { + @DataProvider(name = "paginationLimits") + public Object[][] paginationLimits() { + + return new Object[][]{ + {1}, {2}, {5}, {6}, {10}, {17} + }; + } + + @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "paginationLimits") + public void testGetPaginatedOrganizationsWithCursor(int limit) { String after; String before; - int limit = 2; - int orgLimit = 20; - int largeLimit = 30; // Step 1: Call the first page. String firstPageUrl = @@ -724,12 +729,32 @@ public void testGetPaginatedOrganizationsWithCursor() { // Validate the previous page organizations validateOrganizationsOnPage(previousPageResponse, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); + } + + @DataProvider(name = "edgeCaseLimits") + public Object[][] edgeCaseLimits() { + + return new Object[][]{ + {0}, {20}, {25} + }; + } + + @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "edgeCaseLimits") + public void testGetPaginatedOrganizationsForEdgeCases(int limit) { + + String limitUrl = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + AMPERSAND + + RECURSIVE_QUERY_PARAM + FALSE; + Response response = getResponseOfGetWithOAuth2(limitUrl, m2mToken); + + validateHttpStatusCode(response, HttpStatus.SC_OK); + + List> links = response.jsonPath().getList(LINKS_PATH_PARAM); - // Step 4: Test with orgLimit. (equal to total org count) - validatePaginationForNonPaginatedLimit(orgLimit); + Assert.assertNull(links, "Links should be null when all organizations are returned in one page."); - // Step 5: Test with largeLimit. (greater than total org count) - validatePaginationForNonPaginatedLimit(largeLimit); + // Validate the only page organizations + validateOrganizationsOnPage(response, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); } private List> createOrganizations(int numberOfOrganizations) throws JSONException { @@ -748,7 +773,6 @@ private List> createOrganizations(int numberOfOrganizations) Map org = new HashMap<>(); org.put(ORGANIZATION_NAME, String.format(ORGANIZATION_NAME_FORMAT, i)); newOrganizations.add(org); - } else { throw new RuntimeException("Failed to create organization " + i); } @@ -757,24 +781,6 @@ private List> createOrganizations(int numberOfOrganizations) return newOrganizations; } - private void validatePaginationForNonPaginatedLimit(int limit) { - - String limitUrl = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + AMPERSAND + - RECURSIVE_QUERY_PARAM + FALSE; - Response response = getResponseOfGetWithOAuth2(limitUrl, m2mToken); - - validateHttpStatusCode(response, HttpStatus.SC_OK); - - List> links = response.jsonPath().getList(LINKS_PATH_PARAM); - - Assert.assertNull(links, "Links should be null when all organizations are returned in one page."); - - int returnedOrgCount = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); - Assert.assertEquals(returnedOrgCount, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, - "The number of returned organizations should match the total created organizations."); - } - private String getLink(List> links, String rel) { for (Map link : links) { @@ -794,7 +800,9 @@ private void validateOrganizationsOnPage(Response response, int pageNum, int tot // Validate the organization count int expectedOrgCount = Math.min(limit, totalOrganizations - (pageNum - 1) * limit); - int actualOrgCount = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM).size(); + List> actualOrganizations = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM); + int actualOrgCount = (actualOrganizations != null) ? actualOrganizations.size() : 0; + Assert.assertEquals(actualOrgCount, expectedOrgCount, "Organization count mismatch on page " + pageNum); // Validate the organization names and descriptions @@ -808,6 +816,4 @@ private void validateOrganizationsOnPage(Response response, int pageNum, int tot "Organization name mismatch on page " + pageNum + " at index " + i); } } - - } From 5a45da93a547cad48a46e9046c2b078986badc29 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Wed, 21 Aug 2024 11:44:35 +0530 Subject: [PATCH 07/12] Validate organizations for default limit --- .../v1/OrganizationManagementSuccessTest.java | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 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 1318936fa1e..73d2879ec67 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 @@ -110,6 +110,7 @@ public class OrganizationManagementSuccessTest extends OrganizationManagementBas protected OAuth2RestClient restClient; private static final int NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS = 20; + private static final int DEFAULT_ORG_LIMIT = 15; @Factory(dataProvider = "restAPIUserConfigProvider") public OrganizationManagementSuccessTest(TestUserMode userMode) throws Exception { @@ -757,6 +758,32 @@ public void testGetPaginatedOrganizationsForEdgeCases(int limit) { validateOrganizationsOnPage(response, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); } + @Test(dependsOnMethods = "createOrganizationsForPaginationTests") + public void testGetPaginatedOrganizationsWithDefaultLimit() { + + // Test case 1: URL with LIMIT_QUERY_PARAM but no value + String endpointURLWithEmptyLimit = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + AMPERSAND + + RECURSIVE_QUERY_PARAM + FALSE; + + Response responseWithEmptyLimit = getResponseOfGetWithOAuth2(endpointURLWithEmptyLimit, m2mToken); + + validateHttpStatusCode(responseWithEmptyLimit, HttpStatus.SC_OK); + + validateOrganizationsForDefaultLimit(responseWithEmptyLimit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); + + // Test case 2: URL without LIMIT_QUERY_PARAM + String endpointURLWithoutLimit = + ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + RECURSIVE_QUERY_PARAM + FALSE; + + Response responseWithoutLimit = getResponseOfGetWithOAuth2(endpointURLWithoutLimit, m2mToken); + + validateHttpStatusCode(responseWithoutLimit, HttpStatus.SC_OK); + + validateOrganizationsForDefaultLimit(responseWithoutLimit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); + } + + private List> createOrganizations(int numberOfOrganizations) throws JSONException { List> newOrganizations = new ArrayList<>(); @@ -803,9 +830,10 @@ private void validateOrganizationsOnPage(Response response, int pageNum, int tot List> actualOrganizations = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM); int actualOrgCount = (actualOrganizations != null) ? actualOrganizations.size() : 0; - Assert.assertEquals(actualOrgCount, expectedOrgCount, "Organization count mismatch on page " + pageNum); + Assert.assertEquals(actualOrgCount, expectedOrgCount, + "Organization count mismatch on page " + pageNum); - // Validate the organization names and descriptions + // Validate the organization names List actualOrgNames = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM + ".name"); for (int i = 0; i < expectedOrgCount; i++) { @@ -816,4 +844,38 @@ private void validateOrganizationsOnPage(Response response, int pageNum, int tot "Organization name mismatch on page " + pageNum + " at index " + i); } } + + private void validateOrganizationsForDefaultLimit(Response response, int totalOrganizations) { + + // Validate the organization count + int expectedOrgCount = Math.min(DEFAULT_ORG_LIMIT, totalOrganizations); + List> actualOrganizations = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM); + int actualOrgCount = (actualOrganizations != null) ? actualOrganizations.size() : 0; + + Assert.assertEquals(actualOrgCount, expectedOrgCount, + "Organization count mismatch with default limit."); + + // Validate the organization names + List actualOrgNames = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM + ".name"); + + for (int i = 0; i < expectedOrgCount; i++) { + int orgIndex = totalOrganizations - i - 1; + + String expectedOrgName = String.format(ORGANIZATION_NAME_FORMAT, orgIndex); + Assert.assertEquals(actualOrgNames.get(i), expectedOrgName, + "Organization name mismatch with default limit at index " + i); + } + + // Validate pagination links + List> links = response.jsonPath().getList(LINKS_PATH_PARAM); + + if (totalOrganizations > DEFAULT_ORG_LIMIT) { + String after = getLink(links, LINK_REL_NEXT); + Assert.assertNotNull(after, + "'after' link should be present when organizations exceed default limit."); + } else { + Assert.assertNull(getLink(links, LINK_REL_NEXT), + "'after' link should not be present when organizations are within default limit."); + } + } } From 65f0467e74c6b8cc45a90106edc26a79f529a321 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Mon, 26 Aug 2024 10:30:44 +0530 Subject: [PATCH 08/12] Rename data providers for Organization GET API pagination tests and add full stops to single-lined comments. --- .../v1/OrganizationManagementSuccessTest.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 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 73d2879ec67..c261a27287e 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 @@ -621,8 +621,8 @@ public void createOrganizationsForPaginationTests() throws JSONException { } } - @DataProvider(name = "paginationLimitsDataProvider") - public Object[][] paginationLimitsDataProvider() { + @DataProvider(name = "organizationLimitValidationProvider") + public Object[][] organizationLimitValidationProvider() { return new Object[][]{ {10}, @@ -631,7 +631,7 @@ public Object[][] paginationLimitsDataProvider() { }; } - @Test(dataProvider = "paginationLimitsDataProvider", dependsOnMethods = "createOrganizationsForPaginationTests") + @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "organizationLimitValidationProvider") public void testGetPaginatedOrganizationsWithLimit(int limit) { String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit; @@ -661,15 +661,15 @@ public void testGetPaginatedOrganizationsWithLimit(int limit) { } } - @DataProvider(name = "paginationLimits") - public Object[][] paginationLimits() { + @DataProvider(name = "organizationCursorValidationProvider") + public Object[][] organizationCursorValidationProvider() { return new Object[][]{ {1}, {2}, {5}, {6}, {10}, {17} }; } - @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "paginationLimits") + @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "organizationCursorValidationProvider") public void testGetPaginatedOrganizationsWithCursor(int limit) { String after; @@ -690,7 +690,7 @@ public void testGetPaginatedOrganizationsWithCursor(int limit) { Assert.assertNotNull(after, "After value should not be null on the first page."); Assert.assertNull(before, "Before value should be null on the first page."); - // Validate the first page organizations + // Validate the first page organizations. validateOrganizationsOnPage(firstPageResponse, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); // Step 2: Call the second page using the 'after' value. @@ -711,7 +711,7 @@ public void testGetPaginatedOrganizationsWithCursor(int limit) { Assert.assertNull(after, "After value should be null if this is the last page."); } - // Validate the second page organizations + // Validate the second page organizations. validateOrganizationsOnPage(secondPageResponse, 2, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); // Step 3: Call the previous page using the 'before' value. @@ -728,19 +728,19 @@ public void testGetPaginatedOrganizationsWithCursor(int limit) { Assert.assertNotNull(after, "After value should not be null on the previous (first) page."); Assert.assertNull(before, "Before value should be null on the previous (first) page."); - // Validate the previous page organizations + // Validate the previous page organizations. validateOrganizationsOnPage(previousPageResponse, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); } - @DataProvider(name = "edgeCaseLimits") - public Object[][] edgeCaseLimits() { + @DataProvider(name = "organizationCursorEdgeCasesProvider") + public Object[][] organizationCursorEdgeCasesProvider() { return new Object[][]{ {0}, {20}, {25} }; } - @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "edgeCaseLimits") + @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "organizationCursorEdgeCasesProvider") public void testGetPaginatedOrganizationsForEdgeCases(int limit) { String limitUrl = @@ -754,14 +754,14 @@ public void testGetPaginatedOrganizationsForEdgeCases(int limit) { Assert.assertNull(links, "Links should be null when all organizations are returned in one page."); - // Validate the only page organizations + // Validate the only page organizations. validateOrganizationsOnPage(response, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); } @Test(dependsOnMethods = "createOrganizationsForPaginationTests") public void testGetPaginatedOrganizationsWithDefaultLimit() { - // Test case 1: URL with LIMIT_QUERY_PARAM but no value + // Test case 1: URL with LIMIT_QUERY_PARAM but no value. String endpointURLWithEmptyLimit = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + AMPERSAND + RECURSIVE_QUERY_PARAM + FALSE; @@ -772,7 +772,7 @@ public void testGetPaginatedOrganizationsWithDefaultLimit() { validateOrganizationsForDefaultLimit(responseWithEmptyLimit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); - // Test case 2: URL without LIMIT_QUERY_PARAM + // Test case 2: URL without LIMIT_QUERY_PARAM. String endpointURLWithoutLimit = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + RECURSIVE_QUERY_PARAM + FALSE; @@ -783,7 +783,6 @@ public void testGetPaginatedOrganizationsWithDefaultLimit() { validateOrganizationsForDefaultLimit(responseWithoutLimit, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); } - private List> createOrganizations(int numberOfOrganizations) throws JSONException { List> newOrganizations = new ArrayList<>(); @@ -825,7 +824,7 @@ private String getLink(List> links, String rel) { private void validateOrganizationsOnPage(Response response, int pageNum, int totalOrganizations, int limit) { - // Validate the organization count + // Validate the organization count. int expectedOrgCount = Math.min(limit, totalOrganizations - (pageNum - 1) * limit); List> actualOrganizations = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM); int actualOrgCount = (actualOrganizations != null) ? actualOrganizations.size() : 0; @@ -833,7 +832,7 @@ private void validateOrganizationsOnPage(Response response, int pageNum, int tot Assert.assertEquals(actualOrgCount, expectedOrgCount, "Organization count mismatch on page " + pageNum); - // Validate the organization names + // Validate the organization names. List actualOrgNames = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM + ".name"); for (int i = 0; i < expectedOrgCount; i++) { @@ -847,7 +846,7 @@ private void validateOrganizationsOnPage(Response response, int pageNum, int tot private void validateOrganizationsForDefaultLimit(Response response, int totalOrganizations) { - // Validate the organization count + // Validate the organization count. int expectedOrgCount = Math.min(DEFAULT_ORG_LIMIT, totalOrganizations); List> actualOrganizations = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM); int actualOrgCount = (actualOrganizations != null) ? actualOrganizations.size() : 0; @@ -855,7 +854,7 @@ private void validateOrganizationsForDefaultLimit(Response response, int totalOr Assert.assertEquals(actualOrgCount, expectedOrgCount, "Organization count mismatch with default limit."); - // Validate the organization names + // Validate the organization names. List actualOrgNames = response.jsonPath().getList(ORGANIZATIONS_PATH_PARAM + ".name"); for (int i = 0; i < expectedOrgCount; i++) { @@ -866,7 +865,7 @@ private void validateOrganizationsForDefaultLimit(Response response, int totalOr "Organization name mismatch with default limit at index " + i); } - // Validate pagination links + // Validate pagination links. List> links = response.jsonPath().getList(LINKS_PATH_PARAM); if (totalOrganizations > DEFAULT_ORG_LIMIT) { From 4fec5db6e3a681d56720d442afde687630e53ad7 Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Mon, 26 Aug 2024 14:13:51 +0530 Subject: [PATCH 09/12] Save Organization Id of the created organizations --- .../management/v1/OrganizationManagementBaseTest.java | 1 + .../management/v1/OrganizationManagementSuccessTest.java | 5 +++++ 2 files changed, 6 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 d1d0ec34b09..ea73cd08703 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 @@ -44,6 +44,7 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { static final String ORGANIZATION_DISCOVERY_API_PATH = "/discovery"; static final String PATH_SEPARATOR = "/"; + protected static final String ORGANIZATION_ID = "id"; protected static final String ORGANIZATION_NAME = "name"; protected static final String ORGANIZATION_NAME_FORMAT = "Org-%d"; 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 c261a27287e..4eb025be825 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 @@ -795,9 +795,14 @@ private List> createOrganizations(int numberOfOrganizations) getResponseOfPostWithOAuth2(ORGANIZATION_MANAGEMENT_API_BASE_PATH, body.toString(), m2mToken); if (response.getStatusCode() == HttpStatus.SC_CREATED) { + // Extract the organization ID (UUID) from the response body. + JSONObject responseBody = new JSONObject(response.getBody().asString()); + String organizationId = responseBody.getString("id"); + // Store the created organization details. Map org = new HashMap<>(); org.put(ORGANIZATION_NAME, String.format(ORGANIZATION_NAME_FORMAT, i)); + org.put(ORGANIZATION_ID, organizationId); newOrganizations.add(org); } else { throw new RuntimeException("Failed to create organization " + i); From c84007797fc922c464351fb75713102a0d9f1e3f Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Mon, 26 Aug 2024 15:18:06 +0530 Subject: [PATCH 10/12] Add equal sign as a static variable --- .../v1/OrganizationManagementBaseTest.java | 9 +++-- .../v1/OrganizationManagementFailureTest.java | 10 ++--- .../v1/OrganizationManagementSuccessTest.java | 40 ++++++++++--------- 3 files changed, 31 insertions(+), 28 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 ea73cd08703..b4c940bfca6 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 @@ -48,10 +48,10 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { protected static final String ORGANIZATION_NAME = "name"; protected static final String ORGANIZATION_NAME_FORMAT = "Org-%d"; - protected static final String LIMIT_QUERY_PARAM = "limit="; - protected static final String AFTER_QUERY_PARAM = "after="; - protected static final String BEFORE_QUERY_PARAM = "before="; - protected static final String RECURSIVE_QUERY_PARAM = "recursive="; + protected static final String LIMIT_QUERY_PARAM = "limit"; + protected static final String AFTER_QUERY_PARAM = "after"; + protected static final String BEFORE_QUERY_PARAM = "before"; + protected static final String RECURSIVE_QUERY_PARAM = "recursive"; protected static final String ORGANIZATIONS_PATH_PARAM = "organizations"; protected static final String LINKS_PATH_PARAM = "links"; @@ -63,6 +63,7 @@ public class OrganizationManagementBaseTest extends RESTAPIServerTestBase { protected static final String AMPERSAND = "&"; protected static final String QUESTION_MARK = "?"; + protected static final String EQUAL = "="; protected static final String FALSE = "false"; 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 3dfe54238e9..d3d163bd9da 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 @@ -382,7 +382,7 @@ public void deleteOrganizations() { @Test(dependsOnMethods = "testUpdateDiscoveryAttributesUnauthorized") public void testGetPaginatedOrganizationsWithInvalidLimit() { - String invalidLimitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + "-1"; + String invalidLimitUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + "-1"; Response response = getResponseOfGetWithOAuth2(invalidLimitUrl, m2mToken); validateErrorResponse(response, HttpStatus.SC_BAD_REQUEST, ERROR_CODE_BAD_REQUEST); } @@ -390,8 +390,8 @@ public void testGetPaginatedOrganizationsWithInvalidLimit() { @Test(dependsOnMethods = "testGetPaginatedOrganizationsWithInvalidLimit") public void testGetPaginatedOrganizationsWithInvalidAfterCursor() { - String invalidAfterCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + "10" - + AMPERSAND + AFTER_QUERY_PARAM + INVALID_CURSOR; + String invalidAfterCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_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); } @@ -399,8 +399,8 @@ public void testGetPaginatedOrganizationsWithInvalidAfterCursor() { @Test(dependsOnMethods = "testGetPaginatedOrganizationsWithInvalidAfterCursor") public void testGetPaginatedOrganizationsWithInvalidBeforeCursor() { - String invalidBeforeCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + "10" - + AMPERSAND + BEFORE_QUERY_PARAM + INVALID_CURSOR; + String invalidBeforeCursorUrl = ORGANIZATION_MANAGEMENT_API_BASE_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); } 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 4eb025be825..c4509571892 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 @@ -634,7 +634,7 @@ public Object[][] organizationLimitValidationProvider() { @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "organizationLimitValidationProvider") public void testGetPaginatedOrganizationsWithLimit(int limit) { - String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit; + String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit; Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken); validateHttpStatusCode(response, HttpStatus.SC_OK); @@ -648,8 +648,8 @@ public void testGetPaginatedOrganizationsWithLimit(int limit) { String afterValue = null; - if (nextLink != null && nextLink.contains(AFTER_QUERY_PARAM)) { - afterValue = nextLink.substring(nextLink.indexOf(AFTER_QUERY_PARAM) + 6); + if (nextLink != null && nextLink.contains(AFTER_QUERY_PARAM + EQUAL)) { + afterValue = nextLink.substring(nextLink.indexOf(AFTER_QUERY_PARAM + EQUAL) + 6); } String storedAfterValue = afterValue; @@ -676,9 +676,9 @@ public void testGetPaginatedOrganizationsWithCursor(int limit) { String before; // Step 1: Call the first page. - String firstPageUrl = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + AMPERSAND + - RECURSIVE_QUERY_PARAM + FALSE; + String firstPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit + + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + FALSE; + Response firstPageResponse = getResponseOfGetWithOAuth2(firstPageUrl, m2mToken); validateHttpStatusCode(firstPageResponse, HttpStatus.SC_OK); @@ -694,8 +694,8 @@ public void testGetPaginatedOrganizationsWithCursor(int limit) { validateOrganizationsOnPage(firstPageResponse, 1, NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS, limit); // Step 2: Call the second page using the 'after' value. - String secondPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit - + AMPERSAND + RECURSIVE_QUERY_PARAM + FALSE + AMPERSAND + AFTER_QUERY_PARAM + after; + String secondPageUrl = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit + + AMPERSAND + RECURSIVE_QUERY_PARAM + EQUAL + FALSE + AMPERSAND + AFTER_QUERY_PARAM + EQUAL + after; Response secondPageResponse = getResponseOfGetWithOAuth2(secondPageUrl, m2mToken); validateHttpStatusCode(secondPageResponse, HttpStatus.SC_OK); @@ -715,8 +715,10 @@ public void testGetPaginatedOrganizationsWithCursor(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 + limit - + AMPERSAND + RECURSIVE_QUERY_PARAM + FALSE + AMPERSAND + BEFORE_QUERY_PARAM + 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,8 +746,8 @@ public Object[][] organizationCursorEdgeCasesProvider() { public void testGetPaginatedOrganizationsForEdgeCases(int limit) { String limitUrl = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + limit + AMPERSAND + - RECURSIVE_QUERY_PARAM + FALSE; + 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); @@ -763,8 +765,8 @@ public void testGetPaginatedOrganizationsWithDefaultLimit() { // Test case 1: URL with LIMIT_QUERY_PARAM but no value. String endpointURLWithEmptyLimit = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + AMPERSAND + - RECURSIVE_QUERY_PARAM + FALSE; + ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + AMPERSAND + + RECURSIVE_QUERY_PARAM + EQUAL + FALSE; Response responseWithEmptyLimit = getResponseOfGetWithOAuth2(endpointURLWithEmptyLimit, m2mToken); @@ -774,7 +776,7 @@ public void testGetPaginatedOrganizationsWithDefaultLimit() { // Test case 2: URL without LIMIT_QUERY_PARAM. String endpointURLWithoutLimit = - ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + RECURSIVE_QUERY_PARAM + FALSE; + ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + RECURSIVE_QUERY_PARAM + EQUAL + FALSE; Response responseWithoutLimit = getResponseOfGetWithOAuth2(endpointURLWithoutLimit, m2mToken); @@ -817,10 +819,10 @@ private String getLink(List> links, String rel) { for (Map link : links) { if (rel.equals(link.get(REL))) { String href = link.get(HREF); - if (href.contains(AFTER_QUERY_PARAM)) { - return href.substring(href.indexOf(AFTER_QUERY_PARAM) + 6); - } else if (href.contains(BEFORE_QUERY_PARAM)) { - return href.substring(href.indexOf(BEFORE_QUERY_PARAM) + 7); + if (href.contains(AFTER_QUERY_PARAM + EQUAL)) { + return href.substring(href.indexOf(AFTER_QUERY_PARAM + EQUAL) + 6); + } else if (href.contains(BEFORE_QUERY_PARAM + EQUAL)) { + return href.substring(href.indexOf(BEFORE_QUERY_PARAM + EQUAL) + 7); } } } From 01b24684d7912e03984e54de2a917c858fc213ec Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Tue, 27 Aug 2024 13:39:06 +0530 Subject: [PATCH 11/12] Make organizations an instance variable --- .../management/v1/OrganizationManagementSuccessTest.java | 4 +++- 1 file changed, 3 insertions(+), 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 c4509571892..8fb43a96b57 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 @@ -80,6 +80,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.UUID; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.equalTo; @@ -106,6 +107,7 @@ public class OrganizationManagementSuccessTest extends OrganizationManagementBas private String switchedM2MToken; private String b2bApplicationID; private HttpClient client; + private List> organizations; protected OAuth2RestClient restClient; @@ -614,7 +616,7 @@ private void deleteApplication(String applicationId) throws Exception { @Test(dependsOnMethods = "testDeleteOrganization") public void createOrganizationsForPaginationTests() throws JSONException { - List> organizations = createOrganizations(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); + organizations = createOrganizations(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); if (organizations.size() != NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS) { throw new RuntimeException("Failed to create the expected number of organizations for testing pagination."); From 8c34c1c0ba7a5a33bf7a066453c4edb41366dacc Mon Sep 17 00:00:00 2001 From: BimsaraBodaragama Date: Tue, 27 Aug 2024 14:37:26 +0530 Subject: [PATCH 12/12] Address Comments --- .../v1/OrganizationManagementFailureTest.java | 2 -- .../v1/OrganizationManagementSuccessTest.java | 14 ++++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) 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 d3d163bd9da..e2c6a071262 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 @@ -54,8 +54,6 @@ public class OrganizationManagementFailureTest extends OrganizationManagementBas private static final String ERROR_CODE_BAD_REQUEST = "UE-10000"; private static final String ERROR_CODE_INVALID_PAGINATION_CURSOR = "ORG-60026"; - - private List organizationIDs = new ArrayList<>(); private String applicationID; private String m2mToken; 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 8fb43a96b57..cad6b783254 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 @@ -80,12 +80,12 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.UUID; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.core.IsNull.notNullValue; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.wso2.identity.integration.test.restclients.RestBaseClient.API_SERVER_PATH; import static org.wso2.identity.integration.test.restclients.RestBaseClient.CONTENT_TYPE_ATTRIBUTE; @@ -617,10 +617,7 @@ private void deleteApplication(String applicationId) throws Exception { public void createOrganizationsForPaginationTests() throws JSONException { organizations = createOrganizations(NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); - - if (organizations.size() != NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS) { - throw new RuntimeException("Failed to create the expected number of organizations for testing pagination."); - } + assertEquals(organizations.size(), NUM_OF_ORGANIZATIONS_FOR_PAGINATION_TESTS); } @DataProvider(name = "organizationLimitValidationProvider") @@ -633,7 +630,8 @@ public Object[][] organizationLimitValidationProvider() { }; } - @Test(dependsOnMethods = "createOrganizationsForPaginationTests", dataProvider = "organizationLimitValidationProvider") + @Test(dependsOnMethods = "createOrganizationsForPaginationTests", + dataProvider = "organizationLimitValidationProvider") public void testGetPaginatedOrganizationsWithLimit(int limit) { String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + QUESTION_MARK + LIMIT_QUERY_PARAM + EQUAL + limit; @@ -822,9 +820,9 @@ private String getLink(List> links, String rel) { if (rel.equals(link.get(REL))) { String href = link.get(HREF); if (href.contains(AFTER_QUERY_PARAM + EQUAL)) { - return href.substring(href.indexOf(AFTER_QUERY_PARAM + EQUAL) + 6); + return href.substring(href.indexOf(AFTER_QUERY_PARAM + EQUAL) + AFTER_QUERY_PARAM.length() + 1); } else if (href.contains(BEFORE_QUERY_PARAM + EQUAL)) { - return href.substring(href.indexOf(BEFORE_QUERY_PARAM + EQUAL) + 7); + return href.substring(href.indexOf(BEFORE_QUERY_PARAM + EQUAL) + BEFORE_QUERY_PARAM.length() + 1); } } }