diff --git a/components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java b/components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java index faf6115ec5..4fce4f04d4 100644 --- a/components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java +++ b/components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java @@ -21,11 +21,11 @@ import org.apache.commons.lang.StringUtils; import org.mockito.Mock; import org.mockito.MockedStatic; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.mockito.testng.MockitoTestNGListener; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; -import org.mockito.Mockito; -import org.mockito.testng.MockitoTestNGListener; import org.testng.annotations.DataProvider; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @@ -47,34 +47,33 @@ import org.wso2.carbon.identity.oauth2.dao.TokenManagementDAO; import org.wso2.carbon.identity.oauth2.model.AccessTokenDO; import org.wso2.carbon.identity.oauth2.util.OAuth2Util; +import org.wso2.carbon.identity.organization.management.organization.user.sharing.OrganizationUserSharingService; +import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; import org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants; import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService; import org.wso2.carbon.identity.role.v2.mgt.core.model.RoleBasicInfo; import org.wso2.carbon.user.api.RealmConfiguration; import org.wso2.carbon.user.core.UserStoreManager; -import org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import org.wso2.carbon.identity.oauth2.dao.TokenManagementDAO; -import org.wso2.carbon.identity.organization.management.organization.user.sharing.OrganizationUserSharingService; -import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.user.core.jdbc.UniqueIDJDBCUserStoreManager; +import org.wso2.carbon.user.core.util.UserCoreUtil; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.util.HashMap; -import java.util.Map; - -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.when; import java.util.HashSet; +import java.util.Map; import java.util.Set; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; @@ -92,9 +91,6 @@ public class OAuthUtilTest { private static final String managedOrgClaim = "http://wso2.org/claims/identity/managedOrg"; private static final String orgID = "76dedfb5-99ef-4bf3-92e6-56d296db55ec"; - @Mock - private OAuthComponentServiceHolder oAuthComponentServiceHolderMock; - @Mock private OrganizationManager organizationManagerMock; @@ -104,10 +100,6 @@ public class OAuthUtilTest { @Mock private TokenManagementDAO tokenManagementDAOMock; - @Mock - private OAuthTokenPersistenceFactory oAuthTokenPersistenceFactoryMock; - - @Mock RoleManagementService roleManagementService; @@ -367,7 +359,7 @@ public void testRevokeTokensForOrganizationAudienceRoles() throws Exception { inboundAuthenticationRequestConfigs[0] = inboundAuthenticationRequestConfig; inboundAuthenticationConfig.setInboundAuthenticationRequestConfigs(inboundAuthenticationRequestConfigs); serviceProvider.setInboundAuthenticationConfig(inboundAuthenticationConfig); - when(applicationManagementService.getApplicationByResourceId( + lenient().when(applicationManagementService.getApplicationByResourceId( appId, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)).thenReturn(serviceProvider); when(applicationManagementService.getApplicationResourceIDByInboundKey(anyString(), anyString(), anyString())). thenReturn(appId); @@ -387,11 +379,10 @@ public void testRevokeTokensForOrganizationAudienceRoles() throws Exception { when(mockAccessTokenDAO.getAccessTokens(anyString(), any(AuthenticatedUser.class), nullable(String.class), anyBoolean())).thenReturn(accessTokens); - TokenManagementDAO mockTokenManagementDao = mock(TokenManagementDAO.class); - when(mockOAuthTokenPersistenceFactory.getTokenManagementDAO()).thenReturn(mockTokenManagementDao); + when(mockOAuthTokenPersistenceFactory.getTokenManagementDAO()).thenReturn(tokenManagementDAOMock); Set clientIds = new HashSet<>(); clientIds.add(clientId); - when(mockTokenManagementDao.getAllTimeAuthorizedClientIds(any())).thenReturn(clientIds); + when(tokenManagementDAOMock.getAllTimeAuthorizedClientIds(any())).thenReturn(clientIds); boolean result = OAuthUtil.revokeTokens(username, userStoreManager, roleId); verify(mockAccessTokenDAO, times(1)).revokeAccessTokens(any(), anyBoolean()); @@ -401,49 +392,38 @@ public void testRevokeTokensForOrganizationAudienceRoles() throws Exception { @Test public void testRevokeTokensInSharedUserFlow() throws Exception { - UniqueIDJDBCUserStoreManager userStoreManager = Mockito.spy( - new UniqueIDJDBCUserStoreManager(new RealmConfiguration(), 1)); + try (MockedStatic userCoreUtil = mockStatic(UserCoreUtil.class)) { + + UniqueIDJDBCUserStoreManager userStoreManager = Mockito.spy( + new UniqueIDJDBCUserStoreManager(new RealmConfiguration(), 1)); - org.wso2.carbon.user.core.common.User mockUser = Mockito.mock(org.wso2.carbon.user.core.common.User.class); - doReturn(mockUser).when(userStoreManager).getUser(null, null); + org.wso2.carbon.user.core.common.User mockUser = Mockito.mock(org.wso2.carbon.user.core.common.User.class); + lenient().doReturn(mockUser).when(userStoreManager).getUser(null, null); - Map claimsMap = new HashMap<>(); - claimsMap.put(managedOrgClaim, orgID); - doReturn(claimsMap).when(userStoreManager).getUserClaimValuesWithID(null, new String[]{managedOrgClaim}, null); + Map claimsMap = new HashMap<>(); + claimsMap.put(managedOrgClaim, orgID); + lenient().doReturn(claimsMap).when(userStoreManager) + .getUserClaimValuesWithID(null, new String[]{managedOrgClaim}, null); - try (MockedStatic organizationManagementUtil - = mockStatic(OrganizationManagementUtil.class); - MockedStatic oAuthComponentServiceHolder - = mockStatic(OAuthComponentServiceHolder.class); - MockedStatic oAuthTokenPersistenceFactory - = mockStatic(OAuthTokenPersistenceFactory.class) - ) { - mockStaticMethods(oAuthComponentServiceHolder, oAuthTokenPersistenceFactory, organizationManagementUtil); + OAuthComponentServiceHolder mockOAuthComponentServiceHolder = mock(OAuthComponentServiceHolder.class); + when(OAuthComponentServiceHolder.getInstance()).thenReturn(mockOAuthComponentServiceHolder); + lenient().when(mockOAuthComponentServiceHolder.getOrganizationManager()) + .thenReturn(organizationManagerMock); + lenient().when(mockOAuthComponentServiceHolder.getOrganizationUserSharingService()) + .thenReturn(organizationUserSharingServiceMock); + lenient().when(organizationManagerMock.isPrimaryOrganization(anyString())).thenReturn(true); + when(OrganizationManagementUtil.isOrganization(anyString())).thenReturn(false); + when(UserCoreUtil.removeDomainFromName(null)).thenReturn("carbon.super"); + + OAuthTokenPersistenceFactory mockOAuthTokenPersistenceFactory = mock(OAuthTokenPersistenceFactory.class); + when(OAuthTokenPersistenceFactory.getInstance()).thenReturn(mockOAuthTokenPersistenceFactory); + when(mockOAuthTokenPersistenceFactory.getTokenManagementDAO()).thenReturn(tokenManagementDAOMock); boolean result = OAuthUtil.revokeTokens(null, userStoreManager, null); assertTrue(result); } } - private void mockStaticMethods(MockedStatic oAuthComponentServiceHolder, - MockedStatic oAuthTokenPersistenceFactory, - MockedStatic organizationManagementUtil) - throws Exception { - - oAuthComponentServiceHolder.when(OAuthComponentServiceHolder::getInstance) - .thenReturn(oAuthComponentServiceHolderMock); - when(oAuthComponentServiceHolderMock.getOrganizationManager()).thenReturn(organizationManagerMock); - when(oAuthComponentServiceHolderMock.getOrganizationUserSharingService()) - .thenReturn(organizationUserSharingServiceMock); - - oAuthTokenPersistenceFactory.when(OAuthTokenPersistenceFactory::getInstance) - .thenReturn(oAuthTokenPersistenceFactoryMock); - when(oAuthTokenPersistenceFactoryMock.getTokenManagementDAO()).thenReturn(tokenManagementDAOMock); - - organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyString())).thenReturn(true); - when(organizationManagerMock.isPrimaryOrganization(anyString())).thenReturn(true); - } - private OAuthCache getOAuthCache(OAuthCacheKey oAuthCacheKey) { // Add some value to OAuthCache.