|
41 | 41 | import org.openmrs.test.Verifies;
|
42 | 42 | import org.openmrs.util.PrivilegeConstants;
|
43 | 43 |
|
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.Date; |
| 46 | +import java.util.HashSet; |
| 47 | +import java.util.List; |
| 48 | +import java.util.Set; |
| 49 | + |
44 | 50 | /**
|
45 | 51 | * TODO clean up and test all methods in OrderService
|
46 | 52 | */
|
@@ -322,7 +328,7 @@ public void getActiveOrders_shouldReturnAllActiveDrugOrdersForTheSpecifiedPatien
|
322 | 328 | DrugOrder[] expectedOrders = { (DrugOrder) orderService.getOrder(3), (DrugOrder) orderService.getOrder(5) };
|
323 | 329 | assertThat(orders, hasItems(expectedOrders));
|
324 | 330 | }
|
325 |
| - |
| 331 | + |
326 | 332 | /**
|
327 | 333 | * @verifies return all active test orders for the specified patient
|
328 | 334 | * @see OrderService#getActiveOrders(org.openmrs.Patient, Class, org.openmrs.CareSetting,
|
@@ -415,5 +421,161 @@ public void getActiveOrders_shouldReturnActiveOrdersAsOfTheSpecifiedDate() throw
|
415 | 421 | Order[] expectedOrders5 = { orderService.getOrder(222), orderService.getOrder(3), orderService.getOrder(444),
|
416 | 422 | orderService.getOrder(5), orderService.getOrder(7) };
|
417 | 423 | assertThat(orders, hasItems(expectedOrders5));
|
| 424 | + } |
| 425 | + |
| 426 | + /** |
| 427 | + * @see {@link OrderService#discontinueOrder(org.openmrs.Order, String, java.util.Date)} |
| 428 | + */ |
| 429 | + @Test |
| 430 | + @Verifies(value = "populate correct attributes on the discontinue and discontinued orders", method = "discontinueOrder(Order, String, Date)") |
| 431 | + public void discontinueOrderWithNonCodedReason_shouldPopulateCorrectAttributesOnBothOrders() throws Exception { |
| 432 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml"); |
| 433 | + |
| 434 | + OrderService orderService = Context.getOrderService(); |
| 435 | + Order order = orderService.getOrderByOrderNumber("111"); |
| 436 | + Date discontinueDate = new Date(); |
| 437 | + String discontinueReasonNonCoded = "Test if I can discontinue this"; |
| 438 | + |
| 439 | + Order discontinueOrder = orderService.discontinueOrder(order, discontinueReasonNonCoded, discontinueDate); |
| 440 | + |
| 441 | + Assert.assertEquals(order.getDateStopped(), discontinueDate); |
| 442 | + Assert.assertNotNull(discontinueOrder); |
| 443 | + Assert.assertNotNull(discontinueOrder.getId()); |
| 444 | + Assert.assertEquals(discontinueOrder.getAction(), Action.DISCONTINUE); |
| 445 | + Assert.assertEquals(discontinueOrder.getDiscontinuedReasonNonCoded(), discontinueReasonNonCoded); |
| 446 | + Assert.assertEquals(discontinueOrder.getPreviousOrder(), order); |
| 447 | + } |
| 448 | + |
| 449 | + /** |
| 450 | + * @see {@link OrderService#discontinueOrder(org.openmrs.Order, org.openmrs.Concept, java.util.Date)} |
| 451 | + */ |
| 452 | + @Test |
| 453 | + @Verifies(value = "populate correct attributes on the discontinue and discontinued orders", method = "discontinueOrder(Order, Concept, Date)") |
| 454 | + public void discontinueOrderWithConcept_shouldPopulateCorrectAttributesOnBothOrders() throws Exception { |
| 455 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml"); |
| 456 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-discontinueReason.xml"); |
| 457 | + |
| 458 | + OrderService orderService = Context.getOrderService(); |
| 459 | + Order order = orderService.getOrderByOrderNumber("111"); |
| 460 | + Date discontinueDate = new Date(); |
| 461 | + Concept concept = Context.getConceptService().getConcept(1); |
| 462 | + |
| 463 | + Order discontinueOrder = orderService.discontinueOrder(order, concept, discontinueDate); |
| 464 | + |
| 465 | + Assert.assertEquals(order.getDateStopped(), discontinueDate); |
| 466 | + Assert.assertNotNull(discontinueOrder); |
| 467 | + Assert.assertNotNull(discontinueOrder.getId()); |
| 468 | + Assert.assertEquals(discontinueOrder.getAction(), Action.DISCONTINUE); |
| 469 | + Assert.assertEquals(discontinueOrder.getDiscontinuedReason(), concept); |
| 470 | + Assert.assertEquals(discontinueOrder.getPreviousOrder(), order); |
| 471 | + } |
| 472 | + |
| 473 | + /** |
| 474 | + * @see {@link OrderService#discontinueOrder(org.openmrs.Order, String, java.util.Date)} |
| 475 | + */ |
| 476 | + @Test(expected = APIException.class) |
| 477 | + @Verifies(value = "fail when for a discontinue order", method = "discontinueOrder(Order, String, Date)") |
| 478 | + public void discontinueOrderWithNonCodedReason_shouldFailForADiscontinueOrder() throws Exception { |
| 479 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml"); |
| 480 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-discontinuedOrder.xml"); |
| 481 | + OrderService orderService = Context.getOrderService(); |
| 482 | + Order discontinueOrder = orderService.getOrder(26); |
| 483 | + |
| 484 | + orderService.discontinueOrder(discontinueOrder, "Test if I can discontinue this", null); |
| 485 | + } |
| 486 | + |
| 487 | + |
| 488 | + /** |
| 489 | + * @see {@link OrderService#discontinueOrder(org.openmrs.Order, org.openmrs.Concept, java.util.Date)} |
| 490 | + */ |
| 491 | + @Test(expected = APIException.class) |
| 492 | + @Verifies(value = "fail for a discontinue order", method = "discontinueOrder(Order, Concept, Date)") |
| 493 | + public void discontinueOrderWithConcept_shouldFailForADiscontinueOrder() throws Exception { |
| 494 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml"); |
| 495 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-discontinuedOrder.xml"); |
| 496 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-discontinueReason.xml"); |
| 497 | + OrderService orderService = Context.getOrderService(); |
| 498 | + Order discontinueOrder = orderService.getOrder(26); |
| 499 | + |
| 500 | + orderService.discontinueOrder(discontinueOrder, (Concept) null, null); |
| 501 | + } |
| 502 | + |
| 503 | + /** |
| 504 | + * @see {@link OrderService#saveOrder(org.openmrs.Order)} |
| 505 | + */ |
| 506 | + @Test |
| 507 | + @Verifies(value = "discontinue existing active order if new order being saved with action to discontinue", method = "saveOrder(Order)") |
| 508 | + public void saveOrder_shouldDiscontinueExistingActiveOrderIfNewOrderBeingSavedWithActionToDiscontinue() throws Exception { |
| 509 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml"); |
| 510 | + OrderService orderService = Context.getOrderService(); |
| 511 | + Order order = new Order(); |
| 512 | + order.setAction(Order.Action.DISCONTINUE); |
| 513 | + order.setDiscontinuedReasonNonCoded("Discontinue this"); |
| 514 | + order.setPatient(Context.getPatientService().getPatient(7)); |
| 515 | + order.setConcept(Context.getConceptService().getConcept(88)); |
| 516 | + order.setCareSetting(orderService.getCareSetting(1)); |
| 517 | + order.setStartDate(new Date()); |
| 518 | + |
| 519 | + //We are trying to discontinue order id 111 in standardTestDataset.xml |
| 520 | + Order expectedPreviousOrder = orderService.getOrder(111); |
| 521 | + Assert.assertNull(expectedPreviousOrder.getDateStopped()); |
| 522 | + |
| 523 | + order = orderService.saveOrder(order); |
| 524 | + |
| 525 | + Assert.assertNotNull("should populate dateStopped in previous order", expectedPreviousOrder.getDateStopped()); |
| 526 | + Assert.assertNotNull("should save discontinue order", order.getId()); |
| 527 | + Assert.assertEquals(expectedPreviousOrder, order.getPreviousOrder()); |
| 528 | + Assert.assertNotNull(expectedPreviousOrder.getDateStopped()); |
| 529 | + } |
| 530 | + |
| 531 | + /** |
| 532 | + * @see {@link OrderService#saveOrder(org.openmrs.Order)} |
| 533 | + */ |
| 534 | + @Test |
| 535 | + @Verifies(value = "discontinue previousOrder if it is not already discontinued", method = "saveOrder(Order)") |
| 536 | + public void saveOrder_shouldDiscontinuePreviousOrderIfItIsNotAlreadyDiscontinued() throws Exception { |
| 537 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml"); |
| 538 | + OrderService orderService = Context.getOrderService(); |
| 539 | + //We are trying to discontinue order id 111 in standardTestDataset.xml |
| 540 | + Order order = new Order(); |
| 541 | + order.setAction(Order.Action.DISCONTINUE); |
| 542 | + order.setDiscontinuedReasonNonCoded("Discontinue this"); |
| 543 | + order.setPatient(Context.getPatientService().getPatient(7)); |
| 544 | + order.setConcept(Context.getConceptService().getConcept(88)); |
| 545 | + order.setCareSetting(orderService.getCareSetting(1)); |
| 546 | + order.setStartDate(new Date()); |
| 547 | + Order previousOrder = orderService.getOrder(111); |
| 548 | + order.setPreviousOrder(previousOrder); |
| 549 | + |
| 550 | + orderService.saveOrder(order); |
| 551 | + |
| 552 | + Assert.assertNotNull("previous order should be discontinued", previousOrder.getDateStopped()); |
| 553 | + } |
| 554 | + |
| 555 | + /** |
| 556 | + * @see {@link OrderService#saveOrder(org.openmrs.Order)} |
| 557 | + */ |
| 558 | + @Test(expected = APIException.class) |
| 559 | + @Verifies(value = "fail if concept in previous order does not match this concept", method = "saveOrder(Order)") |
| 560 | + public void saveOrder_shouldFailIfConceptInPreviousOrderDoesNotMatchThisConcept() throws Exception { |
| 561 | + executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml"); |
| 562 | + OrderService orderService = Context.getOrderService(); |
| 563 | + //We are trying to discontinue order id 111 in standardTestDataset.xml |
| 564 | + Order order = new Order(); |
| 565 | + order.setAction(Order.Action.DISCONTINUE); |
| 566 | + order.setDiscontinuedReasonNonCoded("Discontinue this"); |
| 567 | + order.setPatient(Context.getPatientService().getPatient(7)); |
| 568 | + order.setConcept(Context.getConceptService().getConcept(3)); |
| 569 | + order.setCareSetting(orderService.getCareSetting(1)); |
| 570 | + order.setStartDate(new Date()); |
| 571 | + Order previousOrder = orderService.getOrder(111); |
| 572 | + order.setPreviousOrder(previousOrder); |
| 573 | + |
| 574 | + orderService.saveOrder(order); |
| 575 | + } |
| 576 | + |
| 577 | + private boolean isOrderActive(Order order) { |
| 578 | + return order.getDateStopped() == null && order.getAutoExpireDate() == null |
| 579 | + && order.getAction() != Action.DISCONTINUE; |
418 | 580 | }
|
419 | 581 | }
|