Search in sources :

Example 1 with FieldType

use of org.openmrs.FieldType in project openmrs-core by openmrs.

the class FormServiceTest method getFieldTypeByUuid_shouldFindObjectGivenValidUuid.

/**
 * @see FormService#getFieldTypeByUuid(String)
 */
@Test
public void getFieldTypeByUuid_shouldFindObjectGivenValidUuid() {
    String uuid = "e7016b7d-39a5-4911-89da-0eefbfef7cb5";
    FieldType fieldType = Context.getFormService().getFieldTypeByUuid(uuid);
    assertEquals(2, (int) fieldType.getFieldTypeId());
}
Also used : FieldType(org.openmrs.FieldType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 2 with FieldType

use of org.openmrs.FieldType in project openmrs-core by openmrs.

the class FormServiceTest method shouldFieldCreateModifyDelete.

/**
 * Test create then update a field
 *
 * @throws Exception
 */
@Test
public void shouldFieldCreateModifyDelete() {
    executeDataSet(INITIAL_FIELDS_XML);
    FormService formService = Context.getFormService();
    ConceptService conceptService = Context.getConceptService();
    // testing creation
    Concept concept1 = conceptService.getConcept(1);
    String name1 = "name1";
    String descript1 = "descript1";
    FieldType fieldtype1 = formService.getAllFieldTypes().get(0);
    String table1 = "table1";
    String attr1 = "attr1";
    Boolean multi1 = true;
    Field field1 = new Field();
    field1.setConcept(concept1);
    field1.setName(name1);
    field1.setDescription(descript1);
    field1.setFieldType(fieldtype1);
    field1.setTableName(table1);
    field1.setAttributeName(attr1);
    field1.setSelectMultiple(multi1);
    formService.saveField(field1);
    // testing update
    Field field2 = formService.getField(field1.getFieldId());
    Concept concept2 = conceptService.getConcept(2);
    String name2 = "name2";
    String descript2 = "descript2";
    FieldType fieldtype2 = formService.getAllFieldTypes().get(1);
    String table2 = "table2";
    String attr2 = "attr2";
    Boolean multi2 = false;
    field2.setConcept(concept2);
    field2.setName(name2);
    field2.setDescription(descript2);
    field2.setFieldType(fieldtype2);
    field2.setTableName(table2);
    field2.setAttributeName(attr2);
    field2.setSelectMultiple(multi2);
    formService.saveField(field2);
    // testing differences
    Field field3 = formService.getField(field2.getFieldId());
    assertTrue(field3.equals(field1));
    assertTrue(field1.getConcept().equals(concept2));
    assertTrue(field1.getName().equals(name2));
    assertTrue(field1.getDescription().equals(descript2));
    assertTrue(field1.getFieldType().equals(fieldtype2));
    assertTrue(field1.getTableName().equals(table2));
    assertTrue(field1.getAttributeName().equals(attr2));
    assertTrue(field1.getSelectMultiple().equals(multi2));
    // testing deletion
    formService.saveField(field3);
    formService.purgeField(field3);
    assertNull(formService.getField(field3.getFieldId()));
}
Also used : Concept(org.openmrs.Concept) FormField(org.openmrs.FormField) Field(org.openmrs.Field) FieldType(org.openmrs.FieldType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with FieldType

use of org.openmrs.FieldType in project openmrs-core by openmrs.

the class FieldTypeValidator method validate.

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should fail validation if field type name already exist in none retired filed types
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    FieldType fieldType = (FieldType) obj;
    if (fieldType == null) {
        errors.rejectValue("fieldType", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        if (!errors.hasErrors()) {
            FieldType exist = Context.getFormService().getFieldTypeByName(fieldType.getName());
            if (exist != null && !exist.getRetired() && !OpenmrsUtil.nullSafeEquals(fieldType.getUuid(), exist.getUuid())) {
                errors.rejectValue("name", "fieldtype.duplicate.name");
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name");
    }
}
Also used : FieldType(org.openmrs.FieldType)

Example 4 with FieldType

use of org.openmrs.FieldType in project openmrs-core by openmrs.

the class FieldValidatorTest method validate_shouldFailValidationIfNameIsAllWhitespace.

/**
 * @see FieldValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfNameIsAllWhitespace() {
    Field ff = new Field();
    ff.setName("    ");
    FieldType ft = new FieldType();
    Boolean retired = Boolean.FALSE;
    ft.setId(0xdeadcafe);
    ff.setFieldType(ft);
    ff.setRetired(retired);
    Boolean multiple = Boolean.FALSE;
    ff.setSelectMultiple(multiple);
    Errors errors = new BindException(ff, "name");
    new FieldValidator().validate(ff, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
}
Also used : Field(org.openmrs.Field) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) FieldType(org.openmrs.FieldType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with FieldType

use of org.openmrs.FieldType in project openmrs-core by openmrs.

the class FieldValidatorTest method validate_shouldFailValidationIfSelectMultipleIsNull.

/**
 * @see FieldValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfSelectMultipleIsNull() {
    Field ff = new Field();
    ff.setName("good");
    FieldType ft = new FieldType();
    ft.setFieldTypeId(0xdeadcafe);
    Boolean retired = Boolean.FALSE;
    ff.setFieldType(ft);
    ff.setName("valid");
    ff.setRetired(retired);
    Boolean multiple = null;
    ff.setSelectMultiple(multiple);
    Errors errors = new BindException(ff, "selectMultiple");
    new FieldValidator().validate(ff, errors);
    Assert.assertTrue(errors.hasFieldErrors("selectMultiple"));
}
Also used : Field(org.openmrs.Field) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) FieldType(org.openmrs.FieldType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

FieldType (org.openmrs.FieldType)18 Test (org.junit.Test)17 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)17 BindException (org.springframework.validation.BindException)13 Errors (org.springframework.validation.Errors)13 Field (org.openmrs.Field)9 Concept (org.openmrs.Concept)1 FormField (org.openmrs.FormField)1