Search in sources :

Example 1 with TypeValidationDto

use of org.motechproject.mds.dto.TypeValidationDto in project motech by motech.

the class TypeValidation method toDto.

public TypeValidationDto toDto() {
    TypeValidationDto dto = new TypeValidationDto();
    dto.setId(id);
    dto.setDisplayName(displayName);
    dto.setValueType(valueType.getTypeClassName());
    dto.setAnnotations(annotations);
    return dto;
}
Also used : TypeValidationDto(org.motechproject.mds.dto.TypeValidationDto)

Example 2 with TypeValidationDto

use of org.motechproject.mds.dto.TypeValidationDto in project motech by motech.

the class FieldProcessorTest method shouldAssignFieldValidation.

@Test
public void shouldAssignFieldValidation() throws Exception {
    TypeValidationDto intMinValue = new TypeValidationDto("mds.field.validation.minValue", Integer.class.getName());
    TypeValidationDto intMaxValue = new TypeValidationDto("mds.field.validation.maxValue", Integer.class.getName());
    TypeValidationDto intMustBeInSet = new TypeValidationDto("mds.field.validation.mustBeInSet", String.class.getName());
    TypeValidationDto intCannotBeInSet = new TypeValidationDto("mds.field.validation.cannotBeInSet", String.class.getName());
    TypeValidationDto decMinValue = new TypeValidationDto("mds.field.validation.minValue", Double.class.getName());
    TypeValidationDto decMaxValue = new TypeValidationDto("mds.field.validation.maxValue", Double.class.getName());
    TypeValidationDto decMustBeInSet = new TypeValidationDto("mds.field.validation.mustBeInSet", String.class.getName());
    TypeValidationDto decCannotBeInSet = new TypeValidationDto("mds.field.validation.cannotBeInSet", String.class.getName());
    TypeValidationDto regex = new TypeValidationDto("mds.field.validation.regex", String.class.getName());
    TypeValidationDto minLength = new TypeValidationDto("mds.field.validation.minLength", Integer.class.getName());
    TypeValidationDto maxLength = new TypeValidationDto("mds.field.validation.maxLength", Integer.class.getName());
    doReturn(singletonList(intMinValue)).when(schemaHolder).findValidations(Integer.class.getName(), DecimalMin.class);
    doReturn(singletonList(intMaxValue)).when(schemaHolder).findValidations(Integer.class.getName(), DecimalMax.class);
    doReturn(singletonList(intMustBeInSet)).when(schemaHolder).findValidations(Integer.class.getName(), InSet.class);
    doReturn(singletonList(intCannotBeInSet)).when(schemaHolder).findValidations(Integer.class.getName(), NotInSet.class);
    doReturn(singletonList(intMinValue)).when(schemaHolder).findValidations(Integer.class.getName(), Min.class);
    doReturn(singletonList(intMaxValue)).when(schemaHolder).findValidations(Integer.class.getName(), Max.class);
    doReturn(singletonList(decMinValue)).when(schemaHolder).findValidations(Double.class.getName(), DecimalMin.class);
    doReturn(singletonList(decMaxValue)).when(schemaHolder).findValidations(Double.class.getName(), DecimalMax.class);
    doReturn(singletonList(decMustBeInSet)).when(schemaHolder).findValidations(Double.class.getName(), InSet.class);
    doReturn(singletonList(decCannotBeInSet)).when(schemaHolder).findValidations(Double.class.getName(), NotInSet.class);
    doReturn(singletonList(decMinValue)).when(schemaHolder).findValidations(Double.class.getName(), Min.class);
    doReturn(singletonList(decMaxValue)).when(schemaHolder).findValidations(Double.class.getName(), Max.class);
    doReturn(singletonList(regex)).when(schemaHolder).findValidations(String.class.getName(), Pattern.class);
    doReturn(asList(minLength, maxLength)).when(schemaHolder).findValidations(String.class.getName(), Size.class);
    doReturn(singletonList(minLength)).when(schemaHolder).findValidations(String.class.getName(), DecimalMin.class);
    doReturn(singletonList(maxLength)).when(schemaHolder).findValidations(String.class.getName(), DecimalMax.class);
    processor.execute(bundle, schemaHolder);
    Collection<FieldDto> fields = processor.getElements();
    FieldDto pi = findFieldWithName(fields, "pi");
    assertCriterion(pi, "mds.field.validation.minValue", "3");
    assertCriterion(pi, "mds.field.validation.maxValue", "4");
    assertCriterion(pi, "mds.field.validation.mustBeInSet", "3,3.14,4");
    assertCriterion(pi, "mds.field.validation.cannotBeInSet", "1,2,5");
    FieldDto epsilon = findFieldWithName(fields, "epsilon");
    assertCriterion(epsilon, "mds.field.validation.minValue", "0.0");
    assertCriterion(epsilon, "mds.field.validation.maxValue", "1.0");
    assertCriterion(epsilon, "mds.field.validation.mustBeInSet", "1,0.75,0.5,0.25,0");
    assertCriterion(epsilon, "mds.field.validation.cannotBeInSet", "-1,2,3");
    FieldDto random = findFieldWithName(fields, "random");
    assertCriterion(random, "mds.field.validation.minValue", "0");
    assertCriterion(random, "mds.field.validation.maxValue", "10");
    FieldDto gaussian = findFieldWithName(fields, "gaussian");
    assertCriterion(gaussian, "mds.field.validation.minValue", "0.0");
    assertCriterion(gaussian, "mds.field.validation.maxValue", "1.0");
    FieldDto poem = findFieldWithName(fields, "poem");
    assertCriterion(poem, "mds.field.validation.regex", "[A-Z][a-z]{9}");
    assertCriterion(poem, "mds.field.validation.minLength", "10");
    assertCriterion(poem, "mds.field.validation.maxLength", "20");
    FieldDto article = findFieldWithName(fields, "article");
    assertCriterion(article, "mds.field.validation.minLength", "100");
    assertCriterion(article, "mds.field.validation.maxLength", "500");
}
Also used : TypeValidationDto(org.motechproject.mds.dto.TypeValidationDto) Matchers.anyString(org.mockito.Matchers.anyString) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 3 with TypeValidationDto

