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);
}
}
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);
}
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");
}
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")));
}
Aggregations