AdminUserService extends Service { @GET @GenerateLink(rel = "get all users") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get all users", notes = "Get all users in the system") @ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 400, message = "Bad Request"), @ApiResponse(code = 500, message = "Internal Server Error") }) public Response getAll( @ApiParam(value = "Max items") @QueryParam("maxItems") @DefaultValue("30") int maxItems, @ApiParam(value = "Skip count") @QueryParam("skipCount") @DefaultValue("0") int skipCount) throws ServerException, BadRequestException { try { final Page<? extends User> usersPage = userManager.getAll(maxItems, skipCount); return Response.ok() .entity( usersPage.getItems( user -> linksInjector.injectLinks(DtoConverter.asDto(user), getServiceContext()))) .header("Link", createLinkHeader(usersPage)) .build(); } catch (IllegalArgumentException e) { throw new BadRequestException(e.getMessage()); } } @Inject  AdminUserService(UserManager userManager, UserLinksInjector linksInjector); @GET @GenerateLink(rel = "get all users") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get all users", notes = "Get all users in the system") @ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 400, message = "Bad Request"), @ApiResponse(code = 500, message = "Internal Server Error") }) Response getAll(
      @ApiParam(value = "Max items") @QueryParam("maxItems") @DefaultValue("30") int maxItems,
      @ApiParam(value = "Skip count") @QueryParam("skipCount") @DefaultValue("0") int skipCount); @GET @Path("/find") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get all users whose e-mail/name contains(case insensitive) given part") @ApiResponses({ @ApiResponse(code = 200, message = "The response contains searching result"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 500, message = "Internal server error occurred") }) Response find(
      @ApiParam("User e-mail fragment") @QueryParam("emailPart") String emailPart,
      @ApiParam("User name fragment") @QueryParam("namePart") String namePart,
      @ApiParam(value = "Max items") @QueryParam("maxItems") @DefaultValue("30") int maxItems,
      @ApiParam(value = "Skip count") @QueryParam("skipCount") @DefaultValue("0") int skipCount);  }
@Test public void shouldReturnAllUsers() throws Exception { UserImpl testUser = new UserImpl("test_id", "test@email", "name"); when(userManager.getAll(anyInt(), anyLong())) .thenReturn(new Page<>(singletonList(testUser), 0, 1, 1)); final Response response = given() .auth() .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) .when() .get(SECURE_PATH + "/admin/user?maxItems=3&skipCount=4"); assertEquals(response.getStatusCode(), OK.getStatusCode()); verify(userManager).getAll(3, 4); List<UserDto> users = DtoFactory.getInstance().createListDtoFromJson(response.getBody().print(), UserDto.class); assertEquals(users.size(), 1); final UserDto fetchedUser = users.get(0); assertEquals(fetchedUser.getId(), testUser.getId()); assertEquals(fetchedUser.getEmail(), testUser.getEmail()); }
@Test public void shouldThrowServerErrorIfDaoThrowException() throws Exception { when(userManager.getAll(anyInt(), anyLong())).thenThrow(new ServerException("some error")); final Response response = given() .auth() .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) .when() .get(SECURE_PATH + "/admin/user"); assertEquals(response.getStatusCode(), INTERNAL_SERVER_ERROR.getStatusCode()); final ServiceError serviceError = DtoFactory.getInstance().createDtoFromJson(response.body().print(), ServiceError.class); assertEquals(serviceError.getMessage(), "some error"); }