Minor changes to the base test class

revert-70aa11f8
Rasika Perera 7 years ago
parent 030d7cd26d
commit e51e344463

@ -22,7 +22,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.PoolProperties; import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeSuite;
import org.testng.internal.collections.Pair; import org.testng.internal.collections.Pair;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -36,7 +35,6 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.policy.mgt.Profile;
import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -48,7 +46,6 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl;
import org.wso2.carbon.ntask.common.TaskException;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig;
@ -100,7 +97,7 @@ public abstract class BasePolicyManagementDAOTest {
DeviceConfigurationManager.getInstance().initConfig(); DeviceConfigurationManager.getInstance().initConfig();
} }
protected void initializeServices() throws Exception{ protected void initializeServices() throws Exception {
initDatSource(); initDatSource();
initSQLScript(); initSQLScript();
@ -155,7 +152,7 @@ public abstract class BasePolicyManagementDAOTest {
} }
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants
.SUPER_TENANT_DOMAIN_NAME); .SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
} }
@ -260,20 +257,21 @@ public abstract class BasePolicyManagementDAOTest {
} }
public interface Command { public interface Command {
void call(Profile profile) throws Exception; void call(Object paramObj) throws Exception;
} }
protected void testThrowingException(Profile profile, Command command, String fieldName, Object mockObj, protected void testThrowingException(Object targetObj, Object paramObj, Command command, String fieldName,
Class<?> exceptionClass) throws Exception { Object mockObj,
Object oldObj = changeFieldValue(profileManager, fieldName, mockObj); Class<?> exceptionClass) throws Exception {
Object oldObj = changeFieldValue(targetObj, fieldName, mockObj);
try { try {
command.call(profile); command.call(paramObj);
} catch (Exception e) { } catch (Exception e) {
if (!(e.getCause().getClass().getName().equals(exceptionClass.getName()))) { if (!(e.getCause().getClass().getName().equals(exceptionClass.getName()))) {
throw e; throw e;
} }
} finally { } finally {
changeFieldValue(profileManager, fieldName, oldObj); changeFieldValue(targetObj, fieldName, oldObj);
} }
} }

