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