Merge pull request #1068 from Jasintha/devicePolicyServiceTestImprovement

Device policy service test improvement
revert-70aa11f8
Ruwan 7 years ago committed by GitHub
commit 70ab17acde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,13 +23,8 @@ import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.*;
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.group.mgt.DeviceGroup; 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.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; 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.ComplianceFeature;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; 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.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.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.OperationManagerImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; 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.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.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
@ -59,23 +47,14 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask; 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.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mock.TypeXDeviceManagementService; 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.MonitoringTask;
import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; 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.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest {
private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class); private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class);
@ -86,7 +65,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest {
private static final String POLICY1_FEATURE1_CODE = "DISALLOW_ADJUST_VOLUME"; 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 POLICY1_CAM_FEATURE1_CODE = "DISALLOW_OPEN_CAM";
private static final String ADMIN_USER = "admin"; 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"; public static final String DEVICE_TYPE_B = "deviceTypeB";
private OperationManager operationManager; private OperationManager operationManager;
@ -167,12 +146,60 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest {
} }
@Test(dependsOnMethods = "addPolicy") @Test(dependsOnMethods = "addPolicy")
public void activatePolicy() throws PolicyManagementException { public void activatePolicy() throws Exception {
policyManagerService.getPAP().activatePolicy(policy1.getId()); policyManagerService.getPAP().activatePolicy(policy1.getId());
Policy effectivePolicy = policyManagerService.getEffectivePolicy(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); Policy effectivePolicy = policyManagerService.getEffectivePolicy(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A));
Assert.assertEquals(effectivePolicy.getPolicyName(), POLICY1, POLICY1 + " was not activated for " + DEVICE1); 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") @Test(dependsOnMethods = "activatePolicy")
public void applyPolicy() throws PolicyManagementException, OperationManagementException { public void applyPolicy() throws PolicyManagementException, OperationManagementException {
new DelegationTask().execute(); new DelegationTask().execute();
@ -282,19 +309,34 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest {
} }
@Test(dependsOnMethods = "applyPolicy") @Test(dependsOnMethods = "applyPolicy")
public void getEffectiveFeatures( ) throws Exception { public void getEffectiveFeatures() throws Exception {
List<ProfileFeature> effectiveFeatures = policyManagerService. List<ProfileFeature> effectiveFeatures = policyManagerService.
getEffectiveFeatures(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); getEffectiveFeatures(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A));
Assert.assertNotNull(effectiveFeatures); 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{ try{
policyManagerService.getEffectiveFeatures(new DeviceIdentifier(DEVICE_2, DEVICE_TYPE_B)); policyEvaluationPoint = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint();
}catch(FeatureManagementException ex){ PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint("Simple", null);
if(ex.getCause() instanceof PolicyEvaluationException){ policyManagerService.getEffectiveFeatures(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A));
Assert.assertTrue(ex.getCause() instanceof PolicyEvaluationException); } finally {
}else { PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint("Simple", policyEvaluationPoint);
throw ex; }
} }
@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;
} }
} }

Loading…
Cancel
Save