/** * Takes XML like: <something> <location name="Cerca-la-Source"/> </something> returns the * number of locations added * * @deprecated since 2.2.0 migrate the method to your code base if needed */ @Deprecated public static int importLocations(Document document) { int ret = 0; LocationService ls = Context.getLocationService(); List<Node> toAdd = new ArrayList<>(); findNodesNamed(document, "location", toAdd); for (Node node : toAdd) { Element e = (Element) node; String name = e.getAttribute("name"); if (name == null || name.length() == 0) { throw new IllegalArgumentException("each <location /> element must define a name attribute"); } if (ls.getLocation(name) != null) { continue; } Location location = new Location(); location.setName(name); ls.saveLocation(location); ++ret; } return ret; }
/** * @see LocationValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfParentLocationCreatesALoop() { Location location1 = new Location(); Location location2 = new Location(); Location location3 = new Location(); location1.setName("County General"); location2.setName("Radiology"); location3.setName("Radiology Lab"); location3.setParentLocation(location2); location2.setParentLocation(location1); location1.setParentLocation(location3); Errors errors = new BindException(location1, "location"); new LocationValidator().validate(location1, errors); Assert.assertTrue(errors.hasFieldErrors("parentLocation")); }
/** * @see LocationService#saveLocation(Location) */ @Test public void saveLocation_shouldReturnSavedObject() { Location location = new Location(); location.setName("Some name"); location.setDescription("Some description"); Location savedLocation = Context.getLocationService().saveLocation(location); Assert.assertEquals(location, savedLocation); }
/** * @see OpenmrsMetadataSaveHandler#handle(OpenmrsMetadata,User,Date,String) */ @Test public void handle_shouldTrimWhitespaceFromName() { String NAME = "the location name"; OpenmrsMetadataSaveHandler handler = new OpenmrsMetadataSaveHandler(); Location loc = new Location(); loc.setName(" " + NAME + " "); handler.handle(loc, null, null, null); Assert.assertEquals(NAME, loc.getName()); }
/** * You should be able to add a transient tag with an existing tag name. * * @see LocationService#saveLocation(Location) */ @Test public void saveLocation_shouldOverwriteTransientTagIfTagWithSameNameExists() { LocationService ls = Context.getLocationService(); // First, create a new Location Location location = new Location(); location.setName("name"); location.setDescription("is a location"); // Add a transient tag with an existing name location.addTag(new LocationTag("General Hospital", null)); ls.saveLocation(location); Location newSavedLocation = ls.getLocation(location.getLocationId()); // Saved parent location should have exactly 1 tag assertEquals(1, newSavedLocation.getTags().size()); // Tag must be overwritten with tag with locationTagId == 1 assertNotNull("Location tag should have an ID now", newSavedLocation.getTags().iterator().next().getLocationTagId()); assertEquals(1, newSavedLocation.getTags().iterator().next().getLocationTagId().intValue()); }
/** * @see LocationService#saveLocation(Location) */ @Test(expected = APIException.class) public void saveLocation_shouldThrowAPIExceptionIfTransientTagIsNotFound() { LocationTag tagWithoutName = new LocationTag("some random tag name", "a nonexistant tag"); Location location = new Location(); location.setName("Some name"); location.setDescription("Some description"); location.addTag(tagWithoutName); Context.getLocationService().saveLocation(location); }
/** * @see LocationService#saveLocationTag(LocationTag) */ @Test(expected = APIException.class) public void saveLocationTag_shouldThrowAPIExceptionIfTagHasNoName() { LocationTag tagWithoutName = new LocationTag(); Location location = new Location(); location.setName("Some name"); location.setDescription("Some description"); location.addTag(tagWithoutName); Context.getLocationService().saveLocation(location); }
/** * @see OpenmrsMetadataSaveHandler#handle(OpenmrsMetadata,User,Date,String) */ @Test public void handle_shouldTrimWhitespaceFromDescription() { String DESC = "the location desc"; OpenmrsMetadataSaveHandler handler = new OpenmrsMetadataSaveHandler(); Location loc = new Location(); loc.setName("a name"); loc.setDescription(" " + DESC + " "); handler.handle(loc, null, null, null); Assert.assertEquals(DESC, loc.getDescription()); } }
/** * @see org.openmrs.validator.LocationValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfLocationNameAlreadyExist() { Location location = new Location(); location.setName("Unknown Location"); location.setDescription("desc"); Errors errors = new BindException(location, "location"); new LocationValidator().validate(location, errors); Assert.assertTrue(errors.hasErrors()); }
/** * @see LocationValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfAllFieldsAreCorrect() { Location location = new Location(); location.setName("County General"); location.setDescription("desc"); Errors errors = new BindException(location, "location"); new LocationValidator().validate(location, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see LocationValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfRetiredAndRetireReasonIsNullOrEmpty() { Location location = new Location(); location.setName("County General"); location.setRetired(true); Errors errors = new BindException(location, "location"); new LocationValidator().validate(location, errors); Assert.assertTrue(errors.hasFieldErrors("retireReason")); }
/** * @see LocationValidator#validate(Object,Errors) */ @Test public void validate_shouldSetRetiredToFalseIfRetireReasonIsNullOrEmpty() { Location location = new Location(); location.setName("County General"); location.setRetired(true); Errors errors = new BindException(location, "location"); new LocationValidator().validate(location, errors); Assert.assertFalse(location.getRetired()); }
String newDesc = "new desc"; location.setName(newName); location.setDescription(newDesc);
/** * @see LocationService#retireLocation(Location location, String reason) */ @Test public void retireLocation_shouldNotRetireIndependentField() { LocationService locationService = Context.getLocationService(); Location location = new Location(1); location.setName("location to retire"); LocationTag tag = new LocationTag(1); location.addTag(tag); locationService.retireLocation(location, "test retire reason"); Assert.assertFalse(tag.getRetired()); }
/** * @see LocationValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfRetiredLocationIsGivenRetiredReason() { Location location = new Location(); location.setName("County General"); location.setDescription("desc"); location.setRetired(true); location.setRetireReason("Because I don't like County General"); Errors errors = new BindException(location, "location"); new LocationValidator().validate(location, errors); Assert.assertFalse(errors.hasErrors()); }
location.setName("name"); location.setDescription("is a location"); ls.saveLocation(location);
/** * should set audit info if any item in the location is edited * * @see LocationService#saveLocation(Location) */ @Test public void saveLocation_shouldSetAuditInfoIfAnyItemInTheLocationIsEdited() { LocationService ls = Context.getLocationService(); Location location = ls.getLocation(1); Assert.assertNotNull(location); Assert.assertNull(location.getDateChanged()); Assert.assertNull(location.getChangedBy()); location.setName("edited name"); ls.saveLocation(location); Location editedLocation = Context.getLocationService().saveLocation(location); Context.flushSession(); Assert.assertNotNull(editedLocation.getDateChanged()); Assert.assertNotNull(editedLocation.getChangedBy()); }
/** * Test to make sure that a simple save to a new location gets persisted to the database * * @see LocationService#saveLocation(Location) */ @Test public void saveLocation_shouldCreateLocationSuccessfully() { Location location = new Location(); location.setName("testing"); location.setDescription("desc"); location.setAddress1("123"); location.setAddress1("456"); location.setCityVillage("city"); location.setStateProvince("state"); location.setCountry("country"); location.setPostalCode("post"); location.setLatitude("lat"); location.setLongitude("lon"); LocationService ls = Context.getLocationService(); ls.saveLocation(location); Location newSavedLocation = ls.getLocation(location.getLocationId()); assertNotNull("The saved location should have an id now", location.getLocationId()); assertNotNull("We should get back a location", newSavedLocation); assertTrue("The created location needs to equal the pojo location", location.equals(newSavedLocation)); }
/** * @see HibernateAdministrationDAO#validate(Object,Errors) */ @Test public void validate_shouldPassValidationForLocationClassIfFieldLengthsAreCorrect() { Location location = new Location(); location.setName("name"); location.setDescription("description"); location.setAddress1("address1"); location.setAddress2("address2"); location.setAddress3("address3"); location.setAddress4("address4"); location.setAddress5("address5"); location.setAddress6("address6"); location.setCityVillage("cityVillage"); location.setStateProvince("stateProvince"); location.setCountry("country"); location.setPostalCode("postalCode"); location.setLatitude("latitude"); location.setLongitude("longitude"); location.setCountyDistrict("countyDistrict"); location.setRetireReason("retireReason"); Errors errors = new BindException(location, "location"); dao.validate(location, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see LocationValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { Location location = new Location(); location.setName("name"); location.setDescription("description"); location.setAddress1("address1"); location.setAddress2("address2"); location.setAddress3("address3"); location.setAddress4("address4"); location.setAddress5("address5"); location.setAddress6("address6"); location.setCityVillage("cityVillage"); location.setStateProvince("stateProvince"); location.setCountry("country"); location.setPostalCode("postalCode"); location.setLatitude("latitude"); location.setLongitude("longitude"); location.setCountyDistrict("countyDistrict"); location.setRetireReason("retireReason"); Errors errors = new BindException(location, "location"); new LocationValidator().validate(location, errors); Assert.assertFalse(errors.hasErrors()); }