From 3a298be0739c2a8350fff085d1230d13104c3f16 Mon Sep 17 00:00:00 2001 From: Jasintha Date: Wed, 11 Oct 2017 16:22:09 +0530 Subject: [PATCH] Adding couple of missing testcase in policy service --- .../mgt/core/PolicyManagerServiceImpl.java | 10 ++++-- .../core/PolicyManagerServiceImplTest.java | 33 +++++++++++++++++-- .../services/SimplePolicyEvaluationTest.java | 7 +++- 3 files changed, 45 insertions(+), 5 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 7b6eb4bb243..628b829a4d4 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 @@ -149,8 +149,14 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { deviceIdentifier.getId() + " - " + deviceIdentifier.getType()); } } catch (PolicyEvaluationException e) { - String msg = "Error occurred while getting the effective features from the PEP service " + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); + + String msg; + if(deviceIdentifier != null) { + 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 75e5bb69b8b..5af4a20ab07 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 @@ -19,6 +19,7 @@ package org.wso2.carbon.policy.mgt.core; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.mockito.Spy; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -51,6 +52,8 @@ 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; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask; @@ -212,6 +215,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { policy1.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); policy1 = policyManagerService.addPolicy(policy1); + int policyCount = policyManagerService.getPolicyCount(); Assert.assertEquals(policyCount, 1, "Policy count should be 1"); @@ -232,6 +236,8 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { 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(dependsOnMethods = "activatePolicy") @@ -326,9 +332,32 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = "updatePolicy") - public void deletePolicy() throws PolicyManagementException { + public void deletePolicyById() throws PolicyManagementException { policyManagerService.deletePolicy(policy1.getId()); Policy tempPolicy = policyManagerService.getPAP().getPolicy(policy1.getId()); Assert.assertNull(tempPolicy, "Policy was not deleted successfully"); } -} + + @Test(dependsOnMethods = "updatePolicy") + public void deletePolicyByPolicy() throws PolicyManagementException { + policyManagerService.deletePolicy(policy1); + Policy tempPolicy = policyManagerService.getPAP().getPolicy(policy1.getId()); + Assert.assertNull(tempPolicy, "Policy was not deleted successfully"); + } + + @Test(dependsOnMethods = "applyPolicy") + 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()); + try{ + policyManagerService.getEffectiveFeatures(null); + }catch(FeatureManagementException e){ + Assert.assertTrue(e.getCause()instanceof PolicyEvaluationException); + } + + } + + +} \ 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 0d22f62e49a..d8ace70ae26 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 @@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; +import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import java.util.Collections; import java.util.List; @@ -76,7 +77,11 @@ SimplePolicyEvaluationTest implements PolicyEvaluationPoint { @Override public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { - return null; + if(deviceIdentifier!=null) { + return getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList(); + }else { + throw new PolicyEvaluationException(); + } } @Override