From ae34047436e151900e7c191109a9f03a05eebd76 Mon Sep 17 00:00:00 2001 From: IamMujuziMoses Date: Mon, 8 Apr 2024 20:02:25 +0300 Subject: [PATCH] O3-2956: Service queues - Sort queues/locations/services alphabetically wherever we list them --- .../queue/api/QueueServicesWrapper.java | 26 ++++++++ .../queue/api/dao/impl/QueueDaoImpl.java | 2 + .../queue/api/QueueServicesWrapperTest.java | 61 +++++++++++++++++++ .../module/queue/api/dao/QueueDaoTest.java | 14 +++++ 4 files changed, 103 insertions(+) diff --git a/api/src/main/java/org/openmrs/module/queue/api/QueueServicesWrapper.java b/api/src/main/java/org/openmrs/module/queue/api/QueueServicesWrapper.java index 2b638284..8f4b9fcf 100644 --- a/api/src/main/java/org/openmrs/module/queue/api/QueueServicesWrapper.java +++ b/api/src/main/java/org/openmrs/module/queue/api/QueueServicesWrapper.java @@ -10,12 +10,14 @@ package org.openmrs.module.queue.api; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import lombok.Getter; import org.apache.commons.lang3.StringUtils; import org.openmrs.Concept; import org.openmrs.Location; +import org.openmrs.OpenmrsMetadata; import org.openmrs.Patient; import org.openmrs.Provider; import org.openmrs.Visit; @@ -128,6 +130,7 @@ public List getLocations(String[] locationRefs) { for (String locationRef : locationRefs) { ret.add(getLocation(locationRef.trim())); } + sortObjectListByName(ret); return ret; } @@ -191,6 +194,7 @@ public List getQueues(String[] queueRefs) { for (String queueRef : queueRefs) { ret.add(getQueue(queueRef.trim())); } + sortObjectListByName(ret); return ret; } @@ -312,4 +316,26 @@ public List getAllowedPriorities(Queue queue) { public String getGlobalProperty(String property) { return administrationService.getGlobalProperty(property); } + + @SuppressWarnings("unchecked") + private void sortObjectListByName(List unSortedList) { + List sortedList = new ArrayList(); + List objects = (List) unSortedList; + List names = new ArrayList<>(); + + for (OpenmrsMetadata o : objects) { + names.add(o.getName()); + } + + Collections.sort(names); + for (String name : names) { + for (OpenmrsMetadata o : objects) { + if (o.getName().equals(name)) { + sortedList.add((T) o); + } + } + } + unSortedList.clear(); + unSortedList.addAll(sortedList); + } } diff --git a/api/src/main/java/org/openmrs/module/queue/api/dao/impl/QueueDaoImpl.java b/api/src/main/java/org/openmrs/module/queue/api/dao/impl/QueueDaoImpl.java index 908308d2..cbb1660d 100644 --- a/api/src/main/java/org/openmrs/module/queue/api/dao/impl/QueueDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/queue/api/dao/impl/QueueDaoImpl.java @@ -13,6 +13,7 @@ import org.hibernate.Criteria; import org.hibernate.SessionFactory; +import org.hibernate.criterion.Order; import org.openmrs.module.queue.api.dao.QueueDao; import org.openmrs.module.queue.api.search.QueueSearchCriteria; import org.openmrs.module.queue.model.Queue; @@ -31,6 +32,7 @@ public List getQueues(QueueSearchCriteria searchCriteria) { includeVoidedObjects(c, searchCriteria.isIncludeRetired()); limitByCollectionProperty(c, "q.location", searchCriteria.getLocations()); limitByCollectionProperty(c, "q.service", searchCriteria.getServices()); + c.addOrder(Order.asc("name")); return c.list(); } } diff --git a/api/src/test/java/org/openmrs/module/queue/api/QueueServicesWrapperTest.java b/api/src/test/java/org/openmrs/module/queue/api/QueueServicesWrapperTest.java index f5caa071..c18e4d5b 100644 --- a/api/src/test/java/org/openmrs/module/queue/api/QueueServicesWrapperTest.java +++ b/api/src/test/java/org/openmrs/module/queue/api/QueueServicesWrapperTest.java @@ -11,9 +11,13 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import static org.mockito.Mockito.when; import java.util.List; +import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -22,6 +26,7 @@ import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnitRunner; import org.openmrs.Concept; +import org.openmrs.Location; import org.openmrs.api.AdministrationService; import org.openmrs.api.ConceptService; import org.openmrs.api.LocationService; @@ -34,6 +39,12 @@ @RunWith(MockitoJUnitRunner.class) public class QueueServicesWrapperTest { + private static final String UUID = "3eb7fe43-2813-4kbc-80dc-2e5d30252bb5"; + + public static final String TEST_UUID = "5ob8gj90-9090-4kbc-80dc-2e5d30252bb3"; + + private static final String NEW_UUID = "45b9fe43-2813-4kbc-80dc-2e5d30290iik"; + QueueServicesWrapper wrapper; @Mock @@ -159,4 +170,54 @@ public void getAllowedStatuses_shouldSucceedIfConceptConfiguredOnQueue() { List statuses = wrapper.getAllowedStatuses(queue); assertThat(statuses.size(), equalTo(1)); } + + @Test + public void getQueues_shouldReturnQueuesInAscendingOrderByName() { + String[] queueRefs = new String[] { UUID, TEST_UUID, NEW_UUID }; + createTestQueue("Queue", UUID); + createTestQueue("Test Queue", TEST_UUID); + createTestQueue("New Queue", NEW_UUID); + + List queues = wrapper.getQueues(queueRefs); + assertThat(queues, notNullValue()); + assertThat(queues.size(), is(greaterThanOrEqualTo(2))); + + String previousName = ""; + for (Queue q : queues) { + assertThat(q.getName().compareTo(previousName), is(greaterThanOrEqualTo(0))); + previousName = q.getName(); + } + } + + @Test + public void getLocations_shouldReturnLocationsInAscendingOrderByName() { + String[] locationRefs = new String[] { UUID, TEST_UUID, NEW_UUID }; + createTestLocation("Location", UUID); + createTestLocation("Test Location", TEST_UUID); + createTestLocation("New Location", NEW_UUID); + + List locations = wrapper.getLocations(locationRefs); + assertThat(locations, notNullValue()); + assertThat(locations.size(), is(greaterThanOrEqualTo(2))); + + String previousName = ""; + for (Location q : locations) { + assertThat(q.getName().compareTo(previousName), is(greaterThanOrEqualTo(0))); + previousName = q.getName(); + } + } + + private void createTestQueue(String name, String uuid) { + Queue testQueue = new Queue(); + testQueue.setName(name); + testQueue.setUuid(uuid); + when(queueService.getQueueByUuid(uuid)).thenReturn(Optional.of(testQueue)); + } + + private void createTestLocation(String name, String uuid) { + Location testLocation = new Location(); + testLocation.setName(name); + testLocation.setUuid(uuid); + when(locationService.getLocationByUuid(uuid)).thenReturn(testLocation); + } } diff --git a/integration-tests/src/test/java/org/openmrs/module/queue/api/dao/QueueDaoTest.java b/integration-tests/src/test/java/org/openmrs/module/queue/api/dao/QueueDaoTest.java index 87bfd0e7..f9fd3fbb 100644 --- a/integration-tests/src/test/java/org/openmrs/module/queue/api/dao/QueueDaoTest.java +++ b/integration-tests/src/test/java/org/openmrs/module/queue/api/dao/QueueDaoTest.java @@ -10,6 +10,7 @@ package org.openmrs.module.queue.api.dao; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -186,6 +187,19 @@ public void shouldGetQueuesByService() { assertResults(criteria, 1, 3, 4); } + @Test + public void shouldReturnQueuesInAscendingOrderByName() { + List queues = dao.getQueues(criteria); + assertThat(queues, notNullValue()); + assertThat(queues.size(), is(greaterThanOrEqualTo(2))); + + String previousName = ""; + for (Queue q : queues) { + assertThat(q.getName().compareTo(previousName), is(greaterThanOrEqualTo(0))); + previousName = q.getName(); + } + } + @Test public void shouldGetQueuesByIncludeRetired() { assertResults(criteria, 1, 3, 4);