Search in sources :

Example 21 with ConstraintViolation

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)")));
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) ConstraintViolation(org.molgenis.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) UnknownEntityException(org.molgenis.data.UnknownEntityException) UnknownEntityTypeException(org.molgenis.data.UnknownEntityTypeException) RepositoryAlreadyExistsException(org.molgenis.data.RepositoryAlreadyExistsException) MolgenisDataException(org.molgenis.data.MolgenisDataException) EntityTypePermissionDeniedException(org.molgenis.data.security.exception.EntityTypePermissionDeniedException) RepositoryNotCapableException(org.molgenis.data.RepositoryNotCapableException) NestedServletException(org.springframework.web.util.NestedServletException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 22 with ConstraintViolation

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());
    }
}
Also used : Entity(org.molgenis.data.Entity) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Test(org.junit.jupiter.api.Test)

Example 23 with ConstraintViolation

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());
    }
}
Also used : Entity(org.molgenis.data.Entity) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Stream(java.util.stream.Stream) Test(org.junit.jupiter.api.Test)

Example 24 with ConstraintViolation

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());
    }
}
Also used : Entity(org.molgenis.data.Entity) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Stream(java.util.stream.Stream) Test(org.junit.jupiter.api.Test)

Example 25 with ConstraintViolation

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());
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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