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);
}
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());
}
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");
}
}
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());
}
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"));
}
Aggregations