Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTWS-956 - Erroneous response when totalCount is included in query … #625

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, on it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added EncounterSearchHandler2_0Test

.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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you notice that this test passes even without your fix?

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you notice that this test passes even without your fix?

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);
}
}
Loading