Search in sources :

Example 1 with ExternalCodeable

use of eu.bcvsolutions.idm.core.api.domain.ExternalCodeable in project CzechIdMng by bcvsolutions.

the class AbstractReadWriteDtoControllerRestTest method testFindByExternalCode.

@Test
@SuppressWarnings("unchecked")
public void testFindByExternalCode() {
    DTO dto = prepareDto();
    if (!(dto instanceof ExternalCodeable)) {
        // ignore test
        return;
    }
    // 
    ExternalCodeable externalCodeableDto = (ExternalCodeable) dto;
    externalCodeableDto.setExternalCode(getHelper().createName());
    // 
    DTO createdDto = createDto(dto);
    // 
    // create another mock dto
    ExternalCodeable dtoTwo = (ExternalCodeable) prepareDto();
    dtoTwo.setExternalCode(getHelper().createName());
    createDto((DTO) dtoTwo);
    // 
    MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
    parameters.set(ExternalCodeable.PROPERTY_EXTERNAL_CODE, externalCodeableDto.getExternalCode());
    // 
    List<DTO> results = find(parameters);
    // 
    Assert.assertEquals(1, results.size());
    Assert.assertEquals(createdDto.getId(), results.get(0).getId());
    // 
    if (supportsAutocomplete()) {
        results = autocomplete(parameters);
        // 
        Assert.assertEquals(1, results.size());
        Assert.assertEquals(createdDto.getId(), results.get(0).getId());
    } else {
        LOG.info("Controller [{}] doesn't support autocomplete method. Method will not be tested.", getController().getClass());
    }
    // 
    Assert.assertEquals(1, count(parameters));
}
Also used : ExternalCodeable(eu.bcvsolutions.idm.core.api.domain.ExternalCodeable) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Example 2 with ExternalCodeable

use of eu.bcvsolutions.idm.core.api.domain.ExternalCodeable in project CzechIdMng by bcvsolutions.

the class AbstractReadWriteDtoControllerRestTest method testDuplicateExternalCode.

@Test
public void testDuplicateExternalCode() 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 ExternalCodeable)) {
        // ignore test
        return;
    }
    // 
    ExternalCodeable externalIdentifiableDto = (ExternalCodeable) dto;
    String name = getHelper().createName();
    externalIdentifiableDto.setExternalCode(name);
    // 
    createDto(dto);
    // 
    getMockMvc().perform(post(getBaseUrl()).with(authentication(getAdminAuthentication())).content(getMapper().writeValueAsString(dto)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isConflict());
}
Also used : DataFilter(eu.bcvsolutions.idm.core.api.dto.filter.DataFilter) ExternalCodeable(eu.bcvsolutions.idm.core.api.domain.ExternalCodeable) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Example 3 with ExternalCodeable

use of eu.bcvsolutions.idm.core.api.domain.ExternalCodeable 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;
}
Also used : ExternalIdentifiable(eu.bcvsolutions.idm.core.api.domain.ExternalIdentifiable) ExternalCodeable(eu.bcvsolutions.idm.core.api.domain.ExternalCodeable) DuplicateExternalCodeException(eu.bcvsolutions.idm.core.api.exception.DuplicateExternalCodeException) EntityTypeNotExternalIdentifiableException(eu.bcvsolutions.idm.core.api.exception.EntityTypeNotExternalIdentifiableException) EntityTypeNotExternalCodeableException(eu.bcvsolutions.idm.core.api.exception.EntityTypeNotExternalCodeableException) DuplicateExternalIdException(eu.bcvsolutions.idm.core.api.exception.DuplicateExternalIdException)

Aggregations

ExternalCodeable (eu.bcvsolutions.idm.core.api.domain.ExternalCodeable)3 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)2 AbstractRestTest (eu.bcvsolutions.idm.test.api.AbstractRestTest)2 Test (org.junit.Test)2 ExternalIdentifiable (eu.bcvsolutions.idm.core.api.domain.ExternalIdentifiable)1 DataFilter (eu.bcvsolutions.idm.core.api.dto.filter.DataFilter)1 DuplicateExternalCodeException (eu.bcvsolutions.idm.core.api.exception.DuplicateExternalCodeException)1 DuplicateExternalIdException (eu.bcvsolutions.idm.core.api.exception.DuplicateExternalIdException)1 EntityTypeNotExternalCodeableException (eu.bcvsolutions.idm.core.api.exception.EntityTypeNotExternalCodeableException)1 EntityTypeNotExternalIdentifiableException (eu.bcvsolutions.idm.core.api.exception.EntityTypeNotExternalIdentifiableException)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1