Skip to content

Commit

Permalink
O3-2956: Service queues - Sort queues/locations/services alphabetical…
Browse files Browse the repository at this point in the history
…ly wherever we list them
  • Loading branch information
IamMujuziMoses committed Apr 8, 2024
1 parent 21c98ae commit ae34047
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,6 +130,7 @@ public List<Location> getLocations(String[] locationRefs) {
for (String locationRef : locationRefs) {
ret.add(getLocation(locationRef.trim()));
}
sortObjectListByName(ret);
return ret;
}

Expand Down Expand Up @@ -191,6 +194,7 @@ public List<Queue> getQueues(String[] queueRefs) {
for (String queueRef : queueRefs) {
ret.add(getQueue(queueRef.trim()));
}
sortObjectListByName(ret);
return ret;
}

Expand Down Expand Up @@ -312,4 +316,26 @@ public List<Concept> getAllowedPriorities(Queue queue) {
public String getGlobalProperty(String property) {
return administrationService.getGlobalProperty(property);
}

@SuppressWarnings("unchecked")
private <T> void sortObjectListByName(List<T> unSortedList) {
List<T> sortedList = new ArrayList<T>();
List<OpenmrsMetadata> objects = (List<OpenmrsMetadata>) unSortedList;
List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@ public List<Queue> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -159,4 +170,54 @@ public void getAllowedStatuses_shouldSucceedIfConceptConfiguredOnQueue() {
List<Concept> 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<Queue> 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<Location> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -186,6 +187,19 @@ public void shouldGetQueuesByService() {
assertResults(criteria, 1, 3, 4);
}

@Test
public void shouldReturnQueuesInAscendingOrderByName() {
List<Queue> 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);
Expand Down

0 comments on commit ae34047

Please sign in to comment.