forked from openmrs/openmrs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openmrs#366 from rowanseymour/master
TRUNK-3589: Add LocationService method to search for a location by a location attribute
- Loading branch information
Showing
6 changed files
with
181 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,16 @@ | |
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openmrs.GlobalProperty; | ||
import org.openmrs.Location; | ||
import org.openmrs.LocationAttribute; | ||
import org.openmrs.LocationAttributeType; | ||
import org.openmrs.LocationTag; | ||
import org.openmrs.api.context.Context; | ||
|
@@ -302,6 +305,78 @@ public void getLocations_shouldReturnEmptyListWhenNoLocationMatchTheNameFragment | |
Assert.assertEquals(0, Context.getLocationService().getLocations("Mansion").size()); | ||
} | ||
|
||
/** | ||
* @see LocationService#getLocations(String, org.openmrs.Location, java.util.Map, boolean, Integer, Integer) | ||
*/ | ||
@Test | ||
@Verifies(value = "should return empty list when no location has matching attribute values", method = "getLocations(String,Location,Map,boolean,Integer,Integer)") | ||
public void getLocations_shouldNotFindAnyLocationsIfNoneHaveGivenAttributeValues() { | ||
// Save new phone number attribute type | ||
LocationAttributeType phoneAttrType = new LocationAttributeType(); | ||
phoneAttrType.setName("Facility Phone"); | ||
phoneAttrType.setMinOccurs(0); | ||
phoneAttrType.setMaxOccurs(1); | ||
phoneAttrType.setDatatypeClassname("org.openmrs.customdatatype.datatype.FreeTextDatatype"); | ||
Context.getLocationService().saveLocationAttributeType(phoneAttrType); | ||
|
||
Map<LocationAttributeType, Object> attrValues = new HashMap<LocationAttributeType, Object>(); | ||
attrValues.put(phoneAttrType, "xxxxxx"); | ||
Assert.assertEquals(0, Context.getLocationService().getLocations(null, null, attrValues, true, null, null).size()); | ||
} | ||
|
||
/** | ||
* @see LocationService#getLocations(String, org.openmrs.Location, java.util.Map, boolean, Integer, Integer) | ||
*/ | ||
@Test | ||
@Verifies(value = "should get locations having all matching attribute values", method = "getLocations(String,Location,Map,boolean,Integer,Integer)") | ||
public void getLocations_shouldGetLocationsHavingAllMatchingAttributeValues() { | ||
// Save new phone number attribute type | ||
LocationAttributeType phoneAttrType = new LocationAttributeType(); | ||
phoneAttrType.setName("Facility Phone"); | ||
phoneAttrType.setMinOccurs(0); | ||
phoneAttrType.setMaxOccurs(1); | ||
phoneAttrType.setDatatypeClassname("org.openmrs.customdatatype.datatype.FreeTextDatatype"); | ||
Context.getLocationService().saveLocationAttributeType(phoneAttrType); | ||
|
||
// Save new email address attribute type | ||
LocationAttributeType emailAttrType = new LocationAttributeType(); | ||
emailAttrType.setName("Facility Email"); | ||
emailAttrType.setMinOccurs(0); | ||
emailAttrType.setMaxOccurs(1); | ||
emailAttrType.setDatatypeClassname("org.openmrs.customdatatype.datatype.FreeTextDatatype"); | ||
Context.getLocationService().saveLocationAttributeType(emailAttrType); | ||
|
||
// Assign phone number 0123456789 and email address [email protected] to location #1 | ||
Location location1 = Context.getLocationService().getLocation(1); | ||
LocationAttribute la1a = new LocationAttribute(); | ||
la1a.setAttributeType(phoneAttrType); | ||
la1a.setValue("0123456789"); | ||
location1.addAttribute(la1a); | ||
LocationAttribute la1b = new LocationAttribute(); | ||
la1b.setAttributeType(emailAttrType); | ||
la1b.setValue("[email protected]"); | ||
location1.addAttribute(la1b); | ||
Context.getLocationService().saveLocation(location1); | ||
|
||
// Assign same phone number 0123456789 to location #2 | ||
Location location2 = Context.getLocationService().getLocation(2); | ||
LocationAttribute la2 = new LocationAttribute(); | ||
la2.setAttributeType(phoneAttrType); | ||
la2.setValue("0123456789"); | ||
location2.addAttribute(la2); | ||
Context.getLocationService().saveLocation(location2); | ||
|
||
// Search for location #1 by phone number AND email address | ||
Map<LocationAttributeType, Object> attrValues = new HashMap<LocationAttributeType, Object>(); | ||
attrValues.put(phoneAttrType, "0123456789"); | ||
attrValues.put(emailAttrType, "[email protected]"); | ||
|
||
// Check that only location #1 is returned | ||
List<Location> locations = Context.getLocationService().getLocations(null, null, attrValues, false, null, null); | ||
Assert.assertEquals(1, locations.size()); | ||
Assert.assertEquals(location1, locations.get(0)); | ||
} | ||
|
||
/** | ||
* Get locations that have a specified tag among its child tags. | ||
* | ||
|