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