@Override protected LocationTag getExistingObject() { return locationService.getLocationTag(EXISTING_ID); } }
/** * Should be able to retrieve a single LocationTag by its name. * * @see LocationService#getLocationTagByName(String) */ @Test public void getLocationTagByName_shouldGetLocationTagByName() { LocationService ls = Context.getLocationService(); // Existing tag assertEquals(ls.getLocationTag(1), ls.getLocationTagByName("General Hospital")); // Nonexistant tag assertEquals(null, ls.getLocationTagByName("random")); }
@Override protected LocationTag getObjectById(Integer id) { return Context.getLocationService().getLocationTag(id); }
/** * Fetches a location tag by name or id * (Will add support for uuid once we stop supporting OpenMRS 1.6, which doesn't a uuid on location tag) * * @param identifier * @return */ public static LocationTag getLocationTag(String identifier) { LocationTag tag = null; if (identifier != null) { identifier = identifier.trim(); // first try to fetch by id try { Integer id = Integer.valueOf(identifier); tag = Context.getLocationService().getLocationTag(id); if (tag != null) { return tag; } } catch (NumberFormatException e) {} // if not, try to fetch by name tag = Context.getLocationService().getLocationTagByName(identifier); if (tag != null) { return tag; } // TODO add ability to fetch by uuid once we are no longer worried about being compatible with OpenMRS 1.6 (since getLocationTagByUuid is not available in 1.6) } return null; }
@Test public void getLocationsHavingAllTags_shouldGetLocationsHavingAllTags() { LocationService ls = Context.getLocationService(); List<LocationTag> list1 = new ArrayList<>(); list1.add(ls.getLocationTag(1)); list1.add(ls.getLocationTag(2)); assertEquals(1, ls.getLocationsHavingAllTags(list1).size()); }
/** * Get locations that have any of specified set of tags among its child tags. * * @see LocationService#getLocationsHavingAnyTag(List<LocationTag;>) */ @Test public void getLocationsHavingAnyTag_shouldGetLocationsHavingAnyTag() { LocationService ls = Context.getLocationService(); List<LocationTag> list1 = new ArrayList<>(); list1.add(ls.getLocationTag(1)); list1.add(ls.getLocationTag(2)); List<LocationTag> list2 = new ArrayList<>(); list2.add(ls.getLocationTag(3)); list2.add(ls.getLocationTag(4)); List<LocationTag> list3 = new ArrayList<>(); list3.add(ls.getLocationTag(1)); list3.add(ls.getLocationTag(2)); list3.add(ls.getLocationTag(3)); assertEquals(1, ls.getLocationsHavingAnyTag(list1).size()); assertEquals(4, ls.getLocationsHavingAnyTag(list2).size()); assertEquals(3, ls.getLocationsHavingAnyTag(list3).size()); }
/** * Get locations that have a specified tag among its child tags. * * @see LocationService#getLocationsByTag(LocationTag) */ @Test public void getLocationsByTag_shouldGetLocationsByTag() { LocationService ls = Context.getLocationService(); assertEquals(1, ls.getLocationsByTag(ls.getLocationTag(1)).size()); assertEquals(2, ls.getLocationsByTag(ls.getLocationTag(3)).size()); assertEquals(4, ls.getLocationsByTag(ls.getLocationTag(4)).size()); }
/** * @see LocationService#getLocationTag(Integer) */ @Test public void getLocationTag_shouldReturnNullWhenNoLocationTagMatchGivenId() { Assert.assertNull(Context.getLocationService().getLocationTag(9999)); }
/** * Make sure that purging a location tag removes the row from the database * * @see LocationService#purgeLocationTag(LocationTag) */ @Test public void purgeLocationTag_shouldDeleteLocationTag() { LocationService ls = Context.getLocationService(); // Fetch the encounter to delete from the db LocationTag tag = ls.getLocationTag(5); ls.purgeLocationTag(tag); // Try to refetch the location. should get a null object LocationTag t = ls.getLocationTag(tag.getLocationTagId()); assertNull("We shouldn't find the tag after deletion", t); }
LocationTag tag = ls.getLocationTag(1); LocationTag newestTag = ls.getLocationTag(tag.getLocationTagId());
/** * @see LocationService#retireLocationTag(LocationTag,String) */ @Test public void retireLocationTag_shouldRetireLocationTagWithGivenReason() { LocationService ls = Context.getLocationService(); LocationTag tag = ls.getLocationTag(1); Assert.assertFalse(tag.getRetired()); String reason = "because i can"; LocationTag newTag = ls.retireLocationTag(tag, reason); Assert.assertEquals(tag, newTag); Assert.assertTrue(tag.getRetired()); Assert.assertEquals(reason, tag.getRetireReason()); }
/** * Test to make sure that a simple save to a new location tag gets persisted to the database * * @see LocationService#saveLocationTag(LocationTag) */ @Test public void saveLocationTag_shouldCreateLocationTagSuccessfully() { LocationTag tag = new LocationTag(); tag.setName("testing"); tag.setDescription("desc"); LocationService ls = Context.getLocationService(); ls.saveLocationTag(tag); LocationTag newSavedTag = ls.getLocationTag(tag.getLocationTagId()); assertNotNull("The saved tag should have an id now", tag.getLocationTagId()); assertNotNull("We should get back a tag", newSavedTag); assertTrue("The created tag needs to equal the pojo location", tag.equals(newSavedTag)); }
/** * should set audit info if any item in the location tag is edited * * @see LocationService#saveLocationTag(LocationTag) */ @Test public void saveLocationTag_shouldSetAuditInfoIfAnyItemInTheLocationTagIsEdited() { LocationService ls = Context.getLocationService(); LocationTag tag = ls.getLocationTag(1); Assert.assertNull(tag.getDateChanged()); Assert.assertNull(tag.getChangedBy()); tag.setName("testing"); tag.setDescription("desc"); LocationTag editedTag = Context.getLocationService().saveLocationTag(tag); Context.flushSession(); Assert.assertNotNull(editedTag.getDateChanged()); Assert.assertNotNull(editedTag.getChangedBy()); }
LocationTag tag = ls.getLocationTag(1);
@Test public void shouldPurgeLocationTag() throws Exception { LocationTag locationTag = service.getLocationTag(3); MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + locationTag.getUuid()); req.addParameter("purge", "true"); handle(req); Assert.assertNull(service.getLocationTag(3)); }
@Test public void shouldGetALocationTagByUuid() throws Exception { LocationTag locationTag = service.getLocationTag(1); MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + locationTag.getUuid()); SimpleObject result = deserialize(handle(req)); Assert.assertNotNull(result); Assert.assertEquals(locationTag.getUuid(), PropertyUtils.getProperty(result, "uuid")); Assert.assertEquals(locationTag.getName(), PropertyUtils.getProperty(result, "name")); }
@Test public void shouldRetireLocationTag() throws Exception { LocationTag locationTag = service.getLocationTag(2); Assert.assertFalse(locationTag.isRetired()); MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + locationTag.getUuid()); req.addParameter("!purge", ""); req.addParameter("reason", "random reason"); handle(req); LocationTag retiredLocationTag = service.getLocationTag(2); Assert.assertTrue(retiredLocationTag.isRetired()); Assert.assertEquals("random reason", retiredLocationTag.getRetireReason()); }