From a4c7ade205ff91ff9188227aeb6d38cb0982d334 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 29 Sep 2017 16:33:46 +0530 Subject: [PATCH] Adding some testcases for different set of operations. --- .../mgt/dao/impl/ConfigOperationDAOImpl.java | 2 + .../DeviceManagementProviderService.java | 2 - .../DeviceManagementProviderServiceImpl.java | 35 ------- .../operation/OperationManagementTests.java | 96 ++++++++++++++++++- 4 files changed, 97 insertions(+), 38 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java index 052a9aeccb..a2aefc111f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java @@ -118,6 +118,8 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl { bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); configOperation = (ConfigOperation) ois.readObject(); + configOperation.setId(rs.getInt("OPERATION_ID")); + configOperation.setEnabled(rs.getBoolean("ENABLED")); } } catch (IOException e) { throw new OperationManagementDAOException("IO Error occurred while de serialize the policy operation " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index fe77b94180..ff278493f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -481,8 +481,6 @@ public interface DeviceManagementProviderService { */ PlatformConfiguration getConfiguration(String deviceType) throws DeviceManagementException; - void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException; - /** * This method is used to check whether the device is enrolled with the give user. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index e5d065a196..ab31f82599 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1890,41 +1890,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return result; } - @Override - public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { - try { - if (device == null || status == null) { - String msg = "Received incomplete data for updateDeviceEnrolmentInfo"; - log.error(msg); - throw new DeviceManagementException(msg); - } - if (log.isDebugEnabled()) { - log.debug("Updating enrolment for device: " + device.getId() + " of type '" + device.getType() + "'"); - } - DeviceManagementDAOFactory.beginTransaction(); - device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); - device.getEnrolmentInfo().setStatus(status); - deviceDAO.updateDevice(device, this.getTenantId()); - DeviceManagementDAOFactory.commitTransaction(); - } catch (DeviceManagementDAOException e) { - DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while updating device enrolment status for " + device.getDeviceIdentifier() + - " of type " + device.getType(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "Error occurred while initiating transaction"; - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } catch (Exception e) { - String msg = "Error occurred in updateDeviceEnrolmentInfo"; - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - } - @Override public void registerDeviceManagementService(DeviceManagementService deviceManagementService) { if (log.isDebugEnabled()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java index c861e2fb75..6ee4c3af37 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java @@ -25,6 +25,7 @@ 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.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -50,6 +51,7 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.List; @@ -72,6 +74,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { private List deviceIds = new ArrayList<>(); private OperationManager operationMgtService; + private DeviceManagementProviderService deviceMgmtProvider; private Activity commandActivity; private long commandActivityBeforeUpdatedTimestamp; @@ -94,6 +97,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); } } + this.deviceMgmtProvider = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(); NotificationStrategy notificationStrategy = new TestNotificationStrategy(); this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); } @@ -555,8 +559,98 @@ public class OperationManagementTests extends BaseDeviceManagementTest { expectedExceptions = IllegalArgumentException.class) public void getOperationByActivityIdAndDeviceInvalidActivityId() throws DeviceManagementException, OperationManagementException { - this.operationMgtService.getOperationByActivityIdAndDevice(DeviceManagementConstants.OperationAttributes.ACTIVITY + 0, + this.operationMgtService.getOperationByActivityIdAndDevice( + DeviceManagementConstants.OperationAttributes.ACTIVITY + 0, new DeviceIdentifier(INVALID_DEVICE, DEVICE_TYPE)); } + @Test(dependsOnMethods = "getOperationByActivityIdAndDeviceInvalidActivityId") + public void getPendingOperationsInactiveEnrollment() throws DeviceManagementException, + OperationManagementException { + changeStatus(EnrolmentInfo.Status.INACTIVE); + List operations = this.operationMgtService.getPendingOperations(this.deviceIds.get(1)); + Assert.assertTrue(operations != null); + Assert.assertEquals(operations.size(), 4); + changeStatus(EnrolmentInfo.Status.ACTIVE); + } + + private void changeStatus(EnrolmentInfo.Status status) throws DeviceManagementException, + OperationManagementException { + Device device = this.deviceMgmtProvider.getDevice(this.deviceIds.get(1)); + Assert.assertTrue(device != null); + Assert.assertEquals(device.getType(), DEVICE_TYPE); + Assert.assertTrue(device.getEnrolmentInfo() != null); + device.getEnrolmentInfo().setStatus(status); + boolean modified = this.deviceMgmtProvider.changeDeviceStatus(this.deviceIds.get(1), status); + Assert.assertTrue(modified); + device = this.deviceMgmtProvider.getDevice(this.deviceIds.get(1)); + Assert.assertEquals(device.getEnrolmentInfo().getStatus(), status); + } + + @Test(dependsOnMethods = "getPendingOperationsInactiveEnrollment") + public void getNextPendingOperationInactiveEnrollment() throws DeviceManagementException, + OperationManagementException { + changeStatus(EnrolmentInfo.Status.INACTIVE); + Operation operation = this.operationMgtService.getNextPendingOperation(this.deviceIds.get(1)); + Assert.assertTrue(operation != null); + changeStatus(EnrolmentInfo.Status.ACTIVE); + } + + @Test(dependsOnMethods = "getNextPendingOperationInactiveEnrollment") + public void getNextPendingOperationForAllOperations() throws DeviceManagementException, + OperationManagementException { + for (int i = 0; i < 4; i++) { + Operation operation = this.operationMgtService.getNextPendingOperation(this.deviceIds.get(1)); + operation.setStatus(Operation.Status.COMPLETED); + this.operationMgtService.updateOperation(deviceIds.get(1), operation); + } + Assert.assertTrue(this.operationMgtService.getNextPendingOperation(this.deviceIds.get(1)) == null); + } + + @Test(dependsOnMethods = "getNextPendingOperationForAllOperations") + public void getOperationByDeviceAndOperationIdForAllOperations() throws DeviceManagementException, + OperationManagementException { + for (int i = 1; i <= 4; i++) { + Operation operation = this.operationMgtService.getOperationByDeviceAndOperationId(this.deviceIds.get(1), i); + Assert.assertEquals(operation.getStatus(), Operation.Status.COMPLETED); + } + } + + @Test(dependsOnMethods = "getOperationByDeviceAndOperationIdForAllOperations") + public void getOperationForAllOperations() throws DeviceManagementException, + OperationManagementException { + for (int i = 1; i <= 4; i++) { + Operation operation = this.operationMgtService.getOperation(i); + Assert.assertTrue(operation != null); + } + } + + @Test(dependsOnMethods = "getOperationForAllOperations") + public void addCustomPolicyOperation() throws OperationManagementException, InvalidDeviceException { + this.addCustomOperation(Operation.Type.POLICY, DeviceManagementConstants.AuthorizationSkippedOperationCodes. + POLICY_OPERATION_CODE); + } + + @Test(dependsOnMethods = "getOperationForAllOperations") + public void addCustomMonitorOperation() throws OperationManagementException, InvalidDeviceException { + this.addCustomOperation(Operation.Type.COMMAND, DeviceManagementConstants.AuthorizationSkippedOperationCodes. + MONITOR_OPERATION_CODE); + } + + @Test(dependsOnMethods = "getOperationForAllOperations") + public void addCustomPolicyRevokeOperation() throws OperationManagementException, InvalidDeviceException { + this.addCustomOperation(Operation.Type.POLICY, DeviceManagementConstants.AuthorizationSkippedOperationCodes. + POLICY_REVOKE_OPERATION_CODE); + } + + private void addCustomOperation(Operation.Type type, String operationCode) throws OperationManagementException, InvalidDeviceException { + Operation operation = new Operation(); + operation.setCode(operationCode); + operation.setType(type); + Activity activity = this.operationMgtService.addOperation(operation, Collections.singletonList(this.deviceIds.get(2))); + Assert.assertEquals(activity.getActivityStatus().size(), 1); + for (ActivityStatus status : activity.getActivityStatus()) { + Assert.assertEquals(status.getStatus(), ActivityStatus.Status.PENDING); + } + } }