From 4d242f45c090fd03b10050745df201eb77cb2027 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Mon, 25 Nov 2024 11:08:19 +0530 Subject: [PATCH] Add unit tests --- ...IdentityProviderManagementServiceTest.java | 69 ++------- .../idp/mgt/dao/CacheBackedIdPMgtDAOTest.java | 141 +++++++++++++++--- .../idp/mgt/dao/IdPManagementDAOTest.java | 63 +------- .../idp/mgt/util/ActionMgtTestUtil.java | 80 ++++++++++ 4 files changed, 223 insertions(+), 130 deletions(-) create mode 100644 components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/util/ActionMgtTestUtil.java diff --git a/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/IdentityProviderManagementServiceTest.java b/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/IdentityProviderManagementServiceTest.java index 02954b83ebb6..f519750cf51c 100644 --- a/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/IdentityProviderManagementServiceTest.java +++ b/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/IdentityProviderManagementServiceTest.java @@ -30,7 +30,6 @@ import org.wso2.carbon.identity.action.management.ActionManagementService; import org.wso2.carbon.identity.action.management.exception.ActionMgtException; import org.wso2.carbon.identity.action.management.model.Action; -import org.wso2.carbon.identity.action.management.model.Authentication; import org.wso2.carbon.identity.action.management.model.EndpointConfig; import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService; import org.wso2.carbon.identity.application.common.ProvisioningConnectorService; @@ -64,6 +63,7 @@ import org.wso2.carbon.idp.mgt.dao.CacheBackedIdPMgtDAO; import org.wso2.carbon.idp.mgt.dao.IdPManagementDAO; import org.wso2.carbon.idp.mgt.internal.IdpMgtServiceComponentHolder; +import org.wso2.carbon.idp.mgt.util.ActionMgtTestUtil; import org.wso2.carbon.idp.mgt.util.IdPManagementConstants; import org.wso2.carbon.idp.mgt.util.IdPManagementConstants.ErrorMessage; import org.wso2.carbon.idp.mgt.util.MetadataConverter; @@ -119,7 +119,7 @@ public class IdentityProviderManagementServiceTest { private MockedStatic cryptoUtil; private ActionManagementService actionManagementService; - private static final String ASSOCIATED_ACTION_ID = "Dummp_Action_ID"; + private static final String ASSOCIATED_ACTION_ID = "Dummy_Action_ID"; private static final String CUSTOM_IDP_NAME = "customIdP"; private static Action action; private static EndpointConfig endpointConfig; @@ -150,11 +150,13 @@ public void setUpClass() throws Exception { registerSystemAuthenticators(); - endpointConfig = createEndpointConfig("http://localhost", "admin", "admin"); - endpointConfigToBeUpdated = createEndpointConfig("http://localhost1", "admin1", "admin1"); - action = createAction(endpointConfig); - userDefinedIdP = createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME, action.getEndpoint()); - idpForErrorScenarios = createIdPWithUserDefinedFederatedAuthenticatorConfig( + endpointConfig = ActionMgtTestUtil.createEndpointConfig("http://localhost", "admin", "admin"); + endpointConfigToBeUpdated = ActionMgtTestUtil.createEndpointConfig( + "http://localhost1", "admin1", "admin1"); + action = ActionMgtTestUtil.createAction(endpointConfig); + userDefinedIdP = ActionMgtTestUtil.createIdPWithUserDefinedFederatedAuthenticatorConfig( + CUSTOM_IDP_NAME, action.getEndpoint()); + idpForErrorScenarios = ActionMgtTestUtil.createIdPWithUserDefinedFederatedAuthenticatorConfig( CUSTOM_IDP_NAME + "Error", action.getEndpoint()); } @@ -733,7 +735,8 @@ public void testForceDeleteIdPException(String idpName) throws Exception { @Test public void testUpdateIdPActionException() throws Exception { - IdentityProvider idpForErrorScenariosTobeUpdate = createIdPWithUserDefinedFederatedAuthenticatorConfig( + IdentityProvider idpForErrorScenariosTobeUpdate = ActionMgtTestUtil. + createIdPWithUserDefinedFederatedAuthenticatorConfig( idpForErrorScenarios.getIdentityProviderName(), endpointConfig); identityProviderManagementService.addIdP(idpForErrorScenarios); @@ -819,10 +822,10 @@ public Object[][] updateIdPData() { IdentityProvider idp3New = new IdentityProvider(); idp3New.setIdentityProviderName("testIdP3New"); - IdentityProvider updateIdPWithExistingUserDefinedFedAuth = createIdPWithUserDefinedFederatedAuthenticatorConfig( - CUSTOM_IDP_NAME + "new", endpointConfigToBeUpdated); - IdentityProvider updateIdPWithNewUserDefinedFedAuth = createIdPWithUserDefinedFederatedAuthenticatorConfig( - CUSTOM_IDP_NAME + "new", endpointConfigToBeUpdated); + IdentityProvider updateIdPWithExistingUserDefinedFedAuth = ActionMgtTestUtil. + createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME + "new", endpointConfigToBeUpdated); + IdentityProvider updateIdPWithNewUserDefinedFedAuth = ActionMgtTestUtil. + createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME + "new", endpointConfigToBeUpdated); updateIdPWithNewUserDefinedFedAuth.getFederatedAuthenticatorConfigs()[0].setName("New Fed Auth"); @@ -1442,48 +1445,6 @@ private FederatedAuthenticatorConfig federatedAuthenticatorConfigWithIdpEntityId return federatedAuthenticatorConfig; } - private Action createAction(EndpointConfig endpointConfig) { - - Action.ActionResponseBuilder actionResponseBuilder = new Action.ActionResponseBuilder(); - actionResponseBuilder.id(ASSOCIATED_ACTION_ID); - actionResponseBuilder.name("SampleAssociatedAction"); - actionResponseBuilder.type(Action.ActionTypes.AUTHENTICATION); - actionResponseBuilder.description("SampleDescription"); - actionResponseBuilder.status(Action.Status.ACTIVE); - actionResponseBuilder.endpoint(endpointConfig); - return actionResponseBuilder.build(); - } - - private EndpointConfig createEndpointConfig(String uri, String username, String password) { - - EndpointConfig.EndpointConfigBuilder endpointConfigBuilder = new EndpointConfig.EndpointConfigBuilder(); - endpointConfigBuilder.uri(uri); - endpointConfigBuilder.authentication( - new Authentication.BasicAuthBuilder(username, password).build()); - return endpointConfigBuilder.build(); - } - - private IdentityProvider createIdPWithUserDefinedFederatedAuthenticatorConfig(String idpName, - EndpointConfig endpointConfig) { - - // Initialize Test Identity Provider 4 with custom user defined federated authenticator. - IdentityProvider newUserDefinedIdp = new IdentityProvider(); - newUserDefinedIdp.setIdentityProviderName(idpName); - - UserDefinedFederatedAuthenticatorConfig userDefinedFederatedAuthenticatorConfig = new - UserDefinedFederatedAuthenticatorConfig(); - userDefinedFederatedAuthenticatorConfig.setDisplayName("DisplayName1"); - userDefinedFederatedAuthenticatorConfig.setName("customFedAuthenticator"); - userDefinedFederatedAuthenticatorConfig.setEnabled(true); - userDefinedFederatedAuthenticatorConfig.setEndpointConfig( - buildUserDefinedAuthenticatorEndpointConfig(endpointConfig)); - userDefinedFederatedAuthenticatorConfig.setDefinedByType(DefinedByType.USER); - userDefinedFederatedAuthenticatorConfig.setProperties(new Property[]{}); - newUserDefinedIdp.setFederatedAuthenticatorConfigs( - new FederatedAuthenticatorConfig[]{userDefinedFederatedAuthenticatorConfig}); - return newUserDefinedIdp; - } - private UserDefinedAuthenticatorEndpointConfig buildUserDefinedAuthenticatorEndpointConfig( EndpointConfig endpointConfig) { diff --git a/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/dao/CacheBackedIdPMgtDAOTest.java b/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/dao/CacheBackedIdPMgtDAOTest.java index d0af641f4622..825f06870ce4 100644 --- a/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/dao/CacheBackedIdPMgtDAOTest.java +++ b/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/dao/CacheBackedIdPMgtDAOTest.java @@ -22,11 +22,15 @@ import org.apache.commons.lang.StringUtils; import org.mockito.MockedStatic; import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.identity.action.management.ActionManagementService; +import org.wso2.carbon.identity.action.management.model.Action; +import org.wso2.carbon.identity.action.management.model.EndpointConfig; import org.wso2.carbon.identity.application.common.model.Claim; import org.wso2.carbon.identity.application.common.model.ClaimConfig; import org.wso2.carbon.identity.application.common.model.ClaimMapping; @@ -45,8 +49,8 @@ import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; -import org.wso2.carbon.identity.secret.mgt.core.SecretManager; -import org.wso2.carbon.identity.secret.mgt.core.SecretResolveManager; +import org.wso2.carbon.identity.secret.mgt.core.SecretManagerImpl; +import org.wso2.carbon.identity.secret.mgt.core.model.SecretType; import org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException; import org.wso2.carbon.idp.mgt.IdentityProviderManagementException; import org.wso2.carbon.idp.mgt.cache.IdPCacheByHRI; @@ -60,6 +64,7 @@ import org.wso2.carbon.idp.mgt.cache.IdPResourceIdCacheKey; import org.wso2.carbon.idp.mgt.internal.IdpMgtServiceComponentHolder; import org.wso2.carbon.idp.mgt.model.ConnectedAppsResult; +import org.wso2.carbon.idp.mgt.util.ActionMgtTestUtil; import org.wso2.carbon.idp.mgt.util.IdPManagementConstants; import org.wso2.carbon.idp.mgt.util.IdPSecretsProcessor; @@ -74,10 +79,12 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.when; -import static org.testng.Assert.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.*; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertThrows; +import static org.testng.Assert.assertTrue; import static org.wso2.carbon.idp.mgt.util.IdPManagementConstants.RESET_PROVISIONING_ENTITIES_ON_CONFIG_UPDATE; /** @@ -87,22 +94,25 @@ public class CacheBackedIdPMgtDAOTest { private static final String DB_NAME = "test"; - private static final Integer SAMPLE_TENANT_ID1 = -1234; - private static final Integer SAMPLE_TENANT_ID2 = 1; - private static final Integer SAMPLE_TENANT_ID3 = 2; - private static final Integer NOT_EXISTING_TENANT_ID = 4; - private static final String TENANT_DOMAIN = "carbon.super"; - private static Map dataSourceMap = new HashMap<>(); + private static final String CUSTOM_IDP_NAME = "customIdP"; + private static Action action; + private static EndpointConfig endpointConfig; + private static EndpointConfig endpointConfigToBeUpdated; + private IdentityProvider idpForErrorScenarios; + private IdentityProvider userDefinedIdP; + private static Map dataSourceMap = new HashMap<>(); private CacheBackedIdPMgtDAO cacheBackedIdPMgtDAO; + private CacheBackedIdPMgtDAO cacheBackedIdPMgtDAOForException; private IdPManagementDAO idPManagementDAO; - + private IdPManagementDAO idPManagementDAOForException; + private ActionManagementService actionManagementService; MockedStatic identityTenantUtil; MockedStatic idpMgtServiceComponentHolder; @@ -144,6 +154,32 @@ private static Connection getConnection(String database) throws SQLException { throw new RuntimeException("No datasource initiated for database: " + database); } + @BeforeClass + public void setUpClass() throws Exception { + + SecretManagerImpl secretManager = mock(SecretManagerImpl.class); + SecretType secretType = mock(SecretType.class); + IdpMgtServiceComponentHolder.getInstance().setSecretManager(secretManager); + when(secretType.getId()).thenReturn("secretId"); + doReturn(secretType).when(secretManager).getSecretType(any()); + when(secretManager.isSecretExist(anyString(), anyString())).thenReturn(false); + + endpointConfig = ActionMgtTestUtil.createEndpointConfig("http://localhost", "admin", "admin"); + endpointConfigToBeUpdated = ActionMgtTestUtil.createEndpointConfig( + "http://localhost1", "admin1", "admin1"); + action = ActionMgtTestUtil.createAction(endpointConfig); + userDefinedIdP = ActionMgtTestUtil.createIdPWithUserDefinedFederatedAuthenticatorConfig( + CUSTOM_IDP_NAME, action.getEndpoint()); + idpForErrorScenarios = ActionMgtTestUtil.createIdPWithUserDefinedFederatedAuthenticatorConfig( + CUSTOM_IDP_NAME + "Error", action.getEndpoint()); + + actionManagementService = mock(ActionManagementService.class); + when(actionManagementService.addAction(anyString(), any(), any())).thenReturn(action); + when(actionManagementService.updateAction(anyString(), any(), any(), any())).thenReturn(action); + when(actionManagementService.getActionByActionId(anyString(), any(), any())).thenReturn(action); + doNothing().when(actionManagementService).deleteAction(anyString(), any(), any()); + } + @BeforeMethod public void setup() throws Exception { @@ -153,6 +189,8 @@ public void setup() throws Exception { when(idpSecretsProcessor.encryptAssociatedSecrets(any())).thenAnswer( invocation -> invocation.getArguments()[0]); idPManagementDAO = new IdPManagementDAO(); + idPManagementDAOForException = mock(IdPManagementDAO.class); + cacheBackedIdPMgtDAOForException = new CacheBackedIdPMgtDAO(idPManagementDAOForException); Field idpSecretsProcessorField = IdPManagementDAO.class.getDeclaredField("idpSecretsProcessorService"); idpSecretsProcessorField.setAccessible(true); @@ -167,6 +205,7 @@ public void setup() throws Exception { IdpMgtServiceComponentHolder mockIdpMgtServiceComponentHolder = mock(IdpMgtServiceComponentHolder.class); idpMgtServiceComponentHolder.when( IdpMgtServiceComponentHolder::getInstance).thenReturn(mockIdpMgtServiceComponentHolder); + when(mockIdpMgtServiceComponentHolder.getActionManagementService()).thenReturn(actionManagementService); } @AfterMethod @@ -192,7 +231,7 @@ public void tearDown() throws Exception { public Object[][] getIdPsData() { return new Object[][]{ - {SAMPLE_TENANT_ID1, 2}, + {SAMPLE_TENANT_ID1, 3}, {SAMPLE_TENANT_ID2, 1}, {NOT_EXISTING_TENANT_ID, 0} }; @@ -238,7 +277,7 @@ public Object[][] getIdPsSearchData() { return new Object[][]{ {SAMPLE_TENANT_ID1, "testIdP1", 1}, {SAMPLE_TENANT_ID2, "testIdP3", 1}, - {SAMPLE_TENANT_ID1, "", 2}, + {SAMPLE_TENANT_ID1, "", 3}, {SAMPLE_TENANT_ID1, "test*", 2}, {SAMPLE_TENANT_ID1, "????IdP*", 2}, {SAMPLE_TENANT_ID1, "tes_I*", 2}, @@ -352,7 +391,7 @@ public Object[][] getTotalIdPCountData() { expressionNodesList2.add(expressionNode2); return new Object[][]{ - {SAMPLE_TENANT_ID1, expressionNodesList1, 2}, + {SAMPLE_TENANT_ID1, expressionNodesList1, 3}, {SAMPLE_TENANT_ID1, expressionNodesList2, 2}, {SAMPLE_TENANT_ID2, expressionNodesList1, 1}, }; @@ -434,7 +473,7 @@ public Object[][] getIdPByIdData() { return new Object[][]{ {"testIdP1", 1, SAMPLE_TENANT_ID1, true}, {"testIdP3", 3, SAMPLE_TENANT_ID2, true}, - {"notExist", 4, SAMPLE_TENANT_ID1, false}, + {"notExist", 99, SAMPLE_TENANT_ID1, false}, }; } @@ -1175,8 +1214,8 @@ public void testDeleteIdPs(int tenantId) throws Exception { } } - @Test(dataProvider = "deleteIdPsData") - public void testDeleteIdPsDAOException(int tenantId) throws Exception { + @Test + public void testDeleteIdPsDAOException() throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); Connection connection = getConnection(DB_NAME)) { @@ -1184,8 +1223,16 @@ public void testDeleteIdPsDAOException(int tenantId) throws Exception { identityDatabaseUtil.when(IdentityDatabaseUtil::getDBConnection).thenReturn(connection); identityDatabaseUtil.when(IdentityDatabaseUtil::getDataSource).thenReturn(dataSourceMap.get(DB_NAME)); addTestIdps(); + + List idpList = idPManagementDAO.getIdPs(connection, SAMPLE_TENANT_ID1, TENANT_DOMAIN); + when(idPManagementDAOForException.getIdPs(any(), anyInt(), anyString())) + .thenReturn(idpList); + doThrow(IdentityProviderManagementException.class).when(idPManagementDAOForException).deleteIdPs(anyInt()); + // Deleting multiple IDPs on a tenant. - cacheBackedIdPMgtDAO.deleteIdPs(tenantId); + assertThrows(IdentityProviderManagementException.class, () -> + cacheBackedIdPMgtDAOForException.deleteIdPs(SAMPLE_TENANT_ID1)); + verify(actionManagementService, times(1)).addAction(anyString(), any(), anyString()); } } @@ -1244,12 +1291,60 @@ public void testForceDeleteIdP(String idpName, int tenantId) throws Exception { // Force delete IDP using resourceId. cacheBackedIdPMgtDAO.forceDeleteIdP(idpName, tenantId, TENANT_DOMAIN); int resultSize = getIdPCount(connection, idpName, tenantId); - assertEquals(resultSize, 0, "'forceDeleteIdPByResourceId' method fails"); + assertEquals(resultSize, 0, "'forceDeleteIdP' method fails"); IdentityProvider idpFromCache = idpFromCacheByName(idpName); assertNull(idpFromCache, "'deleteIdPByResourceId' method fails"); } } + @Test + public void testForceDeleteIdPDAOException() throws Exception { + + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + Connection connection = getConnection(DB_NAME)) { + identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection); + identityDatabaseUtil.when(IdentityDatabaseUtil::getDBConnection).thenReturn(connection); + identityDatabaseUtil.when(IdentityDatabaseUtil::getDataSource).thenReturn(dataSourceMap.get(DB_NAME)); + addTestIdps(); + + doThrow(IdentityProviderManagementException.class).when(idPManagementDAOForException).forceDeleteIdP( + anyString(), anyInt(), anyString()); + when(idPManagementDAOForException.getIdPByName(any(), anyString(), anyInt(), anyString())) + .thenReturn(userDefinedIdP); + + // Deleting multiple IDPs on a tenant. + assertThrows(IdentityProviderManagementException.class, () -> + cacheBackedIdPMgtDAOForException.forceDeleteIdP( + userDefinedIdP.getIdentityProviderName(), SAMPLE_TENANT_ID1, TENANT_DOMAIN)); + + verify(actionManagementService, times(2)).addAction(anyString(), any(), anyString()); + } + } + + @Test + public void testDeleteIdPDAOException() throws Exception { + + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + Connection connection = getConnection(DB_NAME)) { + identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection); + identityDatabaseUtil.when(IdentityDatabaseUtil::getDBConnection).thenReturn(connection); + identityDatabaseUtil.when(IdentityDatabaseUtil::getDataSource).thenReturn(dataSourceMap.get(DB_NAME)); + addTestIdps(); + + doThrow(IdentityProviderManagementException.class).when(idPManagementDAOForException).deleteIdP( + anyString(), anyInt(), anyString()); + when(idPManagementDAOForException.getIdPByName(any(), anyString(), anyInt(), anyString())) + .thenReturn(userDefinedIdP); + + // Deleting multiple IDPs on a tenant. + assertThrows(IdentityProviderManagementException.class, () -> + cacheBackedIdPMgtDAOForException.deleteIdP( + userDefinedIdP.getIdentityProviderName(), SAMPLE_TENANT_ID1, TENANT_DOMAIN)); + + verify(actionManagementService, times(1)).addAction(anyString(), any(), anyString()); + } + } + @DataProvider public Object[][] addIdPCacheData() { @@ -1604,6 +1699,10 @@ private void addTestIdps() throws IdentityProviderManagementException { idPManagementDAO.addIdP(idp2, SAMPLE_TENANT_ID1); // IDP with Only name. idPManagementDAO.addIdP(idp3, SAMPLE_TENANT_ID2); + // IDP with user defined federated authenticators. + idPManagementDAO.addIdP(userDefinedIdP, SAMPLE_TENANT_ID1); + userDefinedIdP = idPManagementDAO.getIdPByName(null, userDefinedIdP.getIdentityProviderName(), + SAMPLE_TENANT_ID1, TENANT_DOMAIN); } private void addTestIdps(Connection connection) throws IdentityProviderManagementException { diff --git a/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/dao/IdPManagementDAOTest.java b/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/dao/IdPManagementDAOTest.java index 07656accef06..d26215fcd0e2 100644 --- a/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/dao/IdPManagementDAOTest.java +++ b/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/dao/IdPManagementDAOTest.java @@ -59,6 +59,7 @@ import org.wso2.carbon.idp.mgt.IdentityProviderManagementServerException; import org.wso2.carbon.idp.mgt.internal.IdpMgtServiceComponentHolder; import org.wso2.carbon.idp.mgt.model.ConnectedAppsResult; +import org.wso2.carbon.idp.mgt.util.ActionMgtTestUtil; import org.wso2.carbon.idp.mgt.util.IdPManagementConstants; import org.wso2.carbon.idp.mgt.util.IdPSecretsProcessor; @@ -106,7 +107,7 @@ public class IdPManagementDAOTest { private static final String IDP_GROUP2_ID = "idpGroup2Id"; private static Map dataSourceMap = new HashMap<>(); - private static final String ASSOCIATED_ACTION_ID = "Dummp_Action_ID"; + private static final String ASSOCIATED_ACTION_ID = "Dummy_Action_ID"; private static final String CUSTOM_IDP_NAME = "customIdP"; private static EndpointConfig endpointConfig; private static EndpointConfig endpointConfigToBeUpdated; @@ -174,10 +175,10 @@ public void setUp() throws Exception { when(mockCryptoUtil.encryptAndBase64Encode(any())).thenReturn("ENCRYPTED_VALUE2"); when(mockCryptoUtil.base64DecodeAndDecrypt(anyString())).thenReturn("ENCRYPTED_VALUE2".getBytes()); - endpointConfig = createEndpointConfig("http://localhost", "admin", "admin"); - endpointConfigToBeUpdated = createEndpointConfig("http://localhost1", "admin1", "admin1"); - userDefinedIdP = createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME, endpointConfig); - idpForErrorScenarios = createIdPWithUserDefinedFederatedAuthenticatorConfig( + endpointConfig = ActionMgtTestUtil.createEndpointConfig("http://localhost", "admin", "admin"); + endpointConfigToBeUpdated = ActionMgtTestUtil.createEndpointConfig("http://localhost1", "admin1", "admin1"); + userDefinedIdP = ActionMgtTestUtil.createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME, endpointConfig); + idpForErrorScenarios = ActionMgtTestUtil.createIdPWithUserDefinedFederatedAuthenticatorConfig( CUSTOM_IDP_NAME + "Error", endpointConfig); } @@ -1311,8 +1312,8 @@ public Object[][] updateIdPData() { IdentityProvider idp3New = new IdentityProvider(); idp3New.setIdentityProviderName("testIdP3New"); - IdentityProvider userDefinedIdPToBeUpdated = createIdPWithUserDefinedFederatedAuthenticatorConfig( - CUSTOM_IDP_NAME + "new", createEndpointConfig("http://localhostnew1", "adminnew1", "adminnew1")); + IdentityProvider userDefinedIdPToBeUpdated = ActionMgtTestUtil + .createIdPWithUserDefinedFederatedAuthenticatorConfig(CUSTOM_IDP_NAME + "new", ActionMgtTestUtil.createEndpointConfig("http://localhostnew1", "adminnew1", "adminnew1")); return new Object[][]{ // Update PermissionsAndRoleConfig,FederatedAuthenticatorConfig,ProvisioningConnectorConfig,ClaimConfig. @@ -2081,54 +2082,6 @@ private int getIdPCount(Connection connection, String idpName, int tenantId) thr return resultSize; } - private EndpointConfig createEndpointConfig(String uri, String username, String password) { - - EndpointConfig.EndpointConfigBuilder endpointConfigBuilder = new EndpointConfig.EndpointConfigBuilder(); - endpointConfigBuilder.uri(uri); - endpointConfigBuilder.authentication( - new Authentication.BasicAuthBuilder(username, password).build()); - return endpointConfigBuilder.build(); - } - - private IdentityProvider createIdPWithUserDefinedFederatedAuthenticatorConfig(String idpName, - EndpointConfig endpointConfig) { - - // Initialize Test Identity Provider 4 with custom user defined federated authenticator. - IdentityProvider newUserDefinedIdp = new IdentityProvider(); - newUserDefinedIdp.setIdentityProviderName(idpName); - - UserDefinedFederatedAuthenticatorConfig userDefinedFederatedAuthenticatorConfig = new - UserDefinedFederatedAuthenticatorConfig(); - userDefinedFederatedAuthenticatorConfig.setDisplayName("DisplayName1"); - userDefinedFederatedAuthenticatorConfig.setName("customFedAuthenticator"); - userDefinedFederatedAuthenticatorConfig.setEnabled(true); - userDefinedFederatedAuthenticatorConfig.setEndpointConfig( - buildUserDefinedAuthenticatorEndpointConfig(endpointConfig)); - Property property = new Property(); - property.setName("actionId"); - property.setValue(ASSOCIATED_ACTION_ID); - property.setConfidential(false); - userDefinedFederatedAuthenticatorConfig.setProperties(new Property[]{property}); - newUserDefinedIdp.setFederatedAuthenticatorConfigs( - new FederatedAuthenticatorConfig[]{userDefinedFederatedAuthenticatorConfig}); - newUserDefinedIdp.setDefaultAuthenticatorConfig(userDefinedFederatedAuthenticatorConfig); - return newUserDefinedIdp; - } - - private UserDefinedAuthenticatorEndpointConfig buildUserDefinedAuthenticatorEndpointConfig( - EndpointConfig endpointConfig) { - - UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder = - new UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder(); - endpointConfigBuilder.uri(endpointConfig.getUri()); - endpointConfigBuilder.authenticationType(endpointConfig.getAuthentication().getType().getName()); - Map propMap = new HashMap<>(); - endpointConfig.getAuthentication().getProperties() - .forEach(prop -> propMap.put(prop.getName(), prop.getValue())); - endpointConfigBuilder.authenticationProperties(propMap); - return endpointConfigBuilder.build(); - } - private void assertIdPResult(IdentityProvider idpResult, String idpName, boolean isExist) { if (isExist) { diff --git a/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/util/ActionMgtTestUtil.java b/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/util/ActionMgtTestUtil.java new file mode 100644 index 000000000000..3b2e88f26f49 --- /dev/null +++ b/components/idp-mgt/org.wso2.carbon.idp.mgt/src/test/java/org/wso2/carbon/idp/mgt/util/ActionMgtTestUtil.java @@ -0,0 +1,80 @@ +package org.wso2.carbon.idp.mgt.util; + +import org.testng.Assert; +import org.wso2.carbon.identity.action.management.model.Action; +import org.wso2.carbon.identity.action.management.model.Authentication; +import org.wso2.carbon.identity.action.management.model.EndpointConfig; +import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig; +import org.wso2.carbon.identity.application.common.model.IdentityProvider; +import org.wso2.carbon.identity.application.common.model.Property; +import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig; +import org.wso2.carbon.identity.application.common.model.UserDefinedFederatedAuthenticatorConfig; +import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants; + +import java.util.HashMap; +import java.util.Map; + +public class ActionMgtTestUtil { + + public static final String ASSOCIATED_ACTION_ID = "Dummy_Action_ID"; + + public static Action createAction(EndpointConfig endpointConfig) { + + Action.ActionResponseBuilder actionResponseBuilder = new Action.ActionResponseBuilder(); + actionResponseBuilder.id(ASSOCIATED_ACTION_ID); + actionResponseBuilder.name("SampleAssociatedAction"); + actionResponseBuilder.type(Action.ActionTypes.AUTHENTICATION); + actionResponseBuilder.description("SampleDescription"); + actionResponseBuilder.status(Action.Status.ACTIVE); + actionResponseBuilder.endpoint(endpointConfig); + return actionResponseBuilder.build(); + } + + public static EndpointConfig createEndpointConfig(String uri, String username, String password) { + + EndpointConfig.EndpointConfigBuilder endpointConfigBuilder = new EndpointConfig.EndpointConfigBuilder(); + endpointConfigBuilder.uri(uri); + endpointConfigBuilder.authentication( + new Authentication.BasicAuthBuilder(username, password).build()); + return endpointConfigBuilder.build(); + } + + public static IdentityProvider createIdPWithUserDefinedFederatedAuthenticatorConfig(String idpName, + EndpointConfig endpointConfig) { + + // Initialize Test Identity Provider 4 with custom user defined federated authenticator. + IdentityProvider newUserDefinedIdp = new IdentityProvider(); + newUserDefinedIdp.setIdentityProviderName(idpName); + + UserDefinedFederatedAuthenticatorConfig userDefinedFederatedAuthenticatorConfig = new + UserDefinedFederatedAuthenticatorConfig(); + userDefinedFederatedAuthenticatorConfig.setDisplayName("DisplayName1"); + userDefinedFederatedAuthenticatorConfig.setName("customFedAuthenticator"); + userDefinedFederatedAuthenticatorConfig.setEnabled(true); + userDefinedFederatedAuthenticatorConfig.setEndpointConfig( + buildUserDefinedAuthenticatorEndpointConfig(endpointConfig)); + Property property = new Property(); + property.setName("actionId"); + property.setValue(ASSOCIATED_ACTION_ID); + property.setConfidential(false); + userDefinedFederatedAuthenticatorConfig.setProperties(new Property[]{property}); + newUserDefinedIdp.setFederatedAuthenticatorConfigs( + new FederatedAuthenticatorConfig[]{userDefinedFederatedAuthenticatorConfig}); + newUserDefinedIdp.setDefaultAuthenticatorConfig(userDefinedFederatedAuthenticatorConfig); + return newUserDefinedIdp; + } + + public static UserDefinedAuthenticatorEndpointConfig buildUserDefinedAuthenticatorEndpointConfig( + EndpointConfig endpointConfig) { + + UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder = + new UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder(); + endpointConfigBuilder.uri(endpointConfig.getUri()); + endpointConfigBuilder.authenticationType(endpointConfig.getAuthentication().getType().getName()); + Map propMap = new HashMap<>(); + endpointConfig.getAuthentication().getProperties() + .forEach(prop -> propMap.put(prop.getName(), prop.getValue())); + endpointConfigBuilder.authenticationProperties(propMap); + return endpointConfigBuilder.build(); + } +}