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");
}
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"));
}
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"));
}
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"));
}
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());
}
Aggregations