Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow up
- Loading branch information
Showing
8 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
api/src/main/java/org/openmrs/validator/OrderTypeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.openmrs.validator; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.openmrs.OrderType; | ||
import org.openmrs.annotation.Handler; | ||
import org.openmrs.api.context.Context; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.validation.Validator; | ||
|
||
/** | ||
* Validates the {@link OrderType} class. | ||
* | ||
* @since 1.10 | ||
*/ | ||
@Handler(supports = { OrderType.class }) | ||
public class OrderTypeValidator implements Validator { | ||
|
||
// Log for this class | ||
protected final Log log = LogFactory.getLog(getClass()); | ||
|
||
/** | ||
* Determines if the command object being submitted is a valid type | ||
* | ||
* @see org.springframework.validation.Validator#supports(java.lang.Class) | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public boolean supports(Class c) { | ||
return OrderType.class.isAssignableFrom(c); | ||
} | ||
|
||
/** | ||
* Checks theobject for any inconsistencies/errors | ||
* | ||
* @see org.springframework.validation.Validator#validate(java.lang.Object, | ||
* org.springframework.validation.Errors) | ||
* @should fail if the orderType object is null | ||
* @should fail if name is null | ||
* @should fail if name is empty | ||
* @should fail if name is whitespace | ||
* @should fail if name is a duplicate | ||
* @should pass if all fields are correct | ||
*/ | ||
public void validate(Object obj, Errors errors) { | ||
if (obj == null || !(obj instanceof OrderType)) { | ||
throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + OrderType.class); | ||
} else { | ||
OrderType orderType = (OrderType) obj; | ||
|
||
String name = orderType.getName(); | ||
if (!StringUtils.hasText(name)) { | ||
errors.rejectValue("name", "error.name"); | ||
} | ||
|
||
OrderType ot = Context.getOrderService().getOrderTypeByName(name); | ||
if (ot != null && !orderType.equals(ot)) { | ||
errors.rejectValue("name", "OrderType.duplicate.name", "Duplicate order type name: " + name); | ||
} | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
api/src/test/java/org/openmrs/validator/OrderTypeValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.openmrs.validator; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.openmrs.OrderType; | ||
import org.openmrs.test.BaseContextSensitiveTest; | ||
import org.openmrs.test.Verifies; | ||
import org.springframework.validation.BindException; | ||
import org.springframework.validation.Errors; | ||
|
||
/** | ||
* Contains tests methods for the {@link OrderTypeValidator} | ||
*/ | ||
public class OrderTypeValidatorTest extends BaseContextSensitiveTest { | ||
|
||
/** | ||
* @see {@link OrderTypeValidator#validate(Object,Errors)} | ||
*/ | ||
@Test(expected = IllegalArgumentException.class) | ||
@Verifies(value = "should fail if the orderType object is null", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfTheOrderTypeObjectIsNull() throws Exception { | ||
Errors errors = new BindException(new OrderType(), "orderType"); | ||
new OrderTypeValidator().validate(null, errors); | ||
} | ||
|
||
/** | ||
* @see {@link OrderTypeValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should fail if name is null", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfNameIsNull() throws Exception { | ||
OrderType orderType = new OrderType(); | ||
Errors errors = new BindException(orderType, "orderType"); | ||
new OrderTypeValidator().validate(orderType, errors); | ||
Assert.assertEquals(true, errors.hasFieldErrors("name")); | ||
} | ||
|
||
/** | ||
* @see {@link OrderTypeValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should fail if name is empty", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfNameIsEmpty() throws Exception { | ||
OrderType orderType = new OrderType(); | ||
orderType.setName(""); | ||
Errors errors = new BindException(orderType, "orderType"); | ||
new OrderTypeValidator().validate(orderType, errors); | ||
Assert.assertEquals(true, errors.hasFieldErrors("name")); | ||
} | ||
|
||
/** | ||
* @see {@link OrderTypeValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should fail if name is white space", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfNameIsWhiteSpace() throws Exception { | ||
OrderType orderType = new OrderType(); | ||
orderType.setName(" "); | ||
Errors errors = new BindException(orderType, "orderType"); | ||
new OrderTypeValidator().validate(orderType, errors); | ||
Assert.assertEquals(true, errors.hasFieldErrors("name")); | ||
} | ||
|
||
/** | ||
* @see {@link OrderTypeValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should fail if name is a duplicate", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfNameIsADuplicate() throws Exception { | ||
OrderType orderType = new OrderType(); | ||
orderType.setName("Drug order"); | ||
Errors errors = new BindException(orderType, "orderType"); | ||
new OrderTypeValidator().validate(orderType, errors); | ||
Assert.assertEquals(true, errors.hasFieldErrors("name")); | ||
} | ||
|
||
/** | ||
* @see {@link OrderTypeValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should pass if all fields are correct", method = "validate(Object,Errors)") | ||
public void validate_shouldPassIfAllFieldsAreCorrect() throws Exception { | ||
OrderType orderType = new OrderType(); | ||
orderType.setName("unique name"); | ||
Errors errors = new BindException(orderType, "orderType"); | ||
new OrderTypeValidator().validate(orderType, errors); | ||
|
||
Assert.assertFalse(errors.hasErrors()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters