Search in sources :

Example 16 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlRepositoryTest method testMrefValueTooLong.

@Test
void testMrefValueTooLong() {
    Attribute attr = mock(Attribute.class);
    when(attr.getName()).thenReturn("mref_attr");
    Attribute idAttr = mock(Attribute.class);
    when(idAttr.getName()).thenReturn("id");
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    when(entityType.getId()).thenReturn("test_entity");
    MolgenisValidationException mve = new MolgenisValidationException(new ConstraintViolation(VALUE_TOO_LONG_MSG));
    when(jdbcTemplate.batchUpdate(any(), any(BatchPreparedStatementSetter.class))).thenThrow(mve);
    HashMap<String, Object> value = newHashMap();
    value.put("mref_attr", "TOOLONG");
    Exception exception = assertThrows(MolgenisValidationException.class, () -> postgreSqlRepo.addMrefs(newArrayList(value), attr));
    assertThat(exception.getMessage()).containsPattern("One of the mref values in entity type \\[test_entity\\] attribute \\[mref_attr\\] is too long.");
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.validation.ConstraintViolation) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlRepository method verifyUpdate.

private void verifyUpdate(List<? extends Entity> entitiesBatch, int[] counts, Attribute idAttr) {
    int nrUpdatedEntities = Arrays.stream(counts).sum();
    if (nrUpdatedEntities < entitiesBatch.size()) {
        Set<Object> existingEntityIds = findAll(entitiesBatch.stream().map(Entity::getIdValue), new Fetch().field(idAttr.getName())).map(Entity::getIdValue).collect(toSet());
        Object nonExistingEntityId = entitiesBatch.stream().map(Entity::getIdValue).filter(entityId -> !existingEntityIds.contains(entityId)).findFirst().orElseThrow(() -> new IllegalStateException("Not all entities in batch were updated but all are present in the repository."));
        throw new MolgenisValidationException(new ConstraintViolation(format("Cannot update [%s] with id [%s] because it does not exist", entityType.getId(), nonExistingEntityId.toString())));
    }
}
Also used : Fetch(org.molgenis.data.Fetch) ConstraintViolation(org.molgenis.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 18 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlExceptionTranslator method translateUndefinedColumnException.

/**
 * Package private for testability
 */
static MolgenisValidationException translateUndefinedColumnException(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    // FIXME exposes internal message
    String message = serverErrorMessage.getMessage();
    ConstraintViolation constraintViolation = new ConstraintViolation(message);
    return new MolgenisValidationException(singleton(constraintViolation));
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) ConstraintViolation(org.molgenis.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 19 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlRepository method addMrefs.

void addMrefs(final List<Map<String, Object>> mrefs, final Attribute attr) {
    // so validate it ourselves
    if (!attr.isNillable() && mrefs.isEmpty()) {
        throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] attribute [%s] value cannot be null", entityType.getId(), attr.getName())));
    }
    final Attribute idAttr = entityType.getIdAttribute();
    String insertMrefSql = getSqlInsertJunction(entityType, attr);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Adding junction table entries for entity [{}] attribute [{}]", getName(), attr.getName());
        if (LOG.isTraceEnabled()) {
            LOG.trace("SQL: {}", insertMrefSql);
        }
    }
    try {
        jdbcTemplate.batchUpdate(insertMrefSql, new BatchJunctionTableAddPreparedStatementSetter(mrefs, attr, idAttr));
    } catch (MolgenisValidationException mve) {
        if (mve.getMessage().equals(VALUE_TOO_LONG_MSG)) {
            mve = new MolgenisValidationException(new ConstraintViolation(format("One of the mref values in entity type [%s] attribute [%s] is too long.", getEntityType().getId(), attr.getName())));
        }
        throw mve;
    }
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 20 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlRepository method createMrefMap.

private static Map<String, List<Map<String, Object>>> createMrefMap(Attribute idAttr, List<Attribute> junctionTableAttrs, List<? extends Entity> entitiesBatch) {
    Map<String, List<Map<String, Object>>> mrefs = Maps.newHashMapWithExpectedSize(junctionTableAttrs.size());
    AtomicInteger seqNr = new AtomicInteger();
    for (Entity entity : entitiesBatch) {
        for (Attribute attr : junctionTableAttrs) {
            Iterable<Entity> refEntities = entity.getEntities(attr.getName());
            // so validate manually.
            if (!attr.isNillable() && Iterables.isEmpty(refEntities)) {
                throw new MolgenisValidationException(new ConstraintViolation(format("The attribute [%s] of entity [%s] with id [%s] can not be null.", attr.getName(), attr.getEntity().getId(), entity.getIdValue().toString())));
            }
            mrefs.putIfAbsent(attr.getName(), new ArrayList<>());
            seqNr.set(0);
            for (Entity val : refEntities) {
                Map<String, Object> mref = createJunctionTableRowData(seqNr.getAndIncrement(), idAttr, val, attr, entity);
                mrefs.get(attr.getName()).add(mref);
            }
        }
    }
    return mrefs;
}
Also used : Entity(org.molgenis.data.Entity) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Aggregations

ConstraintViolation (org.molgenis.validation.ConstraintViolation)36 Entity (org.molgenis.data.Entity)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Attribute (org.molgenis.data.meta.model.Attribute)14 DynamicEntity (org.molgenis.data.support.DynamicEntity)13 Test (org.junit.jupiter.api.Test)12 EntityType (org.molgenis.data.meta.model.EntityType)11 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)6 AttributeType (org.molgenis.data.meta.AttributeType)4 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)4 Collectors.toList (java.util.stream.Collectors.toList)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 Streams.stream (com.google.common.collect.Streams.stream)2 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2