Search in sources :

Example 6 with UpdateCategoryEntity

use of io.gravitee.rest.api.model.UpdateCategoryEntity 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 7 with UpdateCategoryEntity

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

the class CategoryService_UpdateTest method shouldNotUpdateUnknownCategory_multi_mode.

@Test
public void shouldNotUpdateUnknownCategory_multi_mode() throws TechnicalException {
    UpdateCategoryEntity mockCategory = mock(UpdateCategoryEntity.class);
    when(mockCategory.getId()).thenReturn("unknown");
    when(mockCategoryRepository.findById("unknown")).thenReturn(Optional.empty());
    List<CategoryEntity> list = categoryService.update(singletonList(mockCategory));
    assertTrue(list.isEmpty());
    verify(mockCategoryRepository, times(1)).findById(any());
    verify(mockCategoryRepository, never()).update(any());
    verify(mockAuditService, never()).createEnvironmentAuditLog(any(), eq(CATEGORY_UPDATED), any(), any(), any());
}
Also used : UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) Test(org.junit.Test)

Example 8 with UpdateCategoryEntity

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

the class CategoryService_UpdateTest method shouldUpdateCategory_multi_mode.

@Test
public void shouldUpdateCategory_multi_mode() throws TechnicalException {
    UpdateCategoryEntity mockCategory = mock(UpdateCategoryEntity.class);
    when(mockCategory.getId()).thenReturn("known");
    when(mockCategory.getName()).thenReturn("Known");
    when(mockCategoryRepository.findById("known")).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);
    List<CategoryEntity> list = categoryService.update(singletonList(mockCategory));
    assertFalse(list.isEmpty());
    assertEquals("one element", 1, list.size());
    assertEquals("Id", "category-id", list.get(0).getId());
    assertEquals("Name", "category-name", list.get(0).getName());
    assertEquals("Description", "category-description", list.get(0).getDescription());
    assertEquals("Total APIs", 0, list.get(0).getTotalApis());
    assertEquals("Order", 1, list.get(0).getOrder());
    assertEquals("Hidden", true, list.get(0).isHidden());
    assertEquals("UpdatedAt", new Date(1234567890L), list.get(0).getUpdatedAt());
    assertEquals("CreatedAt", new Date(9876543210L), list.get(0).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)

Aggregations

UpdateCategoryEntity (io.gravitee.rest.api.model.UpdateCategoryEntity)8 CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)7 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 Category (io.gravitee.repository.management.model.Category)4 Test (org.junit.Test)4 CategoryNotFoundException (io.gravitee.rest.api.service.exceptions.CategoryNotFoundException)3 Date (java.util.Date)3 CategoryRepository (io.gravitee.repository.management.api.CategoryRepository)2 CATEGORY_UPDATED (io.gravitee.repository.management.model.Category.AuditEvent.CATEGORY_UPDATED)2 NewCategoryEntity (io.gravitee.rest.api.model.NewCategoryEntity)2 AuditService (io.gravitee.rest.api.service.AuditService)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 CategoryServiceImpl (io.gravitee.rest.api.service.impl.CategoryServiceImpl)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Optional (java.util.Optional)2 Assert (org.junit.Assert)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 InjectMocks (org.mockito.InjectMocks)2