Skip to content

Commit

Permalink
adding dosage from to drug, creating tests for drug
Browse files Browse the repository at this point in the history
  • Loading branch information
johnstoecker committed Oct 18, 2012
1 parent 71e9b58 commit 6dea761
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ private SimpleObject getDrugAsSimpleObject(Drug drug) {
obj.add("description", drug.getDescription());
obj.add("minimumDailyDose", drug.getMinimumDailyDose());
obj.add("maximumDailyDose", drug.getMaximumDailyDose());
if (drug.getDosageForm() != null) {
obj.add("dosageForm", drug.getDosageForm().getName().getName());
}
obj.add("units", drug.getUnits());
obj.add("combination", drug.getCombination());
obj.add("concept", drug.getConcept().getUuid());
Expand All @@ -187,7 +190,7 @@ private SimpleObject getDrugAsSimpleObject(Drug drug) {
@RequestMapping(method = RequestMethod.GET)
@WSDoc("Get All Unretired Drug in the system")
@ResponseBody()
public String getAllDrug(HttpServletRequest request, HttpServletResponse response) throws ResponseException {
public String getAllDrugs(HttpServletRequest request, HttpServletResponse response) throws ResponseException {
initDrugController();
List<Drug> allDrug = service.getAllDrugs(false);
ArrayList results = new ArrayList();
Expand All @@ -208,7 +211,7 @@ public String getAllDrug(HttpServletRequest request, HttpServletResponse respons
@RequestMapping(value = "/{uuid}", method = RequestMethod.GET)
@WSDoc("Gets Drug for the uuid path")
@ResponseBody()
public String getAllDrugByUuid(@PathVariable("uuid") String uuid, HttpServletRequest request) throws ResponseException {
public String getDrugByUuid(@PathVariable("uuid") String uuid, HttpServletRequest request) throws ResponseException {
initDrugController();
Drug drug = service.getDrugByUuid(uuid);
return gson.toJson(getDrugAsSimpleObject(drug));
Expand All @@ -226,7 +229,7 @@ public String getAllDrugByUuid(@PathVariable("uuid") String uuid, HttpServletReq
@RequestMapping(value = "/{uuid}", method = RequestMethod.GET, params = "v")
@WSDoc("Gets Full representation of Drug for the uuid path")
@ResponseBody()
public String getAllDrugByUuidFull(@PathVariable("uuid") String uuid, @RequestParam("v") String rep,
public String getDrugByUuidFull(@PathVariable("uuid") String uuid, @RequestParam("v") String rep,
HttpServletRequest request) throws ResponseException {
initDrugController();
Drug drug = service.getDrugByUuid(uuid);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.raxa.module.raxacore.web.v1_0.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.openmrs.Drug;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

/**
*
* @author joman
*/
public class RaxaDrugControllerTest extends BaseModuleContextSensitiveTest {

private static final String TEST_DATA_PATH = "org/raxa/module/raxacore/include/";

private static final String MODULE_TEST_DATA_XML = TEST_DATA_PATH + "moduleTestData.xml";

private MockHttpServletRequest request = null;

private MockHttpServletResponse response = null;

private RaxaDrugController controller = null;

private ConceptService service = null;

@Before
public void before() throws Exception {
executeDataSet(MODULE_TEST_DATA_XML);
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.controller = new RaxaDrugController();
this.service = Context.getConceptService();
}

/**
* Test of createNewDrug method, of class RaxaDrugController.
*/
@Test
public void createNewDrug_shouldCreateNewDrug() throws Exception {
int before = service.getAllDrugs(true).size();
String json = "{\"concept\":\"0cbe2ed3-cd5f-4f46-9459-26127c9265ab\",\"name\":\"New Drug name\",\"dosageForm\":\"0cbe2ed3-cd5f-4f46-9459-26127c9265ab\",\"minimumDailyDose\":\"10\",\"maximumDailyDose\":\"100\",\"units\":\"mg\"}";
SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
Object drug = controller.createNewDrug(post, request, response);
Assert.assertEquals(before + 1, service.getAllDrugs(true).size());
}

/**
* Test of updateDrug method, of class RaxaDrugController.
*/
@Test
public void updateDrug_shouldUpdateADrug() throws Exception {
int before = service.getAllDrugs(true).size();
String json = "{ \"name\":\"Changed name\",\"description\":\"Test\"}";
SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
controller.updateDrug(getUuid(), post, request, response);
Assert.assertEquals(before, service.getAllDrugs(true).size());
String results = controller.getAllDrugs(request, response);
LinkedHashMap updatedDrugs = (LinkedHashMap) ((ArrayList) SimpleObject.parseJson(results).get("results")).get(0);
Assert.assertEquals(getUuid(), updatedDrugs.get("uuid"));
Assert.assertEquals("Changed name", updatedDrugs.get("name"));
}

/**
* Test of getAllDrug method, of class RaxaDrugController.
*/
@Test
public void getAllDrugs_shouldGetDrugs() throws Exception {
String allDrugs = controller.getAllDrugs(request, response);
Assert.assertEquals(2, ((ArrayList) SimpleObject.parseJson(allDrugs).get("results")).size());
}

/**
* Test of getDrugByUuid method, of class RaxaDrugController.
*/
@Test
public void getDrugByUuid_shouldGetDrug() throws Exception {
String result = controller.getDrugByUuid(getUuid(), request);
SimpleObject drug = SimpleObject.parseJson(result);
Assert.assertNotNull(result);
Assert.assertEquals(getUuid(), drug.get("uuid"));
Assert.assertEquals("Triomune-30", drug.get("name"));
}

/**
* Test of getAllDrugByUuidFull method, of class RaxaDrugController.
*/
@Test
public void getDrugByUuidFull_shouldGetFullDrug() throws Exception {
String result = controller.getDrugByUuidFull(getUuid(), "full", request);
SimpleObject drug = SimpleObject.parseJson(result);
Assert.assertNotNull(result);
Assert.assertEquals(getUuid(), drug.get("uuid"));
Assert.assertEquals(false, drug.get("retired"));
}

/**
* Test of retireDrug method, of class RaxaDrugController.
*/
@Test
public void testRetireDrug() throws Exception {
Drug drug = service.getDrugByUuid(getUuid());
Assert.assertFalse(drug.isRetired());
controller.retireDrug(getUuid(), "testing", request, response);
Drug drug2 = service.getDrugByUuid(getUuid());
Assert.assertTrue(drug2.isRetired());
Assert.assertEquals("testing", drug2.getRetireReason());
}

private String getUuid() {
return "3cfcf118-931c-46f7-8ff6-7b876f0d4202";
}
}

0 comments on commit 6dea761

Please sign in to comment.