Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaminduDilshan committed Jan 3, 2025
1 parent c78c3b2 commit 9b28e2a
Show file tree
Hide file tree
Showing 5 changed files with 427 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public User getUser(String userId, Map<String, Boolean> requiredAttributes) thro
throw resolveError(e, errMsg);
}
} catch (BadRequestException | NotImplementedException e) {
throw new CharonException("Error in getting user information from Carbon User Store", e);
throw new CharonException("Error in getting user information from Carbon User Store", e);
}
return scimUser;
}
Expand Down Expand Up @@ -601,7 +601,7 @@ public void deleteUser(String userId) throws NotFoundException, CharonException,
@Override
@Deprecated
public UsersGetResponse listUsersWithGET(Node rootNode, int startIndex, int count, String sortBy, String sortOrder,
String domainName, Map<String, Boolean> requiredAttributes)
String domainName, Map<String, Boolean> requiredAttributes)
throws CharonException, NotImplementedException, BadRequestException {

if (sortBy != null || sortOrder != null) {
Expand All @@ -615,7 +615,7 @@ public UsersGetResponse listUsersWithGET(Node rootNode, int startIndex, int coun

@Override
public UsersGetResponse listUsersWithGET(Node rootNode, Integer startIndex, Integer count, String sortBy,
String sortOrder, String domainName, Map<String, Boolean> requiredAttributes)
String sortOrder, String domainName, Map<String, Boolean> requiredAttributes)
throws CharonException, NotImplementedException, BadRequestException {

// Validate NULL value for startIndex.
Expand Down Expand Up @@ -689,7 +689,7 @@ private Resource getResourceByTenantId(int tenantId) throws org.wso2.carbon.user
* @throws BadRequestException
*/
private UsersGetResponse listUsers(Map<String, Boolean> requiredAttributes, int offset, Integer limit,
String sortBy, String sortOrder, String domainName) throws CharonException,
String sortBy, String sortOrder, String domainName) throws CharonException,
BadRequestException {

List<User> scimUsers = new ArrayList<>();
Expand Down Expand Up @@ -980,7 +980,7 @@ private Set<org.wso2.carbon.user.core.common.User> listUsernamesAcrossAllDomains
* @throws CharonException Error while retrieving users
*/
private List<User> getUserDetails(Set<org.wso2.carbon.user.core.common.User> coreUsers,
Map<String, Boolean> requiredAttributes)
Map<String, Boolean> requiredAttributes)
throws CharonException, BadRequestException {

List<User> users = new ArrayList<>();
Expand Down Expand Up @@ -1410,7 +1410,7 @@ private int handleLimitEqualsNULL(Integer limit) {
* @throws BadRequestException
*/
private UsersGetResponse filterUsers(Node node, Map<String, Boolean> requiredAttributes, int offset, Integer limit,
String sortBy, String sortOrder, String domainName) throws CharonException,
String sortBy, String sortOrder, String domainName) throws CharonException,
BadRequestException {

// Handle limit equals NULL scenario.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.scim2.common.DAO;

import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.scim2.common.exceptions.IdentitySCIMException;
import org.wso2.carbon.identity.scim2.common.utils.SCIMCommonUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;

public class GroupDAOTest {

@Mock
private Connection connection;

@Mock
private PreparedStatement mockedPreparedStatement;

@Mock
private ResultSet resultSet;

private MockedStatic<IdentityDatabaseUtil> identityDatabaseUtil;
private MockedStatic<SCIMCommonUtils> scimCommonUtils;

@BeforeMethod
public void setUp() {

initMocks(this);
identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class);
scimCommonUtils = mockStatic(SCIMCommonUtils.class);
}

@AfterMethod
public void tearDown() {

identityDatabaseUtil.close();
scimCommonUtils.close();
}

@DataProvider(name = "updateSCIMGroupAttributesDataProvider")
public Object[][] updateSCIMGroupAttributesDataProvider() {

return new Object[][]{
{true},
{false}
};
}

@Test(dataProvider = "updateSCIMGroupAttributesDataProvider")
public void testUpdateSCIMGroupAttributes(boolean isUpdatedColumnMethod) throws Exception {

Map<String, String> attributes = new HashMap<>();
attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.created", "2017-10-10T10:10:10Z");
attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.lastModified", "2017-10-10T10:10:10Z");

when(IdentityDatabaseUtil.getDBConnection(true)).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
when(mockedPreparedStatement.executeBatch()).thenReturn(new int[]{1});
when(resultSet.next()).thenReturn(true);
when(SCIMCommonUtils.getGroupNameWithDomain(anyString())).thenReturn("PRIMARY/GROUP_NAME");

Connection mockedConnection2 = mock(Connection.class);
PreparedStatement mockedPreparedStatement2 = mock(PreparedStatement.class);
ResultSet mockedResultSet2 = mock(ResultSet.class);
when(IdentityDatabaseUtil.getDBConnection()).thenReturn(mockedConnection2);
when(mockedConnection2.prepareStatement(anyString())).thenReturn(mockedPreparedStatement2);
when(mockedPreparedStatement2.executeQuery()).thenReturn(mockedResultSet2);
when(mockedResultSet2.next()).thenReturn(true);

GroupDAO groupDAO = spy(new GroupDAO());
doReturn(true).when(groupDAO).isExistingGroup(anyString(), anyInt());

if (isUpdatedColumnMethod) {
groupDAO.updateSCIMGroupAttributesWithUpdatedColumn(1, "GROUP_NAME", attributes);
} else {
groupDAO.updateSCIMGroupAttributes(1, "GROUP_NAME", attributes);
}
verify(mockedPreparedStatement, times(1)).executeBatch();
}

@Test(dataProvider = "updateSCIMGroupAttributesDataProvider",
expectedExceptions = IdentitySCIMException.class,
expectedExceptionsMessageRegExp = "Error when updating SCIM Attributes for the group: GROUP_NAME " +
"A Group with the same name doesn't exists.")
public void testUpdateSCIMGroupAttributesWithNonExistingGroup(boolean isUpdatedColumnMethod) throws Exception {

Map<String, String> attributes = new HashMap<>();
attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.created", "2017-10-10T10:10:10Z");
attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.lastModified", "2017-10-10T10:10:10Z");

when(IdentityDatabaseUtil.getDBConnection(true)).thenReturn(connection);
when(SCIMCommonUtils.getGroupNameWithDomain(anyString())).thenReturn("PRIMARY/GROUP_NAME");

GroupDAO groupDAO = spy(new GroupDAO());
doReturn(false).when(groupDAO).isExistingGroup(anyString(), anyInt());

if (isUpdatedColumnMethod) {
groupDAO.updateSCIMGroupAttributesWithUpdatedColumn(1, "GROUP_NAME", attributes);
} else {
groupDAO.updateSCIMGroupAttributes(1, "GROUP_NAME", attributes);
}
}

@Test(dataProvider = "updateSCIMGroupAttributesDataProvider",
expectedExceptions = IdentitySCIMException.class,
expectedExceptionsMessageRegExp = "Error when adding SCIM Attribute: nonExisting " +
"An attribute with the same name doesn't exists.")
public void testUpdateSCIMGroupAttributesWithNonExistingAttributes(boolean isUpdatedColumnMethod)
throws Exception {

Map<String, String> attributes = new HashMap<>();
attributes.put("nonExisting", "test-value");

when(IdentityDatabaseUtil.getDBConnection(true)).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
when(mockedPreparedStatement.executeBatch()).thenReturn(new int[]{1});
when(resultSet.next()).thenReturn(true);
when(SCIMCommonUtils.getGroupNameWithDomain(anyString())).thenReturn("PRIMARY/GROUP_NAME");

Connection mockedConnection2 = mock(Connection.class);
PreparedStatement mockedPreparedStatement2 = mock(PreparedStatement.class);
ResultSet mockedResultSet2 = mock(ResultSet.class);
when(IdentityDatabaseUtil.getDBConnection()).thenReturn(mockedConnection2);
when(mockedConnection2.prepareStatement(anyString())).thenReturn(mockedPreparedStatement2);
when(mockedPreparedStatement2.executeQuery()).thenReturn(mockedResultSet2);
when(mockedResultSet2.next()).thenReturn(false);

GroupDAO groupDAO = spy(new GroupDAO());
doReturn(true).when(groupDAO).isExistingGroup(anyString(), anyInt());

if (isUpdatedColumnMethod) {
groupDAO.updateSCIMGroupAttributesWithUpdatedColumn(1, "GROUP_NAME", attributes);
} else {
groupDAO.updateSCIMGroupAttributes(1, "GROUP_NAME", attributes);
}
}

@Test(dataProvider = "updateSCIMGroupAttributesDataProvider",
expectedExceptions = IdentitySCIMException.class,
expectedExceptionsMessageRegExp = "Error updating the SCIM Group Attributes.")
public void testUpdateSCIMGroupAttributesWithSQLException(boolean isUpdatedColumnMethod) throws Exception {

Map<String, String> attributes = new HashMap<>();
attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.created", "2017-10-10T10:10:10Z");
attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.lastModified", "2017-10-10T10:10:10Z");

when(IdentityDatabaseUtil.getDBConnection(true)).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenThrow(new SQLException());
when(SCIMCommonUtils.getGroupNameWithDomain(anyString())).thenReturn("PRIMARY/GROUP_NAME");

GroupDAO groupDAO = spy(new GroupDAO());
doReturn(true).when(groupDAO).isExistingGroup(anyString(), anyInt());

if (isUpdatedColumnMethod) {
groupDAO.updateSCIMGroupAttributesWithUpdatedColumn(1, "GROUP_NAME", attributes);
} else {
groupDAO.updateSCIMGroupAttributes(1, "GROUP_NAME", attributes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,17 @@ public void testListSCIMRoles() throws Exception {
assertNotNull(new SCIMGroupHandler(1).listSCIMRoles());
}

@Test
public void testUpdateSCIMAttributes() throws IdentitySCIMException {

Map<String, String> attributes = new HashMap<String, String>();
attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.created", "2017-10-10T10:10:10Z");
attributes.put("urn:ietf:params:scim:schemas:core:2.0:meta.lastModified", "2017-10-10T10:10:10Z");

try (MockedConstruction<GroupDAO> mockedConstruction = Mockito.mockConstruction(GroupDAO.class)) {
new SCIMGroupHandler(1).updateSCIMAttributes("GROUP_NAME", attributes);
verify(mockedConstruction.constructed().get(0))
.updateSCIMGroupAttributesWithUpdatedColumn(1, "GROUP_NAME", attributes);
}
}
}
Loading

0 comments on commit 9b28e2a

Please sign in to comment.