From 23ff2360a598fa2b5841a11e16f7bed58bfa8938 Mon Sep 17 00:00:00 2001 From: Hasini Samarathunga Date: Fri, 17 Jan 2025 12:13:41 +0530 Subject: [PATCH] Fix merge conflict issue in OAuthUtilTest --- .../carbon/identity/oauth/OAuthUtilTest.java | 113 +++++++++--------- .../carbon/identity/oauth2/TestConstants.java | 7 +- 2 files changed, 61 insertions(+), 59 deletions(-) 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..6cef1835e5 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,39 +47,47 @@ 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.organization.user.sharing.models.UserAssociation; +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.api.UserRealm; 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.service.RealmService; +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.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.doReturn; +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; import static org.testng.Assert.assertTrue; +import static org.wso2.carbon.identity.oauth2.TestConstants.CARBON_TENANT_DOMAIN; import static org.wso2.carbon.identity.oauth2.TestConstants.LOCAL_IDP; +import static org.wso2.carbon.identity.oauth2.TestConstants.MANAGED_ORG_CLAIM_URI; +import static org.wso2.carbon.identity.oauth2.TestConstants.SAMPLE_ID; /** * Unit tests for OAuthUtil class. @@ -89,12 +97,6 @@ @Listeners(MockitoTestNGListener.class) 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; @@ -105,8 +107,7 @@ public class OAuthUtilTest { private TokenManagementDAO tokenManagementDAOMock; @Mock - private OAuthTokenPersistenceFactory oAuthTokenPersistenceFactoryMock; - + private RealmService realmService; @Mock RoleManagementService roleManagementService; @@ -367,7 +368,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 +388,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,47 +401,46 @@ 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)) { - 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); + UniqueIDJDBCUserStoreManager userStoreManager = Mockito.spy( + new UniqueIDJDBCUserStoreManager(new RealmConfiguration(), 1)); - Map claimsMap = new HashMap<>(); - claimsMap.put(managedOrgClaim, orgID); - doReturn(claimsMap).when(userStoreManager).getUserClaimValuesWithID(null, new String[]{managedOrgClaim}, null); + org.wso2.carbon.user.core.common.User mockUser = Mockito.mock(org.wso2.carbon.user.core.common.User.class); + doReturn(mockUser).when(userStoreManager).getUser(any(), eq(null)); - try (MockedStatic organizationManagementUtil - = mockStatic(OrganizationManagementUtil.class); - MockedStatic oAuthComponentServiceHolder - = mockStatic(OAuthComponentServiceHolder.class); - MockedStatic oAuthTokenPersistenceFactory - = mockStatic(OAuthTokenPersistenceFactory.class) - ) { - mockStaticMethods(oAuthComponentServiceHolder, oAuthTokenPersistenceFactory, organizationManagementUtil); + Map claimsMap = new HashMap<>(); + claimsMap.put(MANAGED_ORG_CLAIM_URI, SAMPLE_ID); + doReturn(claimsMap).when(userStoreManager) + .getUserClaimValuesWithID(null, new String[]{MANAGED_ORG_CLAIM_URI}, null); - boolean result = OAuthUtil.revokeTokens(null, userStoreManager, null); - assertTrue(result); - } - } + OAuthComponentServiceHolder mockOAuthComponentServiceHolder = mock(OAuthComponentServiceHolder.class); + when(OAuthComponentServiceHolder.getInstance()).thenReturn(mockOAuthComponentServiceHolder); + when(mockOAuthComponentServiceHolder.getOrganizationManager()).thenReturn(organizationManagerMock); + when(organizationManagerMock.isPrimaryOrganization(anyString())).thenReturn(true); - private void mockStaticMethods(MockedStatic oAuthComponentServiceHolder, - MockedStatic oAuthTokenPersistenceFactory, - MockedStatic organizationManagementUtil) - throws Exception { + when(OrganizationManagementUtil.isOrganization(anyString())).thenReturn(true); + when(UserCoreUtil.removeDomainFromName(null)).thenReturn(CARBON_TENANT_DOMAIN); - oAuthComponentServiceHolder.when(OAuthComponentServiceHolder::getInstance) - .thenReturn(oAuthComponentServiceHolderMock); - when(oAuthComponentServiceHolderMock.getOrganizationManager()).thenReturn(organizationManagerMock); - when(oAuthComponentServiceHolderMock.getOrganizationUserSharingService()) - .thenReturn(organizationUserSharingServiceMock); + UserAssociation userAssociation = new UserAssociation(); + userAssociation.setAssociatedUserId(SAMPLE_ID); + when(mockOAuthComponentServiceHolder.getOrganizationUserSharingService()) + .thenReturn(organizationUserSharingServiceMock); + when(organizationUserSharingServiceMock.getUserAssociation(null, null)) + .thenReturn(userAssociation); - oAuthTokenPersistenceFactory.when(OAuthTokenPersistenceFactory::getInstance) - .thenReturn(oAuthTokenPersistenceFactoryMock); - when(oAuthTokenPersistenceFactoryMock.getTokenManagementDAO()).thenReturn(tokenManagementDAOMock); + when(mockOAuthComponentServiceHolder.getRealmService()).thenReturn(realmService); + UserRealm userRealm = mock(UserRealm.class); + when(userRealm.getUserStoreManager()).thenReturn(userStoreManager); + when(realmService.getTenantUserRealm(anyInt())).thenReturn(userRealm); - organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyString())).thenReturn(true); - when(organizationManagerMock.isPrimaryOrganization(anyString())).thenReturn(true); + 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 OAuthCache getOAuthCache(OAuthCacheKey oAuthCacheKey) { diff --git a/components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/TestConstants.java b/components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/TestConstants.java index b07c1e8dd2..f9aca075ab 100644 --- a/components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/TestConstants.java +++ b/components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/TestConstants.java @@ -1,7 +1,7 @@ /* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2017-2025, WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, + * 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 @@ -19,6 +19,7 @@ package org.wso2.carbon.identity.oauth2; public class TestConstants { + public static final String CARBON_TENANT_DOMAIN = "carbon.super"; public static final String LOACALHOST_DOMAIN = "localhost"; public static final String OAUTH2_TOKEN_EP = "https://localhost:9443/oauth2/token"; @@ -32,6 +33,8 @@ public class TestConstants { public static final String CLAIM_URI2 = "http://wso2.org/claimuri2"; public static final String CLAIM_VALUE1 = "ClaimValue1"; public static final String CLAIM_VALUE2 = "ClaimValue2"; + public static final String MANAGED_ORG_CLAIM_URI = "http://wso2.org/claims/identity/managedOrg"; + public static final String SAMPLE_ID = "76dedfb5-99ef-4bf3-92e6-56d296db55ec"; public static final String USERSTORE_DOMAIN = "user_store_domain"; public static final String NEW_ACCESS_TOKEN = "123456789";