-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c78c3b2
commit fd40eff
Showing
4 changed files
with
412 additions
and
0 deletions.
There are no files selected for viewing
192 changes: 192 additions & 0 deletions
192
...ty.scim2.common/src/test/java/org/wso2/carbon/identity/scim2/common/DAO/GroupDAOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.