From 50903c066ec22f77405e3f2e1946edacf9d9de4b Mon Sep 17 00:00:00 2001 From: Jasintha Date: Sat, 14 Oct 2017 12:29:52 +0530 Subject: [PATCH 1/3] 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; } } From 28480452886ac2d29b73e7967f464357b709792a Mon Sep 17 00:00:00 2001 From: Jasintha Date: Sat, 14 Oct 2017 12:36:11 +0530 Subject: [PATCH 2/3] Coverting Exception paths of methods --- .../core/mgt/impl/PolicyManagerImplTest.java | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImplTest.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImplTest.java new file mode 100644 index 0000000000..b3a5475dfe --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImplTest.java @@ -0,0 +1,146 @@ +package org.wso2.carbon.policy.mgt.core.mgt.impl; + +import org.testng.annotations.Test; + +import static org.testng.Assert.*; + +/** + * Created by jasintha on 10/11/17. + */ +public class PolicyManagerImplTest { + + @Test + public void testAddPolicy() throws Exception { + + } + + @Test + public void testUpdatePolicy() throws Exception { + + } + + @Test + public void testUpdatePolicyPriorities() throws Exception { + + } + + @Test + public void testDeletePolicy() throws Exception { + + } + + @Test + public void testDeletePolicy1() throws Exception { + + } + + @Test + public void testActivatePolicy() throws Exception { + + } + + @Test + public void testInactivatePolicy() throws Exception { + + } + + @Test + public void testAddPolicyToDevice() throws Exception { + + } + + @Test + public void testAddPolicyToRole() throws Exception { + + } + + @Test + public void testAddPolicyToUser() throws Exception { + + } + + @Test + public void testGetPolicyByProfileID() throws Exception { + + } + + @Test + public void testGetPolicy() throws Exception { + + } + + @Test + public void testGetPolicies() throws Exception { + + } + + @Test + public void testGetPoliciesOfDevice() throws Exception { + + } + + @Test + public void testGetPoliciesOfDeviceType() throws Exception { + + } + + @Test + public void testGetPoliciesOfRole() throws Exception { + + } + + @Test + public void testGetPoliciesOfUser() throws Exception { + + } + + @Test + public void testGetPolicyAppliedDevicesIds() throws Exception { + + } + + @Test + public void testAddAppliedPolicyFeaturesToDevice() throws Exception { + + } + + @Test + public void testApplyChangesMadeToPolicies() throws Exception { + + } + + @Test + public void testAddAppliedPolicyToDevice() throws Exception { + + } + + @Test + public void testRemoveAppliedPolicyToDevice() throws Exception { + + } + + @Test + public void testCheckPolicyAvailable() throws Exception { + + } + + @Test + public void testSetPolicyApplied() throws Exception { + + } + + @Test + public void testGetPolicyCount() throws Exception { + + } + + @Test + public void testGetAppliedPolicyToDevice() throws Exception { + + } + + @Test + public void testGetAppliedPolicyIdsDeviceIds() throws Exception { + + } +} \ No newline at end of file From 5cc58b51e85e7846cde9cae303ad8e87884535ca Mon Sep 17 00:00:00 2001 From: Jasintha Date: Sat, 14 Oct 2017 12:41:55 +0530 Subject: [PATCH 3/3] refactoring --- .../core/PolicyManagerServiceImplTest.java | 5 - .../core/mgt/impl/PolicyManagerImplTest.java | 146 ------------------ 2 files changed, 151 deletions(-) delete mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImplTest.java 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 38500f2712..bf24d0e536 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 @@ -50,7 +50,6 @@ import org.wso2.carbon.policy.mgt.core.mock.TypeXDeviceManagementService; 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 java.sql.Timestamp; import java.util.ArrayList; import java.util.List; @@ -169,7 +168,6 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { } } - @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 { @@ -202,8 +200,6 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { } } - - @Test(dependsOnMethods = "activatePolicy") public void applyPolicy() throws PolicyManagementException, OperationManagementException { new DelegationTask().execute(); @@ -333,7 +329,6 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { } } - @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 { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImplTest.java deleted file mode 100644 index b3a5475dfe..0000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImplTest.java +++ /dev/null @@ -1,146 +0,0 @@ -package org.wso2.carbon.policy.mgt.core.mgt.impl; - -import org.testng.annotations.Test; - -import static org.testng.Assert.*; - -/** - * Created by jasintha on 10/11/17. - */ -public class PolicyManagerImplTest { - - @Test - public void testAddPolicy() throws Exception { - - } - - @Test - public void testUpdatePolicy() throws Exception { - - } - - @Test - public void testUpdatePolicyPriorities() throws Exception { - - } - - @Test - public void testDeletePolicy() throws Exception { - - } - - @Test - public void testDeletePolicy1() throws Exception { - - } - - @Test - public void testActivatePolicy() throws Exception { - - } - - @Test - public void testInactivatePolicy() throws Exception { - - } - - @Test - public void testAddPolicyToDevice() throws Exception { - - } - - @Test - public void testAddPolicyToRole() throws Exception { - - } - - @Test - public void testAddPolicyToUser() throws Exception { - - } - - @Test - public void testGetPolicyByProfileID() throws Exception { - - } - - @Test - public void testGetPolicy() throws Exception { - - } - - @Test - public void testGetPolicies() throws Exception { - - } - - @Test - public void testGetPoliciesOfDevice() throws Exception { - - } - - @Test - public void testGetPoliciesOfDeviceType() throws Exception { - - } - - @Test - public void testGetPoliciesOfRole() throws Exception { - - } - - @Test - public void testGetPoliciesOfUser() throws Exception { - - } - - @Test - public void testGetPolicyAppliedDevicesIds() throws Exception { - - } - - @Test - public void testAddAppliedPolicyFeaturesToDevice() throws Exception { - - } - - @Test - public void testApplyChangesMadeToPolicies() throws Exception { - - } - - @Test - public void testAddAppliedPolicyToDevice() throws Exception { - - } - - @Test - public void testRemoveAppliedPolicyToDevice() throws Exception { - - } - - @Test - public void testCheckPolicyAvailable() throws Exception { - - } - - @Test - public void testSetPolicyApplied() throws Exception { - - } - - @Test - public void testGetPolicyCount() throws Exception { - - } - - @Test - public void testGetAppliedPolicyToDevice() throws Exception { - - } - - @Test - public void testGetAppliedPolicyIdsDeviceIds() throws Exception { - - } -} \ No newline at end of file