From 87b160cf19bc02ad05deaafcf806f195e77394f4 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 26 Sep 2017 22:55:52 +0530 Subject: [PATCH 1/8] Adding more operation mgt test cases. --- .../operation/mgt/OperationManagerImpl.java | 209 +++++++++--------- .../mgt/dao/OperationMappingDAO.java | 1 + .../mgt/dao/impl/CommandOperationDAOImpl.java | 2 +- .../mgt/dao/impl/ConfigOperationDAOImpl.java | 2 +- .../mgt/dao/impl/GenericOperationDAOImpl.java | 5 +- .../mgt/dao/impl/PolicyOperationDAOImpl.java | 2 +- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 2 +- .../operation/OperationManagementTests.java | 44 +++- 8 files changed, 155 insertions(+), 112 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index c22f3de950..735d3e71c4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -122,7 +122,7 @@ public class OperationManagerImpl implements OperationManager { log.debug("operation:[" + operation.toString() + "]"); for (DeviceIdentifier deviceIdentifier : deviceIds) { log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + - deviceIdentifier.getType() + "]"); + deviceIdentifier.getType() + "]"); } } try { @@ -137,7 +137,7 @@ public class OperationManagerImpl implements OperationManager { //Send the operation statuses only for admin triggered operations String deviceType = validDeviceIds.get(0).getType(); activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult, - deviceType)); + deviceType)); return activity; } @@ -160,7 +160,7 @@ public class OperationManagerImpl implements OperationManager { boolean hasExistingTaskOperation; int enrolmentId; if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == operationDto. - getControl()) { + getControl()) { isNotRepeated = true; } @@ -177,10 +177,10 @@ public class OperationManagerImpl implements OperationManager { } } else if (isNotRepeated) { operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode, - org.wso2.carbon.device.mgt.core.dto.operation.mgt. - Operation.Status.PENDING, - org.wso2.carbon.device.mgt.core.dto.operation.mgt. - Operation.Status.REPEATED); + org.wso2.carbon.device.mgt.core.dto.operation.mgt. + Operation.Status.PENDING, + org.wso2.carbon.device.mgt.core.dto.operation.mgt. + Operation.Status.REPEATED); operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled); } else { operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled); @@ -200,8 +200,8 @@ public class OperationManagerImpl implements OperationManager { operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.COMPLETED); } catch (PushNotificationExecutionFailedException e) { log.error("Error occurred while sending push notifications to " + - deviceId.getType() + " device carrying id '" + - deviceId + "'", e); + deviceId.getType() + " device carrying id '" + + deviceId + "'", e); // Reschedule if push notification failed. operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED); } @@ -219,7 +219,7 @@ public class OperationManagerImpl implements OperationManager { //Get the device-type from 1st valid DeviceIdentifier. We know the 1st element is definitely there. String deviceType = validDeviceIds.get(0).getType(); activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult, - deviceType)); + deviceType)); } return activity; } else { @@ -242,7 +242,7 @@ public class OperationManagerImpl implements OperationManager { //Add the invalid DeviceIds for (String id : deviceIdValidationResult.getErrorDeviceIdList()) { activityStatus = new ActivityStatus(); - activityStatus.setDeviceIdentifier(new DeviceIdentifier(id,deviceType)); + activityStatus.setDeviceIdentifier(new DeviceIdentifier(id, deviceType)); activityStatus.setStatus(ActivityStatus.Status.INVALID); activityStatuses.add(activityStatus); } @@ -288,7 +288,7 @@ public class OperationManagerImpl implements OperationManager { } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user :" + - this.getUser(), e); + this.getUser(), e); } deviceIDHolder.setValidDeviceIDList(authorizedDeviceList); deviceIDHolder.setErrorDeviceIdList(unAuthorizedDeviceList); @@ -310,8 +310,8 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); @@ -331,8 +331,8 @@ public class OperationManagerImpl implements OperationManager { } } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -350,15 +350,15 @@ public class OperationManagerImpl implements OperationManager { String owner = request.getOwner(); if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "' of owner '" + owner + "'" ); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "' of owner '" + owner + "'"); } EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId, owner); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + - deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type" + + deviceId.getType()); } int enrolmentId = enrolmentInfo.getId(); try { @@ -376,8 +376,8 @@ public class OperationManagerImpl implements OperationManager { paginationResult.setRecordsFiltered(count); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -390,7 +390,7 @@ public class OperationManagerImpl implements OperationManager { @Override public List getPendingOperations(DeviceIdentifier deviceId) throws - OperationManagementException { + OperationManagementException { if (log.isDebugEnabled()) { log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() + "]"); } @@ -399,16 +399,16 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } // EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + - deviceId.getId() + " and given type:" + - deviceId.getType()); + deviceId.getId() + " and given type:" + + deviceId.getType()); } int enrolmentId = enrolmentInfo.getId(); //Changing the enrollment status & attempt count if the device is marked as inactive or unreachable @@ -437,8 +437,8 @@ public class OperationManagerImpl implements OperationManager { Collections.sort(operations, new OperationCreateTimeComparator()); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "pending operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); + "pending operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -457,15 +457,15 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + - deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type" + + deviceId.getType()); } int enrolmentId = enrolmentInfo.getId(); //Changing the enrollment status & attempt count if the device is marked as inactive or unreachable @@ -479,23 +479,23 @@ public class OperationManagerImpl implements OperationManager { try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getNextOperation( - enrolmentInfo.getId()); + enrolmentInfo.getId()); if (dtoOperation != null) { if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND.equals(dtoOperation.getType() )) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation(dtoOperation.getId()); + getOperation(dtoOperation.getId()); dtoOperation.setEnabled(commandOperation.isEnabled()); } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG.equals(dtoOperation. - getType())) { + getType())) { dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE.equals(dtoOperation. - getType())) { + getType())) { dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY.equals(dtoOperation. - getType())) { + getType())) { dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); } operation = OperationDAOUtil.convertOperation(dtoOperation); @@ -520,15 +520,15 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException( "Device not found for device id:" + deviceId.getId() + " " + "type:" + - deviceId.getType()); + deviceId.getType()); } try { @@ -537,9 +537,9 @@ public class OperationManagerImpl implements OperationManager { boolean isUpdated = false; if (operation.getStatus() != null) { isUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt. - Operation.Status.valueOf(operation.getStatus(). - toString())); + org.wso2.carbon.device.mgt.core.dto.operation.mgt. + Operation.Status.valueOf(operation.getStatus(). + toString())); } if (operation.getOperationResponse() != null) { operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); @@ -549,7 +549,7 @@ public class OperationManagerImpl implements OperationManager { OperationManagementDAOFactory.rollbackTransaction(); throw new OperationManagementException( "Error occurred while updating the operation: " + operationId + " status:" + - operation.getStatus(), e); + operation.getStatus(), e); } catch (TransactionManagementException e) { throw new OperationManagementException("Error occurred while initiating a transaction", e); } finally { @@ -584,56 +584,57 @@ public class OperationManagerImpl implements OperationManager { Operation operation = null; if (log.isDebugEnabled()) { log.debug("Operation Id: " + operationId + " Device Type: " + deviceId.getType() + " Device Identifier: " + - deviceId.getId()); + deviceId.getId()); } if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device identifier: " + - deviceId.getId() + " type: " + deviceId.getType()); + deviceId.getId() + " type: " + deviceId.getType()); } try { OperationManagementDAOFactory.openConnection(); - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getOperationByDeviceAndId(enrolmentInfo.getId(), - operationId); - if (dtoOperation.getType(). + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation deviceSpecificOperation = operationDAO. + getOperationByDeviceAndId(enrolmentInfo.getId(), + operationId); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = deviceSpecificOperation; + if (deviceSpecificOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation(dtoOperation.getId()); + getOperation(deviceSpecificOperation.getId()); dtoOperation.setEnabled(commandOperation.isEnabled()); - } else if (dtoOperation.getType(). + } else if (deviceSpecificOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { - dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); - } else if (dtoOperation.getType().equals( + dtoOperation = configOperationDAO.getOperation(deviceSpecificOperation.getId()); + } else if (deviceSpecificOperation.getType().equals( org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE)) { - dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); - } else if (dtoOperation.getType().equals( + dtoOperation = profileOperationDAO.getOperation(deviceSpecificOperation.getId()); + } else if (deviceSpecificOperation.getType().equals( org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY)) { - dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); + dtoOperation = policyOperationDAO.getOperation(deviceSpecificOperation.getId()); } - if (dtoOperation == null) { throw new OperationManagementException("Operation not found for operation Id:" + operationId + - " device id:" + deviceId.getId()); + " device id:" + deviceId.getId()); } - operation = OperationDAOUtil.convertOperation(dtoOperation); + dtoOperation.setStatus(deviceSpecificOperation.getStatus()); + operation = OperationDAOUtil.convertOperation(deviceSpecificOperation); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException("Error occurred while opening connection to the data source", - e); + e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -649,15 +650,15 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException( "Device not found for device id:" + deviceId.getId() + " " + "type:" + - deviceId.getType()); + deviceId.getType()); } try { @@ -667,14 +668,14 @@ public class OperationManagerImpl implements OperationManager { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString()); dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus)); dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. - Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. + Status.PENDING)); dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. - Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. + Status.PENDING)); dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. - Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. + Status.PENDING)); Operation operation; @@ -685,9 +686,9 @@ public class OperationManagerImpl implements OperationManager { } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + - deviceId.getId() + "' and status:" + status.toString(), e); + "operations assigned for '" + deviceId.getType() + + "' device '" + + deviceId.getId() + "' and status:" + status.toString(), e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -703,32 +704,32 @@ public class OperationManagerImpl implements OperationManager { try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation( - operationId); + operationId); if (dtoOperation == null) { throw new OperationManagementException("Operation not found for given Id:" + operationId); } if (dtoOperation.getType() - .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { + .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation(dtoOperation.getId()); + getOperation(dtoOperation.getId()); dtoOperation.setEnabled(commandOperation.isEnabled()); } else if (dtoOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. - PROFILE)) { + PROFILE)) { dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. - POLICY)) { + POLICY)) { dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); } operation = OperationDAOUtil.convertOperation(dtoOperation); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the operation with operation Id '" + - operationId, e); + operationId, e); } catch (SQLException e) { throw new OperationManagementException("Error occurred while opening a connection to the data source", e); } finally { @@ -808,7 +809,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the operation with activity Id '" + - activity, e); + activity, e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -850,7 +851,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while getting the activity list changed after a " + - "given time.", e); + "given time.", e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -866,7 +867,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while getting the activity list changed after a " + - "given time.", e); + "given time.", e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -881,7 +882,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while getting the activity count changed after a " + - "given time.", e); + "given time.", e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -904,12 +905,18 @@ public class OperationManagerImpl implements OperationManager { private OperationDAO lookupOperationDAO(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operation) { - if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) { + if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation || + operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { return commandOperationDAO; - } else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation) { + } else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation || + operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE)) { return profileOperationDAO; - } else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation) { + } else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation || + operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { return configOperationDAO; + } else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation || + operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY)) { + return policyOperationDAO; } else { return operationDAO; } @@ -966,8 +973,8 @@ public class OperationManagerImpl implements OperationManager { enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, status, tenantId); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving metadata of '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "'", e); + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -995,15 +1002,15 @@ public class OperationManagerImpl implements OperationManager { } } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving enrollment data of '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "' of owner '" + owner + "'", e); + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "' of owner '" + owner + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while checking the device access permissions for '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "' of owner '" + owner + "'", e); + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "' of owner '" + owner + "'", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1018,8 +1025,8 @@ public class OperationManagerImpl implements OperationManager { enrolmentInfo = deviceDAO.getActiveEnrolment(deviceId, tenantId); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving enrollment data of '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "'", e); + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -1040,7 +1047,7 @@ public class OperationManagerImpl implements OperationManager { } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new OperationManagementException("Error occurred while updating enrollment status of device of " + - "enrolment-id '" + enrolmentId + "'", e); + "enrolment-id '" + enrolmentId + "'", e); } catch (TransactionManagementException e) { throw new OperationManagementException("Error occurred while initiating a transaction", e); } finally { @@ -1055,7 +1062,7 @@ public class OperationManagerImpl implements OperationManager { List monitoringOperations = deviceManagementProviderService.getMonitoringOperationList(deviceType);//Get task list from each device type - for(MonitoringOperation op : monitoringOperations){ + for (MonitoringOperation op : monitoringOperations) { if (operation.getCode().equals(op.getTaskName())) { return true; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java index b28b773d7b..aa5d9b7bea 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java @@ -18,6 +18,7 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationEnrolmentMapping; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; 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/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index fcf57ab022..37be1b7e9d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -74,11 +74,11 @@ public class CommandOperationDAOImpl extends GenericOperationDAOImpl { public void deleteOperation(int id) throws OperationManagementDAOException { PreparedStatement stmt = null; try { - super.deleteOperation(id); Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("DELETE FROM DM_COMMAND_OPERATION WHERE OPERATION_ID = ?"); stmt.setInt(1, id); stmt.executeUpdate(); + super.deleteOperation(id); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); } finally { 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 82beb1b5f0..0720d6850c 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 @@ -62,11 +62,11 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl { public void deleteOperation(int id) throws OperationManagementDAOException { PreparedStatement stmt = null; try { - super.deleteOperation(id); Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("DELETE FROM DM_CONFIG_OPERATION WHERE OPERATION_ID = ?") ; stmt.setInt(1, id); stmt.executeUpdate(); + super.deleteOperation(id); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); } finally { 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/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 7e174ec714..9418cbeee1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -833,9 +833,9 @@ public class GenericOperationDAOImpl implements OperationDAO { Operation operation = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE, " + + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, om.STATUS, o.OPERATION_CODE, " + "om.ID AS OM_MAPPING_ID, " + - "om.UPDATED_TIMESTAMP FROM (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS," + + "om.UPDATED_TIMESTAMP FROM (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP," + "OPERATION_CODE FROM DM_OPERATION WHERE id = ?) o INNER JOIN (SELECT * FROM " + "DM_ENROLMENT_OP_MAPPING dm where dm.OPERATION_ID = ? AND dm.ENROLMENT_ID = ?) om " + "ON o.ID = om.OPERATION_ID "; @@ -850,6 +850,7 @@ public class GenericOperationDAOImpl implements OperationDAO { operation.setId(rs.getInt("ID")); operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); + operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); // if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { // operation.setReceivedTimeStamp(""); // } else { 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/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java index 4c0243ac80..4053e7c227 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -126,11 +126,11 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { public void deleteOperation(int operationId) throws OperationManagementDAOException { PreparedStatement stmt = null; try { - super.deleteOperation(operationId); Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("DELETE FROM DM_POLICY_OPERATION WHERE OPERATION_ID=?"); stmt.setInt(1, operationId); stmt.executeUpdate(); + super.deleteOperation(operationId); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); } finally { 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/ProfileOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java index 931cc079f4..8bc0a3a9d0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java @@ -127,11 +127,11 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { public void deleteOperation(int id) throws OperationManagementDAOException { PreparedStatement stmt = null; try { - super.deleteOperation(id); Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("DELETE FROM DM_PROFILE_OPERATION WHERE OPERATION_ID=?"); stmt.setInt(1, id); stmt.executeUpdate(); + super.deleteOperation(id); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); } finally { 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 81cc2c4738..3de63c0f48 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 @@ -35,6 +35,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; 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.OperationManager; +import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; @@ -64,7 +65,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -public class OperationManagementTests{ +public class OperationManagementTests extends BaseDeviceManagementTest{ private static final Log log = LogFactory.getLog(OperationManagementTests.class); private static final String DEVICE_TYPE = "OP_TEST_TYPE"; @@ -80,6 +81,7 @@ public class OperationManagementTests{ private List deviceIds = new ArrayList<>(); private OperationManager operationMgtService; + private Activity commandActivity; @BeforeClass public void init() throws Exception { @@ -122,9 +124,9 @@ public class OperationManagementTests{ @Test public void addCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { - Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), + this.commandActivity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), this.deviceIds); - validateOperationResponse(activity); + validateOperationResponse(this.commandActivity); } @Test(dependsOnMethods = "addCommandOperation") @@ -209,7 +211,7 @@ public class OperationManagementTests{ try { this.operationMgtService.getOperations(deviceIdentifier, request); } catch (OperationManagementException ex) { - if (ex.getMessage() == null){ + if (ex.getMessage() == null) { Assert.assertTrue(ex.getMessage().contains("User '" + NON_ADMIN_USER + "' is not authorized")); } } @@ -221,7 +223,7 @@ public class OperationManagementTests{ public void updateOperation() throws OperationManagementException { DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); List operations = this.operationMgtService.getPendingOperations(deviceIdentifier); - Assert.assertTrue(operations!= null && operations.size()==4); + Assert.assertTrue(operations != null && operations.size() == 4); Operation operation = (Operation) operations.get(0); operation.setStatus(Operation.Status.COMPLETED); operation.setOperationResponse("The operation is successfully completed"); @@ -229,4 +231,36 @@ public class OperationManagementTests{ List pendingOperations = this.operationMgtService.getPendingOperations(deviceIdentifier); Assert.assertEquals(pendingOperations.size(), 3); } + + @Test(dependsOnMethods = "updateOperation") + public void getNextPendingOperation() throws OperationManagementException { + DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); + Operation operation = this.operationMgtService.getNextPendingOperation(deviceIdentifier); + Assert.assertTrue(operation.getType().equals(Operation.Type.POLICY)); + } + + + @Test(dependsOnMethods = "getNextPendingOperation", enabled = false) + public void deleteOperation() throws OperationManagementException { + //TODO: Verify the operation management service operations. + DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); + Operation operation = this.operationMgtService.getNextPendingOperation(deviceIdentifier); + this.operationMgtService.deleteOperation(operation.getId()); + Operation operationAfterDeletion = this.operationMgtService.getNextPendingOperation(deviceIdentifier); + Assert.assertTrue(operation.getId() != operation.getId()); + Assert.assertTrue(operationAfterDeletion.getType().equals(Operation.Type.POLICY)); + } + + @Test(dependsOnMethods = "getNextPendingOperation") + public void getOperationByDeviceAndOperationId() throws OperationManagementException { + DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); + String operationId = this.commandActivity.getActivityId(). + replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, ""); + Operation operation = this.operationMgtService.getOperationByDeviceAndOperationId(deviceIdentifier, + Integer.parseInt(operationId)); + Assert.assertTrue(operation.getStatus().equals(Operation.Status.COMPLETED)); + Assert.assertTrue(operation.getType().equals(Operation.Type.COMMAND)); + } + + } From 1b5bd81a6d794910b5704e898bca761ea28856b1 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Wed, 27 Sep 2017 00:30:02 +0530 Subject: [PATCH 2/8] Adding more operation mgt test cases. --- .../operation/OperationManagementTests.java | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) 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 3de63c0f48..42aec46129 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 @@ -38,7 +38,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; -import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; @@ -60,12 +59,14 @@ import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.io.InputStream; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.TimeZone; -public class OperationManagementTests extends BaseDeviceManagementTest{ +public class OperationManagementTests { private static final Log log = LogFactory.getLog(OperationManagementTests.class); private static final String DEVICE_TYPE = "OP_TEST_TYPE"; @@ -262,5 +263,50 @@ public class OperationManagementTests extends BaseDeviceManagementTest{ Assert.assertTrue(operation.getType().equals(Operation.Type.COMMAND)); } + @Test(dependsOnMethods = "getOperationByDeviceAndOperationId") + public void getOperationsByDeviceAndStatus() throws OperationManagementException, DeviceManagementException { + DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); + List operation = this.operationMgtService.getOperationsByDeviceAndStatus(deviceIdentifier, Operation.Status.PENDING); + Assert.assertEquals(operation.size(), 3); + } + + @Test(dependsOnMethods = "getOperationsByDeviceAndStatus") + public void getOperation() throws OperationManagementException, DeviceManagementException { + String operationId = this.commandActivity.getActivityId(). + replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, ""); + Operation operation = this.operationMgtService.getOperation(Integer.parseInt(operationId)); + Assert.assertEquals(operation.getType(), Operation.Type.COMMAND); + } + + @Test(dependsOnMethods = "getOperation") + public void getOperationActivity() throws OperationManagementException { + Activity activity = this.operationMgtService.getOperationByActivityId(commandActivity.getActivityId()); + Assert.assertEquals(activity.getType(), Activity.Type.COMMAND); + Assert.assertEquals(activity.getActivityStatus().size(), this.deviceIds.size()); + Assert.assertEquals(activity.getActivityStatus().get(0).getStatus(), ActivityStatus.Status.COMPLETED); + for (int i = 1; i < this.deviceIds.size(); i++) { + Assert.assertEquals(activity.getActivityStatus().get(i).getStatus(), ActivityStatus.Status.PENDING); + } + } + + @Test(dependsOnMethods = "getOperationActivity") + public void getOperationByActivityIdAndDevice() throws OperationManagementException { + Activity activity = this.operationMgtService. + getOperationByActivityIdAndDevice(this.commandActivity.getActivityId(), this.deviceIds.get(0)); + Assert.assertEquals(activity.getType(), Activity.Type.COMMAND); + Assert.assertEquals(activity.getActivityStatus().size(), 1); + Assert.assertEquals(activity.getActivityStatus().get(0).getStatus(), ActivityStatus.Status.COMPLETED); + } + + @Test(dependsOnMethods = "updateOperation", enabled = false) + public void getOperationUpdatedAfter() throws OperationManagementException, ParseException { + String timestamp = this.commandActivity.getCreatedTimeStamp(); + SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy"); + dateFormat.setTimeZone(TimeZone.getTimeZone("IST")); + Date date = dateFormat.parse(timestamp); + List operations = this.operationMgtService.getActivitiesUpdatedAfter(date.getTime()); + } + + } From 9f502c022f2aa14f75f474c165fc98fd502b6e73 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Wed, 27 Sep 2017 14:27:29 +0530 Subject: [PATCH 3/8] Cleaning up the unused methods on the operation mgmt. --- .../operation/mgt/OperationManager.java | 19 +- .../operation/mgt/OperationManagerImpl.java | 86 -------- .../core/operation/mgt/dao/OperationDAO.java | 12 -- .../mgt/dao/impl/CommandOperationDAOImpl.java | 16 -- .../mgt/dao/impl/ConfigOperationDAOImpl.java | 16 -- .../mgt/dao/impl/GenericOperationDAOImpl.java | 204 ------------------ .../mgt/dao/impl/PolicyOperationDAOImpl.java | 16 -- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 16 -- ...PushNotificationBasedOperationManager.java | 15 -- .../DeviceManagementProviderService.java | 2 - .../DeviceManagementProviderServiceImpl.java | 5 - .../operation/OperationManagementTests.java | 12 -- 12 files changed, 5 insertions(+), 414 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java index 8de9f3199e..9c79e58adf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java @@ -63,7 +63,8 @@ public interface OperationManager { * @throws OperationManagementException If some unusual behaviour is observed while fetching the * operation list. */ - PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request) throws OperationManagementException; + PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request) + throws OperationManagementException; /** * Method to retrieve the list of available operations to a device. @@ -73,15 +74,12 @@ public interface OperationManager { * @throws OperationManagementException If some unusual behaviour is observed while fetching the * operation list. */ - List getPendingOperations( - DeviceIdentifier deviceId) throws OperationManagementException; + List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException; Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException; void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException; - void deleteOperation(int operationId) throws OperationManagementException; - Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException; @@ -93,9 +91,8 @@ public interface OperationManager { Activity getOperationByActivityId(String activity) throws OperationManagementException; - Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException; - - List getOperationUpdatedAfter(long timestamp) throws OperationManagementException; + Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) + throws OperationManagementException; List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException; @@ -103,12 +100,6 @@ public interface OperationManager { int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException; - /** - * Operation manger implementation can have a push notification stratergy - * @param notificationStrategy eg: mqtt/xmpp - */ - void setNotificationStrategy(NotificationStrategy notificationStrategy); - /** * retrive the push notification strategy. * @return NotificationStrategy diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 735d3e71c4..8f6c3e29db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -105,10 +105,6 @@ public class OperationManagerImpl implements OperationManager { return notificationStrategy; } - public void setNotificationStrategy(NotificationStrategy notificationStrategy) { - this.notificationStrategy = notificationStrategy; - } - public OperationManagerImpl(String deviceType, NotificationStrategy notificationStrategy) { this(deviceType); this.notificationStrategy = notificationStrategy; @@ -557,27 +553,6 @@ public class OperationManagerImpl implements OperationManager { } } - @Override - public void deleteOperation(int operationId) throws OperationManagementException { - try { - OperationManagementDAOFactory.beginTransaction(); - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operation = - operationDAO.getOperation(operationId); - if (operation == null) { - throw new OperationManagementException("Operation not found for operation id : " + operationId); - } - lookupOperationDAO(operation).deleteOperation(operationId); - OperationManagementDAOFactory.commitTransaction(); - } catch (OperationManagementDAOException e) { - OperationManagementDAOFactory.rollbackTransaction(); - throw new OperationManagementException("Error occurred while deleting the operation: " + operationId, e); - } catch (TransactionManagementException e) { - throw new OperationManagementException("Error occurred while initiating a transaction", e); - } finally { - OperationManagementDAOFactory.closeConnection(); - } - } - @Override public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException { @@ -738,62 +713,6 @@ public class OperationManagerImpl implements OperationManager { return operation; } - // @Override - // public Operation getOperationByActivityId(String activity) throws OperationManagementException { - // // This parses the operation id from activity id (ex : ACTIVITY_23) and converts to the integer. - // Operation operation; - // int enrollmentOpMappingId = Integer.parseInt( - // activity.replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, "")); - // if (enrollmentOpMappingId == 0) { - // throw new IllegalArgumentException("Operation ID cannot be null or zero (0)."); - // } - // try { - // OperationManagementDAOFactory.openConnection(); - // org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = - // operationDAO.getOperationFromEnrollment(enrollmentOpMappingId); - // - // if (dtoOperation == null) { - // throw new OperationManagementException("Operation not found for given activity Id:" + activity); - // } - // org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status status = dtoOperation.getStatus(); - // if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { - // org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; - // commandOperation = - // (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - // getOperation(dtoOperation.getId()); - // dtoOperation.setEnabled(commandOperation.isEnabled()); - // } else if (dtoOperation.getType(). - // equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { - // dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); - // } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. - // PROFILE)) { - // dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); - // } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. - // POLICY)) { - // dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); - // } - // operation = OperationDAOUtil.convertOperation(dtoOperation); - // int enrolmentId = operationDAO.getEnrolmentIdFromMappingId(enrollmentOpMappingId); - // if (enrolmentId != 0) { - // operation.setResponses(operationDAO.getOperationResponses(enrolmentId, operation.getId())); - // } - // - // operation.setStatus(Operation.Status.valueOf(status.toString())); - // operation.setActivityId(activity); - // - // } catch (SQLException e) { - // throw new OperationManagementException("Error occurred while opening a connection to the data source", e); - // } catch (OperationManagementDAOException e) { - // throw new OperationManagementException("Error occurred while retrieving the operation with activity Id '" + - // activity, e); - // } finally { - // OperationManagementDAOFactory.closeConnection(); - // } - // - // // return this.getOperation(operationId); - // return operation; - // } - @Override public Activity getOperationByActivityId(String activity) throws OperationManagementException { // This parses the operation id from activity id (ex : ACTIVITY_23) and converts to the integer. @@ -837,11 +756,6 @@ public class OperationManagerImpl implements OperationManager { } } - @Override - public List getOperationUpdatedAfter(long timestamp) throws OperationManagementException { - return null; - } - @Override public List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException { try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index dc9b6dfb9a..58b0083b05 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -33,12 +33,8 @@ public interface OperationDAO { void updateOperation(Operation operation) throws OperationManagementDAOException; - void deleteOperation(int operationId) throws OperationManagementDAOException; - Operation getOperation(int operationId) throws OperationManagementDAOException; - Operation getOperationFromEnrollment(int enrollmentOpMappingId) throws OperationManagementDAOException; - Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException; List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) @@ -66,24 +62,16 @@ public interface OperationDAO { void addOperationResponse(int enrolmentId, int operationId, Object operationResponse) throws OperationManagementDAOException; - List getOperationResponses(int enrolmentId, int operationId) throws OperationManagementDAOException; - Activity getActivity(int operationId) throws OperationManagementDAOException; Activity getActivityByDevice(int operationId, int deviceId) throws OperationManagementDAOException; - int getEnrolmentIdFromMappingId(int enrollmentOpMappingId) throws OperationManagementDAOException; - - List getOperationsUpdatedAfter(long timestamp) throws OperationManagementDAOException; - List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementDAOException; List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException; int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException; - boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException; - /** * This method provides operation mappings for given status * @param opStatus Operation status 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/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index 37be1b7e9d..ac64d6550c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -70,22 +70,6 @@ public class CommandOperationDAOImpl extends GenericOperationDAOImpl { } } - @Override - public void deleteOperation(int id) throws OperationManagementDAOException { - PreparedStatement stmt = null; - try { - Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("DELETE FROM DM_COMMAND_OPERATION WHERE OPERATION_ID = ?"); - stmt.setInt(1, id); - stmt.executeUpdate(); - super.deleteOperation(id); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); - } - } - public CommandOperation getOperation(int id) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; 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 0720d6850c..052a9aeccb 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 @@ -58,22 +58,6 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl { return operationId; } - @Override - public void deleteOperation(int id) throws OperationManagementDAOException { - PreparedStatement stmt = null; - try { - Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("DELETE FROM DM_CONFIG_OPERATION WHERE OPERATION_ID = ?") ; - stmt.setInt(1, id); - stmt.executeUpdate(); - super.deleteOperation(id); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); - } - } - @Override public void updateOperation(Operation operation) throws OperationManagementDAOException { PreparedStatement stmt = null; 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/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 9418cbeee1..7ab5a4bd94 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -249,68 +249,6 @@ public class GenericOperationDAOImpl implements OperationDAO { } } - @Override - public List getOperationResponses(int enrolmentId, int operationId) throws - OperationManagementDAOException { - - PreparedStatement stmt = null; - ResultSet rs = null; - List responces = new ArrayList<>(); - try { - Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT * FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ? AND OPERATION_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrolmentId); - stmt.setInt(2, operationId); - rs = stmt.executeQuery(); - - while (rs.next()) { - OperationResponse response = new OperationResponse(); - response.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); - ByteArrayInputStream bais = null; - ObjectInputStream ois = null; - byte[] contentBytes; - try { - contentBytes = (byte[]) rs.getBytes("OPERATION_RESPONSE"); - bais = new ByteArrayInputStream(contentBytes); - ois = new ObjectInputStream(bais); - response.setResponse(ois.readObject().toString()); - - } finally { - if (bais != null) { - try { - bais.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ByteArrayOutputStream", e); - } - } - if (ois != null) { - try { - ois.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ObjectOutputStream", e); - } - } - } - responces.add(response); - } - - } catch (SQLException e) { - throw new OperationManagementDAOException("SQL Error occurred while retrieving the operation responses for " + - "operation id " + operationId + " and enrolment id " + enrolmentId, e); - } catch (ClassNotFoundException e) { - throw new OperationManagementDAOException("Error occurred while converting the operation responses to string" + - " for operation id " + operationId + " and enrolment id " + enrolmentId, e); - } catch (IOException e) { - throw new OperationManagementDAOException("Error occurred while converting the operation responses to string" + - " for operation id " + operationId + " and enrolment id " + enrolmentId, e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); - } - - return responces; - } - @Override public Activity getActivity(int operationId) throws OperationManagementDAOException { @@ -674,80 +612,6 @@ public class GenericOperationDAOImpl implements OperationDAO { return 0; } - @Override - public int getEnrolmentIdFromMappingId(int enrollmentOpMappingId) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - try { - Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT * FROM DM_ENROLMENT_OP_MAPPING WHERE ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrollmentOpMappingId); - rs = stmt.executeQuery(); - - if (rs.next()) { - return rs.getInt("ENROLMENT_ID"); - } - - } catch (SQLException e) { - throw new OperationManagementDAOException("SQL Error occurred while retrieving the enrolment id " + - " for the mapping id '" + enrollmentOpMappingId, e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); - } - return -1; - } - - @Override - public List getOperationsUpdatedAfter(long timestamp) throws OperationManagementDAOException { - - PreparedStatement stmt = null; - ResultSet rs = null; - List operations = new ArrayList<>(); - try { - Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, OPERATION_CODE " + - "FROM DM_OPERATION AS o \n" + - "INNER JOIN DM_ENROLMENT_OP_MAPPING AS eom ON eom.OPERATION_ID=o.ID WHERE eom.UPDATED_TIMESTAMP = ?"; - stmt = conn.prepareStatement(sql); - stmt.setLong(1, timestamp); - rs = stmt.executeQuery(); - - if (rs.next()) { - Operation operation = new Operation(); - operation.setId(rs.getInt("ID")); - operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); - operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); - operation.setCode(rs.getString("OPERATION_CODE")); - - operations.add(operation); - } - - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while retrieving the operations updated " + - "after a given time", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); - } - return operations; - } - - - @Override - public void deleteOperation(int id) throws OperationManagementDAOException { - PreparedStatement stmt = null; - try { - Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("DELETE FROM DM_OPERATION WHERE ID = ?"); - stmt.setInt(1, id); - stmt.executeUpdate(); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); - } - } - @Override public Operation getOperation(int id) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -783,49 +647,6 @@ public class GenericOperationDAOImpl implements OperationDAO { return operation; } - @Override - public Operation getOperationFromEnrollment(int enrollmentOpMappingId) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - Operation operation = null; - try { - Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT o.ID, TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, OPERATION_CODE, \n" + - " om.STATUS, om.UPDATED_TIMESTAMP FROM DM_OPERATION o \n" + - "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm WHERE dm.ID = ? ) om \n" + - "ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC "; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrollmentOpMappingId); - rs = stmt.executeQuery(); - - if (rs.next()) { - operation = new Operation(); - operation.setId(rs.getInt("ID")); - operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); - operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); -// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { -// operation.setReceivedTimeStamp(""); -// } else { -// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); -// } - if (rs.getLong("UPDATED_TIMESTAMP") == 0) { - operation.setReceivedTimeStamp(""); - } else { - operation.setReceivedTimeStamp( - new java.sql.Timestamp((rs.getLong("UPDATED_TIMESTAMP") * 1000)).toString()); - } - operation.setCode(rs.getString("OPERATION_CODE")); - operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); - OperationDAOUtil.setActivityId(operation, rs.getInt("ID")); - } - } catch (SQLException e) { - throw new OperationManagementDAOException("SQL error occurred while retrieving the operation .", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); - } - return operation; - } - @Override public Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -1177,31 +998,6 @@ public class GenericOperationDAOImpl implements OperationDAO { return operations; } - @Override - public boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException { - boolean status = false; - Connection conn; - PreparedStatement stmt = null; - Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { - conn = OperationManagementDAOFactory.getConnection(); - String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + - "WHERE ENROLMENT_ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setTimestamp(1, currentTimestamp); - stmt.setInt(2, enrolmentId); - stmt.setInt(3, tenantId); - stmt.executeUpdate(); - status = true; - } catch (SQLException e) { - throw new OperationManagementDAOException("Unable to reset the attempt count in database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, null); - } - return status; - } - @Override public Map> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus, int limit) throws OperationManagementDAOException { 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/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java index 4053e7c227..f24cfb3ae3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -122,22 +122,6 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { } } - @Override - public void deleteOperation(int operationId) throws OperationManagementDAOException { - PreparedStatement stmt = null; - try { - Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("DELETE FROM DM_POLICY_OPERATION WHERE OPERATION_ID=?"); - stmt.setInt(1, operationId); - stmt.executeUpdate(); - super.deleteOperation(operationId); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); - } - } - @Override public Operation getOperation(int operationId) throws OperationManagementDAOException { PreparedStatement stmt = null; 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/ProfileOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java index 8bc0a3a9d0..78b88f9855 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java @@ -123,22 +123,6 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { } } - @Override - public void deleteOperation(int id) throws OperationManagementDAOException { - PreparedStatement stmt = null; - try { - Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("DELETE FROM DM_PROFILE_OPERATION WHERE OPERATION_ID=?"); - stmt.setInt(1, id); - stmt.executeUpdate(); - super.deleteOperation(id); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); - } - } - public Operation getOperation(int id) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java index 79ffacb370..371dfa16bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java @@ -83,11 +83,6 @@ public class PushNotificationBasedOperationManager implements OperationManager { this.operationManager.updateOperation(deviceId, operation); } - @Override - public void deleteOperation(int operationId) throws OperationManagementException { - this.operationManager.deleteOperation(operationId); - } - @Override public Operation getOperationByDeviceAndOperationId( DeviceIdentifier deviceId, int operationId) throws OperationManagementException { @@ -121,11 +116,6 @@ public class PushNotificationBasedOperationManager implements OperationManager { return this.operationManager.getOperationByActivityIdAndDevice(activity, deviceId); } - @Override - public List getOperationUpdatedAfter(long timestamp) throws OperationManagementException { - return this.operationManager.getOperationUpdatedAfter(timestamp); - } - @Override public List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException { return this.operationManager.getActivitiesUpdatedAfter(timestamp); @@ -141,11 +131,6 @@ public class PushNotificationBasedOperationManager implements OperationManager { return this.operationManager.getActivityCountUpdatedAfter(timestamp); } - @Override - public void setNotificationStrategy(NotificationStrategy notificationStrategy) { - - } - @Override public NotificationStrategy getNotificationStrategy() { return notificationProvider; 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 ffd559b1ad..151a774554 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 @@ -553,8 +553,6 @@ public interface DeviceManagementProviderService { void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException; - void deleteOperation(String type, int operationId) throws OperationManagementException; - Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException; 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 e91d5eb5ce..a6c077b8fc 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 @@ -1429,11 +1429,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv .updateOperation(deviceId, operation); } - @Override - public void deleteOperation(String type, int operationId) throws OperationManagementException { - pluginRepository.getOperationManager(type, this.getTenantId()).deleteOperation(operationId); - } - @Override public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException { 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 42aec46129..ac332c8e16 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 @@ -240,18 +240,6 @@ public class OperationManagementTests { Assert.assertTrue(operation.getType().equals(Operation.Type.POLICY)); } - - @Test(dependsOnMethods = "getNextPendingOperation", enabled = false) - public void deleteOperation() throws OperationManagementException { - //TODO: Verify the operation management service operations. - DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); - Operation operation = this.operationMgtService.getNextPendingOperation(deviceIdentifier); - this.operationMgtService.deleteOperation(operation.getId()); - Operation operationAfterDeletion = this.operationMgtService.getNextPendingOperation(deviceIdentifier); - Assert.assertTrue(operation.getId() != operation.getId()); - Assert.assertTrue(operationAfterDeletion.getType().equals(Operation.Type.POLICY)); - } - @Test(dependsOnMethods = "getNextPendingOperation") public void getOperationByDeviceAndOperationId() throws OperationManagementException { DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); From dc58a388d626283e0fa9f1514e94a03843e8571c Mon Sep 17 00:00:00 2001 From: sinthuja Date: Wed, 27 Sep 2017 15:36:17 +0530 Subject: [PATCH 4/8] Adding more testcases for the operation management. --- .../OperationManagementServiceProvider.java | 22 --- .../operation/mgt/OperationMgtConstants.java | 2 - ...PushNotificationBasedOperationManager.java | 139 ------------------ .../operation/OperationManagementTests.java | 54 ++++++- 4 files changed, 47 insertions(+), 170 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagementServiceProvider.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagementServiceProvider.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagementServiceProvider.java deleted file mode 100644 index 844abe6801..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagementServiceProvider.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2015, 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.device.mgt.core.operation.mgt; - -public interface OperationManagementServiceProvider { -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java index cd39b73060..b0bd195dee 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java @@ -26,8 +26,6 @@ public class OperationMgtConstants { } public static final String DEVICE_ID_NOT_FOUND = "Device not found for device id: %s"; - public static final String DEVICE_ID_SERVICE_NOT_FOUND = - "Issue in retrieving device management service instance for device found at %s"; } public final class OperationCodes { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java deleted file mode 100644 index 371dfa16bb..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2016, 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.device.mgt.core.push.notification.mgt; - -import org.wso2.carbon.device.mgt.common.*; -import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; -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.OperationManager; -import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; -import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; -import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; - -import java.util.List; - -public class PushNotificationBasedOperationManager implements OperationManager { - - private OperationManager operationManager; - private NotificationStrategy notificationProvider; - - public PushNotificationBasedOperationManager( - OperationManager operationManager, NotificationStrategy notificationProvider) { - this.operationManager = operationManager; - this.notificationProvider = notificationProvider; - } - - @Override - public Activity addOperation(Operation operation, - List devices) throws OperationManagementException, InvalidDeviceException { - Activity activity = this.operationManager.addOperation(operation, devices); - for (DeviceIdentifier deviceId : devices) { - try { - this.notificationProvider.execute(new NotificationContext(deviceId, operation)); - } catch (PushNotificationExecutionFailedException e) { - throw new OperationManagementException("Error occurred while sending push notification to device", e); - } - } - return activity; - } - - @Override - public List getOperations( - DeviceIdentifier deviceId) throws OperationManagementException { - return this.operationManager.getOperations(deviceId); - } - - @Override - public PaginationResult getOperations(DeviceIdentifier deviceId, - PaginationRequest request) throws OperationManagementException { - return this.operationManager.getOperations(deviceId, request); - } - - @Override - public List getPendingOperations( - DeviceIdentifier deviceId) throws OperationManagementException { - return this.operationManager.getPendingOperations(deviceId); - } - - @Override - public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { - return this.operationManager.getNextPendingOperation(deviceId); - } - - @Override - public void updateOperation(DeviceIdentifier deviceId, - Operation operation) throws OperationManagementException { - this.operationManager.updateOperation(deviceId, operation); - } - - @Override - public Operation getOperationByDeviceAndOperationId( - DeviceIdentifier deviceId, int operationId) throws OperationManagementException { - return this.operationManager.getOperationByDeviceAndOperationId(deviceId, operationId); - } - - @Override - public List getOperationsByDeviceAndStatus( - DeviceIdentifier deviceId, - Operation.Status status) throws OperationManagementException { - try { - return this.operationManager.getOperationsByDeviceAndStatus(deviceId, status); - } catch (DeviceManagementException e) { - throw new OperationManagementException("Error occurred while retrieving the list of operations by " + - "device and status", e); - } - } - - @Override - public Operation getOperation(int operationId) throws OperationManagementException { - return this.operationManager.getOperation(operationId); - } - - @Override - public Activity getOperationByActivityId(String activity) throws OperationManagementException { - return this.operationManager.getOperationByActivityId(activity); - } - - @Override - public Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException { - return this.operationManager.getOperationByActivityIdAndDevice(activity, deviceId); - } - - @Override - public List getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException { - return this.operationManager.getActivitiesUpdatedAfter(timestamp); - } - - @Override - public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException { - return this.operationManager.getActivitiesUpdatedAfter(timestamp, limit, offset); - } - - @Override - public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException { - return this.operationManager.getActivityCountUpdatedAfter(timestamp); - } - - @Override - public NotificationStrategy getNotificationStrategy() { - return notificationProvider; - } - -} 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 ac332c8e16..2005268142 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 @@ -18,8 +18,6 @@ package org.wso2.carbon.device.mgt.core.operation; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -66,8 +64,10 @@ import java.util.Date; import java.util.List; import java.util.TimeZone; +/** + * This is the testcase which covers the methods from {@link OperationManager} + */ public class OperationManagementTests { - private static final Log log = LogFactory.getLog(OperationManagementTests.class); private static final String DEVICE_TYPE = "OP_TEST_TYPE"; private static final String DEVICE_ID_PREFIX = "OP-TEST-DEVICE-ID-"; @@ -87,7 +87,6 @@ public class OperationManagementTests { @BeforeClass public void init() throws Exception { DeviceConfigurationManager.getInstance().initConfig(); - log.info("Initializing"); for (int i = 0; i < NO_OF_DEVICES; i++) { deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); } @@ -222,6 +221,11 @@ public class OperationManagementTests { @Test(dependsOnMethods = "getPaginatedRequestAsAdmin") public void updateOperation() throws OperationManagementException { + //This is required to introduce a delay for the update operation of the device. + try { + Thread.sleep(2000); + } catch (InterruptedException ignored) { + } DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); List operations = this.operationMgtService.getPendingOperations(deviceIdentifier); Assert.assertTrue(operations != null && operations.size() == 4); @@ -286,15 +290,51 @@ public class OperationManagementTests { Assert.assertEquals(activity.getActivityStatus().get(0).getStatus(), ActivityStatus.Status.COMPLETED); } - @Test(dependsOnMethods = "updateOperation", enabled = false) - public void getOperationUpdatedAfter() throws OperationManagementException, ParseException { + @Test(dependsOnMethods = "updateOperation") + public void getOperationUpdatedAfterWithLimitAndOffet() throws OperationManagementException, ParseException { String timestamp = this.commandActivity.getCreatedTimeStamp(); SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy"); dateFormat.setTimeZone(TimeZone.getTimeZone("IST")); Date date = dateFormat.parse(timestamp); - List operations = this.operationMgtService.getActivitiesUpdatedAfter(date.getTime()); + List operations = this.operationMgtService.getActivitiesUpdatedAfter(date.getTime() / 1000, 10, 0); + Assert.assertTrue(operations != null && operations.size() == 1, + "The operations updated after the created should be 1"); + Activity operation = operations.get(0); + Assert.assertTrue(operation.getActivityStatus() != null && operation.getActivityStatus().size() == 1, + "The operation should be having the activity status of atleast one device"); + Assert.assertEquals(operation.getActivityStatus().get(0).getDeviceIdentifier().getId(), + deviceIds.get(0).getId()); + Assert.assertEquals(operation.getActivityStatus().get(0).getDeviceIdentifier().getType(), + deviceIds.get(0).getType()); } + @Test(dependsOnMethods = "updateOperation") + public void getOperationUpdatedAfter() throws OperationManagementException, ParseException { + String timestamp = this.commandActivity.getCreatedTimeStamp(); + SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy"); + dateFormat.setTimeZone(TimeZone.getTimeZone("IST")); + Date date = dateFormat.parse(timestamp); + List operations = this.operationMgtService.getActivitiesUpdatedAfter(date.getTime() / 1000); + Assert.assertTrue(operations != null && operations.size() == 1, + "The operations updated after the created should be 1"); + Activity operation = operations.get(0); + Assert.assertTrue(operation.getActivityStatus() != null && operation.getActivityStatus().size() == 1, + "The operation should be having the activity status of atleast one device"); + Assert.assertEquals(operation.getActivityStatus().get(0).getDeviceIdentifier().getId(), + deviceIds.get(0).getId()); + Assert.assertEquals(operation.getActivityStatus().get(0).getDeviceIdentifier().getType(), + deviceIds.get(0).getType()); + } + @Test(dependsOnMethods = "getOperationUpdatedAfter") + public void getActivityCountUpdatedAfter() throws OperationManagementException, ParseException { + String timestamp = this.commandActivity.getCreatedTimeStamp(); + SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy"); + dateFormat.setTimeZone(TimeZone.getTimeZone("IST")); + Date date = dateFormat.parse(timestamp); + int activityCount = this.operationMgtService.getActivityCountUpdatedAfter(date.getTime() / 1000); + Assert.assertTrue(activityCount == 1, + "The activities updated after the created should be 1"); + } } From e57fe2bf37467da76f1d6ec80ccfaaeae2d6e182 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Wed, 27 Sep 2017 16:47:52 +0530 Subject: [PATCH 5/8] Adding more testcases. --- .../operation/OperationManagementTests.java | 25 ++++++++- .../operation/TestNotificationStrategy.java | 51 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/TestNotificationStrategy.java 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 2005268142..bcc4aa114e 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 @@ -33,9 +33,11 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; 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.OperationManager; +import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; +import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; @@ -109,7 +111,8 @@ public class OperationManagementTests { throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); } } - this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE); + NotificationStrategy notificationStrategy = new TestNotificationStrategy(); + this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); } private RegistryService getRegistryService() throws RegistryException { @@ -129,6 +132,21 @@ public class OperationManagementTests { validateOperationResponse(this.commandActivity); } + @Test (expectedExceptions = InvalidDeviceException.class) + public void addEmptyDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), + new ArrayList<>()); + } + + @Test (expectedExceptions = InvalidDeviceException.class) + public void addNonInitializedDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + List deviceIdentifiers = new ArrayList<>(); + deviceIdentifiers.add(deviceIdentifier); + this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), + deviceIdentifiers); + } + @Test(dependsOnMethods = "addCommandOperation") public void addPolicyOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { Activity activity = this.operationMgtService.addOperation(getOperation(new PolicyOperation(), Operation.Type.POLICY, POLICY_OPERATION_CODE), @@ -337,4 +355,9 @@ public class OperationManagementTests { "The activities updated after the created should be 1"); } + @Test + public void getNotificationStrategy(){ + Assert.assertTrue(this.operationMgtService.getNotificationStrategy() != null); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/TestNotificationStrategy.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/TestNotificationStrategy.java new file mode 100644 index 0000000000..28afa2da1f --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/TestNotificationStrategy.java @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2017, 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.device.mgt.core.operation; + +import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; +import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; +import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; + +public class TestNotificationStrategy implements NotificationStrategy { + + @Override + public void init() { + + } + + @Override + public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { + + } + + @Override + public NotificationContext buildContext() { + return null; + } + + @Override + public void undeploy() { + + } + + @Override + public PushNotificationConfig getConfig() { + return null; + } +} From 888d49dac144db34ea2730f290f07cadbc4c6024 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Wed, 27 Sep 2017 16:50:24 +0530 Subject: [PATCH 6/8] Fixing code formatting. --- .../mgt/core/operation/OperationManagementTests.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 bcc4aa114e..e566c207de 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 @@ -132,13 +132,13 @@ public class OperationManagementTests { validateOperationResponse(this.commandActivity); } - @Test (expectedExceptions = InvalidDeviceException.class) + @Test(expectedExceptions = InvalidDeviceException.class) public void addEmptyDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { - this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), + this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), new ArrayList<>()); } - @Test (expectedExceptions = InvalidDeviceException.class) + @Test(expectedExceptions = InvalidDeviceException.class) public void addNonInitializedDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); List deviceIdentifiers = new ArrayList<>(); @@ -356,7 +356,7 @@ public class OperationManagementTests { } @Test - public void getNotificationStrategy(){ + public void getNotificationStrategy() { Assert.assertTrue(this.operationMgtService.getNotificationStrategy() != null); } From ce23e30403a38d3a6ae5958dea97fa5dac96baf6 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Wed, 27 Sep 2017 16:50:57 +0530 Subject: [PATCH 7/8] Removing unused import. --- .../device/mgt/core/operation/OperationManagementTests.java | 1 - 1 file changed, 1 deletion(-) 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 e566c207de..84efb21cad 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 @@ -37,7 +37,6 @@ import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; -import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; From 161d69850fc735dee7f45fee4b583190b0c679df Mon Sep 17 00:00:00 2001 From: sinthuja Date: Wed, 27 Sep 2017 17:02:37 +0530 Subject: [PATCH 8/8] Adding more testcases on operation management. --- .../operation/OperationManagementTests.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) 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 84efb21cad..c99ab40ff1 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 @@ -128,7 +128,7 @@ public class OperationManagementTests { public void addCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { this.commandActivity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), this.deviceIds); - validateOperationResponse(this.commandActivity); + validateOperationResponse(this.commandActivity, ActivityStatus.Status.PENDING); } @Test(expectedExceptions = InvalidDeviceException.class) @@ -146,25 +146,37 @@ public class OperationManagementTests { deviceIdentifiers); } + @Test + public void addNonAdminUserDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, + InvalidDeviceException { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID, true); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(NON_ADMIN_USER); + Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), + deviceIds); + PrivilegedCarbonContext.endTenantFlow(); + validateOperationResponse(activity, ActivityStatus.Status.UNAUTHORIZED); + } + @Test(dependsOnMethods = "addCommandOperation") public void addPolicyOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { Activity activity = this.operationMgtService.addOperation(getOperation(new PolicyOperation(), Operation.Type.POLICY, POLICY_OPERATION_CODE), this.deviceIds); - validateOperationResponse(activity); + validateOperationResponse(activity, ActivityStatus.Status.PENDING); } @Test(dependsOnMethods = "addPolicyOperation") public void addConfigOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { Activity activity = this.operationMgtService.addOperation(getOperation(new ConfigOperation(), Operation.Type.CONFIG, CONFIG_OPERATION_CODE), this.deviceIds); - validateOperationResponse(activity); + validateOperationResponse(activity, ActivityStatus.Status.PENDING); } @Test(dependsOnMethods = "addConfigOperation") public void addProfileOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { Activity activity = this.operationMgtService.addOperation(getOperation(new ProfileOperation(), Operation.Type.PROFILE, PROFILE_OPERATION_CODE), this.deviceIds); - validateOperationResponse(activity); + validateOperationResponse(activity, ActivityStatus.Status.PENDING); } private Operation getOperation(Operation operation, Operation.Type type, String code) { @@ -175,11 +187,11 @@ public class OperationManagementTests { return operation; } - private void validateOperationResponse(Activity activity) { + private void validateOperationResponse(Activity activity, ActivityStatus.Status expectedStatus) { Assert.assertEquals(activity.getActivityStatus().size(), NO_OF_DEVICES, "The operation reponse for add operation only have - " + activity.getActivityStatus().size()); for (ActivityStatus status : activity.getActivityStatus()) { - Assert.assertEquals(status.getStatus(), ActivityStatus.Status.PENDING); + Assert.assertEquals(status.getStatus(), expectedStatus); } }