Search in sources :

Example 1 with NewCategoryEntity

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

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

the class CategoryService_CreateTest method shouldNotCreateCategoryBecauseEnvironmentDoesNotExist.

@Test(expected = EnvironmentNotFoundException.class)
public void shouldNotCreateCategoryBecauseEnvironmentDoesNotExist() throws TechnicalException {
    when(mockEnvironmentService.findById(any())).thenThrow(EnvironmentNotFoundException.class);
    NewCategoryEntity nv1 = new NewCategoryEntity();
    nv1.setName("v1");
    categoryService.create(nv1);
}
Also used : NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) Test(org.junit.Test)

Example 3 with NewCategoryEntity

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

Example 4 with NewCategoryEntity

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

the class CategoryService_CreateTest method shouldCreateCategory.

@Test
public void shouldCreateCategory() throws TechnicalException {
    NewCategoryEntity v1 = new NewCategoryEntity();
    v1.setName("v1");
    when(mockCategoryRepository.create(argThat(cat -> cat.getCreatedAt() != null))).thenReturn(new Category());
    when(mockEnvironmentService.findById("DEFAULT")).thenReturn(new EnvironmentEntity());
    CategoryEntity category = categoryService.create(v1);
    assertNotNull("result is null", category);
    verify(mockAuditService, times(1)).createEnvironmentAuditLog(any(), eq(CATEGORY_CREATED), any(), isNull(), any());
    verify(mockCategoryRepository, times(1)).create(argThat(arg -> arg != null && arg.getName().equals("v1")));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) InjectMocks(org.mockito.InjectMocks) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) CategoryServiceImpl(io.gravitee.rest.api.service.impl.CategoryServiceImpl) Mock(org.mockito.Mock) CATEGORY_CREATED(io.gravitee.repository.management.model.Category.AuditEvent.CATEGORY_CREATED) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) AuditService(io.gravitee.rest.api.service.AuditService) Test(org.junit.Test) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Mockito(org.mockito.Mockito) Category(io.gravitee.repository.management.model.Category) CategoryRepository(io.gravitee.repository.management.api.CategoryRepository) EnvironmentNotFoundException(io.gravitee.rest.api.service.exceptions.EnvironmentNotFoundException) DuplicateCategoryNameException(io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException) Assert(org.junit.Assert) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Category(io.gravitee.repository.management.model.Category) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) Test(org.junit.Test)

Aggregations

NewCategoryEntity (io.gravitee.rest.api.model.NewCategoryEntity)4 Category (io.gravitee.repository.management.model.Category)3 DuplicateCategoryNameException (io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException)3 Test (org.junit.Test)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 CategoryRepository (io.gravitee.repository.management.api.CategoryRepository)2 CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)2 AuditService (io.gravitee.rest.api.service.AuditService)2 IdGenerator (io.gravitee.common.utils.IdGenerator)1 CATEGORY (io.gravitee.repository.management.model.Audit.AuditProperties.CATEGORY)1 AuditEvent (io.gravitee.repository.management.model.Category.AuditEvent)1 CATEGORY_CREATED (io.gravitee.repository.management.model.Category.AuditEvent.CATEGORY_CREATED)1 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)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 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