/** * @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"); }
/** * @see LocationService#getLocationAttributeTypeByName(String) + */ @Test public void getLocationAttributeTypeByName_shouldReturnNullIfNoLocationAttributeTypeExistsWithTheSpecifiedName() { executeDataSet(LOC_ATTRIBUTE_DATA_XML); Assert.assertNull(Context.getLocationService().getLocationAttributeTypeByName("not-a-name")); }
/** * @see LocationService#getLocationAttributeTypeByName(String) */ @Test public void getLocationAttributeTypeByName_shouldReturnTheLocationAttributeTypeWithTheSpecifiedName() { executeDataSet(LOC_ATTRIBUTE_DATA_XML); LocationAttributeType locationAttributeType = Context.getLocationService().getLocationAttributeTypeByName( "Audit Date"); Assert.assertNotNull(locationAttributeType); Assert.assertEquals("Audit Date", locationAttributeType.getName()); }
/** * @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()); }
/** * @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")); }