Creating a RadiologyOrder via REST

I’m trying to create a radiology order via REST. To have POST working on /radiologyorder, I implemented the following methods

and these

The radiologyorder is a subclass of TestOrder so the RadiologyOrderResource.getCreatableProperties method similar to OrderResource1_8.getCreatableProperties.

Trying to figure out what the request data should look like, I found this thread

where @darius proposed a request body like this

{
    "type": "testorder",
    "patient": "1681f4b7-7f4d-4227-9562-718ee347bbd8",
    "concept": "840AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
    "encounter": "8a32e7cb-0bbb-4c4e-8ed4-fc982443859f", // orders must have an encounter
    "orderer": "9e28cb36-0df4-4972-b375-4a5ff88e39d9", // from the provider resource
    "careSetting": "c365e560-c3ec-11e3-9c1a-0800200c9a66" // from the caresetting resource
}

Now my question is, do I need to make a call to create the encounter then use it’s uuid to create the radiologyorder implying 2 rest calls? If that’s the case then what if the creation of the encounter is successful and for some reason the second call to create the order does not complete. That will mean I’m having encounter not attached to the order. The same question goes for careSetting.

The current add order form in the radiology module makes no mention of an encounter and I also tried looking at the code and I don’t see how an encounter is been created when creating a radiologyorder. The controller basically calls new RadiologyOrder and the jsp views use data binding to bind form inputs to the radiologyOrder model attribute. But after saving the order a new entry is added to the encounters table which means an encounter was created.

cc @teleivo

If after saving a new radiology order, you find an encounter automatically created, then you do not have to make a call to create an encounter.

1 Like

@dkayiwa thanks for your response. Yes an encounter is been created by the Java API when creating a new RadiologyOrder. I found this in RadiologyOrderServiceImpl.java

final Encounter encounter =
            saveRadiologyOrderEncounter(radiologyOrder.getPatient(), radiologyOrder.getOrderer(), new Date());
encounter.addOrder(radiologyOrder);
    
final OrderContext orderContext = new OrderContext();
orderContext.setCareSetting(radiologyProperties.getRadiologyCareSetting());
orderContext.setOrderType(radiologyProperties.getRadiologyTestOrderType());
    
final RadiologyOrder result = (RadiologyOrder) orderService.saveOrder(radiologyOrder, orderContext);
this.radiologyStudyService.saveRadiologyStudy(result.getStudy());
return result;

So when implementing the getCreatableProperties I didn’t and encounter

@Override
public DelegatingResourceDescription getCreatableProperties() {
    DelegatingResourceDescription description = new DelegatingResourceDescription();
    description.addRequiredProperty("patient");
    description.addRequiredProperty("concept");
    description.addProperty("instructions");
    description.addProperty("orderer");
    return description;
}

But when I do a POST with the following request body

{
    "patient":"86ec0a66-2897-456a-a6fe-9be547fffa3f",
    "concept":"4955c10a-0916-4ee1-9fc1-6d7b533b2d9b",
    "orderer":"5a4cf63d-efbe-4ee0-bb70-c0cf6c40907f"
}

And I still get this error. This one is hard to trouble shoot for me as it doesn’t return any stacktrace I can follow. Maybe someone with your experience may know what’s going on.

It has something to do with encounter but I’ve not been able to figure out what’s wrong. I’ve placed breakpoints in the following methods to see if I can catch the error.

RadiologyOrderResource.create() RadiologyOrderResource.newDelegate() RadiologyOrderResource.getCreatableProperties() RadiologyOrderResource.save()

But none of these breakpoints are hit.

Do you have a test to reproduce it?

@dkayiwa can’t believe it is 20 days already since you requested for this. Been occupied with other stuffs. Hope we can continue with this. I’m still having this issue. We had other more pressing issues to resolve and this was not a blocker. But now with all those resolved, this is now a blocker as we cannot move forward without fixing this.

About writing the test to reproduce it I would like you to show me some examples of how it is done in other places. To implement POST on /radiologyorder, I had to implement 3 methods, newDelegate, getCreatableProperties and save. I’ve written unit tests these methods but none of those are actually testing if POST is working. You can see the methods I implemented at

And my tests

Looking at the tests you’ll notice that I did not test the save method. That is because I’m not sure how to mock this line

Context.getService(RadiologyOrderService.class)
            .placeRadiologyOrder(delegate);

But even if I was able to actually mock that method call and write the test, all these will just be unit tests and to achieve what you want(i.e reproduce the error with POSTing), I would need to write an integration tests. That is what I’ll need help with. I’ve browsed through a lot of existing code but none of them could help me. I’ve also tried to follow this tutorial http://www.baeldung.com/integration-testing-a-rest-api but to use the method provided there, I’d need to import a third party library and I’d also need a running server to test against. I’m guessing there is a conventional way openmrs tests that resources will work as expected when a POST request is made.

Did you take a look at the controller tests in the REST web services module?