Skip to content

Commit

Permalink
Improve unit test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Nov 23, 2024
1 parent 6899f28 commit ec3d693
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ public int getCountOfFilteredIdPs(int tenantId, List<ExpressionNode> expressionC
return dao.getCountOfFilteredIdPs(tenantId, expressionConditions);
}

public ProvisioningConnectorConfig[] getProvisioningConnectorConfigs(Connection dbConnection,
String idPName, int idPId, int tenantId)
throws IdentityProviderManagementException, SQLException {

return dao.getProvisioningConnectorConfigs(dbConnection, idPName, idPId, tenantId);
}

public IdentityProvider getIdPByName(Connection dbConnection, String idPName, int tenantId, String tenantDomain)
throws IdentityProviderManagementException {

Expand Down Expand Up @@ -268,12 +261,6 @@ public void renameTenantRole(String newRoleName, String oldRoleName, int tenantI
dao.renameTenantRole(newRoleName, oldRoleName, tenantId, tenantDomain);
}

public void renameClaimURI(String newClaimURI, String oldClaimURI, int tenantId, String tenantDomain)
throws IdentityProviderManagementException {

dao.renameClaimURI(newClaimURI, oldClaimURI, tenantId, tenantDomain);
}

public boolean isIdPAvailableForAuthenticatorProperty(String authenticatorName, String propertyName,
String idPEntityId, int tenantId)
throws IdentityProviderManagementException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,25 @@ public void testForceDeleteIdP(String idpName) throws Exception {
Assert.assertNull(identityProviderManagementService.getIdPByName(idpName));
}

@Test(dataProvider = "forceDeleteIdPData")
public void testForceDeleteIdPDAOException(String idpName) throws Exception {

addTestIdps();
Assert.assertNotNull(identityProviderManagementService.getIdPByName(idpName));

IdPManagementDAO daoForError = mock(IdPManagementDAO.class);
doThrow(IdentityProviderManagementServerException.class).when(daoForError)
.forceDeleteIdPByResourceId(anyString(), anyInt(), anyString());
daoForException = new CacheBackedIdPMgtDAO(daoForError);
field.set(identityProviderManager, daoForException);

assertThrows(IdentityProviderManagementException.class, () ->
identityProviderManagementService.forceDeleteIdP(idpName));

field.set(identityProviderManager, dao);
Assert.assertNotNull(identityProviderManagementService.getIdPByName(idpName));
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.wso2.carbon.identity.application.common.model.ProvisioningConnectorConfig;
import org.wso2.carbon.identity.application.common.model.RoleMapping;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.core.model.ExpressionNode;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
Expand Down Expand Up @@ -68,26 +69,21 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

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.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.*;
import static org.wso2.carbon.idp.mgt.util.IdPManagementConstants.RESET_PROVISIONING_ENTITIES_ON_CONFIG_UPDATE;

/**
* Unit tests for CacheBackedIdPManagementDAO.
*/
@WithCarbonHome
public class CacheBackedIdPMgtDAOTest {

private static final String DB_NAME = "test";
Expand Down Expand Up @@ -469,6 +465,29 @@ public void testGetIdPById(String idpName, int idpId, int tenantId, boolean isEx
}
}

@Test
public void testGetIdPNamesById() throws Exception {

try (MockedStatic<IdentityDatabaseUtil> identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class);
Connection connection = getConnection(DB_NAME)) {
identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection);
identityDatabaseUtil.when(IdentityDatabaseUtil::getDataSource).thenReturn(dataSourceMap.get(DB_NAME));
addTestIdps();

Set<String> idpIds = new HashSet<>(Arrays.asList(
cacheBackedIdPMgtDAO.getIdPByName(connection, "testIdP1", SAMPLE_TENANT_ID1, TENANT_DOMAIN)
.getId(),
cacheBackedIdPMgtDAO.getIdPByName(connection, "testIdP2", SAMPLE_TENANT_ID1, TENANT_DOMAIN)
.getId()
));
// Retrieving IDP form DB and adding to cache.
Map<String, String> idpNameMap = cacheBackedIdPMgtDAO.getIdPNamesById(SAMPLE_TENANT_ID1, idpIds);

assertTrue(idpNameMap.containsValue("testIdP1"));
assertTrue(idpNameMap.containsValue("testIdP2"));
}
}

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

Expand Down Expand Up @@ -558,6 +577,31 @@ public void testGetIdPNameByResourceId(String idpName, int tenantId) throws Exce
}
}

@Test(dataProvider = "getIDPNameByResourceIdData")
public void testGetIdPNameByResourceIdFromDB(String idpName, int tenantId) 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();

try (MockedStatic<CarbonContext> carbonContext = mockStatic(CarbonContext.class)) {
CarbonContext mockCarbonContext = mock(CarbonContext.class);
carbonContext.when(CarbonContext::getThreadLocalCarbonContext).thenReturn(mockCarbonContext);
when(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()).thenReturn(TENANT_DOMAIN);

// Retrieving IDP from DB.
IdentityProvider idPResult = idPManagementDAO.getIdPByName(connection, idpName, tenantId, TENANT_DOMAIN);
String uuid = idPResult.getResourceId();

String nameFromDB = cacheBackedIdPMgtDAO.getIdPNameByResourceId(uuid);
assertEquals(nameFromDB, idpName);
}
}
}

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

Expand Down Expand Up @@ -1113,7 +1157,7 @@ public void testDeleteIdPs(int tenantId) throws Exception {
identityDatabaseUtil.when(IdentityDatabaseUtil::getDataSource).thenReturn(dataSourceMap.get(DB_NAME));
addTestIdps();
// Deleting multiple IDPs on a tenant.
idPManagementDAO.deleteIdPs(tenantId);
cacheBackedIdPMgtDAO.deleteIdPs(tenantId);
}

try (Connection connection = getConnection(DB_NAME)) {
Expand All @@ -1131,6 +1175,20 @@ public void testDeleteIdPs(int tenantId) throws Exception {
}
}

@Test(dataProvider = "deleteIdPsData")
public void testDeleteIdPsDAOException(int tenantId) 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();
// Deleting multiple IDPs on a tenant.
cacheBackedIdPMgtDAO.deleteIdPs(tenantId);
}
}

@Test(dataProvider = "deleteIdPData")
public void testDeleteIdPByResourceId(String idpName, int tenantId) throws Exception {

Expand Down Expand Up @@ -1173,6 +1231,25 @@ public void testForceDeleteIdPByResourceId(String idpName, int tenantId) throws
}
}

@Test(dataProvider = "deleteIdPData")
public void testForceDeleteIdP(String idpName, int tenantId) 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();

// Force delete IDP using resourceId.
cacheBackedIdPMgtDAO.forceDeleteIdP(idpName, tenantId, TENANT_DOMAIN);
int resultSize = getIdPCount(connection, idpName, tenantId);
assertEquals(resultSize, 0, "'forceDeleteIdPByResourceId' method fails");
IdentityProvider idpFromCache = idpFromCacheByName(idpName);
assertNull(idpFromCache, "'deleteIdPByResourceId' method fails");
}
}

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

Expand Down

0 comments on commit ec3d693

Please sign in to comment.