Search in sources :

Example 1 with LocationAttributeType

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

the class LocationAttributeTypeValidator method validate.

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null
 * @should fail validation if name already in use
 * @should pass validation if the location attribute type description is null or empty or whitespace
 * @should pass validation if all fields are correct
 * @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) {
    super.validate(obj, errors);
    LocationAttributeType locationObj = (LocationAttributeType) obj;
    LocationService ls = Context.getLocationService();
    if (locationObj.getName() != null && !locationObj.getName().isEmpty()) {
        LocationAttributeType loc = ls.getLocationAttributeTypeByName(locationObj.getName());
        if (loc != null && !loc.getUuid().equals(locationObj.getUuid())) {
            errors.rejectValue("name", "LocationAttributeType.error.nameAlreadyInUse");
        }
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "LocationAttributeType.error.nameEmpty");
    }
    ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "description", "datatypeClassname", "preferredHandlerClassname", "retireReason");
}
Also used : LocationService(org.openmrs.api.LocationService) LocationAttributeType(org.openmrs.LocationAttributeType)

Example 2 with LocationAttributeType

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

the class LocationAttributeTypeValidatorTest method validate_shouldFailIfLocationAttributeTypeNameIsDuplicate.

/**
 * @see LocationAttributeTypeValidator#validate(Object, Errors)
 */
@Test
public void validate_shouldFailIfLocationAttributeTypeNameIsDuplicate() {
    Assert.assertNotNull(Context.getLocationService().getLocationAttributeTypeByName("Audit Date"));
    LocationAttributeType type = new LocationAttributeType();
    type.setName("Audit Date");
    type.setDatatypeClassname("org.openmrs.customdatatype.datatype.FreeTextDatatype");
    Errors errors = new BindException(type, "locationAttributeType");
    new LocationAttributeTypeValidator().validate(type, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) LocationAttributeType(org.openmrs.LocationAttributeType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with LocationAttributeType

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

the class LocationAttributeTypeValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see LocationAttributeTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    LocationAttributeType type = new LocationAttributeType();
    type.setName("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    type.setDatatypeClassname("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    type.setDescription("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    type.setPreferredHandlerClassname("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    type.setRetireReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    Errors errors = new BindException(type, "type");
    new LocationAttributeTypeValidator().validate(type, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
    Assert.assertTrue(errors.hasFieldErrors("datatypeClassname"));
    Assert.assertTrue(errors.hasFieldErrors("description"));
    Assert.assertTrue(errors.hasFieldErrors("preferredHandlerClassname"));
    Assert.assertTrue(errors.hasFieldErrors("retireReason"));
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) LocationAttributeType(org.openmrs.LocationAttributeType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 4 with LocationAttributeType

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

the class LocationAttributeTypeValidatorTest method validate_shouldFailValidationIfNameIsNullOrEmptyOrWhitespace.

/**
 * @see LocationAttributeTypeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfNameIsNullOrEmptyOrWhitespace() {
    LocationAttributeType type = new LocationAttributeType();
    type.setName(null);
    type.setDescription("description");
    Errors errors = new BindException(type, "type");
    new LocationAttributeTypeValidator().validate(type, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
    type.setName("");
    errors = new BindException(type, "type");
    new LocationAttributeTypeValidator().validate(type, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
    type.setName(" ");
    errors = new BindException(type, "type");
    new LocationAttributeTypeValidator().validate(type, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) LocationAttributeType(org.openmrs.LocationAttributeType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with LocationAttributeType

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

the class LocationAttributeTypeValidatorTest method validate_shouldPassEditingLocationAttributeTypeName.

/**
 * @see LocationAttributeTypeValidator#validate(Object, Errors)
 */
@Test
public void validate_shouldPassEditingLocationAttributeTypeName() {
    LocationAttributeType et = Context.getLocationService().getLocationAttributeTypeByName("Audit Date");
    Assert.assertNotNull(et);
    Errors errors = new BindException(et, "locationAttributeType");
    new LocationAttributeTypeValidator().validate(et, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) LocationAttributeType(org.openmrs.LocationAttributeType) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

LocationAttributeType (org.openmrs.LocationAttributeType)16 Test (org.junit.Test)13 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)13 BindException (org.springframework.validation.BindException)6 Errors (org.springframework.validation.Errors)6 HashMap (java.util.HashMap)2 Location (org.openmrs.Location)2 LocationAttribute (org.openmrs.LocationAttribute)2 LocationService (org.openmrs.api.LocationService)1 FreeTextDatatype (org.openmrs.customdatatype.datatype.FreeTextDatatype)1