Skip to content

Commit

Permalink
Improve unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Dec 11, 2024
1 parent 1191c5b commit ea2b2b1
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@

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;
import org.testng.annotations.DataProvider;
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;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
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;
Expand All @@ -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.
Expand All @@ -72,7 +68,6 @@
@WithRealmService(injectToSingletons = {IdentityCoreServiceDataHolder.class})
public class ApplicationAuthenticatorServiceTest {

private MockedStatic<IdentityDatabaseUtil> identityDatabaseUtil;
private String tenantDomain;

private UserDefinedLocalAuthenticatorConfig authenticatorConfig1;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<String, String> authProperties = new HashMap<>();
authProperties.put("username", "admin");
authProperties.put("password", "admin");
endpointConfigBuilder.authenticationProperties(authProperties);
return endpointConfigBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<String, String> propMap = new HashMap<>();
endpointConfig.getAuthentication().getProperties()
.forEach(prop -> propMap.put(prop.getName(), prop.getValue()));
endpointConfigBuilder.authenticationProperties(propMap);
return endpointConfigBuilder.build();
return actionManagementService;
}
}
Loading

0 comments on commit ea2b2b1

Please sign in to comment.