From ec91735fa3046978fa833b5d29df36053c62d5fa Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Mon, 8 Dec 2014 19:53:32 +0530 Subject: [PATCH] Adding test cases for policy management --- .../org.wso2.carbon.policy.mgt.common/pom.xml | 4 ++ .../carbon/policy/mgt/common/Feature.java | 23 ++++++++ .../wso2/carbon/policy/mgt/common/Policy.java | 23 ++++++++ .../mgt/common/impl/PolicyManagement.java | 56 ++++++++++++++++++ .../mgt/common/spi/PolicyManagerService.java | 14 +++-- .../mgt/common/PolicyManagementTestCase.java | 59 +++++++++++++++++++ .../mgt/common/utils/PolicyCreator.java | 55 +++++++++++++++++ 7 files changed, 228 insertions(+), 6 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/PolicyManagementTestCase.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/utils/PolicyCreator.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml index da740f2c430..76d141791d9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml @@ -78,6 +78,10 @@ org.wso2.carbon org.wso2.carbon.logging + + org.testng + testng + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java index b06a59e940c..cbe7e55ae56 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java @@ -24,4 +24,27 @@ public class Feature { private String name; private Object attribute; + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Object getAttribute() { + return attribute; + } + + public void setAttribute(Object attribute) { + this.attribute = attribute; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java index 192745c1ec8..96d687365b0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java @@ -25,4 +25,27 @@ public class Policy { private String policyName; private List featuresList; + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getPolicyName() { + return policyName; + } + + public void setPolicyName(String policyName) { + this.policyName = policyName; + } + + public List getFeaturesList() { + return featuresList; + } + + public void setFeaturesList(List featuresList) { + this.featuresList = featuresList; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java new file mode 100644 index 00000000000..5fc857ab4ee --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java @@ -0,0 +1,56 @@ +/* +* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +package org.wso2.carbon.policy.mgt.common.impl; + +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.spi.PolicyManagerService; + +public class PolicyManagement implements PolicyManagerService { + @Override + public int addPolicyToDevice(String deviceId, String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException { + return 0; + } + + @Override + public int addPolicyToDeviceType(String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException { + return 0; + } + + @Override + public int addPolicyToRole(String roleName, Policy policy) throws FeatureManagementException, PolicyManagementException { + return 0; + } + + @Override + public Policy getPolicyOfDevice(String deviceId, String deviceType) throws FeatureManagementException, PolicyManagementException { + return null; + } + + @Override + public Policy getPolicyOfDeviceType(String deviceType) throws FeatureManagementException, PolicyManagementException { + return null; + } + + @Override + public Policy getPolicyOfRole(String roleName) throws FeatureManagementException, PolicyManagementException { + return null; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java index 9cb5ea37311..2ee94d8da42 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java @@ -18,7 +18,9 @@ package org.wso2.carbon.policy.mgt.common.spi; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; /** * This interface defines the policy management which should be implemented by the plugins @@ -35,7 +37,7 @@ public interface PolicyManagerService { * @param policy */ - void addPolicyToDevice(String deviceId, String deviceType, Policy policy); + int addPolicyToDevice(String deviceId, String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException; /** * This method adds a policy to device type by the related device type plugins. @@ -43,14 +45,14 @@ public interface PolicyManagerService { * @param policy */ - void addPolicyToDeviceType(String deviceType,Policy policy); + int addPolicyToDeviceType(String deviceType,Policy policy) throws FeatureManagementException, PolicyManagementException; /** * This method adds the policy to specific role. * @param roleName * @param policy */ - void addPolicyToRole(String roleName, Policy policy); + int addPolicyToRole(String roleName, Policy policy) throws FeatureManagementException, PolicyManagementException; // Policy getPolicy(); @@ -61,7 +63,7 @@ public interface PolicyManagerService { * @return Policy */ - Policy getPolicyOfDevice(String deviceId, String deviceType); + Policy getPolicyOfDevice(String deviceId, String deviceType) throws FeatureManagementException, PolicyManagementException; /** * This method returns the device type specific policy. @@ -69,7 +71,7 @@ public interface PolicyManagerService { * @return Policy */ - Policy getPolicyOfDeviceType(String deviceType); + Policy getPolicyOfDeviceType(String deviceType) throws FeatureManagementException, PolicyManagementException; /** * This method returns the role specific policy. @@ -77,7 +79,7 @@ public interface PolicyManagerService { * @return */ - Policy getPolicyOfRole(String roleName); + Policy getPolicyOfRole(String roleName) throws FeatureManagementException, PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/PolicyManagementTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/PolicyManagementTestCase.java new file mode 100644 index 00000000000..a9198b7b3c8 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/PolicyManagementTestCase.java @@ -0,0 +1,59 @@ +/* +* Copyright (c) 2005-2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +package org.wos2.carbon.policy.mgt.common; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.Test; +import org.wos2.carbon.policy.mgt.common.utils.PolicyCreator; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.impl.PolicyManagement; + +public class PolicyManagementTestCase { + + private static final Log log = LogFactory.getLog(PolicyManagementTestCase.class); + + PolicyCreator creator = new PolicyCreator(); + Policy policy = creator.getPolicy(); + + private PolicyManagement policyManagement = new PolicyManagement(); + + @Test(groups = "policy.mgt.test", description = "Testing the first test case with testng.") + public void testPolicy() { + Assert.assertEquals("A", "A"); + } + + + @Test(groups = "policy.mgt.test", description = "Testing the adding policy to a device") + public void testAddPolicy() { + try { + Assert.assertEquals(policyManagement.addPolicyToDevice("1212-ESDD-12ER-7890", "MD", policy), 0); + } catch (FeatureManagementException e) { + log.error("Feature management exception happened.", e); + Assert.fail(); + } catch (PolicyManagementException e) { + log.error("Policy management exception happened.", e); + Assert.fail(); + } + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/utils/PolicyCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/utils/PolicyCreator.java new file mode 100644 index 00000000000..5b8da71dba4 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/utils/PolicyCreator.java @@ -0,0 +1,55 @@ +/* +* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +package org.wos2.carbon.policy.mgt.common.utils; + +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.ArrayList; +import java.util.List; + +public class PolicyCreator { + + private Policy policy = new Policy(); + + public PolicyCreator() { + createPolicy(); + } + + private void createPolicy() { + + Feature feature = new Feature(); + feature.setName("Camera"); + feature.setCode("502A"); + feature.setAttribute("disable"); + + List featureList = new ArrayList(); + featureList.add(feature); + + policy.setFeaturesList(featureList); + policy.setPolicyName("Camera_related_policy"); + + } + + public Policy getPolicy() { + return policy; + } + + +}