Search in sources :

Example 6 with Drug

use of org.openmrs.Drug in project openmrs-core by openmrs.

the class OrderServiceTest method saveOrder_shouldFailIfTheExistingDrugOrderMatchesTheConceptAndNotDrugOfTheRevisedOrder.

/**
 * @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
 */
@Test
public void saveOrder_shouldFailIfTheExistingDrugOrderMatchesTheConceptAndNotDrugOfTheRevisedOrder() {
    final DrugOrder orderToDiscontinue = (DrugOrder) orderService.getOrder(5);
    // create a different test drug
    Drug discontinuationOrderDrug = new Drug();
    discontinuationOrderDrug.setConcept(orderToDiscontinue.getConcept());
    discontinuationOrderDrug = conceptService.saveDrug(discontinuationOrderDrug);
    assertNotEquals(discontinuationOrderDrug, orderToDiscontinue.getDrug());
    assertNotNull(orderToDiscontinue.getDrug());
    DrugOrder order = orderToDiscontinue.cloneForRevision();
    order.setDateActivated(new Date());
    order.setOrderer(providerService.getProvider(1));
    order.setEncounter(encounterService.getEncounter(6));
    order.setDrug(discontinuationOrderDrug);
    expectedException.expect(EditedOrderDoesNotMatchPreviousException.class);
    expectedException.expectMessage("The orderable of the previous order and the new one order don't match");
    orderService.saveOrder(order, null);
}
Also used : DrugOrder(org.openmrs.DrugOrder) Drug(org.openmrs.Drug) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 7 with Drug

use of org.openmrs.Drug in project openmrs-core by openmrs.

the class OrderServiceTest method saveOrder_shouldSetOrderTypeOfDrugOrderToDrugOrderIfNotSetAndConceptNotMapped.

/**
 * @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
 */
@Test
public void saveOrder_shouldSetOrderTypeOfDrugOrderToDrugOrderIfNotSetAndConceptNotMapped() {
    Drug drug = conceptService.getDrug(2);
    Concept unmappedConcept = conceptService.getConcept(113);
    Assert.assertNull(orderService.getOrderTypeByConcept(unmappedConcept));
    drug.setConcept(unmappedConcept);
    DrugOrder drugOrder = new DrugOrder();
    Encounter encounter = encounterService.getEncounter(3);
    drugOrder.setEncounter(encounter);
    drugOrder.setPatient(patientService.getPatient(7));
    drugOrder.setCareSetting(orderService.getCareSetting(1));
    drugOrder.setOrderer(Context.getProviderService().getProvider(1));
    drugOrder.setDateActivated(encounter.getEncounterDatetime());
    drugOrder.setDrug(drug);
    drugOrder.setDosingType(SimpleDosingInstructions.class);
    drugOrder.setDose(300.0);
    drugOrder.setDoseUnits(conceptService.getConcept(50));
    drugOrder.setQuantity(20.0);
    drugOrder.setQuantityUnits(conceptService.getConcept(51));
    drugOrder.setFrequency(orderService.getOrderFrequency(3));
    drugOrder.setRoute(conceptService.getConcept(22));
    drugOrder.setNumRefills(10);
    drugOrder.setOrderType(null);
    orderService.saveOrder(drugOrder, null);
    Assert.assertNotNull(drugOrder.getOrderType());
    Assert.assertEquals(orderService.getOrderTypeByUuid(OrderType.DRUG_ORDER_TYPE_UUID), drugOrder.getOrderType());
}
Also used : Drug(org.openmrs.Drug) Concept(org.openmrs.Concept) DrugOrder(org.openmrs.DrugOrder) Encounter(org.openmrs.Encounter) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 8 with Drug

use of org.openmrs.Drug in project openmrs-core by openmrs.

the class DrugValidator method validate.

