use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryResource method getImageResponse.
private Response getImageResponse(Request request, InlinePictureEntity image) {
boolean canShowCategory = hasPermission(RolePermission.ENVIRONMENT_CATEGORY, RolePermissionAction.READ);
CategoryEntity category = categoryService.findById(categoryId);
if (!canShowCategory && category.isHidden()) {
throw new UnauthorizedAccessException();
}
CacheControl cc = new CacheControl();
cc.setNoTransform(true);
cc.setMustRevalidate(false);
cc.setNoCache(false);
cc.setMaxAge(86400);
if (image == null || image.getContent() == null) {
return Response.ok().build();
}
EntityTag etag = new EntityTag(Integer.toString(new String(image.getContent()).hashCode()));
Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
if (builder != null) {
// Preconditions are not met, returning HTTP 304 'not-modified'
return builder.cacheControl(cc).build();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(image.getContent(), 0, image.getContent().length);
return Response.ok(baos).cacheControl(cc).tag(etag).type(image.getType()).build();
}
use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryMapperTest method testConvert.
@Test
public void testConvert() {
Instant now = Instant.now();
Date nowDate = Date.from(now);
CategoryEntity categoryEntity = new CategoryEntity();
categoryEntity.setCreatedAt(nowDate);
categoryEntity.setDescription(CATEGORY_DESCRIPTION);
categoryEntity.setHidden(true);
categoryEntity.setHighlightApi(CATEGORY_HIGHLIGHT_API);
categoryEntity.setId(CATEGORY_ID);
categoryEntity.setKey(CATEGORY_KEY);
categoryEntity.setName(CATEGORY_NAME);
categoryEntity.setOrder(11);
categoryEntity.setPage(CATEGORY_PAGE);
categoryEntity.setPicture(CATEGORY_PICTURE);
categoryEntity.setPictureUrl(CATEGORY_PICTURE_URL);
categoryEntity.setTotalApis(42);
categoryEntity.setUpdatedAt(nowDate);
categoryEntity.setPicture(CATEGORY_BACKGROUND);
categoryEntity.setPictureUrl(CATEGORY_BACKGROUND_URL);
// init
Category category = categoryMapper.convert(categoryEntity, UriBuilder.fromPath(CATEGORY_BASE_URL));
assertEquals(CATEGORY_DESCRIPTION, category.getDescription());
assertEquals(CATEGORY_KEY, category.getId());
assertEquals(CATEGORY_NAME, category.getName());
assertEquals(CATEGORY_PAGE, category.getPage());
assertEquals(11, category.getOrder().intValue());
assertEquals(42, category.getTotalApis().longValue());
CategoryLinks links = category.getLinks();
assertNotNull(links);
assertEquals(CATEGORY_BASE_URL + "/environments/DEFAULT/apis/" + CATEGORY_HIGHLIGHT_API, links.getHighlightedApi());
assertEquals(CATEGORY_BASE_URL + "/environments/DEFAULT/categories/" + CATEGORY_ID + "/picture?" + nowDate.getTime(), links.getPicture());
assertEquals(CATEGORY_BASE_URL + "/environments/DEFAULT/categories/" + CATEGORY_ID + "/background?" + nowDate.getTime(), links.getBackground());
assertEquals(CATEGORY_BASE_URL + "/environments/DEFAULT/categories/" + CATEGORY_ID, links.getSelf());
}
use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryResource method get.
@GET
@Produces(APPLICATION_JSON)
@RequirePortalAuth
public Response get(@PathParam("categoryId") String categoryId) {
CategoryEntity category = categoryService.findNotHiddenById(categoryId);
// FIXME: retrieve all the apis of the user can be heavy because it involves a lot of data fetching. Find a way to just retrieve only necessary data.
Set<ApiEntity> apis = apiService.findPublishedByUser(getAuthenticatedUserOrNull());
category.setTotalApis(categoryService.getTotalApisByCategory(apis, category));
return Response.ok(categoryMapper.convert(category, uriInfo.getBaseUriBuilder())).build();
}
use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class ApisResourceTest method shouldHavePromotedApiIfCategoryWithHighLightedApi.
@Test
public void shouldHavePromotedApiIfCategoryWithHighLightedApi() throws TechnicalException {
CategoryEntity myCatEntity = new CategoryEntity();
myCatEntity.setHighlightApi("4");
doReturn(myCatEntity).when(categoryService).findById("myCat");
final Response response = target().queryParam("size", 3).queryParam("promoted", true).queryParam("category", "myCat").request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
ArgumentCaptor<ApiEntity> apiEntityCaptor = ArgumentCaptor.forClass(ApiEntity.class);
Mockito.verify(apiMapper, Mockito.times(1)).convert(apiEntityCaptor.capture());
final List<String> allNameValues = apiEntityCaptor.getAllValues().stream().map(a -> a.getName()).collect(Collectors.toList());
assertEquals(1, allNameValues.size());
assertTrue(allNameValues.containsAll(Arrays.asList("4")));
ApisResponse apiResponse = response.readEntity(ApisResponse.class);
assertEquals(1, apiResponse.getData().size());
}
use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class ApisResourceTest method shouldHavePromotedApiIfCategoryWithHighLightedApiNotInFilteredList.
@Test
public void shouldHavePromotedApiIfCategoryWithHighLightedApiNotInFilteredList() throws TechnicalException {
CategoryEntity myCatEntity = new CategoryEntity();
myCatEntity.setHighlightApi("7");
doReturn(myCatEntity).when(categoryService).findById("myCat");
final Response response = target().queryParam("size", 3).queryParam("promoted", true).queryParam("category", "myCat").request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
ArgumentCaptor<ApiEntity> apiEntityCaptor = ArgumentCaptor.forClass(ApiEntity.class);
Mockito.verify(apiMapper, Mockito.times(1)).convert(apiEntityCaptor.capture());
final List<String> allNameValues = apiEntityCaptor.getAllValues().stream().map(a -> a.getName()).collect(Collectors.toList());
assertEquals(1, allNameValues.size());
assertTrue(allNameValues.containsAll(Arrays.asList("1")));
ApisResponse apiResponse = response.readEntity(ApisResponse.class);
assertEquals(1, apiResponse.getData().size());
}
Aggregations