DeviceManagementServiceImpl implements DeviceManagementService { @PUT @Path("/{type}/{id}/changestatus") public Response changeDeviceStatus(@PathParam("type") @Size(max = 45) String type, @PathParam("id") @Size(max = 45) String id, @QueryParam("newStatus") EnrolmentInfo.Status newsStatus) { RequestValidationUtil.validateDeviceIdentifier(type, id); DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(id, type); Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, false); if (persistedDevice == null) { return Response.status(Response.Status.NOT_FOUND).build(); } boolean response = deviceManagementProviderService.changeDeviceStatus(deviceIdentifier, newsStatus); return Response.status(Response.Status.OK).entity(response).build(); } catch (DeviceManagementException e) { String msg = "Error occurred while changing device status of type : " + type + " and " + "device id : " + id; log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } }  @GET @Path("/{type}/{id}/status") @Override Response isEnrolled(@PathParam("type") String type, @PathParam("id") String id); @GET @Override Response getDevices(
            @QueryParam("name") String name,
            @QueryParam("type") String type,
            @QueryParam("user") String user,
            @QueryParam("userPattern") String userPattern,
            @QueryParam("role") String role,
            @QueryParam("ownership") String ownership,
            @QueryParam("status") String status,
            @QueryParam("groupId") int groupId,
            @QueryParam("since") String since,
            @HeaderParam("If-Modified-Since") String ifModifiedSince,
            @QueryParam("requireDeviceInfo") boolean requireDeviceInfo,
            @QueryParam("offset") int offset,
            @QueryParam("limit") int limit); @GET @Override @Path("/user-devices") Response getDeviceByUser(@QueryParam("requireDeviceInfo") boolean requireDeviceInfo,
                                    @QueryParam("offset") int offset,
                                    @QueryParam("limit") int limit); @DELETE @Override @Path("/type/{device-type}/id/{device-id}") Response deleteDevice(@PathParam("device-type") String deviceType, @PathParam("device-id") String deviceId); @POST @Override @Path("/type/{device-type}/id/{device-id}/rename") Response renameDevice(Device device, @PathParam("device-type") String deviceType,
                                 @PathParam("device-id") String deviceId); @GET @Path("/{type}/{id}") @Override Response getDevice(
            @PathParam("type") @Size(max = 45) String type,
            @PathParam("id") @Size(max = 45) String id,
            @QueryParam("owner") @Size(max = 100) String owner,
            @HeaderParam("If-Modified-Since") String ifModifiedSince); @GET @Path("/{type}/{id}/location") @Override Response getDeviceLocation(
            @PathParam("type") @Size(max = 45) String type,
            @PathParam("id") @Size(max = 45) String id,
            @HeaderParam("If-Modified-Since") String ifModifiedSince); @GET @Path("/{type}/{id}/info") @Override Response getDeviceInformation(
            @PathParam("type") @Size(max = 45) String type,
            @PathParam("id") @Size(max = 45) String id,
            @HeaderParam("If-Modified-Since") String ifModifiedSince); @GET @Path("/{type}/{id}/features") @Override Response getFeaturesOfDevice(
            @PathParam("type") @Size(max = 45) String type,
            @PathParam("id") @Size(max = 45) String id,
            @HeaderParam("If-Modified-Since") String ifModifiedSince); @POST @Path("/search-devices") @Override Response searchDevices(@QueryParam("offset") int offset,
                                  @QueryParam("limit") int limit, SearchContext searchContext); @GET @Path("/{type}/{id}/applications") @Override Response getInstalledApplications(
            @PathParam("type") @Size(max = 45) String type,
            @PathParam("id") @Size(max = 45) String id,
            @HeaderParam("If-Modified-Since") String ifModifiedSince,
            @QueryParam("offset") int offset,
            @QueryParam("limit") int limit); @GET @Path("/{type}/{id}/operations") @Override Response getDeviceOperations(
            @PathParam("type") @Size(max = 45) String type,
            @PathParam("id") @Size(max = 45) String id,
            @HeaderParam("If-Modified-Since") String ifModifiedSince,
            @QueryParam("offset") int offset,
            @QueryParam("limit") int limit,
            @QueryParam("owner") String owner); @GET @Path("/{type}/{id}/effective-policy") @Override Response getEffectivePolicyOfDevice(@PathParam("type") @Size(max = 45) String type,
                                               @PathParam("id") @Size(max = 45) String id,
                                               @HeaderParam("If-Modified-Since") String ifModifiedSince); @GET @Path("{type}/{id}/compliance-data") Response getComplianceDataOfDevice(@PathParam("type") @Size(max = 45) String type,
                                              @PathParam("id") @Size(max = 45) String id); @PUT @Path("/{type}/{id}/changestatus") Response changeDeviceStatus(@PathParam("type") @Size(max = 45) String type,
                                       @PathParam("id") @Size(max = 45) String id,
                                       @QueryParam("newStatus") EnrolmentInfo.Status newsStatus); @POST @Path("/{type}/operations") Response addOperation(@PathParam("type") String type, @Valid OperationRequest operationRequest); static final String DATE_FORMAT_NOW; }
@Test(description = "Testing changing device status") public void testChangeDeviceStatus() { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Response response = this.deviceManagementService .changeDeviceStatus(TEST_DEVICE_TYPE, UUID.randomUUID().toString(), EnrolmentInfo.Status.INACTIVE); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); }
@Test(description = "Testing changing device status when unable to change device status") public void testChangeDeviceStatusWhenUnableToChangeStatus() throws DeviceManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Mockito.when(this.deviceManagementProviderService .changeDeviceStatus(Mockito.any(DeviceIdentifier.class), Mockito.any())) .thenThrow(new DeviceManagementException()); Response response = this.deviceManagementService .changeDeviceStatus(TEST_DEVICE_TYPE, UUID.randomUUID().toString(), EnrolmentInfo.Status.ACTIVE); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }