Search in sources :

Example 1 with DuplicateCategoryNameException

use of io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException 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 2 with DuplicateCategoryNameException

use of io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException in project gravitee-management-rest-api by gravitee-io.

the class CategoryService_CreateTest method shouldNotCreateExistingCategory.

@Test(expected = DuplicateCategoryNameException.class)
public void shouldNotCreateExistingCategory() throws TechnicalException {
    Category v1 = new Category();
    NewCategoryEntity nv1 = new NewCategoryEntity();
    v1.setName("v1");
    nv1.setName("v1");
    when(mockCategoryRepository.findAllByEnvironment(any())).thenReturn(Collections.singleton(v1));
    try {
        categoryService.create(nv1);
    } catch (DuplicateCategoryNameException e) {
        verify(mockCategoryRepository, never()).create(any());
        throw e;
    }
    Assert.fail("should throw DuplicateCategoryNameException");
}
Also used : Category(io.gravitee.repository.management.model.Category) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) DuplicateCategoryNameException(io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException) Test(org.junit.Test)

Aggregations

Category (io.gravitee.repository.management.model.Category)2 NewCategoryEntity (io.gravitee.rest.api.model.NewCategoryEntity)2 DuplicateCategoryNameException (io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException)2 IdGenerator (io.gravitee.common.utils.IdGenerator)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 CategoryRepository (io.gravitee.repository.management.api.CategoryRepository)1 CATEGORY (io.gravitee.repository.management.model.Audit.AuditProperties.CATEGORY)1 AuditEvent (io.gravitee.repository.management.model.Category.AuditEvent)1 CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)1 InlinePictureEntity (io.gravitee.rest.api.model.InlinePictureEntity)1 UpdateCategoryEntity (io.gravitee.rest.api.model.UpdateCategoryEntity)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 ApiService (io.gravitee.rest.api.service.ApiService)1 AuditService (io.gravitee.rest.api.service.AuditService)1 CategoryService (io.gravitee.rest.api.service.CategoryService)1 EnvironmentService (io.gravitee.rest.api.service.EnvironmentService)1 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 CategoryNotFoundException (io.gravitee.rest.api.service.exceptions.CategoryNotFoundException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1