From 7eff68b807adef79bebab029a29ea9e5a52f897f Mon Sep 17 00:00:00 2001 From: Jasintha Date: Wed, 11 Oct 2017 21:04:53 +0530 Subject: [PATCH] improve some exsiting testcase to increase coverage and new testcase --- .../mgt/core/PolicyManagerServiceImpl.java | 8 ++------ .../mgt/core/PolicyManagerServiceImplTest.java | 16 ++++++++++++++-- .../services/SimplePolicyEvaluationTest.java | 8 +++++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index 628b829a4d..b1fe61ab7e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -150,13 +150,9 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { } } catch (PolicyEvaluationException e) { - String msg; - if(deviceIdentifier != null) { - msg = "Error occurred while getting the effective features from the PEP service " + + String msg = "Error occurred while getting the effective features from the PEP service " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); - }else { - msg = "Null deviceIdentifier object is passed to the method"; - } + log.error(msg, e); throw new FeatureManagementException(msg, e); } 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 5af4a20ab0..8f4880ed3e 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 @@ -40,6 +40,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; 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.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; @@ -84,6 +85,8 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { private static final String POLICY1 = "policy1"; private static final String POLICY1_FEATURE1_CODE = "DISALLOW_ADJUST_VOLUME"; private static final String ADMIN_USER = "admin"; + public static final String DEVICE_2 = "device2"; + public static final String DEVICE_TYPE_B = "deviceTypeB"; private DeviceManagementProviderService deviceMgtService; private GroupManagementProviderService groupMgtService; @@ -309,8 +312,11 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { complianceFeatures.add(complianceFeature); policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); - Assert.assertFalse(deviceCompliance, "Policy was compliant even though the response was not compliant"); + List complianceFeatureList = policyManagerService. + checkPolicyCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); + Assert.assertNotNull(complianceFeature); + Assert.assertEquals(POLICY1_FEATURE1_CODE,complianceFeatureList.get(0).getFeatureCode()); } @Test(dependsOnMethods = "inactivatePolicy") @@ -352,12 +358,18 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { Assert.assertNotNull(effectiveFeatures); Assert.assertEquals(POLICY1_FEATURE1_CODE,effectiveFeatures.get(0).getFeatureCode()); try{ - policyManagerService.getEffectiveFeatures(null); + policyManagerService.getEffectiveFeatures(new DeviceIdentifier(DEVICE_2, DEVICE_TYPE_B)); }catch(FeatureManagementException e){ Assert.assertTrue(e.getCause()instanceof PolicyEvaluationException); } } + @Test(dependsOnMethods = "applyPolicy") + public void getDeviceCompliance() throws Exception{ + NonComplianceData deviceCompliance = policyManagerService.getDeviceCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); + Assert.assertNotNull(deviceCompliance); + } + } \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java index d8ace70ae2..b424befe3d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java @@ -36,6 +36,7 @@ public class SimplePolicyEvaluationTest implements PolicyEvaluationPoint { private static final Log log = LogFactory.getLog(SimplePolicyEvaluationTest.class); + public static final String DEVICE2 = "device2"; // assuming this device does not have valid policy @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { @@ -77,10 +78,11 @@ SimplePolicyEvaluationTest implements PolicyEvaluationPoint { @Override public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { - if(deviceIdentifier!=null) { - return getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList(); - }else { + if(DEVICE2.equals(deviceIdentifier.getId())) { throw new PolicyEvaluationException(); + }else { + return getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList(); + } }