Skip to content

Commit

Permalink
Updating tests broken due to changes and writing new tests for cursor…
Browse files Browse the repository at this point in the history
… pagination
  • Loading branch information
BojithaPiyathilake committed Jul 7, 2022
1 parent cd64d81 commit 3884ce4
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<User> 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<User> 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() {

Expand Down Expand Up @@ -991,21 +1050,21 @@ public void testUpdateWithPUTBadRequestException(String id, String scimObjectStr
@DataProvider(name = "dataForListWithPOST")
public Object[][] dataToListWithPOST() throws BadRequestException, CharonException, InternalErrorException {

List<Object> tempList = new ArrayList<>();
tempList.add(1);
List<User> tempList = new ArrayList<>();
tempList.add(getNewUser());
return new Object[][]{
{RESOURCE_STRING, tempList}
};
}

@Test(dataProvider = "dataForListWithPOST")
public void testListWithPOST(String resourceString, UsersGetResponse usersGetResponse)
public void testListWithPOST(String resourceString, List<User> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {

Expand Down

0 comments on commit 3884ce4

Please sign in to comment.