/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.validator;
import java.util.Calendar;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import org.openmrs.DrugOrder;
import org.openmrs.api.context.Context;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.test.Verifies;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
/**
* Tests methods on the {@link DrugOrderValidator} class.
*/
public class DrugOrderValidatorTest extends BaseContextSensitiveTest {
/**
* @see {@link DrugOrderValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should fail validation if prn is null", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfPrnIsNull() throws Exception {
DrugOrder order = new DrugOrder();
order.setPrn(null);
order.setDrug(Context.getConceptService().getDrug(3));
Errors errors = new BindException(order, "order");
new DrugOrderValidator().validate(order, errors);
Assert.assertTrue(errors.hasFieldErrors("prn"));
}
/**
* @see {@link DrugOrderValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should fail validation if complex is null", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfComplexIsNull() throws Exception {
DrugOrder order = new DrugOrder();
order.setComplex(null);
order.setDrug(Context.getConceptService().getDrug(3));
Errors errors = new BindException(order, "order");
new DrugOrderValidator().validate(order, errors);
Assert.assertTrue(errors.hasFieldErrors("complex"));
}
/**
* @see {@link DrugOrderValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should not fail validation if drug is null", method = "validate(Object,Errors)")
public void validate_shouldNotFailValidationIfDrugIsNull() throws Exception {
DrugOrder order = new DrugOrder();
Errors errors = new BindException(order, "order");
new DrugOrderValidator().validate(order, errors);
Assert.assertFalse(errors.hasFieldErrors("drug"));
}
/**
* @see {@link DrugOrderValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should pass validation if all fields are correct", method = "validate(Object,Errors)")
public void validate_shouldPassValidationIfAllFieldsAreCorrect() throws Exception {
DrugOrder order = new DrugOrder();
order.setConcept(Context.getConceptService().getConcept(88));
order.setPatient(Context.getPatientService().getPatient(2));
order.setOrderType(Context.getOrderService().getOrderType(1));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 1);
order.setStartDate(cal.getTime());
order.setDiscontinuedDate(new Date());
order.setAutoExpireDate(new Date());
order.setDrug(Context.getConceptService().getDrug(3));
Errors errors = new BindException(order, "order");
new DrugOrderValidator().validate(order, errors);
Assert.assertFalse(errors.hasErrors());
}
}