/** @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();
    Encounter encounter = new Encounter();
    Patient patient = Context.getPatientService().getPatient(2);
    order.setConcept(Context.getConceptService().getConcept(88));
    order.setOrderer(Context.getProviderService().getProvider(1));
    order.setDosingType(DrugOrder.DosingType.FREE_TEXT);
    order.setInstructions("Instructions");
    order.setDosingInstructions("Test Instruction");
    order.setPatient(patient);
    encounter.setPatient(patient);
    order.setEncounter(encounter);
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 1);
    order.setStartDate(cal.getTime());
    order.setAutoExpireDate(new Date());
    order.setOrderType(Context.getOrderService().getOrderTypeByName("Drug order"));
    order.setDrug(Context.getConceptService().getDrug(3));
    order.setCareSetting(Context.getOrderService().getCareSetting(1));
    order.setQuantity(2.00);
    order.setQuantityUnits(Context.getConceptService().getConcept(51));
    order.setNumRefills(10);

    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertFalse(errors.hasErrors());
  }
 /**
  * @verifies fail validation if frequency is null for SIMPLE dosingType
  * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
  */
 @Test
 public void validate_shouldFailValidationIfFrequencyIsNullForSIMPLEDosingType() throws Exception {
   DrugOrder order = new DrugOrder();
   order.setDosingType(DrugOrder.DosingType.SIMPLE);
   order.setFrequency(null);
   Errors errors = new BindException(order, "order");
   new DrugOrderValidator().validate(order, errors);
   Assert.assertTrue(errors.hasFieldErrors("frequency"));
 }
 /**
  * @verifies fail validation if doseUnits is null when dose is present
  * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
  */
 @Test
 public void validate_shouldFailValidationIfDoseUnitsIsNullWhenDoseIsPresent() throws Exception {
   DrugOrder order = new DrugOrder();
   order.setDosingType(DrugOrder.DosingType.FREE_TEXT);
   order.setDose(20.0);
   order.setDoseUnits(null);
   Errors errors = new BindException(order, "order");
   new DrugOrderValidator().validate(order, errors);
   Assert.assertTrue(errors.hasFieldErrors("doseUnits"));
 }
 /**
  * @verifies fail validation if dosingInstructions is null for FREE_TEXT dosingType
  * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
  */
 @Test
 public void validate_shouldFailValidationIfDosingInstructionsIsNullForFREE_TEXTDosingType()
     throws Exception {
   DrugOrder order = new DrugOrder();
   order.setDosingType(DrugOrder.DosingType.FREE_TEXT);
   order.setDosingInstructions(null);
   Errors errors = new BindException(order, "order");
   new DrugOrderValidator().validate(order, errors);
   Assert.assertTrue(errors.hasFieldErrors("dosingInstructions"));
 }
  /** @see {@link DrugOrderValidator#validate(Object,Errors)} */
  @Test
  @Verifies(
      value = "should fail validation if dosingType is null",
      method = "validate(Object,Errors)")
  public void validate_shouldFailValidationIfDosingTypeIsNull() throws Exception {
    DrugOrder order = new DrugOrder();
    order.setDosingType(null);
    order.setDrug(Context.getConceptService().getDrug(3));

    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);

    Assert.assertTrue(errors.hasFieldErrors("dosingType"));
  }
  /**
   * @see DrugOrderValidator#validate(Object,Errors)
   * @verifies fail validation if doseUnits is not a dose unit concept
   */
  @Test
  public void validate_shouldFailValidationIfDoseUnitsIsNotADoseUnitConcept() throws Exception {
    Concept concept = Context.getConceptService().getConcept(3);
    assertThat(concept, not(isIn(Context.getOrderService().getDrugDosingUnits())));

    DrugOrder order = new DrugOrder();
    order.setDosingType(DrugOrder.DosingType.FREE_TEXT);
    order.setDuration(5.0);
    order.setDurationUnits(concept);
    order.setDose(1.0);
    order.setDoseUnits(concept);
    order.setQuantity(30.0);
    order.setQuantityUnits(concept);

    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertTrue(errors.hasFieldErrors("doseUnits"));
  }
  /**
   * @verifies not require all fields for a discontinuation order
   * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
   */
  @Test
  public void validate_shouldNotRequireAllFieldsForADiscontinuationOrder() throws Exception {
    DrugOrder orderToDiscontinue = (DrugOrder) Context.getOrderService().getOrder(111);
    assertTrue(OrderUtilTest.isActiveOrder(orderToDiscontinue, null));
    DrugOrder discontinuationOrder = new DrugOrder();
    discontinuationOrder.setDosingType(null);
    discontinuationOrder.setCareSetting(orderToDiscontinue.getCareSetting());
    discontinuationOrder.setConcept(orderToDiscontinue.getConcept());
    discontinuationOrder.setAction(Order.Action.DISCONTINUE);
    discontinuationOrder.setPreviousOrder(orderToDiscontinue);
    discontinuationOrder.setPatient(orderToDiscontinue.getPatient());
    discontinuationOrder.setDrug(orderToDiscontinue.getDrug());
    discontinuationOrder.setOrderType(orderToDiscontinue.getOrderType());
    discontinuationOrder.setOrderer(Context.getProviderService().getProvider(1));
    discontinuationOrder.setEncounter(Context.getEncounterService().getEncounter(3));

    Errors errors = new BindException(discontinuationOrder, "order");
    new DrugOrderValidator().validate(discontinuationOrder, errors);
    Assert.assertFalse(errors.hasErrors());
  }
  /**
   * @verifies fail if route is not a valid concept
   * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
   */
  @Test
  public void validate_shouldFailIfRouteIsNotAValidConcept() throws Exception {
    Concept concept = Context.getConceptService().getConcept(3);
    assertThat(concept, not(isIn(Context.getOrderService().getDrugRoutes())));

    DrugOrder order = new DrugOrder();
    order.setDosingType(DrugOrder.DosingType.FREE_TEXT);
    order.setDuration(5.0);
    order.setDurationUnits(concept);
    order.setDose(1.0);
    order.setDoseUnits(concept);
    order.setQuantity(30.0);
    order.setQuantityUnits(concept);
    order.setRoute(concept);

    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertTrue(errors.hasFieldErrors("route"));
    Assert.assertEquals(
        "DrugOrder.error.routeNotAmongAllowedConcepts", errors.getFieldError("route").getCode());
  }
 @Override
 public void setDosingInstructions(DrugOrder order) {
   order.setDosingType(this.getClass());
 }