From 50903c066ec22f77405e3f2e1946edacf9d9de4b Mon Sep 17 00:00:00 2001 From: Jasintha Date: Sat, 14 Oct 2017 12:29:52 +0530 Subject: [PATCH] Coverting Exception paths of methods --- .../core/PolicyManagerServiceImplTest.java | 115 ++++++++++++------ 1 file changed, 81 insertions(+), 34 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java index a218429863..38500f2712 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java @@ -23,13 +23,8 @@ import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.*; 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.GroupManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; @@ -41,17 +36,10 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; -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.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; -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.GroupManagementProviderService; -import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; @@ -59,23 +47,15 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask; 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.services.SimplePolicyEvaluationTest; import org.wso2.carbon.policy.mgt.core.task.MonitoringTask; import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; -import org.wso2.carbon.registry.core.config.RegistryContext; -import org.wso2.carbon.registry.core.exceptions.RegistryException; -import org.wso2.carbon.registry.core.internal.RegistryDataHolder; -import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService; -import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.user.core.service.RealmService; - -import java.io.InputStream; + import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; -public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { +public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class); @@ -86,7 +66,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { private static final String POLICY1_FEATURE1_CODE = "DISALLOW_ADJUST_VOLUME"; private static final String POLICY1_CAM_FEATURE1_CODE = "DISALLOW_OPEN_CAM"; private static final String ADMIN_USER = "admin"; - public static final String DEVICE_2 = "device2"; + public static final String DEVICE_WITHOUT_POLICY = "device2"; public static final String DEVICE_TYPE_B = "deviceTypeB"; private OperationManager operationManager; @@ -167,12 +147,63 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = "addPolicy") - public void activatePolicy() throws PolicyManagementException { + public void activatePolicy() throws Exception { policyManagerService.getPAP().activatePolicy(policy1.getId()); Policy effectivePolicy = policyManagerService.getEffectivePolicy(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); Assert.assertEquals(effectivePolicy.getPolicyName(), POLICY1, POLICY1 + " was not activated for " + DEVICE1); } + @Test(description = "Get active policy but there is no EvaluationPoint define for device yet should be return PolicyManagement exception" + + " caused by PolicyEvaluationException",dependsOnMethods = "addPolicy",expectedExceptions = PolicyManagementException.class) + public void getActivePolicyForDeviceWithNoEvaluationEPDefine() throws Exception { + PolicyEvaluationPoint policyEvaluationPoint = null; + try{ + policyEvaluationPoint = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(); + PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint("Simple", null); + policyManagerService.getEffectivePolicy(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); + }catch (Exception e){ + Assert.assertTrue(e.getCause() instanceof PolicyEvaluationException); + throw e; + }finally { + PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint("Simple", policyEvaluationPoint); + } + } + + + @Test(description = "Get active policy but no any policy applied for device yet should be return PolicyManagement exception" + + " caused by PolicyEvaluationException",dependsOnMethods = "addPolicy",expectedExceptions = PolicyManagementException.class) + public void getActivePolicyForDeviceWithNoAppliedPolicy() throws Exception { + try{ + policyManagerService.getEffectivePolicy(new DeviceIdentifier(DEVICE_WITHOUT_POLICY, DEVICE_TYPE_B)); + }catch (Exception e){ + Assert.assertTrue(e.getCause() instanceof PolicyEvaluationException); + throw e; + } + } + + @Test(description = "Get active policy if enrollment status removed should be return PolicyManagement exception" + + " caused by InvalidDeviceException",dependsOnMethods = "addPolicy",expectedExceptions = PolicyManagementException.class) + public void getActivatePolicyForEnrolmentStatusAsRemoved() throws Exception { + EnrolmentInfo.Status status =null; + try{ + Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + getDevice(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), false); + status = device.getEnrolmentInfo().getStatus(); + device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); + policyManagerService.getEffectivePolicy(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); + }catch (Exception e){ + Assert.assertTrue(e.getCause() instanceof InvalidDeviceException); + throw e; + }finally { + if(status != null){ + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + getDevice(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), false).getEnrolmentInfo().setStatus(status); + } + } + } + + + @Test(dependsOnMethods = "activatePolicy") public void applyPolicy() throws PolicyManagementException, OperationManagementException { new DelegationTask().execute(); @@ -282,19 +313,35 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = "applyPolicy") - public void getEffectiveFeatures( ) throws Exception { + public void getEffectiveFeatures() throws Exception { List effectiveFeatures = policyManagerService. getEffectiveFeatures(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); Assert.assertNotNull(effectiveFeatures); - Assert.assertEquals(POLICY1_FEATURE1_CODE,effectiveFeatures.get(0).getFeatureCode()); + Assert.assertEquals(POLICY1_FEATURE1_CODE, effectiveFeatures.get(0).getFeatureCode()); + } + + @Test(description = "Get active policy but there is no EvaluationPoint define for device yet should be return FeatureManagement exception" + + " caused by PolicyEvaluationException",dependsOnMethods = "addPolicy",expectedExceptions = FeatureManagementException.class) + public void getEffectiveFeaturesForDeviceWithNoEvaluationEPDefine() throws Exception { + PolicyEvaluationPoint policyEvaluationPoint = null; try{ - policyManagerService.getEffectiveFeatures(new DeviceIdentifier(DEVICE_2, DEVICE_TYPE_B)); - }catch(FeatureManagementException ex){ - if(ex.getCause() instanceof PolicyEvaluationException){ - Assert.assertTrue(ex.getCause() instanceof PolicyEvaluationException); - }else { - throw ex; - } + policyEvaluationPoint = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(); + PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint("Simple", null); + policyManagerService.getEffectiveFeatures(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); + } finally { + PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint("Simple", policyEvaluationPoint); + } + } + + + @Test(description = "Get effective features but no any policy applied for device yet should be return FeatureManagement exception" + + " caused by PolicyEvaluationException",dependsOnMethods = "addPolicy",expectedExceptions = FeatureManagementException.class) + public void getEffectiveFeaturesForDeviceWithNoAppliedPolicy() throws Exception { + try{ + policyManagerService.getEffectiveFeatures(new DeviceIdentifier(DEVICE_WITHOUT_POLICY, DEVICE_TYPE_B)); + }catch (Exception e){ + Assert.assertTrue(e.getCause() instanceof PolicyEvaluationException); + throw e; } }