/**
 * Validates an Drug object
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail if the drug object is null
 * @should fail if drug on drugReferenceMap is null
 * @should fail if conceptReferenceTerm on drugReferenceMap is null
 * @should invoke ConceptReferenceTermValidator if term on drugReferenceMap is new
 * @should invoke ConceptMapTypeValidator if conceptMapType on drugReferenceMap is new
 * @should pass if all fields are correct
 * @should reject drug multiple mappings to the same term
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    if (obj == null || !(obj instanceof Drug)) {
        throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + Drug.class);
    } else {
        Drug drug = (Drug) obj;
        Set<DrugReferenceMap> drugReferenceMaps = drug.getDrugReferenceMaps();
        Set<String> mappedTermUuids = new HashSet<>();
        int index = 0;
        for (DrugReferenceMap referenceMap : drugReferenceMaps) {
            Drug mappedDrug = referenceMap.getDrug();
            ConceptReferenceTerm referenceTerm = referenceMap.getConceptReferenceTerm();
            ConceptMapType mapType = referenceMap.getConceptMapType();
            if (mappedDrug == null) {
                errors.rejectValue("drugReferenceMaps[" + index + "].drug", "Drug.drugReferenceMap.mappedDrug");
            }
            if (referenceTerm == null) {
                errors.rejectValue("drugReferenceMaps[" + index + "].conceptReferenceTerm", "Drug.drugReferenceMap.conceptReferenceTerm");
            } else if (referenceTerm.getConceptReferenceTermId() == null) {
                try {
                    errors.pushNestedPath("drugReferenceMaps[" + index + "].conceptReferenceTerm");
                    ValidationUtils.invokeValidator(new ConceptReferenceTermValidator(), referenceTerm, errors);
                } finally {
                    errors.popNestedPath();
                }
            }
            if (mapType == null) {
                errors.rejectValue("drugReferenceMaps[" + index + "].conceptMapType", "Drug.drugReferenceMap.conceptMapType");
            } else if (mapType.getConceptMapTypeId() == null) {
                try {
                    errors.pushNestedPath("drugReferenceMaps[" + index + "].conceptMapType");
                    ValidationUtils.invokeValidator(new ConceptMapTypeValidator(), mapType, errors);
                } finally {
                    errors.popNestedPath();
                }
            }
            // don't proceed to the next map
            if (errors.hasErrors()) {
                return;
            }
            // if we already have a mapping to this term, reject it this map
            if (!mappedTermUuids.add(referenceMap.getConceptReferenceTerm().getUuid())) {
                errors.rejectValue("drugReferenceMaps[" + index + "].conceptReferenceTerm", "Drug.drugReferenceMap.termAlreadyMapped", "Cannot map a drug multiple times to the same reference term");
            }
            index++;
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "retireReason", "strength");
    }
}
Also used : Drug(org.openmrs.Drug) ConceptMapType(org.openmrs.ConceptMapType) DrugReferenceMap(org.openmrs.DrugReferenceMap) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm) HashSet(java.util.HashSet)

Example 9 with Drug

use of org.openmrs.Drug in project openmrs-core by openmrs.

the class DrugOrderValidatorTest method validate_shouldPassIfConceptIsNullAndDrugIsSet.

/**
 * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldPassIfConceptIsNullAndDrugIsSet() {
    DrugOrder order = new DrugOrder();
    order.setPatient(Context.getPatientService().getPatient(7));
    order.setCareSetting(Context.getOrderService().getCareSetting(2));
    order.setEncounter(Context.getEncounterService().getEncounter(3));
    order.setOrderer(Context.getProviderService().getProvider(1));
    Drug drug = Context.getConceptService().getDrug(3);
    order.setDrug(drug);
    order.setConcept(null);
    FreeTextDosingInstructions di = new FreeTextDosingInstructions();
    di.setInstructions("testing");
    di.setDosingInstructions(order);
    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertFalse(errors.hasFieldErrors());
}
Also used : DrugOrder(org.openmrs.DrugOrder) Drug(org.openmrs.Drug) FreeTextDosingInstructions(org.openmrs.FreeTextDosingInstructions) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest)

Example 10 with Drug

use of org.openmrs.Drug in project openmrs-core by openmrs.

the class DrugOrderValidatorTest method validate_shouldFailValidationIfDrugConceptIsDifferentFromOrderConcept.

/**
 * @see DrugOrderValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfDrugConceptIsDifferentFromOrderConcept() {
    DrugOrder order = new DrugOrder();
    Drug drug = Context.getConceptService().getDrug(3);
    Concept concept = Context.getConceptService().getConcept(792);
    order.setDrug(drug);
    // the actual concept which matches with drug is "88"
    order.setConcept(concept);
    Assert.assertNotEquals(drug.getConcept(), concept);
    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertTrue(errors.hasFieldErrors("concept"));
    Assert.assertTrue(errors.hasFieldErrors("drug"));
}
Also used : DrugOrder(org.openmrs.DrugOrder) Drug(org.openmrs.Drug) Concept(org.openmrs.Concept) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest)

Aggregations

Drug (org.openmrs.Drug)51 Test (org.junit.Test)46 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)41 Concept (org.openmrs.Concept)15 BindException (org.springframework.validation.BindException)15 Errors (org.springframework.validation.Errors)14 DrugReferenceMap (org.openmrs.DrugReferenceMap)9 ConceptMapType (org.openmrs.ConceptMapType)8 DrugOrder (org.openmrs.DrugOrder)7 OrderUtilTest (org.openmrs.order.OrderUtilTest)7 Date (java.util.Date)5 ConceptSource (org.openmrs.ConceptSource)5 ArrayList (java.util.ArrayList)4 Obs (org.openmrs.Obs)3 HashSet (java.util.HashSet)2 ConceptAnswer (org.openmrs.ConceptAnswer)2 ConceptDatatype (org.openmrs.ConceptDatatype)2 ConceptReferenceTerm (org.openmrs.ConceptReferenceTerm)2 Encounter (org.openmrs.Encounter)2 Person (org.openmrs.Person)2