/**
   * @see RegimenManager#findDefinitions(String, org.openmrs.module.kenyaemr.regimen.RegimenOrder,
   *     boolean)
   */
  @Test
  public void findDefinitions_shouldFindDefinitionsForRegimen() {
    // Create regimen that matches the regimen2 definition exactly
    DrugOrder lamivudine = new DrugOrder();
    lamivudine.setConcept(Context.getConceptService().getConcept(78643));
    lamivudine.setDose(150d);
    lamivudine.setUnits("mg");
    lamivudine.setFrequency("BD");
    DrugOrder stavudine = new DrugOrder();
    stavudine.setConcept(Context.getConceptService().getConcept(84309));
    stavudine.setDose(30d);
    stavudine.setUnits("mg");
    stavudine.setFrequency("OD");
    RegimenOrder regimen =
        new RegimenOrder(new HashSet<DrugOrder>(Arrays.asList(lamivudine, stavudine)));

    // Test exact match
    List<RegimenDefinition> defsExact = regimenManager.findDefinitions("category1", regimen, true);
    Assert.assertEquals(1, defsExact.size());
    Assert.assertEquals("regimen2", defsExact.get(0).getName());

    // Test non-exact match
    List<RegimenDefinition> defsNonExact =
        regimenManager.findDefinitions("category1", regimen, false);
    Assert.assertEquals(2, defsNonExact.size());
    Assert.assertEquals("regimen2", defsNonExact.get(0).getName());
    Assert.assertEquals("regimen3", defsNonExact.get(1).getName());
  }
 /**
  * @verifies fail validation if dose is null for SIMPLE dosingType
  * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
  */
 @Test
 public void validate_shouldFailValidationIfDoseIsNullForSIMPLEDosingType() throws Exception {
   DrugOrder order = new DrugOrder();
   order.setDosingType(DrugOrder.DosingType.SIMPLE);
   order.setDose(null);
   Errors errors = new BindException(order, "order");
   new DrugOrderValidator().validate(order, errors);
   Assert.assertTrue(errors.hasFieldErrors("dose"));
 }
 /**
  * @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"));
 }
 private DrugOrder createValidDrugOrder() {
   DrugOrder drugOrder = new DrugOrder();
   drugOrder.setDose(10.0);
   drugOrder.setDoseUnits(createConceptWithName("ml"));
   drugOrder.setRoute(createConceptWithName("IV"));
   OrderFrequency frequency = new OrderFrequency();
   frequency.setConcept(createConceptWithName("Twice a day"));
   drugOrder.setFrequency(frequency);
   return drugOrder;
 }
  /**
   * @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 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());
  }