From eb0b5d7a6a6da72c8f11fba2edc18fef5bba7691 Mon Sep 17 00:00:00 2001 From: Amos Laboso Date: Fri, 20 Sep 2024 12:58:39 +0300 Subject: [PATCH] =?UTF-8?q?RESTWS-956=20-=20Erroneous=20response=20when=20?= =?UTF-8?q?totalCount=20is=20included=20in=20query=20=E2=80=A6=20(#625)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../openmrs2_0/EncounterSearchHandler2_0.java | 4 +- .../EncounterSearchHandler2_0Test.java | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_0/EncounterSearchHandler2_0Test.java diff --git a/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_0/EncounterSearchHandler2_0.java b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_0/EncounterSearchHandler2_0.java index 82de4fb0a..5ea5f84a8 100644 --- a/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_0/EncounterSearchHandler2_0.java +++ b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_0/EncounterSearchHandler2_0.java @@ -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() { diff --git a/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_0/EncounterSearchHandler2_0Test.java b/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_0/EncounterSearchHandler2_0Test.java new file mode 100644 index 000000000..21874d9a0 --- /dev/null +++ b/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_0/EncounterSearchHandler2_0Test.java @@ -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 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 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 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); + } +}