Search in sources :

Example 16 with CategoryEntity

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();
}
Also used : UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 17 with CategoryEntity

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());
}
Also used : Category(io.gravitee.rest.api.portal.rest.model.Category) Instant(java.time.Instant) CategoryLinks(io.gravitee.rest.api.portal.rest.model.CategoryLinks) Date(java.util.Date) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) Test(org.junit.Test)

Example 18 with CategoryEntity

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();
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 19 with CategoryEntity

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());
}
Also used : Response(javax.ws.rs.core.Response) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) ApiLifecycleState(io.gravitee.rest.api.model.api.ApiLifecycleState) io.gravitee.rest.api.portal.rest.model(io.gravitee.rest.api.portal.rest.model) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Test(org.junit.Test) Error(io.gravitee.rest.api.portal.rest.model.Error) Collectors(java.util.stream.Collectors) Entity(javax.ws.rs.client.Entity) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Mockito(org.mockito.Mockito) ArgumentCaptor(org.mockito.ArgumentCaptor) Response(javax.ws.rs.core.Response) Assert(org.junit.Assert) Mockito.doReturn(org.mockito.Mockito.doReturn) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) Before(org.junit.Before) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) Test(org.junit.Test)

Example 20 with CategoryEntity

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());
}
Also used : Response(javax.ws.rs.core.Response) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) ApiLifecycleState(io.gravitee.rest.api.model.api.ApiLifecycleState) io.gravitee.rest.api.portal.rest.model(io.gravitee.rest.api.portal.rest.model) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Test(org.junit.Test) Error(io.gravitee.rest.api.portal.rest.model.Error) Collectors(java.util.stream.Collectors) Entity(javax.ws.rs.client.Entity) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Mockito(org.mockito.Mockito) ArgumentCaptor(org.mockito.ArgumentCaptor) Response(javax.ws.rs.core.Response) Assert(org.junit.Assert) Mockito.doReturn(org.mockito.Mockito.doReturn) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) Before(org.junit.Before) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) Test(org.junit.Test)

Aggregations

CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)29 Test (org.junit.Test)14 UpdateCategoryEntity (io.gravitee.rest.api.model.UpdateCategoryEntity)13 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)12 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)12 Before (org.junit.Before)11 Assert (org.junit.Assert)9 Mockito (org.mockito.Mockito)9 Category (io.gravitee.repository.management.model.Category)7 NewCategoryEntity (io.gravitee.rest.api.model.NewCategoryEntity)7 java.util (java.util)7 Collectors (java.util.stream.Collectors)7 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)6 ApiLifecycleState (io.gravitee.rest.api.model.api.ApiLifecycleState)6 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)6 FilteredEntities (io.gravitee.rest.api.model.filtering.FilteredEntities)6 io.gravitee.rest.api.portal.rest.model (io.gravitee.rest.api.portal.rest.model)6 Error (io.gravitee.rest.api.portal.rest.model.Error)6 Entity (javax.ws.rs.client.Entity)6 Response (javax.ws.rs.core.Response)6