DeviceManagementServiceImpl implements DeviceManagementService { @POST @Path("/search-devices") @Override public Response searchDevices(@QueryParam("offset") int offset, @QueryParam("limit") int limit, SearchContext searchContext) { SearchManagerService searchManagerService; List<Device> devices; DeviceList deviceList = new DeviceList(); try { searchManagerService = DeviceMgtAPIUtils.getSearchManagerService(); devices = searchManagerService.search(searchContext); } catch (SearchMgtException e) { String msg = "Error occurred while searching for devices that matches the provided selection criteria"; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } deviceList.setList(devices); deviceList.setCount(devices.size()); return Response.status(Response.Status.OK).entity(deviceList).build(); }  @GET @Path("/{type}/{id}/status") @Override Response isEnrolled(@PathParam("type") String type, @PathParam("id") String id); @GET @Deprecated @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("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") @Deprecated @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); @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 search devices") public void testSearchDevices() { SearchManagerService searchManagerService = Mockito.mock(SearchManagerServiceImpl.class, Mockito.RETURNS_MOCKS); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getSearchManagerService")) .toReturn(searchManagerService); Response response = this.deviceManagementService .searchDevices(10, 5, new SearchContext()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "Expects to return HTTP 200 when the search is successful"); }
@Test(description = "Testing search devices when unable to search devices") public void testSearchDevicesException() throws SearchMgtException { SearchManagerService searchManagerService = Mockito.mock(SearchManagerServiceImpl.class, Mockito.RETURNS_MOCKS); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getSearchManagerService")) .toReturn(searchManagerService); Mockito.when(searchManagerService.search(Mockito.any(SearchContext.class))).thenThrow(new SearchMgtException()); Response response = this.deviceManagementService .searchDevices(10, 5, new SearchContext()); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Expects HTTP 500 when an exception occurred while searching the device"); }