use of io.gravitee.management.service.exceptions.DuplicateViewNameException in project gravitee-management-rest-api by gravitee-io.
the class ViewServiceImpl method create.
@Override
public List<ViewEntity> create(final List<NewViewEntity> viewEntities) {
// First we prevent the duplicate view name
final List<String> viewNames = viewEntities.stream().map(NewViewEntity::getName).collect(Collectors.toList());
final Optional<ViewEntity> optionalView = findAll().stream().filter(view -> viewNames.contains(view.getName())).findAny();
if (optionalView.isPresent()) {
throw new DuplicateViewNameException(optionalView.get().getName());
}
final List<ViewEntity> savedViews = new ArrayList<>(viewEntities.size());
viewEntities.forEach(viewEntity -> {
try {
View view = convert(viewEntity);
savedViews.add(convert(viewRepository.create(view)));
auditService.createPortalAuditLog(Collections.singletonMap(VIEW, view.getId()), VIEW_CREATED, new Date(), null, view);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create view {}", viewEntity.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to create view " + viewEntity.getName(), ex);
}
});
return savedViews;
}
use of io.gravitee.management.service.exceptions.DuplicateViewNameException in project gravitee-management-rest-api by gravitee-io.
the class ViewService_CreateTest method shouldNotCreateExistingView.
@Test(expected = DuplicateViewNameException.class)
public void shouldNotCreateExistingView() throws TechnicalException {
View v1 = new View();
NewViewEntity nv1 = new NewViewEntity();
v1.setName("v1");
nv1.setName("v1");
when(mockViewRepository.findAll()).thenReturn(Collections.singleton(v1));
try {
viewService.create(singletonList(nv1));
} catch (DuplicateViewNameException e) {
verify(mockViewRepository, never()).create(any());
throw e;
}
Assert.fail("should throw DuplicateViewNameException");
}
Aggregations