Search in sources :

Example 11 with CategoryEntity

use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.

the class ApisResourceTest method shouldHaveAllButPromotedApiIfCategoryWithoutHighLightedApi.

@Test
public void shouldHaveAllButPromotedApiIfCategoryWithoutHighLightedApi() throws TechnicalException {
    doReturn(new CategoryEntity()).when(categoryService).findById("myCat");
    final Response response = target().queryParam("size", 3).queryParam("promoted", false).queryParam("category", "myCat").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<ApiEntity> apiEntityCaptor = ArgumentCaptor.forClass(ApiEntity.class);
    Mockito.verify(apiMapper, Mockito.times(4)).convert(apiEntityCaptor.capture());
    final List<String> allNameValues = apiEntityCaptor.getAllValues().stream().map(a -> a.getName()).collect(Collectors.toList());
    assertEquals(4, allNameValues.size());
    assertTrue(allNameValues.containsAll(Arrays.asList("3", "4", "5", "6")));
    ApisResponse apiResponse = response.readEntity(ApisResponse.class);
    assertEquals(3, 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 12 with CategoryEntity

use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.

the class ApisResourceTest method shouldHaveAllButPromotedApiIfCategoryWithHighLightedApiNotInFilteredList.

@Test
public void shouldHaveAllButPromotedApiIfCategoryWithHighLightedApiNotInFilteredList() throws TechnicalException {
    CategoryEntity myCatEntity = new CategoryEntity();
    myCatEntity.setHighlightApi("7");
    doReturn(myCatEntity).when(categoryService).findById("myCat");
    final Response response = target().queryParam("size", 3).queryParam("promoted", false).queryParam("category", "myCat").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<ApiEntity> apiEntityCaptor = ArgumentCaptor.forClass(ApiEntity.class);
    Mockito.verify(apiMapper, Mockito.times(4)).convert(apiEntityCaptor.capture());
    final List<String> allNameValues = apiEntityCaptor.getAllValues().stream().map(a -> a.getName()).collect(Collectors.toList());
    assertEquals(4, allNameValues.size());
    assertTrue(allNameValues.containsAll(Arrays.asList("3", "4", "5", "6")));
    ApisResponse apiResponse = response.readEntity(ApisResponse.class);
    assertEquals(3, 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 13 with CategoryEntity

use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.

the class CategoryService_UpdateTest method shouldUpdateCategory_single_mode.

@Test
public void shouldUpdateCategory_single_mode() throws TechnicalException {
    UpdateCategoryEntity mockCategory = mock(UpdateCategoryEntity.class);
    when(mockCategory.getId()).thenReturn("category-id");
    when(mockCategory.getName()).thenReturn("Category ID");
    when(mockCategoryRepository.findById("category-id")).thenReturn(Optional.of(new Category()));
    Category updatedCategory = mock(Category.class);
    when(updatedCategory.getId()).thenReturn("category-id");
    when(updatedCategory.getName()).thenReturn("category-name");
    when(updatedCategory.getDescription()).thenReturn("category-description");
    when(updatedCategory.getOrder()).thenReturn(1);
    when(updatedCategory.isHidden()).thenReturn(true);
    when(updatedCategory.getUpdatedAt()).thenReturn(new Date(1234567890L));
    when(updatedCategory.getCreatedAt()).thenReturn(new Date(9876543210L));
    when(mockCategoryRepository.update(argThat(cat -> cat.getUpdatedAt() != null))).thenReturn(updatedCategory);
    CategoryEntity category = categoryService.update("category-id", mockCategory);
    assertNotNull(category);
    assertEquals("Id", "category-id", category.getId());
    assertEquals("Name", "category-name", category.getName());
    assertEquals("Description", "category-description", category.getDescription());
    assertEquals("Total APIs", 0, category.getTotalApis());
    assertEquals("Order", 1, category.getOrder());
    assertEquals("Hidden", true, category.isHidden());
    assertEquals("UpdatedAt", new Date(1234567890L), category.getUpdatedAt());
    assertEquals("CreatedAt", new Date(9876543210L), category.getCreatedAt());
    verify(mockCategoryRepository, times(1)).findById(any());
    verify(mockCategoryRepository, times(1)).update(any());
    verify(mockAuditService, times(1)).createEnvironmentAuditLog(any(), eq(CATEGORY_UPDATED), any(), any(), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) CategoryServiceImpl(io.gravitee.rest.api.service.impl.CategoryServiceImpl) Date(java.util.Date) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) AuditService(io.gravitee.rest.api.service.AuditService) Test(org.junit.Test) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) Collections.singletonList(java.util.Collections.singletonList) Mockito(org.mockito.Mockito) Category(io.gravitee.repository.management.model.Category) List(java.util.List) CategoryRepository(io.gravitee.repository.management.api.CategoryRepository) Optional(java.util.Optional) Assert(org.junit.Assert) CATEGORY_UPDATED(io.gravitee.repository.management.model.Category.AuditEvent.CATEGORY_UPDATED) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Category(io.gravitee.repository.management.model.Category) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) Date(java.util.Date) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) Test(org.junit.Test)

Example 14 with CategoryEntity

use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.

the class CategoryResource method updateCategory.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update the category", notes = "User must have the PORTAL_CATEGORY[UPDATE] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Category successfully updated", response = CategoryEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_CATEGORY, acls = RolePermissionAction.UPDATE) })
public Response updateCategory(@Valid @NotNull final UpdateCategoryEntity category) {
    try {
        ImageUtils.verify(category.getPicture());
        ImageUtils.verify(category.getBackground());
    } catch (InvalidImageException e) {
        throw new BadRequestException("Invalid image format");
    }
    CategoryEntity categoryEntity = categoryService.update(categoryId, category);
    setPictures(categoryEntity, false);
    return Response.ok(categoryEntity).build();
}
Also used : InvalidImageException(io.gravitee.rest.api.exception.InvalidImageException) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 15 with CategoryEntity

use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.

the class CategoryResource method getCategory.

@GET
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get the category", notes = "User must have the PORTAL_CATEGORY[READ] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Category's definition", response = CategoryEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
public CategoryEntity getCategory() {
    boolean canShowCategory = hasPermission(RolePermission.ENVIRONMENT_CATEGORY, RolePermissionAction.READ);
    CategoryEntity category = categoryService.findById(categoryId);
    if (!canShowCategory && category.isHidden()) {
        throw new UnauthorizedAccessException();
    }
    // set picture
    setPictures(category, false);
    return category;
}
Also used : UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

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