Skip to content

Commit

Permalink
[BAH-3180]Exception Handler for OrderEntryException to ensure duplica…
Browse files Browse the repository at this point in the history
…te drugs can't be added (#229)

Co-authored-by: yenugukeerthana <keerthana.yenugu@thoughtworks.com>
  • Loading branch information
deeptirawat1510 and yenugukeerthana authored Aug 29, 2023
1 parent e129f78 commit 201d4f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.bahmni.module.bahmnicore.web.v1_0.controller;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.bahmni.module.bahmnicore.web.v1_0.VisitClosedException;
import org.openmrs.Encounter;
import org.openmrs.Visit;
import org.openmrs.api.EncounterService;
import org.openmrs.api.OrderEntryException;
import org.openmrs.module.bahmniemrapi.encountertransaction.contract.BahmniEncounterSearchParameters;
import org.openmrs.module.bahmniemrapi.encountertransaction.contract.BahmniEncounterTransaction;
import org.openmrs.module.bahmniemrapi.encountertransaction.mapper.BahmniEncounterTransactionMapper;
Expand All @@ -14,16 +17,15 @@
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static org.bahmni.module.bahmnicore.util.MiscUtils.setUuidsForObservations;

Expand All @@ -35,6 +37,7 @@ public class BahmniEncounterController extends BaseRestController {
private EncounterTransactionMapper encounterTransactionMapper;
private BahmniEncounterTransactionService bahmniEncounterTransactionService;
private BahmniEncounterTransactionMapper bahmniEncounterTransactionMapper;
private static Logger logger = LogManager.getLogger(BahmniEncounterController.class);

public BahmniEncounterController() {
}
Expand Down Expand Up @@ -100,5 +103,15 @@ public BahmniEncounterTransaction get(String encounterUuid) {
EncounterTransaction encounterTransaction = encounterTransactionMapper.map(encounter, includeAll);
return bahmniEncounterTransactionMapper.map(encounterTransaction, includeAll);
}
@ExceptionHandler(OrderEntryException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Object> handleOrderEntryException(OrderEntryException ex) {
Map<String, String> errorBody = new HashMap<>();
errorBody.put("message", "[" + ex.getMessage() + "]");
Map<String, Object> responseBody = new HashMap<>();
responseBody.put("error", errorBody);
logger.warn("OrderEntryException: " + ex.getMessage());
return new ResponseEntity<Object>(responseBody, HttpStatus.BAD_REQUEST);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import org.openmrs.Encounter;
import org.openmrs.Visit;
import org.openmrs.api.EncounterService;
import org.openmrs.api.OrderEntryException;
import org.openmrs.module.bahmniemrapi.encountertransaction.contract.BahmniEncounterSearchParameters;
import org.openmrs.module.bahmniemrapi.encountertransaction.contract.BahmniEncounterTransaction;
import org.openmrs.module.bahmniemrapi.encountertransaction.mapper.BahmniEncounterTransactionMapper;
import org.openmrs.module.bahmniemrapi.encountertransaction.service.BahmniEncounterTransactionService;
import org.openmrs.module.emrapi.encounter.EmrEncounterService;
import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -91,5 +95,20 @@ public void shouldThrowVisitClosedExceptionIfEncounterVisitIsClosed() throws Exc

bahmniEncounterController.delete("410491d2-b617-42ad-bf0f-de2fc9b42998","Undo Discharge");
}
@Test
public void shouldThrowBadRequestStatusCodeForOrderEntryException() throws Exception{
bahmniEncounterController = new BahmniEncounterController(encounterService, emrEncounterService, null, bahmniEncounterTransactionService, bahmniEncounterTransactionMapper);

OrderEntryException mockException = new OrderEntryException("Order.cannot.have.more.than.one");

ResponseEntity<Object> response = bahmniEncounterController.handleOrderEntryException(mockException);

assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());

Map<String, Object> responseBody = (Map<String, Object>) response.getBody();
Map<String, String> errorBody = (Map<String, String>) responseBody.get("error");

assertEquals("[Order.cannot.have.more.than.one]", errorBody.get("message"));
}

}

0 comments on commit 201d4f0

Please sign in to comment.