Search in sources :

Example 1 with Category

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

the class CategoryServiceImpl method update.

@Override
public CategoryEntity update(String categoryId, UpdateCategoryEntity categoryEntity) {
    try {
        LOGGER.debug("Update Category {}", categoryId);
        Optional<Category> optCategoryToUpdate = categoryRepository.findById(categoryId);
        if (!optCategoryToUpdate.isPresent()) {
            throw new CategoryNotFoundException(categoryId);
        }
        final Category categoryToUpdate = optCategoryToUpdate.get();
        Category category = convert(categoryEntity, categoryToUpdate.getEnvironmentId());
        // If no new picture and the current picture url is not the default one, keep the current picture
        if (categoryEntity.getPicture() == null && categoryEntity.getPictureUrl() != null && categoryEntity.getPictureUrl().indexOf("?hash") > 0) {
            category.setPicture(categoryToUpdate.getPicture());
        }
        final Date updatedAt = new Date();
        category.setCreatedAt(categoryToUpdate.getCreatedAt());
        category.setUpdatedAt(updatedAt);
        CategoryEntity updatedCategory = convert(categoryRepository.update(category));
        auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_UPDATED, updatedAt, categoryToUpdate, category);
        return updatedCategory;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update category {}", categoryEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to update category " + categoryEntity.getName(), ex);
    }
}
Also used : Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 2 with Category

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

the class CategoryServiceImpl method create.

@Override
public CategoryEntity create(NewCategoryEntity newCategory) {
    // First we prevent the duplicate category name
    final List<CategoryEntity> categories = findAll();
    final Optional<CategoryEntity> optionalCategory = categories.stream().filter(v -> v.getName().equals((newCategory.getName()))).findAny();
    if (optionalCategory.isPresent()) {
        throw new DuplicateCategoryNameException(optionalCategory.get().getName());
    }
    try {
        // check if environment exists
        String environment = GraviteeContext.getCurrentEnvironment();
        this.environmentService.findById(environment);
        Category category = convert(newCategory);
        final Date createdAt = new Date();
        category.setCreatedAt(createdAt);
        category.setUpdatedAt(createdAt);
        category.setEnvironmentId(environment);
        category.setOrder(categories.size());
        CategoryEntity createdCategory = convert(categoryRepository.create(category));
        auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_CREATED, createdAt, null, category);
        return createdCategory;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create category {}", newCategory.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to create category " + newCategory.getName(), ex);
    }
}
Also used : java.util(java.util) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) Autowired(org.springframework.beans.factory.annotation.Autowired) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) ApiService(io.gravitee.rest.api.service.ApiService) IdGenerator(io.gravitee.common.utils.IdGenerator) Category(io.gravitee.repository.management.model.Category) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) AuditEvent(io.gravitee.repository.management.model.Category.AuditEvent) DuplicateCategoryNameException(io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Logger(org.slf4j.Logger) EnvironmentService(io.gravitee.rest.api.service.EnvironmentService) AuditService(io.gravitee.rest.api.service.AuditService) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) CategoryService(io.gravitee.rest.api.service.CategoryService) Collectors(java.util.stream.Collectors) CATEGORY(io.gravitee.repository.management.model.Audit.AuditProperties.CATEGORY) Component(org.springframework.stereotype.Component) CategoryRepository(io.gravitee.repository.management.api.CategoryRepository) DatatypeConverter(javax.xml.bind.DatatypeConverter) Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateCategoryNameException(io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 3 with Category

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

the class CategoryServiceImpl method convert.

private Category convert(final NewCategoryEntity categoryEntity) {
    final Category category = new Category();
    category.setId(UuidString.generateRandom());
    category.setKey(IdGenerator.generate(categoryEntity.getName()));
    category.setName(categoryEntity.getName());
    category.setDescription(categoryEntity.getDescription());
    category.setOrder(categoryEntity.getOrder());
    category.setHidden(categoryEntity.isHidden());
    category.setHighlightApi(categoryEntity.getHighlightApi());
    category.setPicture(categoryEntity.getPicture());
    category.setBackground(categoryEntity.getBackground());
    category.setPage(categoryEntity.getPage());
    return category;
}
Also used : Category(io.gravitee.repository.management.model.Category)

Example 4 with Category

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

the class CategoryServiceImpl method update.

@Override
public List<CategoryEntity> update(final List<UpdateCategoryEntity> categoriesEntities) {
    final List<CategoryEntity> savedCategories = new ArrayList<>(categoriesEntities.size());
    categoriesEntities.forEach(categoryEntity -> {
        try {
            Optional<Category> optCategoryToUpdate = categoryRepository.findById(categoryEntity.getId());
            if (optCategoryToUpdate.isPresent()) {
                final Category categoryToUpdate = optCategoryToUpdate.get();
                Category category = convert(categoryEntity, categoryToUpdate.getEnvironmentId());
                // check if picture has been set
                if (category.getPicture() == null) {
                    // Picture can not be updated when re-ordering categories
                    category.setPicture(categoryToUpdate.getPicture());
                }
                // check if background has been set
                if (category.getBackground() == null) {
                    // Background can not be updated when re-ordering categories
                    category.setBackground(categoryToUpdate.getBackground());
                }
                final Date updatedAt = new Date();
                category.setCreatedAt(categoryToUpdate.getCreatedAt());
                category.setUpdatedAt(updatedAt);
                savedCategories.add(convert(categoryRepository.update(category)));
                auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_UPDATED, updatedAt, categoryToUpdate, category);
            }
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to update category {}", categoryEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to update category " + categoryEntity.getName(), ex);
        }
    });
    return savedCategories;
}
Also used : Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 5 with Category

use of io.gravitee.repository.management.model.Category 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)

Aggregations

Category (io.gravitee.repository.management.model.Category)13 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)9 CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)7 CategoryRepository (io.gravitee.repository.management.api.CategoryRepository)5 NewCategoryEntity (io.gravitee.rest.api.model.NewCategoryEntity)5 UpdateCategoryEntity (io.gravitee.rest.api.model.UpdateCategoryEntity)5 CategoryNotFoundException (io.gravitee.rest.api.service.exceptions.CategoryNotFoundException)5 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)5 Test (org.junit.Test)5 AuditService (io.gravitee.rest.api.service.AuditService)4 Date (java.util.Date)4 DuplicateCategoryNameException (io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException)3 CategoryServiceImpl (io.gravitee.rest.api.service.impl.CategoryServiceImpl)3 Optional (java.util.Optional)3 Assert (org.junit.Assert)3 RunWith (org.junit.runner.RunWith)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 InjectMocks (org.mockito.InjectMocks)3 Mock (org.mockito.Mock)3 Mockito (org.mockito.Mockito)3