Adding more testcases.

revert-70aa11f8
sinthuja 7 years ago
parent 122ae96376
commit f42b760884

@ -344,18 +344,22 @@ public class OperationManagerImpl implements OperationManager {
PaginationResult paginationResult = null; PaginationResult paginationResult = null;
List<Operation> operations = new ArrayList<>(); List<Operation> operations = new ArrayList<>();
String owner = request.getOwner(); 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)) { if (!isActionAuthorized(deviceId)) {
throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" +
deviceId.getType() + "' device, which carries the identifier '" + deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "' of owner '" + owner + "'"); deviceId.getId() + "' of owner '" + owner + "'");
} }
EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId, 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(); int enrolmentId = enrolmentInfo.getId();
try { try {
OperationManagementDAOFactory.openConnection(); OperationManagementDAOFactory.openConnection();

@ -474,6 +474,13 @@ public final class DeviceManagerUtil {
return true; 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() { private static CacheManager getCacheManager() {
return Caching.getCacheManagerFactory().getCacheManager(DeviceManagementConstants.DM_CACHE_MANAGER); return Caching.getCacheManagerFactory().getCacheManager(DeviceManagementConstants.DM_CACHE_MANAGER);
} }

@ -68,6 +68,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
private static final int NO_OF_DEVICES = 5; private static final int NO_OF_DEVICES = 5;
private static final String ADMIN_USER = "admin"; private static final String ADMIN_USER = "admin";
private static final String NON_ADMIN_USER = "test"; private static final String NON_ADMIN_USER = "test";
private static final String INVALID_DEVICE = "ThisIsInvalid";
private List<DeviceIdentifier> deviceIds = new ArrayList<>(); private List<DeviceIdentifier> deviceIds = new ArrayList<>();
private OperationManager operationMgtService; private OperationManager operationMgtService;
@ -113,7 +114,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
try { try {
ArrayList<DeviceIdentifier> invalidDevices = new ArrayList<>(); ArrayList<DeviceIdentifier> invalidDevices = new ArrayList<>();
for (int i = 0; i < 3; i++) { 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); invalidDevices.addAll(this.deviceIds);
Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(),
@ -461,4 +462,27 @@ public class OperationManagementTests extends BaseDeviceManagementTest {
Assert.assertTrue(operations == null); 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));
}
} }

Loading…
Cancel
Save