Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Nov 25, 2024
1 parent ca69b24 commit e3f0170
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -119,7 +119,7 @@ public class IdentityProviderManagementServiceTest {
private MockedStatic<CryptoUtil> 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;
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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");


Expand Down Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;

/**
Expand All @@ -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<String, BasicDataSource> 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<String, BasicDataSource> dataSourceMap = new HashMap<>();
private CacheBackedIdPMgtDAO cacheBackedIdPMgtDAO;
private CacheBackedIdPMgtDAO cacheBackedIdPMgtDAOForException;
private IdPManagementDAO idPManagementDAO;

private IdPManagementDAO idPManagementDAOForException;
private ActionManagementService actionManagementService;
MockedStatic<IdentityTenantUtil> identityTenantUtil;
MockedStatic<IdpMgtServiceComponentHolder> idpMgtServiceComponentHolder;

Expand Down Expand Up @@ -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 {

Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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}
};
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
};
Expand Down Expand Up @@ -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},
};
}

Expand Down Expand Up @@ -1184,8 +1223,12 @@ public void testDeleteIdPsDAOException(int tenantId) throws Exception {
identityDatabaseUtil.when(IdentityDatabaseUtil::getDBConnection).thenReturn(connection);
identityDatabaseUtil.when(IdentityDatabaseUtil::getDataSource).thenReturn(dataSourceMap.get(DB_NAME));
addTestIdps();

doThrow(IdentityProviderManagementException.class).when(idPManagementDAOForException).deleteIdPs(anyInt());

// Deleting multiple IDPs on a tenant.
cacheBackedIdPMgtDAO.deleteIdPs(tenantId);
assertThrows(IdentityProviderManagementException.class, () ->
cacheBackedIdPMgtDAOForException.deleteIdPs(tenantId));
}
}

Expand Down Expand Up @@ -1244,12 +1287,36 @@ 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> 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(1)).addAction(anyString(), any(), anyString());
}
}

@DataProvider
public Object[][] addIdPCacheData() {

Expand Down Expand Up @@ -1604,6 +1671,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 {
Expand Down
Loading

0 comments on commit e3f0170

Please sign in to comment.