@ -13,14 +13,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.Profile;
import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature;
import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.ProfileManagementException; import org.wso2.carbon.policy.mgt.common.ProfileManagementException;
import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest; import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest;
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
@ -30,9 +23,7 @@ import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO; import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO;
import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException;
import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mock.TypeXDeviceManagementService; import org.wso2.carbon.policy.mgt.core.mock.TypeXDeviceManagementService;
import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest;
import org.wso2.carbon.policy.mgt.core.util.FeatureCreator; import org.wso2.carbon.policy.mgt.core.util.FeatureCreator;
import org.wso2.carbon.policy.mgt.core.util.ProfileCreator; import org.wso2.carbon.policy.mgt.core.util.ProfileCreator;
@ -89,7 +80,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
when(profileDAO.addProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException()); when(profileDAO.addProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException());
//Creating profile object //Creating profile object
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C); Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C);
testThrowingException(profile, p -> profileManager.addProfile(p), "profileDAO", profileDAO, testThrowingException(profileManager, profile, p -> profileManager.addProfile((Profile) p), "profileDAO",
profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -101,7 +93,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
new FeatureManagerDAOException()); new FeatureManagerDAOException());
//Creating profile object //Creating profile object
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C); Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C);
testThrowingException(profile, p -> profileManager.addProfile(p), "featureDAO", featureDAO, testThrowingException(profileManager, profile, p -> profileManager.addProfile((Profile) p), "featureDAO",
featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }
@ -139,7 +132,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
String newProfileName = "Updated Test Profile"; String newProfileName = "Updated Test Profile";
Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
savedProfile.setProfileName(newProfileName); savedProfile.setProfileName(newProfileName);
testThrowingException(savedProfile, p -> profileManager.updateProfile(p), "profileDAO", profileDAO, testThrowingException(profileManager, savedProfile, p -> profileManager.updateProfile((Profile) p),
"profileDAO", profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -153,7 +147,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
String newProfileName = "Updated Test Profile"; String newProfileName = "Updated Test Profile";
Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
savedProfile.setProfileName(newProfileName); savedProfile.setProfileName(newProfileName);
testThrowingException(savedProfile, p -> profileManager.updateProfile(p), "featureDAO", featureDAO, testThrowingException(profileManager, savedProfile, p -> profileManager.updateProfile((Profile) p),
"featureDAO", featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }
@ -196,7 +191,9 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetProfileThrowingProfileManagerDAOException() throws Exception { public void testGetProfileThrowingProfileManagerDAOException() throws Exception {
ProfileDAO profileDAO = mock(ProfileDAOImpl.class); ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException()); when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException());
testThrowingException(profile1, p -> profileManager.getProfile(p.getProfileId()), "profileDAO", profileDAO, testThrowingException(profileManager, profile1, p -> profileManager.getProfile(((Profile) p).getProfileId()),
"profileDAO",
profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -205,7 +202,9 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetProfileThrowingFeatureManagerDAOException() throws Exception { public void testGetProfileThrowingFeatureManagerDAOException() throws Exception {
FeatureDAO featureDAO = mock(FeatureDAO.class); FeatureDAO featureDAO = mock(FeatureDAO.class);
when(featureDAO.getFeaturesForProfile(anyInt())).thenThrow(new FeatureManagerDAOException()); when(featureDAO.getFeaturesForProfile(anyInt())).thenThrow(new FeatureManagerDAOException());
testThrowingException(profile1, p -> profileManager.getProfile(p.getProfileId()), "featureDAO", featureDAO, testThrowingException(profileManager, profile1, p -> profileManager.getProfile(((Profile) p).getProfileId()),
"featureDAO",
featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }
@ -235,7 +234,7 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetAllProfilesThrowingProfileManagerDAOException() throws Exception { public void testGetAllProfilesThrowingProfileManagerDAOException() throws Exception {
ProfileDAO profileDAO = mock(ProfileDAOImpl.class); ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
when(profileDAO.getAllProfiles()).thenThrow(new ProfileManagerDAOException()); when(profileDAO.getAllProfiles()).thenThrow(new ProfileManagerDAOException());
testThrowingException(profile1, p -> profileManager.getAllProfiles(), "profileDAO", profileDAO, testThrowingException(profileManager, profile1, p -> profileManager.getAllProfiles(), "profileDAO", profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -244,7 +243,7 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetAllProfilesThrowingFeatureManagerDAOException() throws Exception { public void testGetAllProfilesThrowingFeatureManagerDAOException() throws Exception {
FeatureDAO featureDAO = mock(FeatureDAO.class); FeatureDAO featureDAO = mock(FeatureDAO.class);
when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException()); when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException());
testThrowingException(profile1, p -> profileManager.getAllProfiles(), "featureDAO", featureDAO, testThrowingException(profileManager, profile1, p -> profileManager.getAllProfiles(), "featureDAO", featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }
@ -275,7 +274,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException() throws Exception { public void testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException() throws Exception {
ProfileDAO profileDAO = mock(ProfileDAOImpl.class); ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
when(profileDAO.getProfilesOfDeviceType(anyString())).thenThrow(new ProfileManagerDAOException()); when(profileDAO.getProfilesOfDeviceType(anyString())).thenThrow(new ProfileManagerDAOException());
testThrowingException(profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), "profileDAO", testThrowingException(profileManager, profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C),
"profileDAO",
profileDAO, profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -286,7 +286,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException() throws Exception { public void testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException() throws Exception {
FeatureDAO featureDAO = mock(FeatureDAO.class); FeatureDAO featureDAO = mock(FeatureDAO.class);
when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException()); when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException());
testThrowingException(profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), "featureDAO", testThrowingException(profileManager, profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C),
"featureDAO",
featureDAO, featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }
@ -311,7 +312,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testDeleteProfileThrowingProfileManagerDAOException() throws Exception { public void testDeleteProfileThrowingProfileManagerDAOException() throws Exception {
ProfileDAO profileDAO = mock(ProfileDAOImpl.class); ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
when(profileDAO.deleteProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException()); when(profileDAO.deleteProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException());
testThrowingException(profile1, p -> profileManager.deleteProfile(profile1), "profileDAO", profileDAO, testThrowingException(profileManager, profile1, p -> profileManager.deleteProfile(profile1), "profileDAO",
profileDAO,
ProfileManagerDAOException.class); ProfileManagerDAOException.class);
} }
@ -320,7 +322,8 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
public void testDeleteProfileThrowingFeatureManagerDAOException() throws Exception { public void testDeleteProfileThrowingFeatureManagerDAOException() throws Exception {
FeatureDAO featureDAO = mock(FeatureDAO.class); FeatureDAO featureDAO = mock(FeatureDAO.class);
when(featureDAO.deleteFeaturesOfProfile(any(Profile.class))).thenThrow(new FeatureManagerDAOException()); when(featureDAO.deleteFeaturesOfProfile(any(Profile.class))).thenThrow(new FeatureManagerDAOException());
testThrowingException(profile1, p -> profileManager.deleteProfile(profile1), "featureDAO", featureDAO, testThrowingException(profileManager, profile1, p -> profileManager.deleteProfile(profile1), "featureDAO",
featureDAO,
FeatureManagerDAOException.class); FeatureManagerDAOException.class);
} }

Loading…
Cancel
Save