Skip to content

Commit

Permalink
avniproject/avni-webapp#1306 - location creator test with verificatio…
Browse files Browse the repository at this point in the history
…n of lineage created. few more scenarios. handled GeoLocation errors explicitly, instead of logging exception.
  • Loading branch information
petmongrels committed Aug 26, 2024
1 parent 82c254c commit 94d4913
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public AbstractEncounter updateEncounter(Row row, AbstractEncounter basicEncount
));
if (visitDate != null) basicEncounter.setEncounterDateTime(visitDate.toDateTimeAtStartOfDay(), userService.getCurrentUser());

basicEncounter.setEncounterLocation(locationCreator.getLocation(row, CommonEncounterHeaders.encounterLocation, allErrorMsgs));
basicEncounter.setCancelLocation(locationCreator.getLocation(row, CommonEncounterHeaders.cancelLocation, allErrorMsgs));
basicEncounter.setEncounterLocation(locationCreator.getGeoLocation(row, CommonEncounterHeaders.encounterLocation, allErrorMsgs));
basicEncounter.setCancelLocation(locationCreator.getGeoLocation(row, CommonEncounterHeaders.cancelLocation, allErrorMsgs));
EncounterType encounterType = encounterTypeCreator.getEncounterType(row.get(CommonEncounterHeaders.encounterTypeHeaderName), CommonEncounterHeaders.encounterTypeHeaderName);
basicEncounter.setEncounterType(encounterType);
return basicEncounter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
public class LocationCreator {
private static final Logger logger = LoggerFactory.getLogger(LocationCreator.class);

public Point getLocation(Row row, String header, List<String> errorMsgs) {
try {
String location = row.get(header);
if (!S.isEmpty(location)) {
String[] points = location.split(",");
public Point getGeoLocation(Row row, String header, List<String> errorMsgs) {
String location = row.get(header);
if (!S.isEmpty(location)) {
String[] points = location.split(",");
if (points.length != 2) {
errorMsgs.add("Invalid 'GPS coordinates'");
return null;
}
try {
return new Point(Double.parseDouble(points[0].trim()), Double.parseDouble(points[1].trim()));
} catch (NumberFormatException e) {
errorMsgs.add("Invalid 'GPS coordinates'");
return null;
}
} catch (Exception ex) {
logger.error(String.format("Error processing row %s", row), ex);
errorMsgs.add(String.format("Invalid '%s'", header));
return null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public BulkLocationModifier(LocationRepository locationRepository, ObservationCr

protected void updateLocationProperties(Row row, List<String> allErrorMsgs, AddressLevel location) {
LocationCreator locationCreator = new LocationCreator();
location.setGpsCoordinates(locationCreator.getLocation(row, LocationHeaders.gpsCoordinates, allErrorMsgs));
location.setGpsCoordinates(locationCreator.getGeoLocation(row, LocationHeaders.gpsCoordinates, allErrorMsgs));
location.setLocationProperties(observationCreator.getObservations(row, headers, allErrorMsgs, FormType.Location, location.getLocationProperties()));
locationRepository.save(location);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ private void write(Row row) throws Exception {
if (exitDate != null) programEnrolment.setProgramExitDateTime(exitDate.toDateTimeAtStartOfDay());

LocationCreator locationCreator = new LocationCreator();
programEnrolment.setEnrolmentLocation(locationCreator.getLocation(row, ProgramEnrolmentHeaders.enrolmentLocation, allErrorMsgs));
programEnrolment.setExitLocation(locationCreator.getLocation(row, ProgramEnrolmentHeaders.exitLocation, allErrorMsgs));
programEnrolment.setEnrolmentLocation(locationCreator.getGeoLocation(row, ProgramEnrolmentHeaders.enrolmentLocation, allErrorMsgs));
programEnrolment.setExitLocation(locationCreator.getGeoLocation(row, ProgramEnrolmentHeaders.exitLocation, allErrorMsgs));
programEnrolment.setProgram(program);
FormMapping formMapping = formMappingRepository.getRequiredFormMapping(individual.getSubjectType().getUuid(), program.getUuid(), null, FormType.ProgramEnrolment);
if (formMapping == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void write(Row row) throws Exception {
individual.setDateOfBirthVerified(row.getBool(SubjectHeaders.dobVerified));
setRegistrationDate(individual, row, allErrorMsgs);
LocationCreator locationCreator = new LocationCreator();
individual.setRegistrationLocation(locationCreator.getLocation(row, SubjectHeaders.registrationLocation, allErrorMsgs));
individual.setRegistrationLocation(locationCreator.getGeoLocation(row, SubjectHeaders.registrationLocation, allErrorMsgs));

AddressLevelTypes registrationLocationTypes = subjectTypeService.getRegistrableLocationTypes(subjectType);
individual.setAddressLevel(addressLevelCreator.findAddressLevel(row, registrationLocationTypes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;
import wiremock.org.checkerframework.checker.units.qual.A;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -83,9 +82,10 @@ private static int newLocationsCreated(int count) {
return count;
}

private void lineageExists(String... lineage) {
AddressLevel address = this.locationRepository.findByTitleLineageIgnoreCase(String.join(", ", lineage)).get();
assertNotNull(address);
private void assertlineageExists(String... lineage) {
String titleLineage = String.join(", ", lineage);
AddressLevel address = this.locationRepository.findByTitleLineageIgnoreCase(titleLineage).get();
assertNotNull(titleLineage, address);
}

private void locationHasAttribute(String[] lineage, String conceptName) {
Expand All @@ -101,11 +101,12 @@ private void treatAsDescriptor(String[] headers, String... additionalHeaders) {
assertEquals(before, after);
}

private void success(String[] headers, String[] cells, int numberOfNewLocations) {
private void success(String[] headers, String[] cells, int numberOfNewLocations, String[]... lineages) {
long before = addressLevelRepository.count();
bulkLocationCreator.write(Collections.singletonList(new Row(headers, cells)), hierarchy);
long after = addressLevelRepository.count();
assertEquals(before + newLocationsCreated(numberOfNewLocations), after);
Arrays.stream(lineages).forEach(this::assertlineageExists);
}

private void failure(String[] headers, String[] cells, String errorMessage) {
Expand All @@ -120,26 +121,43 @@ private void failure(String[] headers, String[] cells, String errorMessage) {
assertEquals(before, after);
}

private String[] lineageExists(String... lineage) {
return lineage;
}

@Test
public void shouldCreate() {
// three locations, full lineage created
success(header("State", "District", "Block", "GPS coordinates"),
dataRow("Bihar", "Vaishali", "Mahua", "23.45,43.85"),
newLocationsCreated(3));
newLocationsCreated(3),
lineageExists("Bihar", "Vaishali", "Mahua")
);

// Location with space
success(header("State", "District", "Block", "GPS coordinates"),
dataRow(" Bihar", " Vaishali ", " Jamui ", "23.20,43.85"),
newLocationsCreated(1));
newLocationsCreated(1),
lineageExists("Bihar", "Vaishali", "Jamui")
);
success(header("State", "District", "Block", "GPS coordinates"),
dataRow(" Bihar", " Darbhanga "),
newLocationsCreated(1));
newLocationsCreated(1),
lineageExists("Bihar", "Darbhanga"));
success(header("State", "District", "Block", "GPS coordinates"),
dataRow(" Bihar", " Aara ", " ", "24.20,43.85"),
newLocationsCreated(1));
newLocationsCreated(1),
lineageExists("Bihar", "Aara"));
success(header(" State ", "District", " Block", " GPS coordinates"),
dataRow(" Bihar", " Chapra ", " ", "24.20,43.85"),
newLocationsCreated(1));
newLocationsCreated(1),
lineageExists("Bihar", "Chapra"));

// upper case
success(header("State", "District", "Block", "GPS coordinates"),
dataRow(" bihar", " VAISHALI ", " Tarora ", "23.20,43.85"),
newLocationsCreated(1));
newLocationsCreated(1),
lineageExists("Bihar", "Vaishali", "Tarora"));

failure(header("State1", "District", "Block", "GPS coordinates"),
dataRow("Bihar", "Vaishali", "Nijma", "23.45,43.85"),
Expand All @@ -163,12 +181,13 @@ public void shouldCreate() {
dataRow("Bihar", "Vaishali", "Block 1", "23.45,43.86", "Answer 1"),
newLocationsCreated(1));
locationHasAttribute(lineage("Bihar", "Vaishali", "Block 1"), "Coded Concept");

//attributes with space in header
success(header("State", "District", "Block", "GPS coordinates", " Coded Concept"),
dataRow("Bihar", "Vaishali", "Block 2", "23.45,43.86", " Answer 1"),
newLocationsCreated(1));
locationHasAttribute(lineage("Bihar", "Vaishali", "Block 2"), "Coded Concept");

//attributes with text concept type
success(header("State", "District", "Block", "GPS coordinates", "Text Concept"),
dataRow("Bihar", "Vaishali", "Block 3", "23.45,43.86", "any text"),
newLocationsCreated(1));
Expand All @@ -178,30 +197,30 @@ public void shouldCreate() {
dataRow("Bihar", "Vaishali", "Block 4", "23.45,43.86", "not an answer to this concept"),
error("Invalid answer 'not an answer to this concept' for 'Coded Concept'"));

// multiple attributes
success(header("State", "District", "Block", "GPS coordinates", " Coded Concept", "Text Concept"),
dataRow("Bihar", "Vaishali", "Block 5", "23.45,43.86", " Answer 1", "any text"),
newLocationsCreated(1));
locationHasAttribute(lineage("Bihar", "Vaishali", "Block 5"), "Coded Concept");
locationHasAttribute(lineage("Bihar", "Vaishali", "Block 5"), "Text Concept");
// end


// without full hierarchy
success(header("State", "District", "Block", "GPS coordinates"),
dataRow("Bihar", "District 1", " ", "23.45,43.85"), newLocationsCreated(1));
lineageExists("Bihar", "District 1");

dataRow("Bihar", "District 1", " ", "23.45,43.85"),
newLocationsCreated(1),
lineageExists("Bihar", "District 1"));
success(header("State", "District", "Block", "GPS coordinates"),
dataRow("State 2", "District 1", " ", "23.45,43.85"), newLocationsCreated(2));
lineageExists("State 2", "District 1");

dataRow("State 2", "District 1", " ", "23.45,43.85"),
newLocationsCreated(2),
lineageExists("State 2", "District 1"));
success(header("State", "District", "Block", "GPS coordinates"),
dataRow("State 3", " ", " ", "23.45,43.85"), newLocationsCreated(1));
lineageExists("State 3");
// end
dataRow("State 3", " ", " ", "23.45,43.85"),
newLocationsCreated(1),
lineageExists("State 3"));


// if in random steps
// if done in random steps
failure(header("State", "District", "Block", "GPS coordinates"),
dataRow(" ", " ", "Block11", "23.45,43.85"),
error(ParentMissingOfLocation));
Expand All @@ -214,6 +233,15 @@ public void shouldCreate() {
// end


// create existing location, results in no new location created
success(header("State", "District", "Block", "GPS coordinates"),
dataRow("Bihar", "Vaishali", "Mahua", "23.45,43.85"),
newLocationsCreated(0));
success(header("State", "District", "Block", "GPS coordinates"),
dataRow("Bihar", "Vaishali", " Mahua ", "23.45,43.85"),
newLocationsCreated(0));


treatAsDescriptor(header("State", "District", "Block", "GPS coordinates"),
dataRow("Example: state 1", "Ex: distr 1", "Ex: blo 1", "Ex. 23.45,43.85"));
treatAsDescriptor(header("State", "District", "Block", "GPS coordinates"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ public class BulkLocationEditorIntegrationTest extends BaseCSVImportTest {
@Autowired
private FormRepository formRepository;
@Autowired
private LocationHierarchyService locationHierarchyService;
@Autowired
private TestLocationService testLocationService;
@Autowired
private BulkLocationEditor bulkLocationEditor;
private String hierarchy;
@Autowired
private LocationRepository locationRepository;

Expand Down Expand Up @@ -87,8 +84,6 @@ public void setUp() throws Exception {
testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block33").parent(district3).type(block).build());

setUser(organisationData.getUser().getUsername());
Map<String, String> hierarchies = locationHierarchyService.determineAddressHierarchiesForAllAddressLevelTypesInOrg();
hierarchy = String.join(".", hierarchies.keySet());
}

private void lineageExists(String... lineage) {
Expand Down

0 comments on commit 94d4913

Please sign in to comment.