Skip to content

Commit

Permalink
RESTWS-956 - Erroneous response when totalCount is included in query … (
Browse files Browse the repository at this point in the history
#625)

* RESTWS-956 - Erroneous response when totalCount is included in query parameters for fetching encounters

* Added EncounterSearchHandler2_0Test to test for encounter list based on filters (patient, encounterType) and limit (size of results limited is not necessarily same as totalCount)

---------

Co-authored-by: Amos Laboso <[email protected]>
  • Loading branch information
alaboso and Amos Laboso authored Sep 20, 2024
1 parent c459439 commit eb0b5d7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public class EncounterSearchHandler2_0 implements SearchHandler {
Collections.singletonList("2.0.* - 9.*"),
Collections.singletonList(new SearchQuery.Builder(
"Allows you to find Encounter by patient and encounterType (and optionally by from and to date range)")
.withRequiredParameters("patient").withOptionalParameters("visit", "encounterType", DATE_FROM, DATE_TO, "order")
.build()));
.withRequiredParameters("patient").withOptionalParameters("visit", "encounterType", DATE_FROM, DATE_TO,
"order", "totalCount").build()));

@Override
public SearchConfig getSearchConfig() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.search.openmrs2_0;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.openmrs.Encounter;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
import org.openmrs.module.webservices.rest.web.RestTestConstants1_9;
import org.openmrs.module.webservices.rest.web.v1_0.controller.RestControllerTestUtils;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod;

public class EncounterSearchHandler2_0Test extends RestControllerTestUtils {

protected String getURI() {
return "encounter";
}

/**
* @verifies returns encounters and totalCount filtered by patient uuid only
* @see @see EncounterSearchHandler2_0#search(RequestContext)
* @throws Exception
*/
@Test
public void search_shouldReturnEncountersWithTotalCountFilteredByPatient() throws Exception {
MockHttpServletRequest req = request(RequestMethod.GET, getURI());
req.addParameter("patient", RestTestConstants1_9.PATIENT_WITH_OBS_UUID);
req.addParameter("totalCount", String.valueOf(Boolean.TRUE));

SimpleObject result = deserialize(handle(req));
List<Encounter> encounters = result.get("results");
Assert.assertEquals(3, encounters.size());
int totalCount = result.get("totalCount");
Assert.assertNotNull(totalCount);
Assert.assertEquals(3, totalCount);
Assert.assertEquals(encounters.size(), totalCount);
}

/**
* @verifies returns encounters and totalCount filtered by patient uuid and encounterType uuid
* @see @see EncounterSearchHandler2_0#search(RequestContext)
* @throws Exception
*/
@Test
public void search_shouldReturnEncountersWithTotalCountFilteredByPatientAndEncounterType() throws Exception {
MockHttpServletRequest req = request(RequestMethod.GET, getURI());
req.addParameter("patient", RestTestConstants1_9.PATIENT_WITH_OBS_UUID);
req.addParameter("encounterType", RestTestConstants1_8.ENCOUNTER_TYPE_UUID);
req.addParameter("totalCount", String.valueOf(Boolean.TRUE));

SimpleObject result = deserialize(handle(req));
List<Encounter> encounters = result.get("results");
Assert.assertEquals(2, encounters.size());
int totalCount = result.get("totalCount");
Assert.assertNotNull(totalCount);
Assert.assertEquals(2, totalCount);
Assert.assertEquals(encounters.size(), totalCount);
}

/**
* @verifies returns encounters and totalCount filtered by patient uuid and limit
* i.e. (limit 1, results size should not be the same as totalCount)
*
* @see @see EncounterSearchHandler2_0#search(RequestContext)
* @throws Exception
*/
@Test
public void search_shouldReturnEncountersWithTotalCountFilteredByPatientLimitedToOne() throws Exception {
MockHttpServletRequest req = request(RequestMethod.GET, getURI());
req.addParameter("patient", RestTestConstants1_9.PATIENT_WITH_OBS_UUID);
req.addParameter("limit", String.valueOf(1));
req.addParameter("totalCount", String.valueOf(Boolean.TRUE));

SimpleObject result = deserialize(handle(req));
List<Encounter> encounters = result.get("results");
Assert.assertEquals(1, encounters.size());
int totalCount = result.get("totalCount");
Assert.assertNotNull(totalCount);
Assert.assertEquals(3, totalCount);
Assert.assertNotEquals(encounters.size(), totalCount);
}
}

0 comments on commit eb0b5d7

Please sign in to comment.