use of eu.bcvsolutions.idm.core.api.exception.DuplicateExternalIdException in project CzechIdMng by bcvsolutions.
the class ExternalIdentifiableFilterBuilderIntegrationTest method testCreateDuplicateExternalId.
@Test
public void testCreateDuplicateExternalId() {
// prepare data
IdmIdentityDto identityOne = getHelper().createIdentity((GuardedString) null);
identityOne.setExternalId("one");
identityOne = identityService.save(identityOne);
IdmIdentityDto identityThree = getHelper().createIdentity((GuardedString) null);
identityThree.setExternalId(null);
identityService.save(identityThree);
IdmIdentityDto identityTwo = getHelper().createIdentity((GuardedString) null);
identityTwo.setExternalId("one");
try {
identityTwo = identityService.save(identityTwo);
fail();
} catch (DuplicateExternalIdException ex) {
assertEquals(identityOne.getId(), ex.getDuplicateId());
}
}
use of eu.bcvsolutions.idm.core.api.exception.DuplicateExternalIdException in project CzechIdMng by bcvsolutions.
the class AbstractReadWriteDtoControllerRestTest method testDuplicateExternalId.
@Test
public void testDuplicateExternalId() throws Exception {
if (!DataFilter.class.isAssignableFrom(getController().getFilterClass())) {
LOG.warn("Controller [{}] doesn't support DataFilter. Find by external id will not be tested.", getController().getClass());
return;
}
//
DTO dto = prepareDto();
if (!(dto instanceof ExternalIdentifiable)) {
// ignore test
return;
}
//
ExternalIdentifiable externalIdentifiableDto = (ExternalIdentifiable) dto;
String name = getHelper().createName();
externalIdentifiableDto.setExternalId(name);
//
DTO original = createDto(dto);
DTO duplicate = prepareDto();
((ExternalIdentifiable) duplicate).setExternalId(name);
//
if (!supportsPost()) {
try {
createDto(duplicate);
Assert.fail();
} catch (DuplicateExternalIdException ex) {
Assert.assertEquals(original.getId(), ex.getDuplicateId());
}
} else {
getMockMvc().perform(post(getBaseUrl()).with(authentication(getAdminAuthentication())).content(getMapper().writeValueAsString(duplicate)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isConflict());
}
}
use of eu.bcvsolutions.idm.core.api.exception.DuplicateExternalIdException in project CzechIdMng by bcvsolutions.
the class AbstractReadWriteDtoService method validateEntity.
/**
* Validates JRS303 before entity is saved
*
* @param dto
* @return
*/
@SuppressWarnings({ "unchecked" })
protected E validateEntity(E entity) {
entity = validate(entity);
// unique external id in business logic (external id can be null)
if (entity instanceof ExternalIdentifiable) {
if (!ExternalIdentifiable.class.isAssignableFrom(getFilterClass())) {
throw new EntityTypeNotExternalIdentifiableException(getFilterClass().getCanonicalName());
}
ExternalIdentifiable externalIdentifiable = (ExternalIdentifiable) entity;
if (StringUtils.isNotEmpty(externalIdentifiable.getExternalId())) {
// empty string are not valid external id
try {
ExternalIdentifiable filter = (ExternalIdentifiable) getFilterClass().getDeclaredConstructor().newInstance();
filter.setExternalId(externalIdentifiable.getExternalId());
List<DTO> dtos = find((F) filter, null).getContent();
DTO other = dtos.stream().filter(dto -> !dto.getId().equals(((E) externalIdentifiable).getId())).findFirst().orElse(null);
if (other != null) {
throw new DuplicateExternalIdException(getEntityClass().getCanonicalName(), externalIdentifiable.getExternalId(), other.getId());
}
} catch (ReflectiveOperationException ex) {
throw new EntityTypeNotExternalIdentifiableException(getFilterClass().getCanonicalName(), ex);
}
}
}
// unique external code in business logic (external id can be null)
if (entity instanceof ExternalCodeable) {
if (!ExternalCodeable.class.isAssignableFrom(getFilterClass())) {
throw new EntityTypeNotExternalCodeableException(getFilterClass().getCanonicalName());
}
ExternalCodeable externalCodeable = (ExternalCodeable) entity;
if (StringUtils.isNotEmpty(externalCodeable.getExternalCode())) {
// empty string are not valid external code
try {
ExternalCodeable filter = (ExternalCodeable) getFilterClass().getDeclaredConstructor().newInstance();
filter.setExternalCode(externalCodeable.getExternalCode());
List<DTO> dtos = find((F) filter, null).getContent();
DTO other = dtos.stream().filter(dto -> !dto.getId().equals(((E) externalCodeable).getId())).findFirst().orElse(null);
if (other != null) {
throw new DuplicateExternalCodeException(getEntityClass().getCanonicalName(), externalCodeable.getExternalCode(), other.getId());
}
} catch (ReflectiveOperationException ex) {
throw new EntityTypeNotExternalCodeableException(getFilterClass().getCanonicalName(), ex);
}
}
}
return entity;
}
Aggregations