use of org.motechproject.mds.dto.TypeValidationDto in project motech by motech.

the class FieldProcessor method createValidation.

private FieldValidationDto createValidation(AccessibleObject ac, TypeDto type) {
    FieldValidationDto validationDto = null;
    for (Annotation annotation : ac.getAnnotations()) {
        List<TypeValidationDto> validations = getSchemaHolder().findValidations(type.getTypeClass(), annotation.annotationType());
        for (TypeValidationDto validation : validations) {
            String displayName = validation.getDisplayName();
            TypeDto valueType = getSchemaHolder().getType(validation.getValueType());
            if (null == valueType) {
                throw new IllegalStateException("The valueType is not set in: " + validation);
            }
            String valueAsString = getValidationValue(displayName, annotation);
            if (InSet.class.isAssignableFrom(annotation.annotationType()) || NotInSet.class.isAssignableFrom(annotation.annotationType())) {
                valueAsString = valueAsString.replaceAll("(\\{|\\})", "");
            }
            Object value = TypeHelper.parse(valueAsString, valueType.getClassObjectForType());
            ValidationCriterionDto dto = new ValidationCriterionDto();
            dto.setDisplayName(displayName);
            dto.setType(valueType);
            dto.setEnabled(true);
            dto.setValue(value);
            if (null == validationDto) {
                validationDto = new FieldValidationDto();
            }
            validationDto.addCriterion(dto);
        }
    }
    return validationDto;
}
Also used : NotInSet(org.motechproject.mds.annotations.NotInSet) TypeValidationDto(org.motechproject.mds.dto.TypeValidationDto) ValidationCriterionDto(org.motechproject.mds.dto.ValidationCriterionDto) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) TypeDto(org.motechproject.mds.dto.TypeDto) InSet(org.motechproject.mds.annotations.InSet) NotInSet(org.motechproject.mds.annotations.NotInSet) AccessibleObject(java.lang.reflect.AccessibleObject) Annotation(java.lang.annotation.Annotation)

Aggregations

TypeValidationDto (org.motechproject.mds.dto.TypeValidationDto)3 Annotation (java.lang.annotation.Annotation)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InSet (org.motechproject.mds.annotations.InSet)1 NotInSet (org.motechproject.mds.annotations.NotInSet)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)1 TypeDto (org.motechproject.mds.dto.TypeDto)1 ValidationCriterionDto (org.motechproject.mds.dto.ValidationCriterionDto)1