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 #568

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.wso2.carbon.identity.scim2.common.impl;

import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;

import org.mockito.MockedStatic;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -33,17 +33,16 @@
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Matchers.anyString;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.testng.Assert.assertEquals;
import static org.mockito.Mockito.mockStatic;

/**
* Contains the unit test cases for IdentityResourceURLBuilder.
*/
@PrepareForTest({IdentityTenantUtil.class, ServiceURLBuilder.class})
public class IdentityResourceURLBuilderTest extends PowerMockTestCase {
public class IdentityResourceURLBuilderTest {

private static final Map<String, String> DUMMY_ENDPOINT_URI_MAP = new HashMap<String, String>() {{
put("Users", "https://localhost:9444/scim2/Users");
Expand All @@ -56,14 +55,17 @@ public class IdentityResourceURLBuilderTest extends PowerMockTestCase {
@Mock
ServiceURL mockServiceUrl;

private MockedStatic<ServiceURLBuilder> serviceURLBuilder;
private MockedStatic<IdentityTenantUtil> identityTenantUtil;

@BeforeMethod
public void setUpMethod() {

initMocks(this);
mockStatic(ServiceURLBuilder.class);
when(ServiceURLBuilder.create()).thenReturn(mockServiceURLBuilder);
serviceURLBuilder = mockStatic(ServiceURLBuilder.class);
serviceURLBuilder.when(() -> ServiceURLBuilder.create()).thenReturn(mockServiceURLBuilder);
when(mockServiceURLBuilder.addPath(anyString())).thenReturn(mockServiceURLBuilder);
mockStatic(IdentityTenantUtil.class);
identityTenantUtil = mockStatic(IdentityTenantUtil.class);
}

@DataProvider(name = "dataProviderForBuild")
Expand All @@ -82,7 +84,7 @@ public Object[][] dataProviderForBuild() {
public void testBuild(boolean isTenantQualifiedUrlsEnabled, String url, String resource, boolean throwError,
String expected) throws NotFoundException, URLBuilderException {

when(IdentityTenantUtil.isTenantQualifiedUrlsEnabled()).thenReturn(isTenantQualifiedUrlsEnabled);
identityTenantUtil.when(() -> IdentityTenantUtil.isTenantQualifiedUrlsEnabled()).thenReturn(isTenantQualifiedUrlsEnabled);
when(mockServiceURLBuilder.build()).thenAnswer(invocationOnMock -> {
if (throwError) {
throw new URLBuilderException("Protocol of service URL is not available.");
Expand All @@ -109,7 +111,7 @@ public Object[][] dataProviderForBuildThrowingNotFoundException() {
public void testBuildThrowingNotFoundException(boolean isTenantQualifiedUrlsEnabled, String resource)
throws URLBuilderException, NotFoundException {

when(IdentityTenantUtil.isTenantQualifiedUrlsEnabled()).thenReturn(isTenantQualifiedUrlsEnabled);
identityTenantUtil.when(() -> IdentityTenantUtil.isTenantQualifiedUrlsEnabled()).thenReturn(isTenantQualifiedUrlsEnabled);
when(mockServiceURLBuilder.build()).thenThrow(
new URLBuilderException("Protocol of service URL is not available."));
IdentityResourceURLBuilder identityResourceURLBuilder = new IdentityResourceURLBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
package org.wso2.carbon.identity.scim2.common.impl;

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.testng.IObjectFactory;
import org.mockito.MockedStatic;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;
import org.testng.annotations.Listeners;
import org.mockito.testng.MockitoTestNGListener;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.scim2.common.internal.SCIMCommonComponentHolder;
import org.wso2.carbon.identity.scim2.common.test.utils.CommonTestUtils;
Expand All @@ -44,19 +42,18 @@

import java.nio.file.Paths;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.anyInt;

/**
* Contains the unit test cases for IdentitySCIMManager.
*/
@PrepareForTest({SCIMCommonUtils.class, PrivilegedCarbonContext.class, SCIMCommonComponentHolder.class,CharonConfiguration.class})
@PowerMockIgnore({"javax.xml.*","org.w3c.dom.*","org.xml.sax.*"})
public class IdentitySCIMManagerTest extends PowerMockTestCase {
@Listeners(MockitoTestNGListener.class)
public class IdentitySCIMManagerTest {

@Mock
RealmService realmService;
Expand All @@ -76,22 +73,25 @@ public class IdentitySCIMManagerTest extends PowerMockTestCase {
private SCIMConfigProcessor scimConfigProcessor;
private IdentitySCIMManager identitySCIMManager;

private MockedStatic<SCIMCommonUtils> scimCommonUtils;
private MockedStatic<SCIMCommonComponentHolder> scimCommonComponentHolder;

@BeforeMethod
public void setUp() throws Exception {

mockStatic(SCIMCommonUtils.class);
when(SCIMCommonUtils.getSCIMUserURL()).thenReturn("http://scimUserUrl:9443");
scimCommonUtils = mockStatic(SCIMCommonUtils.class);
scimCommonUtils.when(() -> SCIMCommonUtils.getSCIMUserURL()).thenReturn("http://scimUserUrl:9443");

mockStatic(SCIMCommonComponentHolder.class);
when(SCIMCommonComponentHolder.getRealmService()).thenReturn(realmService);
scimCommonComponentHolder = mockStatic(SCIMCommonComponentHolder.class);
scimCommonComponentHolder.when(() -> SCIMCommonComponentHolder.getRealmService()).thenReturn(realmService);

scimConfigProcessor = SCIMConfigProcessor.getInstance();
String filePath = Paths
.get(System.getProperty("user.dir"), "src", "test", "resources", "charon-config-test.xml").toString();
scimConfigProcessor.buildConfigFromFile(filePath);
identitySCIMManager = IdentitySCIMManager.getInstance();

mockStatic(SCIMCommonComponentHolder.class);
scimCommonComponentHolder = mockStatic(SCIMCommonComponentHolder.class);

when(realmService.getTenantManager()).thenReturn(mockedTenantManager);
when(mockedTenantManager.getTenantId(anyString())).thenReturn(-1234);
Expand All @@ -102,12 +102,6 @@ public void setUp() throws Exception {
CommonTestUtils.initPrivilegedCarbonContext();
}

@ObjectFactory
public IObjectFactory getObjectFactory() {

return new org.powermock.modules.testng.PowerMockObjectFactory();
}

@Test
public void testGetInstance() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
package org.wso2.carbon.identity.scim2.common.impl;

import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.mockito.MockedStatic;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.core.ServiceURLBuilder;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.identity.role.v2.mgt.core.exception.IdentityRoleManagementException;
Expand All @@ -43,15 +42,15 @@
import java.util.List;
import java.util.Map;

import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.testng.Assert.assertEquals;

/**
* Contains the unit test cases for SCIMRoleManagerV2.
*/
@PrepareForTest({IdentityUtil.class})
public class SCIMRoleManagerV2Test extends PowerMockTestCase {
public class SCIMRoleManagerV2Test {

private static final String SAMPLE_TENANT_DOMAIN = "carbon.super";
private static final String SAMPLE_VALID_ROLE_ID = "595f5508-f286-446a-86c4-5071e07b98fc";
Expand All @@ -64,6 +63,8 @@ public class SCIMRoleManagerV2Test extends PowerMockTestCase {

private SCIMRoleManagerV2 scimRoleManagerV2;

private MockedStatic<IdentityUtil> identityUtil;

@BeforeClass
public void setUpClass() {

Expand All @@ -73,7 +74,7 @@ public void setUpClass() {
@BeforeMethod
public void setUpMethod() {

PowerMockito.mockStatic(IdentityUtil.class);
identityUtil = mockStatic(IdentityUtil.class);
scimRoleManagerV2 = new SCIMRoleManagerV2(roleManagementService, SAMPLE_TENANT_DOMAIN);
}

Expand Down
Loading
Loading