diff --git a/modules/charon-core/src/test/java/org/wso2/charon3/core/protocol/endpoints/UserResourceManagerTest.java b/modules/charon-core/src/test/java/org/wso2/charon3/core/protocol/endpoints/UserResourceManagerTest.java index eeab6d3ee..d93e235d9 100644 --- a/modules/charon-core/src/test/java/org/wso2/charon3/core/protocol/endpoints/UserResourceManagerTest.java +++ b/modules/charon-core/src/test/java/org/wso2/charon3/core/protocol/endpoints/UserResourceManagerTest.java @@ -37,7 +37,9 @@ import org.wso2.charon3.core.exceptions.NotFoundException; import org.wso2.charon3.core.exceptions.NotImplementedException; import org.wso2.charon3.core.extensions.UserManager; +import org.wso2.charon3.core.objects.ListedResource; import org.wso2.charon3.core.objects.User; +import org.wso2.charon3.core.objects.plainobjects.Cursor; import org.wso2.charon3.core.objects.plainobjects.UsersGetResponse; import org.wso2.charon3.core.protocol.ResponseCodeConstants; import org.wso2.charon3.core.protocol.SCIMResponse; @@ -55,8 +57,10 @@ import java.util.Map; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; @@ -575,6 +579,61 @@ public void testListWithGetInteger(String filter, Integer startIndexInt, Assert.assertEquals(outputScimResponse.getResponseStatus(), ResponseCodeConstants.CODE_OK); } + @DataProvider(name = "dataForListWithGetCursor") + public Object[][] dataToGetListCursor() throws BadRequestException, CharonException, InternalErrorException { + + User user = getNewUser(); + List users = new ArrayList<>(); + users.add(user); + UsersGetResponse userResponse = new UsersGetResponse(1, "Adam_123", "Bob_456", users); + UsersGetResponse userResponse2 = new UsersGetResponse(1, users); + + return new Object[][]{ + {null, "eyJ2YWx1ZSI6IkFjaGFsYV8xMzEiLCJkaXJlY3Rpb24iOiJQUkVWSU9VUyJ9", 2, null, null, DOMAIN_NAME, + "emails", null, userResponse}, + {"userName sw A", "eyJ2YWx1ZSI6IkFjaGFsYV8xMzEiLCJkaXJlY3Rpb24iOiJQUkVWSU9VUyJ9", 10, null, null, + DOMAIN_NAME, "userName", null, userResponse2} + }; + } + + @Test(dataProvider = "dataForListWithGetCursor") + public void testListWithGetCursor(String filter, String cursorString, + Integer count, String sortBy, String sortOrder, String domainName, + String attributes, String excludeAttributes, UsersGetResponse usersResponse) + throws NotImplementedException, BadRequestException, CharonException { + + Mockito.when(userManager.listUsersWithGET(any(), any(Cursor.class), anyInt(), anyString(), anyString(), anyString(), anyMap())).thenReturn(usersResponse); + SCIMResponse outputScimResponse = userResourceManager.listWithGET(userManager, filter, cursorString, + count, sortBy, sortOrder, domainName, attributes, excludeAttributes); + + Assert.assertEquals(outputScimResponse.getResponseStatus(), ResponseCodeConstants.CODE_OK); + } + + @DataProvider(name = "dataForTestCreateListedResource") + public Object[][] dataToTestCreateListedResource() + throws BadRequestException, CharonException, InternalErrorException { + + User user = getNewUser(); + List users = new ArrayList<>(); + users.add(user); + UsersGetResponse userResponse = new UsersGetResponse(1, "Adam_123", "Bob_456", users); + UsersGetResponse userResponse2 = new UsersGetResponse(1, users); + return new Object[][]{ + {userResponse, null, "Jake_5", 5}, + {userResponse2, 1, null, 5}, + {userResponse, null, "", 100} + }; + } + + @Test(dataProvider = "dataForTestCreateListedResource") + public void testCreateListedResource(UsersGetResponse usersGetResponse, Integer startIndex, String cursor, + Integer limit) throws NotFoundException, CharonException { + + ListedResource listedResource = userResourceManager.createListedResource(usersGetResponse, startIndex, + cursor, limit); + Assert.assertEquals(listedResource.getResources().size(), 1); + } + @DataProvider(name = "dataForTestCreateUserSuccess") public Object[][] dataToTestCreateUserSuccess() { @@ -991,8 +1050,7 @@ public void testUpdateWithPUTBadRequestException(String id, String scimObjectStr @DataProvider(name = "dataForListWithPOST") public Object[][] dataToListWithPOST() throws BadRequestException, CharonException, InternalErrorException { - List tempList = new ArrayList<>(); - tempList.add(1); + List tempList = new ArrayList<>(); tempList.add(getNewUser()); return new Object[][]{ {RESOURCE_STRING, tempList} @@ -1000,12 +1058,13 @@ public Object[][] dataToListWithPOST() throws BadRequestException, CharonExcepti } @Test(dataProvider = "dataForListWithPOST") - public void testListWithPOST(String resourceString, UsersGetResponse usersGetResponse) + public void testListWithPOST(String resourceString, List tempList) throws NotImplementedException, BadRequestException, CharonException { abstractResourceManager.when(() -> AbstractResourceManager.getResourceEndpointURL(SCIMConstants.USER_ENDPOINT)) .thenReturn(SCIM2_USER_ENDPOINT + "/.search"); - Mockito.when(userManager.listUsersWithPost(any(SearchRequest.class), anyMap())).thenReturn(usersGetResponse); + Mockito.when(userManager.listUsersWithPost(any(SearchRequest.class), anyMap())) + .thenReturn(new UsersGetResponse(1, tempList)); SCIMResponse scimResponse = userResourceManager.listWithPOST(resourceString, userManager); Assert.assertEquals(scimResponse.getResponseStatus(), ResponseCodeConstants.CODE_OK); } diff --git a/modules/charon-core/src/test/java/org/wso2/charon3/core/utils/ResourceManagerUtilTest.java b/modules/charon-core/src/test/java/org/wso2/charon3/core/utils/ResourceManagerUtilTest.java index f6d688ab2..a772d6630 100644 --- a/modules/charon-core/src/test/java/org/wso2/charon3/core/utils/ResourceManagerUtilTest.java +++ b/modules/charon-core/src/test/java/org/wso2/charon3/core/utils/ResourceManagerUtilTest.java @@ -23,6 +23,7 @@ import org.testng.annotations.Test; import org.wso2.charon3.core.exceptions.BadRequestException; import org.wso2.charon3.core.exceptions.CharonException; +import org.wso2.charon3.core.objects.plainobjects.Cursor; import org.wso2.charon3.core.schema.AttributeSchema; import org.wso2.charon3.core.schema.SCIMAttributeSchema; import org.wso2.charon3.core.schema.SCIMResourceTypeSchema; @@ -265,6 +266,67 @@ public void testProcessStartIndex(Integer startIndex, Integer expectedIndex) { Assert.assertEquals(index, expectedIndex); } + @DataProvider(name = "dataForProcessCursor") + public Object[][] dataToProcessCursor() { + + return new Object[][]{ + + {"eyJ2YWx1ZSI6IkFjaGFsYV8xMzEiLCJkaXJlY3Rpb24iOiJQUkVWSU9VUyJ9", "Achala_131", "PREVIOUS"}, + {"", "" , "NEXT"}, + {null, "", "NEXT"} + }; + } + + @Test(dataProvider = "dataForProcessCursor") + public void testProcessCursor(String cursorStr, String expectedCursor, String expectedDirection) { + + Cursor cursor = ResourceManagerUtil.processCursor(cursorStr); + Assert.assertEquals(cursor.getCursorValue(), expectedCursor); + Assert.assertEquals(cursor.getDirection(), expectedDirection); + } + + @DataProvider(name = "dataForProcessPagination") + public Object[][] dataToProcessPagination() { + + return new Object[][]{ + + {null, "eyJ2YWx1ZSI6IkFjaGFsYV8xMzEiLCJkaXJlY3Rpb24iOiJQUkVWSU9VUyJ9", "cursor"}, + {null, "eyJ2YWx1ZSI6IkFjaGFsYV8xMzEiLCJkaXJlY3Rpb24iOiJQUkVWSU9VUyJ9", "cursor"}, + {0, null, "offset"}, + {null, null, "offset"}, + {10, null, "offset"}, + {-5, null, "offset"}, + }; + } + + @Test(dataProvider = "dataForProcessPagination") + public void testProcessPagination(Integer startIndex, String cursorString, String expectedResult) + throws CharonException { + + String paginationType = ResourceManagerUtil.processPagination(startIndex, cursorString); + Assert.assertEquals(paginationType, expectedResult); + } + + @DataProvider(name = "dataForProcessPaginationException") + public Object[][] dataToProcessPaginationException() { + + return new Object[][]{ + + {0, "eyJ2YWx1ZSI6IkFjaGFsYV8xMzEiLCJkaXJlY3Rpb24iOiJQUkVWSU9VUyJ9"}, + {10, "eyJ2YWx1ZSI6IkFjaGFsYV8xMzEiLCJkaXJlY3Rpb24iOiJQUkVWSU9VUyJ9"}, + {-3, "eyJ2YWx1ZSI6IkFjaGFsYV8xMzEiLCJkaXJlY3Rpb24iOiJQUkVWSU9VUyJ9"}, + }; + } + + @Test(dataProvider = "dataForProcessPaginationException", expectedExceptions = CharonException.class) + public void testProcessPaginationException(Integer startIndex, String cursorString) + throws CharonException { + + ResourceManagerUtil.processPagination(startIndex, cursorString); + // This method is for testing of throwing CharonException when both cursor and offset are given, + // hence no assertion. + } + @DataProvider(name = "dataForProcessStartIndexString") public Object[][] dataToProcessStartIndexString() {