Skip to content

Commit

Permalink
Updated OrderFrequencyValidator to implement spring Validator and add…
Browse files Browse the repository at this point in the history
…ed tests to ensure that frequency and order type validators get invoked when their supported types get saved
  • Loading branch information
wluyima committed Mar 20, 2014
1 parent 216e0e1 commit 30fe90d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
12 changes: 12 additions & 0 deletions api/src/main/java/org/openmrs/OrderType.java
Expand Up @@ -16,6 +16,7 @@
import java.util.Collection;
import java.util.LinkedHashSet;

import org.apache.commons.lang.StringUtils;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;

Expand Down Expand Up @@ -167,4 +168,15 @@ public Class getJavaClass() throws APIException {
public void addConceptClass(ConceptClass conceptClass) {
getConceptClasses().add(conceptClass);
}

/**
* @see org.openmrs.BaseOpenmrsObject#toString()
*/
@Override
public String toString() {
if (StringUtils.isNotBlank(getName())) {
return getName();
}
return super.toString();
}
}
Expand Up @@ -21,14 +21,15 @@
import org.openmrs.api.context.Context;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
* Validates the {@link OrderFrequency} class.
*
* @since 1.10
*/
@Handler(supports = { OrderFrequency.class })
public class OrderFrequencyValidator {
public class OrderFrequencyValidator implements Validator {

/** Log for this class and subclasses */
protected final Log log = LogFactory.getLog(getClass());
Expand All @@ -39,6 +40,7 @@ public class OrderFrequencyValidator {
* @see org.springframework.validation.Validator#supports(java.lang.Class)
*/
@SuppressWarnings("unchecked")
@Override
public boolean supports(Class c) {
return OrderFrequency.class.isAssignableFrom(c);
}
Expand All @@ -54,7 +56,9 @@ public boolean supports(Class c) {
* @should fail if concept is used by another frequency
* @should pass for a valid new order frequency
* @should pass for a valid existing order frequency
* @should be invoked when an order frequency is saved
*/
@Override
public void validate(Object obj, Errors errors) {
OrderFrequency orderFrequency = (OrderFrequency) obj;
if (orderFrequency == null) {
Expand Down
Expand Up @@ -41,6 +41,7 @@ public class OrderTypeValidator implements Validator {
* @see org.springframework.validation.Validator#supports(java.lang.Class)
*/
@SuppressWarnings("rawtypes")
@Override
public boolean supports(Class c) {
return OrderType.class.isAssignableFrom(c);
}
Expand All @@ -59,7 +60,9 @@ public boolean supports(Class c) {
* @should fail if conceptClass is a duplicate
* @should pass if all fields are correct for a new order type
* @should pass if all fields are correct for an existing order type
* @should be invoked when an order type is saved
*/
@Override
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);
Expand Down
Expand Up @@ -16,10 +16,13 @@
import static junit.framework.Assert.assertNotNull;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.openmrs.Concept;
import org.openmrs.ConceptName;
import org.openmrs.OrderFrequency;
import org.openmrs.api.APIException;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
import org.openmrs.test.BaseContextSensitiveTest;
Expand All @@ -32,6 +35,9 @@
*/
public class OrderFrequencyValidatorTest extends BaseContextSensitiveTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

/**
* @see {@link OrderFrequencyValidator#validate(Object,Errors)}
*/
Expand Down Expand Up @@ -120,4 +126,18 @@ public void validate_shouldPassForAValidExistingOrderFrequency() throws Exceptio

Assert.assertFalse(errors.hasErrors());
}

/**
* @verifies be invoked when an order frequency is saved
* @see OrderFrequencyValidator#validate(Object, org.springframework.validation.Errors)
*/
@Test
public void validate_shouldBeInvokedWhenAnOrderFrequencyIsSaved() throws Exception {
OrderFrequency orderFrequency = Context.getOrderService().getOrderFrequency(2);
orderFrequency.setConcept(null);
expectedException.expect(APIException.class);
String expectedMsg = "'" + orderFrequency + "' failed to validate with reason: concept: Concept.noConceptSelected";
expectedException.expectMessage(expectedMsg);
Context.getOrderService().saveOrderFrequency(orderFrequency);
}
}
Expand Up @@ -20,9 +20,12 @@
import java.util.HashSet;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.openmrs.ConceptClass;
import org.openmrs.OrderType;
import org.openmrs.api.APIException;
import org.openmrs.api.OrderService;
import org.openmrs.api.context.Context;
import org.openmrs.test.BaseContextSensitiveTest;
Expand All @@ -39,6 +42,9 @@ public class OrderTypeValidatorTest extends BaseContextSensitiveTest {
@Autowired
private OrderService orderService;

@Rule
public ExpectedException expectedException = ExpectedException.none();

/**
* @see {@link OrderTypeValidator#validate(Object,Errors)}
*/
Expand Down Expand Up @@ -174,4 +180,18 @@ public void validate_shouldPassIfAllFieldsAreCorrectForAnExistingOrderType() thr

Assert.assertFalse(errors.hasErrors());
}

/**
* @verifies be invoked when an order type is saved
* @see OrderTypeValidator#validate(Object, org.springframework.validation.Errors)
*/
@Test
public void validate_shouldBeInvokedWhenAnOrderTypeIsSaved() throws Exception {
OrderType orderType = orderService.getOrderType(1);
orderType.setName(null);
expectedException.expect(APIException.class);
String expectedMsg = "'" + orderType + "' failed to validate with reason: name: error.name";
expectedException.expectMessage(expectedMsg);
orderService.saveOrderType(orderType);
}
}

0 comments on commit 30fe90d

Please sign in to comment.