Search in sources :

Example 1 with ConstraintViolation

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

the class RestController method handleMolgenisValidationException.

@ExceptionHandler(MolgenisValidationException.class)
@ResponseStatus(BAD_REQUEST)
public ErrorMessageResponse handleMolgenisValidationException(MolgenisValidationException e) {
    LOG.info("", e);
    List<ErrorMessage> messages = Lists.newArrayList();
    for (ConstraintViolation violation : e.getViolations()) {
        messages.add(new ErrorMessage(violation.getMessage()));
    }
    return new ErrorMessageResponse(messages);
}
Also used : ConstraintViolation(org.molgenis.validation.ConstraintViolation) ErrorMessage(org.molgenis.web.ErrorMessageResponse.ErrorMessage) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 2 with ConstraintViolation

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

the class RepositoryValidationDecoratorTest method updateEntityAttributesValidationError.

@Test
void updateEntityAttributesValidationError() {
    // entities
    Entity entity0 = mock(Entity.class);
    when(entity0.getIdValue()).thenReturn("id1");
    when(entity0.getEntityType()).thenReturn(entityType);
    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("unique1");
    when(entity0.getEntity(attrUniqueXrefName)).thenReturn(refEntity1);
    when(entity0.get(attrIdName)).thenReturn("id1");
    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("unique1");
    when(entity0.get(attrUniqueXrefName)).thenReturn(refEntity1);
    Set<ConstraintViolation> violations = singleton(new ConstraintViolation("violation", 2L));
    when(entityAttributesValidator.validate(entity0, entityType)).thenReturn(violations);
    // actual tests
    try {
        repositoryValidationDecorator.update(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 3 with ConstraintViolation

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

the class EntityAttributesValidatorTest method validateNullableExpression.

@Test
void validateNullableExpression() {
    String expression0 = "expression0";
    String expression1 = "expression1";
    String expression2 = "expression2";
    Attribute attr0 = createMockAttribute("attr0", STRING, expression0);
    Attribute attr1 = createMockAttribute("attr1", STRING, expression1);
    Attribute attr2 = createMockAttribute("attr2", MREF, expression2);
    Attribute attr3 = createMockAttribute("attr2", INT, null);
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn("entityType");
    when(entityType.getAtomicAttributes()).thenReturn(asList(attr0, attr1, attr2, attr3));
    when(entityType.toString()).thenReturn("entityType");
    Entity entity = mock(Entity.class);
    when(entity.getIdValue()).thenReturn("MyEntityId");
    when(entity.getLabelValue()).thenReturn("lbl-entity");
    when(entity.getString("attr0")).thenReturn(null);
    when(entity.getString("attr1")).thenReturn(null);
    when(entity.getEntities("attr2")).thenReturn(emptyList());
    when(entity.getInt("attr3")).thenReturn(null);
    List<Boolean> expressionResults = asList(true, false, false);
    when(simpleExpressionValidator.resolveBooleanExpressions(asList(expression0, expression1, expression2), entity)).thenReturn(expressionResults);
    Set<ConstraintViolation> constraintViolations = entityAttributesValidator.validate(entity, entityType);
    Set<ConstraintViolation> expectedConstraintViolations = newHashSet(new ConstraintViolation("Invalid [string] value [null] for attribute [lbl-attr1] of entity [lbl-entity] with type [entityType]. Offended nullable expression: expression1"), new ConstraintViolation("Invalid [mref] value [[]] for attribute [lbl-attr2] of entity [lbl-entity] with type [entityType]. Offended nullable expression: expression2"));
    assertEquals(expectedConstraintViolations, newHashSet(constraintViolations));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ConstraintViolation

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

the class EntityAttributesValidatorTest method checkRangeMinOnlyInvalid.

@Test
void checkRangeMinOnlyInvalid() {
    Entity entity = new DynamicEntity(intRangeMinMeta);
    entity.set("id", "123");
    entity.set("intrangemin", -1);
    Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity, intRangeMinMeta);
    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)

Example 5 with ConstraintViolation

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

the class EntityAttributesValidatorTest method checkRangeMaxOnly.

@Test
void checkRangeMaxOnly() {
    Entity entity = new DynamicEntity(intRangeMaxMeta);
    entity.set("id", "123");
    entity.set("intrangemin", 0);
    Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity, intRangeMaxMeta);
    assertTrue(constraints.isEmpty());
}
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