CategoryResource extends AbstractResource { @GET @Produces(APPLICATION_JSON) public Response get(@PathParam("categoryId") String categoryId) { CategoryEntity category = categoryService.findNotHiddenById(categoryId); Set<ApiEntity> apis = apiService.findPublishedByUser(getAuthenticatedUserOrNull()); category.setTotalApis(categoryService.getTotalApisByCategory(apis, category)); return Response .ok(categoryMapper.convert(category, uriInfo.getBaseUriBuilder())) .build(); }  @GET @Produces(APPLICATION_JSON) Response get(@PathParam("categoryId") String categoryId); @GET @Path("picture") Response picture(@Context Request request, @PathParam("categoryId") String categoryId); @GET @Path("background") Response background(@Context Request request, @PathParam("categoryId") String categoryId);  }
@Test public void shouldGetCategory() { final Response response = target(CATEGORY_ID).request().get(); assertEquals(OK_200, response.getStatus()); Mockito.verify(categoryService).findNotHiddenById(CATEGORY_ID); Mockito.verify(apiService).findPublishedByUser(USER_NAME); Mockito.verify(categoryService).getTotalApisByCategory(any(), any()); Mockito.verify(categoryMapper).convert(any(), any()); final Category responseCategory = response.readEntity(Category.class); assertNotNull(responseCategory); }
@Test public void shouldNotGetCategory() { doThrow(new CategoryNotFoundException(UNKNOWN_CATEGORY)).when(categoryService).findNotHiddenById(UNKNOWN_CATEGORY); final Response response = target(UNKNOWN_CATEGORY).request().get(); assertEquals(NOT_FOUND_404, response.getStatus()); ErrorResponse errorResponse = response.readEntity(ErrorResponse.class); List<Error> errors = errorResponse.getErrors(); assertNotNull(errors); assertEquals(1, errors.size()); Error error = errors.get(0); assertNotNull(error); assertEquals("errors.category.notFound", error.getCode()); assertEquals("404", error.getStatus()); assertEquals("Category ["+UNKNOWN_CATEGORY+"] can not be found.", error.getMessage()); }
@Test public void shouldGetCategoryPicture() throws IOException { final Response response = target(CATEGORY_ID).path("picture").request().get(); assertEquals(OK_200, response.getStatus()); MultivaluedMap<String, Object> headers = response.getHeaders(); String contentType = (String) headers.getFirst(HttpHeader.CONTENT_TYPE.asString()); String etag = (String) headers.getFirst("ETag"); assertEquals(mockImage.getType(), contentType); File result = response.readEntity(File.class); byte[] fileContent = Files.readAllBytes(Paths.get(result.getAbsolutePath())); assertTrue(Arrays.equals(fileContent, apiLogoContent)); String expectedTag = '"'+Integer.toString(new String(fileContent).hashCode())+'"'; assertEquals(expectedTag, etag); final Response cachedResponse = target(CATEGORY_ID).path("picture").request().header(HttpHeader.IF_NONE_MATCH.asString(), etag).get(); assertEquals(NOT_MODIFIED_304, cachedResponse.getStatus()); }