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 772c5268e59..fda94e31119 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 @@ -344,18 +344,22 @@ public class OperationManagerImpl implements OperationManager { PaginationResult paginationResult = null; List operations = new ArrayList<>(); String owner = request.getOwner(); + try { + if (!DeviceManagerUtil.isDeviceExists(deviceId)) { + throw new OperationManagementException("Device not found for given device " + + "Identifier:" + deviceId.getId() + " and given type : " + + deviceId.getType()); + } + } catch (DeviceManagementException e) { + throw new OperationManagementException("Error while checking the existence of the device identifier - " + + deviceId.getId() + " of the device type - " + deviceId.getType(), e); + } 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 + "'"); } - 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()); - } int enrolmentId = enrolmentInfo.getId(); try { OperationManagementDAOFactory.openConnection(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 19ab94ca8a6..bfb48267621 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -474,6 +474,13 @@ public final class DeviceManagerUtil { return true; } + public static boolean isDeviceExists(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { + Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier, + false); + return !(device == null || device.getDeviceIdentifier() == null || + device.getDeviceIdentifier().isEmpty() || device.getEnrolmentInfo() == null); + } + private static CacheManager getCacheManager() { return Caching.getCacheManagerFactory().getCacheManager(DeviceManagementConstants.DM_CACHE_MANAGER); } 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 b26a5125478..c06f4ae70f3 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 @@ -68,6 +68,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { private static final int NO_OF_DEVICES = 5; private static final String ADMIN_USER = "admin"; private static final String NON_ADMIN_USER = "test"; + private static final String INVALID_DEVICE = "ThisIsInvalid"; private List deviceIds = new ArrayList<>(); private OperationManager operationMgtService; @@ -113,7 +114,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { try { ArrayList invalidDevices = new ArrayList<>(); for (int i = 0; i < 3; i++) { - invalidDevices.add(new DeviceIdentifier("12345" + i, DEVICE_TYPE)); + invalidDevices.add(new DeviceIdentifier(INVALID_DEVICE + i, DEVICE_TYPE)); } invalidDevices.addAll(this.deviceIds); Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), @@ -461,4 +462,27 @@ public class OperationManagementTests extends BaseDeviceManagementTest { Assert.assertTrue(operations == null); } + @Test(dependsOnMethods = "getOperationForInactiveDevice", expectedExceptions = OperationManagementException.class) + public void getPaginatedOperationDeviceForInvalidDevice() throws DeviceManagementException, OperationManagementException { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID, true); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(ADMIN_USER); + try { + PaginationRequest request = new PaginationRequest(1, 2); + request.setDeviceType(DEVICE_TYPE); + request.setOwner(ADMIN_USER); + PaginationResult result = this.operationMgtService.getOperations(new DeviceIdentifier(INVALID_DEVICE, DEVICE_TYPE), request); + Assert.assertEquals(result.getRecordsFiltered(), 4); + Assert.assertEquals(result.getData().size(), 2); + Assert.assertEquals(result.getRecordsTotal(), 4); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + } + + @Test(dependsOnMethods = "getOperationForInactiveDevice", expectedExceptions = OperationManagementException.class) + public void getPendingOperationDeviceForInvalidDevice() throws DeviceManagementException, OperationManagementException { + this.operationMgtService.getPendingOperations(new DeviceIdentifier(INVALID_DEVICE, DEVICE_TYPE)); + } + }