use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class RestControllerV2Test method testUpdateEntitiesMolgenisValidationException.
@SuppressWarnings("unchecked")
@Test
void testUpdateEntitiesMolgenisValidationException() throws Exception {
Exception e = new MolgenisValidationException(Collections.singleton(new ConstraintViolation("Message", 5L)));
doThrow(e).when(dataService).update(eq(ENTITY_NAME), (Stream<Entity>) any(Stream.class));
mockMvc.perform(put(HREF_ENTITY_COLLECTION).content("{entities:[{id:'p1', name:'Example data'}]}").contentType(APPLICATION_JSON)).andExpect(status().isBadRequest()).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(header().doesNotExist("Location")).andExpect(jsonPath(FIRST_ERROR_MESSAGE, is("Message (entity 5)")));
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class RepositoryValidationDecoratorTest method addEntityAttributesValidationError.
@Test
void addEntityAttributesValidationError() {
// entities
Entity entity0 = mock(Entity.class);
when(entity0.getEntityType()).thenReturn(entityType);
when(entity0.getIdValue()).thenReturn("id0");
when(entity0.getEntity(attrXrefName)).thenReturn(refEntity0);
when(entity0.getEntity(attrNillableXrefName)).thenReturn(null);
when(entity0.getEntities(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity0.getEntities(attrNillableMrefName)).thenReturn(emptyList());
when(entity0.getString(attrUniqueStringName)).thenReturn("unique0");
when(entity0.getEntity(attrUniqueXrefName)).thenReturn(refEntity0);
when(entity0.get(attrIdName)).thenReturn("id0");
when(entity0.get(attrXrefName)).thenReturn(refEntity0);
when(entity0.get(attrNillableXrefName)).thenReturn(null);
when(entity0.get(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity0.get(attrNillableMrefName)).thenReturn(emptyList());
when(entity0.get(attrUniqueStringName)).thenReturn("unique0");
when(entity0.get(attrUniqueXrefName)).thenReturn(refEntity0);
Set<ConstraintViolation> violations = singleton(new ConstraintViolation("violation", 2L));
when(entityAttributesValidator.validate(entity0, entityType)).thenReturn(violations);
// actual tests
try {
repositoryValidationDecorator.add(entity0);
throw new RuntimeException("Expected MolgenisValidationException instead of no exception");
} catch (MolgenisValidationException e) {
verify(entityAttributesValidator, times(1)).validate(entity0, entityType);
assertEquals(violations, e.getViolations());
}
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class RepositoryValidationDecoratorTest method addStreamEntityAttributesValidationError.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
void addStreamEntityAttributesValidationError() {
// entities
Entity entity0 = mock(Entity.class);
when(entity0.getEntityType()).thenReturn(entityType);
when(entity0.getIdValue()).thenReturn("id0");
when(entity0.getEntity(attrXrefName)).thenReturn(refEntity0);
when(entity0.getEntity(attrNillableXrefName)).thenReturn(null);
when(entity0.getEntities(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity0.getEntities(attrNillableMrefName)).thenReturn(emptyList());
when(entity0.getString(attrUniqueStringName)).thenReturn("unique0");
when(entity0.getEntity(attrUniqueXrefName)).thenReturn(refEntity0);
when(entity0.get(attrIdName)).thenReturn("id0");
when(entity0.get(attrXrefName)).thenReturn(refEntity0);
when(entity0.get(attrNillableXrefName)).thenReturn(null);
when(entity0.get(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity0.get(attrNillableMrefName)).thenReturn(emptyList());
when(entity0.get(attrUniqueStringName)).thenReturn("unique0");
when(entity0.get(attrUniqueXrefName)).thenReturn(refEntity0);
Entity entity1 = mock(Entity.class);
when(entity1.getIdValue()).thenReturn("id1");
when(entity1.getEntityType()).thenReturn(entityType);
when(entity1.getEntity(attrXrefName)).thenReturn(refEntity0);
when(entity1.getEntity(attrNillableXrefName)).thenReturn(null);
when(entity1.getEntities(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity1.getEntities(attrNillableMrefName)).thenReturn(emptyList());
when(entity1.getString(attrUniqueStringName)).thenReturn("unique1");
when(entity1.getEntity(attrUniqueXrefName)).thenReturn(refEntity1);
when(entity1.get(attrIdName)).thenReturn("id1");
when(entity1.get(attrXrefName)).thenReturn(refEntity0);
when(entity1.get(attrNillableXrefName)).thenReturn(null);
when(entity1.get(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity1.get(attrNillableMrefName)).thenReturn(emptyList());
when(entity1.get(attrUniqueStringName)).thenReturn("unique1");
when(entity1.get(attrUniqueXrefName)).thenReturn(refEntity1);
Set<ConstraintViolation> violations = singleton(new ConstraintViolation("violation", 2L));
when(entityAttributesValidator.validate(entity1, entityType)).thenReturn(violations);
// actual tests
List<Entity> entities = Arrays.asList(entity0, entity1);
repositoryValidationDecorator.add(entities.stream());
ArgumentCaptor<Stream<Entity>> captor = ArgumentCaptor.forClass(Stream.class);
verify(delegateRepository, times(1)).add(captor.capture());
Stream<Entity> stream = captor.getValue();
try {
// process stream to enable validation
stream.collect(toList());
throw new RuntimeException("Expected MolgenisValidationException instead of no exception");
} catch (MolgenisValidationException e) {
verify(entityAttributesValidator, times(1)).validate(entity0, entityType);
verify(entityAttributesValidator, times(1)).validate(entity1, entityType);
assertEquals(violations, e.getViolations());
}
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class RepositoryValidationDecoratorTest method updateStreamEntityAttributesValidationError.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
void updateStreamEntityAttributesValidationError() {
// entities
Entity entity0 = mock(Entity.class);
when(entity0.getEntityType()).thenReturn(entityType);
when(entity0.getIdValue()).thenReturn("id0");
when(entity0.getEntity(attrXrefName)).thenReturn(refEntity0);
when(entity0.getEntity(attrNillableXrefName)).thenReturn(null);
when(entity0.getEntities(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity0.getEntities(attrNillableMrefName)).thenReturn(emptyList());
when(entity0.getString(attrUniqueStringName)).thenReturn("unique0");
when(entity0.getEntity(attrUniqueXrefName)).thenReturn(refEntity0);
when(entity0.get(attrIdName)).thenReturn("id0");
when(entity0.get(attrXrefName)).thenReturn(refEntity0);
when(entity0.get(attrNillableXrefName)).thenReturn(null);
when(entity0.get(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity0.get(attrNillableMrefName)).thenReturn(emptyList());
when(entity0.get(attrUniqueStringName)).thenReturn("unique0");
when(entity0.get(attrUniqueXrefName)).thenReturn(refEntity0);
Entity entity1 = mock(Entity.class);
when(entity1.getIdValue()).thenReturn("id1");
when(entity1.getEntityType()).thenReturn(entityType);
when(entity1.getEntity(attrXrefName)).thenReturn(refEntity0);
when(entity1.getEntity(attrNillableXrefName)).thenReturn(null);
when(entity1.getEntities(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity1.getEntities(attrNillableMrefName)).thenReturn(emptyList());
when(entity1.getString(attrUniqueStringName)).thenReturn("unique1");
when(entity1.getEntity(attrUniqueXrefName)).thenReturn(refEntity1);
when(entity1.get(attrIdName)).thenReturn("id1");
when(entity1.get(attrXrefName)).thenReturn(refEntity0);
when(entity1.get(attrNillableXrefName)).thenReturn(null);
when(entity1.get(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity1.get(attrNillableMrefName)).thenReturn(emptyList());
when(entity1.get(attrUniqueStringName)).thenReturn("unique1");
when(entity1.get(attrUniqueXrefName)).thenReturn(refEntity1);
Set<ConstraintViolation> violations = singleton(new ConstraintViolation("violation", 2L));
when(entityAttributesValidator.validate(entity1, entityType)).thenReturn(violations);
// actual tests
List<Entity> entities = Arrays.asList(entity0, entity1);
repositoryValidationDecorator.update(entities.stream());
ArgumentCaptor<Stream<Entity>> captor = ArgumentCaptor.forClass(Stream.class);
verify(delegateRepository, times(1)).update(captor.capture());
Stream<Entity> stream = captor.getValue();
try {
// process stream to enable validation
stream.collect(toList());
throw new RuntimeException("Expected MolgenisValidationException instead of no exception");
} catch (MolgenisValidationException e) {
verify(entityAttributesValidator, times(1)).validate(entity0, entityType);
verify(entityAttributesValidator, times(1)).validate(entity1, entityType);
assertEquals(violations, e.getViolations());
}
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method checkRangeMaxOnlyInvalid.
@Test
void checkRangeMaxOnlyInvalid() {
Entity entity = new DynamicEntity(intRangeMaxMeta);
entity.set("id", "123");
entity.set("intrangemin", 2);
Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity, intRangeMaxMeta);
assertEquals(1, constraints.size());
}
Aggregations