@Test
 public void getAutoExpireDate_shouldInferAutoExpireDateForAKnownSNOMEDCTDurationUnit()
     throws Exception {
   DrugOrder drugOrder = new DrugOrder();
   drugOrder.setDateActivated(createDateTime("2014-07-01 10:00:00"));
   drugOrder.setDuration(30);
   drugOrder.setDurationUnits(createUnits(Duration.SNOMED_CT_SECONDS_CODE));
   Date autoExpireDate = new SimpleDosingInstructions().getAutoExpireDate(drugOrder);
   assertEquals(createDateTime("2014-07-01 10:00:29"), autoExpireDate);
 }
 /**
  * @verifies fail validation if durationUnits is null when duration is present
  * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
  */
 @Test
 public void validate_shouldFailValidationIfDurationUnitsIsNullWhenDurationIsPresent()
     throws Exception {
   DrugOrder order = new DrugOrder();
   order.setDosingType(DrugOrder.DosingType.FREE_TEXT);
   order.setDuration(20.0);
   order.setDurationUnits(null);
   Errors errors = new BindException(order, "order");
   new DrugOrderValidator().validate(order, errors);
   Assert.assertTrue(errors.hasFieldErrors("durationUnits"));
 }
 @Test
 public void getAutoExpireDate_shouldInferAutoExpireDateForScheduledDrugOrder() throws Exception {
   DrugOrder drugOrder = new DrugOrder();
   drugOrder.setDateActivated(createDateTime("2014-07-01 00:00:00"));
   drugOrder.setScheduledDate(createDateTime("2014-07-05 00:00:00"));
   drugOrder.setUrgency(Order.Urgency.ON_SCHEDULED_DATE);
   drugOrder.setDuration(10);
   drugOrder.setDurationUnits(createUnits(Duration.SNOMED_CT_DAYS_CODE));
   Date autoExpireDate = new SimpleDosingInstructions().getAutoExpireDate(drugOrder);
   assertEquals(createDateTime("2014-07-14 23:59:59"), autoExpireDate);
 }
  @Test
  public void getAutoExpireDate_shouldNotInferAutoExpireDateWhenDurationUnitsDoesNotExist()
      throws Exception {
    DrugOrder drugOrder = new DrugOrder();
    drugOrder.setDateActivated(createDateTime("2014-07-01 10:00:00"));
    drugOrder.setDuration(1);
    drugOrder.setDurationUnits(null);

    Date autoExpireDate = new SimpleDosingInstructions().getAutoExpireDate(drugOrder);

    assertEquals(null, autoExpireDate);
  }
  /**
   * @verifies reject a duration unit with a mapping of an invalid type
   * @see SimpleDosingInstructions#validate(DrugOrder, org.springframework.validation.Errors)
   */
  @Test
  public void validate_shouldRejectADurationUnitWithAMappingOfAnInvalidType() throws Exception {
    DrugOrder drugOrder = createValidDrugOrder();
    drugOrder.setDuration(30);
    Concept durationUnitWithInvalidMapType =
        createUnits("SCT", Duration.SNOMED_CT_DAYS_CODE, "Some-uuid");
    drugOrder.setDurationUnits(durationUnitWithInvalidMapType);
    Errors errors = new BindException(drugOrder, "drugOrder");

    new SimpleDosingInstructions().validate(drugOrder, errors);

    assertEquals(true, errors.hasErrors());
  }
  @Test
  public void
      getAutoExpireDate_shouldNotInferAutoExpireDateWhenConceptMappingOfSourceSNOMEDCTDurationDoesNotExist()
          throws Exception {
    DrugOrder drugOrder = new DrugOrder();
    drugOrder.setDateActivated(createDateTime("2014-07-01 10:00:00"));
    drugOrder.setDuration(30);
    drugOrder.setDurationUnits(createUnits("Other.Source", Duration.SNOMED_CT_HOURS_CODE, null));

    Date autoExpireDate = new SimpleDosingInstructions().getAutoExpireDate(drugOrder);

    assertEquals(null, autoExpireDate);
  }
  @Test
  public void getAutoExpireDate_shouldNotInferAutoExpireDateWhenDrugOrderHasOneOrMoreRefill()
      throws Exception {
    DrugOrder drugOrder = new DrugOrder();
    drugOrder.setDateActivated(createDateTime("2014-07-01 10:00:00"));
    drugOrder.setDuration(30);
    drugOrder.setDurationUnits(createUnits(Duration.SNOMED_CT_SECONDS_CODE));
    drugOrder.setNumRefills(1);

    Date autoExpireDate = new SimpleDosingInstructions().getAutoExpireDate(drugOrder);

    assertEquals(null, autoExpireDate);
  }
  @Test
  public void
      validate_shouldPassValidationIfAutoExpireDateIsSetAndDurationUnitsIsNotMappedToSNOMEDCTDuration()
          throws Exception {
    DrugOrder drugOrder = createValidDrugOrder();
    drugOrder.setDuration(30);
    Concept unMappedDurationUnits = new Concept();
    drugOrder.setDurationUnits(unMappedDurationUnits);
    drugOrder.setAutoExpireDate(createDateTime("2014-07-01 10:00:00"));
    Errors errors = new BindException(drugOrder, "drugOrder");

    new SimpleDosingInstructions().validate(drugOrder, errors);

    Assert.assertFalse(errors.hasErrors());
  }
  /**
   * @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"));
  }
  @Test
  public void
      validate_shouldFailValidationIfAutoExpireDateIsNotSetAndDurationUnitsIsNotMappedToSNOMEDCTDuration()
          throws Exception {
    DrugOrder drugOrder = createValidDrugOrder();
    drugOrder.setDuration(30);
    Concept unMappedDurationUnits = new Concept();
    drugOrder.setDurationUnits(unMappedDurationUnits);
    drugOrder.setAutoExpireDate(null);
    Errors errors = new BindException(drugOrder, "drugOrder");

    new SimpleDosingInstructions().validate(drugOrder, errors);

    Assert.assertTrue(errors.hasFieldErrors("durationUnits"));
    Assert.assertEquals(
        "DrugOrder.error.durationUnitsNotMappedToSnomedCtDurationCode",
        errors.getFieldError("durationUnits").getCode());
  }
  /**
   * @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());
  }