From ea2b2b16d59515a135cb4648c6b87ce1eba22f17 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Wed, 11 Dec 2024 17:29:47 +0530 Subject: [PATCH] Improve unit tests. --- .../ApplicationAuthenticatorServiceTest.java | 67 +------ .../AuthenticatorManagementDAOImplTest.java | 165 ++++++++++++++++++ .../model/test/util/ActionMgtTestUtil.java | 55 ++---- ...UserDefinedLocalAuthenticatorDataUtil.java | 130 ++++++++++++++ .../src/test/resources/testng.xml | 1 + 5 files changed, 314 insertions(+), 104 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/dao/AuthenticatorManagementDAOImplTest.java create mode 100644 components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/util/UserDefinedLocalAuthenticatorDataUtil.java diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/ApplicationAuthenticatorServiceTest.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/ApplicationAuthenticatorServiceTest.java index 2aa81ce8a8db..b78a9911f760 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/ApplicationAuthenticatorServiceTest.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/ApplicationAuthenticatorServiceTest.java @@ -18,7 +18,6 @@ package org.wso2.carbon.identity.application.common.model.test; -import org.mockito.MockedStatic; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; @@ -26,7 +25,6 @@ import org.testng.annotations.Test; 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.action.management.service.ActionManagementService; import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService; @@ -34,8 +32,6 @@ import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtServerRuntimeException; import org.wso2.carbon.identity.application.common.internal.ApplicationCommonServiceDataHolder; import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig; -import org.wso2.carbon.identity.application.common.model.Property; -import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder; import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.test.util.ActionMgtTestUtil; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.AuthenticationType; @@ -46,19 +42,19 @@ import org.wso2.carbon.identity.common.testng.WithRealmService; import org.wso2.carbon.identity.common.testng.WithRegistry; import org.wso2.carbon.identity.core.internal.IdentityCoreServiceDataHolder; -import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.wso2.carbon.identity.application.common.model.test.util.ActionMgtTestUtil.mockActionService; +import static org.wso2.carbon.identity.application.common.model.test.util.UserDefinedLocalAuthenticatorDataUtil.createSystemDefinedAuthenticatorConfig; +import static org.wso2.carbon.identity.application.common.model.test.util.UserDefinedLocalAuthenticatorDataUtil.createUserDefinedAuthenticatorConfig; /** * This class is a test suite for the ApplicationAuthenticatorServiceTest class. @@ -72,7 +68,6 @@ @WithRealmService(injectToSingletons = {IdentityCoreServiceDataHolder.class}) public class ApplicationAuthenticatorServiceTest { - private MockedStatic identityDatabaseUtil; private String tenantDomain; private UserDefinedLocalAuthenticatorConfig authenticatorConfig1; @@ -110,12 +105,7 @@ public void setUpClass() throws Exception { endpointConfigToBeUpdated = ActionMgtTestUtil.createEndpointConfig( "http://localhost1", "admin1", "admin1"); action = ActionMgtTestUtil.createAction(endpointConfig); - 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()); + actionManagementService = mockActionService(action); ApplicationCommonServiceDataHolder.getInstance().setApplicationAuthenticatorService( ApplicationAuthenticatorService.getInstance()); @@ -359,53 +349,4 @@ public void testDeleteUserDefinedLocalAuthenticatorWithNonExistingAuthenticator( ApplicationCommonServiceDataHolder.getInstance().getApplicationAuthenticatorService() .deleteUserDefinedLocalAuthenticator(nonExistAuthenticatorConfig.getName(), tenantDomain); } - - private UserDefinedLocalAuthenticatorConfig createUserDefinedAuthenticatorConfig(String uniqueIdentifier, - AuthenticationType type) { - - UserDefinedLocalAuthenticatorConfig authenticatorConfig = new - UserDefinedLocalAuthenticatorConfig(AuthenticationType.IDENTIFICATION); - authenticatorConfig.setName(uniqueIdentifier); - authenticatorConfig.setDisplayName("Custom " + uniqueIdentifier); - authenticatorConfig.setEnabled(true); - authenticatorConfig.setDefinedByType(DefinedByType.USER); - authenticatorConfig.setAuthenticationType(type); - UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder = buildAuthenticatorEndpointConfig(); - authenticatorConfig.setEndpointConfig(endpointConfigBuilder.build()); - - return authenticatorConfig; - } - - private LocalAuthenticatorConfig createSystemDefinedAuthenticatorConfig(String uniqueIdentifier) { - - LocalAuthenticatorConfig authenticatorConfig = new LocalAuthenticatorConfig(); - authenticatorConfig.setName(uniqueIdentifier); - authenticatorConfig.setDisplayName("Custom " + uniqueIdentifier); - authenticatorConfig.setEnabled(true); - authenticatorConfig.setDefinedByType(DefinedByType.SYSTEM); - Property prop1 = new Property(); - prop1.setName("PropertyName1_" + uniqueIdentifier); - prop1.setValue("PropertyValue1_" + uniqueIdentifier); - prop1.setConfidential(false); - Property prop2 = new Property(); - prop2.setName("PropertyName2_" + uniqueIdentifier); - prop2.setValue("PropertyValue2_" + uniqueIdentifier); - prop2.setConfidential(true); - authenticatorConfig.setProperties(new Property[]{prop1, prop2}); - - return authenticatorConfig; - } - - private static UserDefinedAuthenticatorEndpointConfigBuilder buildAuthenticatorEndpointConfig() { - - UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder = - new UserDefinedAuthenticatorEndpointConfigBuilder(); - endpointConfigBuilder.uri("https://localhost:8080/test"); - endpointConfigBuilder.authenticationType(Authentication.Type.BASIC.getName()); - HashMap authProperties = new HashMap<>(); - authProperties.put("username", "admin"); - authProperties.put("password", "admin"); - endpointConfigBuilder.authenticationProperties(authProperties); - return endpointConfigBuilder; - } } diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/dao/AuthenticatorManagementDAOImplTest.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/dao/AuthenticatorManagementDAOImplTest.java new file mode 100644 index 000000000000..d9da3005e490 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/dao/AuthenticatorManagementDAOImplTest.java @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.common.model.test.dao; + +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.wso2.carbon.identity.application.common.dao.impl.AuthenticatorManagementDAOImpl; +import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException; +import org.wso2.carbon.identity.application.common.model.Property; +import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; +import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants; +import org.wso2.carbon.identity.common.testng.WithCarbonHome; +import org.wso2.carbon.identity.common.testng.WithH2Database; +import org.wso2.carbon.identity.common.testng.WithRealmService; +import org.wso2.carbon.identity.core.internal.IdentityCoreServiceDataHolder; + +import static org.junit.Assert.assertThrows; +import static org.wso2.carbon.identity.application.common.model.test.util.UserDefinedLocalAuthenticatorDataUtil.createUserDefinedAuthenticatorConfig; +import static org.wso2.carbon.identity.application.common.model.test.util.UserDefinedLocalAuthenticatorDataUtil.createUserDefinedAuthenticatorConfigForSQLException; +import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError.ERROR_WHILE_ADDING_AUTHENTICATOR; + +@WithH2Database(files = {"dbscripts/h2.sql"}) +@WithCarbonHome +@WithRealmService(injectToSingletons = {IdentityCoreServiceDataHolder.class}) +public class AuthenticatorManagementDAOImplTest { + + private final int tenantId = -1234; + + private UserDefinedLocalAuthenticatorConfig authenticatorConfig1; + private UserDefinedLocalAuthenticatorConfig authenticatorConfig2; + private UserDefinedLocalAuthenticatorConfig authenticatorConfigForException; + private UserDefinedLocalAuthenticatorConfig nonExistAuthenticatorConfig; + + private static final String AUTHENTICATOR1_NAME = "auth1"; + private static final String AUTHENTICATOR2_NAME = "auth2"; + private static final String AUTHENTICATOR_CONFIG_FOR_EXCEPTION_NAME = "exception_auth"; + private static final String NON_EXIST_AUTHENTICATOR_NAME = "non_exist_auth"; + + private final AuthenticatorManagementDAOImpl authenticatorManagementDAO = new AuthenticatorManagementDAOImpl(); + + @BeforeClass + public void setUpClass() { + + authenticatorConfig1 = createUserDefinedAuthenticatorConfig(AUTHENTICATOR1_NAME, + AuthenticatorPropertyConstants.AuthenticationType.IDENTIFICATION); + authenticatorConfig2 = createUserDefinedAuthenticatorConfig(AUTHENTICATOR2_NAME, + AuthenticatorPropertyConstants.AuthenticationType.VERIFICATION); + nonExistAuthenticatorConfig = createUserDefinedAuthenticatorConfig(NON_EXIST_AUTHENTICATOR_NAME, + AuthenticatorPropertyConstants.AuthenticationType.IDENTIFICATION); + authenticatorConfigForException = createUserDefinedAuthenticatorConfigForSQLException( + AUTHENTICATOR_CONFIG_FOR_EXCEPTION_NAME, + AuthenticatorPropertyConstants.AuthenticationType.IDENTIFICATION); + + } + + @DataProvider(name = "authenticatorConfig") + public Object[][] authenticatorConfig() { + + return new Object[][]{ + {authenticatorConfig1}, + {authenticatorConfig2} + }; + } + + @Test(dataProvider = "authenticatorConfig") + public void testAddUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorConfig config) + throws AuthenticatorMgtException { + + UserDefinedLocalAuthenticatorConfig createdAuthenticator = authenticatorManagementDAO + .addUserDefinedLocalAuthenticator(config, tenantId); + Assert.assertNotNull(createdAuthenticator); + } + + @Test + public void testAddUserDefinedLocalAuthenticatorWithSQLException() { + + AuthenticatorMgtException exception = assertThrows(AuthenticatorMgtException.class, () -> + authenticatorManagementDAO.addUserDefinedLocalAuthenticator(authenticatorConfigForException, tenantId)); + Assert.assertEquals(exception.getErrorCode(), ERROR_WHILE_ADDING_AUTHENTICATOR.getCode()); + } + + @Test + public void testAddUserDefinedLocalAuthenticatorWithOutActionProperty() { + + authenticatorConfigForException.setProperties(new Property[0]); + AuthenticatorMgtException exception = assertThrows(AuthenticatorMgtException.class, () -> + authenticatorManagementDAO.addUserDefinedLocalAuthenticator(authenticatorConfigForException, tenantId)); + Assert.assertEquals(exception.getErrorCode(), ERROR_WHILE_ADDING_AUTHENTICATOR.getCode()); + } + + /* + @Test + public void testUpdateUserDefinedLocalAuthenticator() throws AuthenticatorMgtException { + + authenticatorManagementDAO.updateUserDefinedLocalAuthenticator(authenticatorConfig1, tenantId); + } + + @Test + public void testUpdateUserDefinedLocalAuthenticatorForException() throws AuthenticatorMgtException { + + authenticatorManagementDAO.updateUserDefinedLocalAuthenticator(authenticatorConfigForException, tenantId); + } + + @Test + public void testDeleteUserDefinedLocalAuthenticator() throws AuthenticatorMgtException { + + authenticatorManagementDAO.deleteUserDefinedLocalAuthenticator(AUTHENTICATOR1_NAME, tenantId); + } + + @Test + public void testDeleteUserDefinedLocalAuthenticatorForException() throws AuthenticatorMgtException { + + authenticatorManagementDAO.deleteUserDefinedLocalAuthenticator(AUTHENTICATOR_CONFIG_FOR_EXCEPTION_NAME, + tenantId); + } + + @Test(dataProvider = "authenticatorConfig") + public void testDeleteUserDefinedLocalAuthenticatorForException(UserDefinedLocalAuthenticatorConfig config) + throws AuthenticatorMgtException { + + authenticatorManagementDAO.deleteUserDefinedLocalAuthenticator(config.getName(), config, tenantId); + } + */ + + @Test(dataProvider = "authenticatorConfig") + public void testGetUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorConfig config) + throws AuthenticatorMgtException { + + UserDefinedLocalAuthenticatorConfig retrievedConfig = authenticatorManagementDAO + .getUserDefinedLocalAuthenticator(config.getName(), tenantId); + Assert.assertNotNull(retrievedConfig); + Assert.assertEquals(retrievedConfig.getName(), config.getName()); + } + + @Test + public void testGetUserDefinedLocalAuthenticatorForException() throws AuthenticatorMgtException { + + authenticatorManagementDAO.getUserDefinedLocalAuthenticator(AUTHENTICATOR_CONFIG_FOR_EXCEPTION_NAME, tenantId); + } + + @Test + public void testGetUserDefinedLocalAuthenticatorForNonExist() throws AuthenticatorMgtException { + + Assert.assertNull(authenticatorManagementDAO.getUserDefinedLocalAuthenticator( + NON_EXIST_AUTHENTICATOR_NAME, tenantId)); + } +} diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/util/ActionMgtTestUtil.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/util/ActionMgtTestUtil.java index 108bfe4e0ce2..810cfd73a976 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/util/ActionMgtTestUtil.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/util/ActionMgtTestUtil.java @@ -18,17 +18,17 @@ package org.wso2.carbon.identity.application.common.model.test.util; +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.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.action.management.service.ActionManagementService; -import java.util.HashMap; -import java.util.Map; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ActionMgtTestUtil { @@ -55,42 +55,15 @@ public static EndpointConfig createEndpointConfig(String uri, String username, S return endpointConfigBuilder.build(); } - public static IdentityProvider createIdPWithUserDefinedFederatedAuthenticatorConfig(String idpName, - EndpointConfig endpointConfig) { + public static ActionManagementService mockActionService(Action action) throws ActionMgtException { - // Initialize Test Identity Provider 4 with custom user defined federated authenticator. - IdentityProvider newUserDefinedIdp = new IdentityProvider(); - newUserDefinedIdp.setIdentityProviderName(idpName); + ActionManagementService actionManagementService = mock(ActionManagementService.class); - 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) { + 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()); - 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(); + return actionManagementService; } } diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/util/UserDefinedLocalAuthenticatorDataUtil.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/util/UserDefinedLocalAuthenticatorDataUtil.java new file mode 100644 index 000000000000..033c68decd13 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/java/org/wso2/carbon/identity/application/common/model/test/util/UserDefinedLocalAuthenticatorDataUtil.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.common.model.test.util; + +import org.wso2.carbon.identity.action.management.model.Authentication; +import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig; +import org.wso2.carbon.identity.application.common.model.Property; +import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder; +import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; +import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants; + +import java.util.HashMap; + +public class UserDefinedLocalAuthenticatorDataUtil { + + /** + * Create a user defined authenticator configuration. + * + * @param uniqueIdentifier Unique identifier for the authenticator. + * @param type Authentication type. + * @return UserDefinedLocalAuthenticatorConfig + */ + public static UserDefinedLocalAuthenticatorConfig createUserDefinedAuthenticatorConfig(String uniqueIdentifier, + AuthenticatorPropertyConstants.AuthenticationType type) { + + UserDefinedLocalAuthenticatorConfig authenticatorConfig = new + UserDefinedLocalAuthenticatorConfig(AuthenticatorPropertyConstants.AuthenticationType.IDENTIFICATION); + authenticatorConfig.setName(uniqueIdentifier); + authenticatorConfig.setDisplayName("Custom " + uniqueIdentifier); + authenticatorConfig.setEnabled(true); + authenticatorConfig.setDefinedByType(AuthenticatorPropertyConstants.DefinedByType.USER); + authenticatorConfig.setAuthenticationType(type); + authenticatorConfig.setProperties(buildAuthenticatorProperties()); + UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder = buildAuthenticatorEndpointConfig(); + authenticatorConfig.setEndpointConfig(endpointConfigBuilder.build()); + + return authenticatorConfig; + } + + /** + * Create a system defined authenticator configuration. + * + * @param uniqueIdentifier Unique identifier for the authenticator. + * @return LocalAuthenticatorConfig + */ + public static LocalAuthenticatorConfig createSystemDefinedAuthenticatorConfig(String uniqueIdentifier) { + + LocalAuthenticatorConfig authenticatorConfig = new LocalAuthenticatorConfig(); + authenticatorConfig.setName(uniqueIdentifier); + authenticatorConfig.setDisplayName("Custom " + uniqueIdentifier); + authenticatorConfig.setEnabled(true); + authenticatorConfig.setDefinedByType(AuthenticatorPropertyConstants.DefinedByType.SYSTEM); + Property prop1 = new Property(); + prop1.setName("PropertyName1_" + uniqueIdentifier); + prop1.setValue("PropertyValue1_" + uniqueIdentifier); + prop1.setConfidential(false); + Property prop2 = new Property(); + prop2.setName("PropertyName2_" + uniqueIdentifier); + prop2.setValue("PropertyValue2_" + uniqueIdentifier); + prop2.setConfidential(true); + authenticatorConfig.setProperties(new Property[]{prop1, prop2}); + + return authenticatorConfig; + } + + /** + * Create a user defined authenticator configuration for an SQL exception. + * + * @param uniqueIdentifier Unique identifier for the authenticator. + * @param type Authentication type. + * @return UserDefinedLocalAuthenticatorConfig + */ + public static UserDefinedLocalAuthenticatorConfig createUserDefinedAuthenticatorConfigForSQLException( + String uniqueIdentifier, AuthenticatorPropertyConstants.AuthenticationType type) { + + UserDefinedLocalAuthenticatorConfig authenticatorConfigForException = + createUserDefinedAuthenticatorConfig(uniqueIdentifier, type); + authenticatorConfigForException.setName("Authenticator name with 254 characters".repeat(50)); + + return authenticatorConfigForException; + } + + /** + * Build the endpoint configuration for the user defined authenticator. + * + * @return UserDefinedAuthenticatorEndpointConfigBuilder + */ + public static UserDefinedAuthenticatorEndpointConfigBuilder buildAuthenticatorEndpointConfig() { + + UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder = + new UserDefinedAuthenticatorEndpointConfigBuilder(); + endpointConfigBuilder.uri("https://localhost:8080/test"); + endpointConfigBuilder.authenticationType(Authentication.Type.BASIC.getName()); + HashMap authProperties = new HashMap<>(); + authProperties.put("username", "admin"); + authProperties.put("password", "admin"); + endpointConfigBuilder.authenticationProperties(authProperties); + return endpointConfigBuilder; + } + + /** + * Build the properties for the user defined authenticator. + * + * @return Property[] + */ + public static Property[] buildAuthenticatorProperties() { + + Property property = new Property(); + property.setName("actionId"); + property.setValue("actionId"); + + return new Property[]{property}; + } +} diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/resources/testng.xml b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/resources/testng.xml index 9512040a62d7..0556d823350c 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/resources/testng.xml +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/test/resources/testng.xml @@ -23,6 +23,7 @@ +