Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove powermock usage from unit tests #570

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
10 changes: 6 additions & 4 deletions components/org.wso2.carbon.identity.scim2.common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,48 @@
import org.apache.commons.lang.StringUtils;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wso2.carbon.base.CarbonBaseConstants;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.scim2.common.DAO.GroupDAO;
import org.wso2.carbon.identity.scim2.common.exceptions.IdentitySCIMException;
import org.wso2.carbon.identity.scim2.common.utils.SCIMCommonUtils;
import org.wso2.charon3.core.objects.Group;
import org.wso2.charon3.core.utils.AttributeUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Calendar;
import java.util.Set;
import java.util.HashSet;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.AssertJUnit.assertTrue;

@PrepareForTest({IdentityDatabaseUtil.class, StringUtils.class, SCIMCommonUtils.class, SCIMGroupHandler.class,
IdentityTenantUtil.class})
@PowerMockIgnore("java.sql.*")
public class SCIMGroupHandlerTest extends PowerMockTestCase {
public class SCIMGroupHandlerTest {

@Mock
private GroupDAO mockedGroupDAO;
Expand All @@ -73,16 +73,32 @@ public class SCIMGroupHandlerTest extends PowerMockTestCase {
@Mock
private PreparedStatement mockedPreparedStatement;

private MockedStatic<SCIMCommonUtils> scimCommonUtils;
private MockedStatic<IdentityDatabaseUtil> identityDatabaseUtil;
private MockedStatic<IdentityTenantUtil> identityTenantUtil;
private MockedStatic<StringUtils> stringUtils;

@BeforeMethod
public void setUp() throws Exception {
initMocks(this);
scimCommonUtils = mockStatic(SCIMCommonUtils.class);
identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class);
identityTenantUtil = mockStatic(IdentityTenantUtil.class);
stringUtils = mockStatic(StringUtils.class);
}

@AfterMethod
public void tearDown() throws Exception {
scimCommonUtils.close();
identityDatabaseUtil.close();
identityTenantUtil.close();
stringUtils.close();
System.clearProperty(CarbonBaseConstants.CARBON_HOME);
}

@Test
public void testAddMandatoryAttributes() throws Exception {
ResultSet resultSet = mock(ResultSet.class);
mockStatic(SCIMCommonUtils.class);
mockStatic(IdentityDatabaseUtil.class);

when(SCIMCommonUtils.getSCIMGroupURL(anyString())).thenReturn("ID");
when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
Expand Down Expand Up @@ -110,8 +126,6 @@ public void testGetGroupAttributesById() throws Exception {
@Test
public void testCreateSCIMAttributes() throws Exception {
ResultSet resultSet = mock(ResultSet.class);
mockStatic(IdentityDatabaseUtil.class);
mockStatic(SCIMCommonUtils.class);

Group group = new Group();
Date date = new Date();
Expand All @@ -132,8 +146,6 @@ public void testCreateSCIMAttributes() throws Exception {

@Test
public void testCreateSCIMAttributesExceptions() throws Exception {
mockStatic(IdentityDatabaseUtil.class);
mockStatic(SCIMCommonUtils.class);
ResultSet resultSet = mock(ResultSet.class);

Group group = new Group();
Expand All @@ -147,22 +159,26 @@ public void testCreateSCIMAttributesExceptions() throws Exception {
when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
when(mockedPreparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
whenNew(GroupDAO.class).withNoArguments().thenReturn(mockedGroupDAO);
when(mockedGroupDAO.isExistingGroup(SCIMCommonUtils.getGroupNameWithDomain("ALREADY_EXISTANT_GROUP_NAME"), 1)).thenReturn(false);

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
scimGroupHandler.createSCIMAttributes(group);
verify(mockedGroupDAO).addSCIMGroupAttributes(anyInt(), argumentCaptor.capture(), anyMap());
assertEquals("testDisplayName", argumentCaptor.getValue());
try (MockedConstruction<GroupDAO> mockedConstruction = Mockito.mockConstruction(
GroupDAO.class,
(mock, context) -> {
when(mock.isExistingGroup(SCIMCommonUtils.getGroupNameWithDomain("ALREADY_EXISTANT_GROUP_NAME"), 1))
.thenReturn(false);
})) {

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
scimGroupHandler.createSCIMAttributes(group);
verify(mockedConstruction.constructed().get(0))
.addSCIMGroupAttributes(anyInt(), argumentCaptor.capture(), anyMap());
assertEquals("testDisplayName", argumentCaptor.getValue());
}
}

@Test
public void testGetGroupName() throws Exception {
ResultSet resultSet = mock(ResultSet.class);
mockStatic(IdentityDatabaseUtil.class);
mockStatic(StringUtils.class);
mockStatic(SCIMCommonUtils.class);

when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
Expand All @@ -185,9 +201,6 @@ public void testGetGroupId() throws Exception {
public void testGetGroupWithAttributes() throws Exception {
Group group = new Group();
ResultSet resultSet = mock(ResultSet.class);
mockStatic(SCIMCommonUtils.class);
mockStatic(IdentityDatabaseUtil.class);
mockStatic(StringUtils.class);

Date date = new Date(2017, 10, 10, 10, 10, 10);
Map<String, String> attributes = new HashMap<String, String>();
Expand All @@ -208,9 +221,6 @@ public void testGetGroupWithAttributes() throws Exception {
public void testGetGroupWithAttributesSecondScenario() throws Exception {
Group group = new Group();
ResultSet resultSet = mock(ResultSet.class);
mockStatic(IdentityDatabaseUtil.class);
mockStatic(IdentityTenantUtil.class);
mockStatic(SCIMCommonUtils.class);

Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Expand All @@ -226,18 +236,28 @@ public void testGetGroupWithAttributesSecondScenario() throws Exception {
when(resultSet.next()).thenReturn(true);
when(mockedPreparedStatement.executeQuery()).thenReturn(resultSet);
when(mockedGroupDAO.isExistingGroup("EXISTING_GROUP_NAME", 1)).thenReturn(true);
whenNew(GroupDAO.class).withNoArguments().thenReturn(mockedGroupDAO);
when(mockedGroupDAO.getSCIMGroupAttributes(anyInt(), anyString())).thenReturn(attributes);
when(IdentityTenantUtil.getTenantDomain(1)).thenReturn("TENANT_DOMAIN");
when(SCIMCommonUtils.getSCIMGroupURL()).thenReturn("https://localhost:9443/t/TENANT_DOMAIN/Groups");
assertEquals(new SCIMGroupHandler(1).getGroupWithAttributes(group, "EXISTING_GROUP_NAME"), group);

Instant mockInstant = Instant.now();
try (MockedStatic<AttributeUtil> mockedAttributeUtil = Mockito.mockStatic(AttributeUtil.class)) {
mockedAttributeUtil.when(() -> AttributeUtil.parseDateTime(anyString())).thenReturn(mockInstant);

try (MockedConstruction<GroupDAO> mockedConstruction = Mockito.mockConstruction(
GroupDAO.class,
(mock, context) -> {
when(mock.getSCIMGroupAttributes(anyInt(), anyString())).thenReturn(attributes);
})) {

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
assertEquals(scimGroupHandler.getGroupWithAttributes(group, "EXISTING_GROUP_NAME"), group);
}
}
}

@Test
public void testIsGroupExisting() throws Exception {
ResultSet resultSet = mock(ResultSet.class);
mockStatic(IdentityDatabaseUtil.class);
mockStatic(SCIMCommonUtils.class);

when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
Expand All @@ -254,61 +274,66 @@ public void testIsGroupExisting() throws Exception {
@Test
public void testDeleteGroupAttributes() throws Exception {
ResultSet resultSet = mock(ResultSet.class);
mockStatic(IdentityDatabaseUtil.class);
mockStatic(SCIMCommonUtils.class);

when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
when(mockedPreparedStatement.executeQuery()).thenReturn(resultSet);
whenNew(GroupDAO.class).withNoArguments().thenReturn(mockedGroupDAO);
when(resultSet.next()).thenReturn(true);
when(mockedGroupDAO.isExistingGroup(anyString(), anyInt())).thenReturn(true);

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
scimGroupHandler.deleteGroupAttributes("GROUP_DELETABLE");
verify(mockedGroupDAO).removeSCIMGroup(anyInt(), argumentCaptor.capture());
assertEquals(argumentCaptor.getValue(),"GROUP_DELETABLE");

try (MockedConstruction<GroupDAO> mockedConstruction = Mockito.mockConstruction(
GroupDAO.class,
(mock, context) -> {
when(mock.isExistingGroup(anyString(), anyInt())).thenReturn(true);
})) {

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
scimGroupHandler.deleteGroupAttributes("GROUP_DELETABLE");
verify(mockedConstruction.constructed().get(0)).removeSCIMGroup(anyInt(), argumentCaptor.capture());
assertEquals(argumentCaptor.getValue(), "GROUP_DELETABLE");
}
}

@Test
public void testUpdateRoleName() throws Exception {
ResultSet resultSet = mock(ResultSet.class);
mockStatic(IdentityDatabaseUtil.class);
mockStatic(SCIMCommonUtils.class);

when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
whenNew(GroupDAO.class).withNoArguments().thenReturn(mockedGroupDAO);
when(mockedGroupDAO.isExistingGroup(anyString(), anyInt())).thenReturn(true);

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
scimGroupHandler.updateRoleName("EXISTENT_ROLE_NAME", "NEW_ROLE_NAME");
verify(mockedGroupDAO).updateRoleName(anyInt(),argumentCaptor.capture(),anyString());
assertEquals(argumentCaptor.getValue(),"EXISTENT_ROLE_NAME");
try (MockedConstruction<GroupDAO> mockedConstruction = Mockito.mockConstruction(
GroupDAO.class,
(mock, context) -> {
when(mock.isExistingGroup(anyString(), anyInt())).thenReturn(true);
})) {

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
scimGroupHandler.updateRoleName("EXISTENT_ROLE_NAME", "NEW_ROLE_NAME");
verify(mockedConstruction.constructed().get(0)).updateRoleName(anyInt(), argumentCaptor.capture(), anyString());
assertEquals(argumentCaptor.getValue(), "EXISTENT_ROLE_NAME");
}
}

@Test(expectedExceptions = IdentitySCIMException.class)
public void testUpdateRoleNameNonExistent() throws Exception {
ResultSet resultSet = mock(ResultSet.class);
mockStatic(IdentityDatabaseUtil.class);
mockStatic(SCIMCommonUtils.class);

when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
whenNew(GroupDAO.class).withNoArguments().thenReturn(mockedGroupDAO);
when(mockedGroupDAO.isExistingGroup("NON_EXISTENT_ROLE_NAME", 1)).thenReturn(false);

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
scimGroupHandler.updateRoleName("NON_EXISTENT_ROLE_NAME", "NEW_ROLE_NAME");
// This method is to test the throwing of an IdentitySCIMException, hence no assertion.
try (MockedConstruction<GroupDAO> mockedConstruction = Mockito.mockConstruction(
GroupDAO.class,
(mock, context) -> {
when(mock.isExistingGroup("NON_EXISTENT_ROLE_NAME", 1)).thenReturn(false);
})) {

SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(1);
scimGroupHandler.updateRoleName("NON_EXISTENT_ROLE_NAME", "NEW_ROLE_NAME");
}
}

@Test
public void testListSCIMRoles() throws Exception {
Set<String> groups = mock(HashSet.class);
ResultSet resultSet = mock(ResultSet.class);
mockStatic(IdentityDatabaseUtil.class);

when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
when(connection.prepareStatement(anyString())).thenReturn(mockedPreparedStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.wso2.carbon.identity.scim2.common.impl;

import org.apache.http.HttpStatus;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.scim2.common.extenstion.SCIMUserStoreException;
Expand All @@ -31,7 +30,7 @@
/**
* Contains the unit test cases for DefaultSCIMUserStoreErrorResolver.
*/
public class DefaultSCIMUserStoreErrorResolverTest extends PowerMockTestCase {
public class DefaultSCIMUserStoreErrorResolverTest {

@Test
public void testGetOrder() {
Expand Down
Loading
Loading