Skip to content

Commit

Permalink
Fixes for issue AtlasOfLivingAustralia#12 Based on maniplulating the …
Browse files Browse the repository at this point in the history
…instance list. This will be replaced in the main branch with something that reports per-query instances and per-species instances separately.
  • Loading branch information
charvolant committed Feb 15, 2023
1 parent 3aa7a34 commit d4531af
Show file tree
Hide file tree
Showing 9 changed files with 8,875 additions and 8,760 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -118,10 +119,10 @@ public void testGetSpecies1() throws Exception {
URL req = new URL("http://localhost:9189/ws/species");
List result = readList(req);
assertNotNull(result);
assertEquals(1, result.size());
assertEquals(2, result.size());
assertTrue(result.get(0) instanceof Map);
Map instance = (Map) result.get(0);
assertEquals("Pterostylis oreophila", instance.get("scientificName"));
assertEquals("Pandion haliaetus", instance.get("scientificName"));
assertEquals("species", instance.get("taxonRank"));
}

Expand Down Expand Up @@ -186,4 +187,49 @@ public void testGetReport3() throws Exception {
assertFalse(report.isSensitive());
}

@Test
public void testGetReport4() throws Exception {
List<Generalisation> generalisations = this.client.getGeneralisations();
SensitivityReport report = this.client.report("Pandion haliaetus", null, null, "South Australia", "Australia", null);
assertNotNull(report);
assertTrue(report.isSensitive());
Map<String, String> supplied = new HashMap<>();
Map<String, Object> original = new HashMap<>();
Map<String, Object> updated = new HashMap<>();
supplied.put("stateProvince", "South Australia");
supplied.put("country", "Australia");
supplied.put("decimalLatitude", "-35.49386");
supplied.put("decimalLongitude", "138.54762");
supplied.put("eventDate", "2022-10-30");
for (Generalisation g: generalisations) {
g.process(supplied, original, updated, report);
}
assertEquals("Record is South Australia in SA Category 2 Conservation Sensitive. Generalised to 10km by SA DEWNR.", updated.get("dataGeneralizations"));
assertEquals("138.6", updated.get("decimalLongitude"));
assertEquals("-35.5", updated.get("decimalLatitude"));
assertNull(updated.get("eventDate"));
}
@Test
public void testGetReport5() throws Exception {
List<Generalisation> generalisations = this.client.getGeneralisations();
SensitivityReport report = this.client.report("Pandion haliaetus", null, null, "Western Australia", "Australia", null);
assertNotNull(report);
assertTrue(report.isSensitive());
Map<String, String> supplied = new HashMap<>();
Map<String, Object> original = new HashMap<>();
Map<String, Object> updated = new HashMap<>();
supplied.put("stateProvince", "West Australia");
supplied.put("country", "Australia");
supplied.put("decimalLatitude", "-32.70613");
supplied.put("decimalLongitude", "124.97978");
supplied.put("eventDate", "2022-09-21");
for (Generalisation g: generalisations) {
g.process(supplied, original, updated, report);
}
assertEquals("Record is Western Australia in Sensitive. Generalised to 10km by WA DEC.", updated.get("dataGeneralizations"));
assertEquals("125.0", updated.get("decimalLongitude"));
assertEquals("-32.7", updated.get("decimalLatitude"));
assertNull(updated.get("eventDate"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
<conservationInstance category="High" authority="ACT Government" dataResourceId="dr2627" generalisation="1km" zone="ACT" />
</instances>
</sensitiveSpecies>
<sensitiveSpecies name="Pandion haliaetus" family="Accipitridae" guid="https://biodiversity.org.au/afd/taxa/23a8017a-3a2b-4a52-8ca6-d168bf52659c" rank="SPECIES" commonName="osprey">
<instances>
<conservationInstance category="" authority="WA DEC" dataResourceId="dr467" generalisation="10km" zone="WA" />
<conservationInstance category="C2" authority="SA DEWNR" dataResourceId="dr884" generalisation="10km" zone="SA" />
</instances>
</sensitiveSpecies>
</sensitiveSpeciesList>
4 changes: 2 additions & 2 deletions ala-sensitive-data-server/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM java:openjdk-8-jre-alpine
FROM amazoncorretto:11.0.18-alpine3.17

ARG ENV

Expand All @@ -15,7 +15,7 @@ COPY ./docker/sds-config.properties /data/sds/config/sds-config.properties

RUN if [ "$ENV" = "gbif-backbone" ]; then \
curl -sf -o /data/lucene/namematching.tgz -L https://datos.gbif.es/others/namematching-gbif-backbone-2020-06-18.tgz; else \
curl -sf -o /data/lucene/namematching.tgz -L https://archives.ala.org.au/archives/nameindexes/latest/namematching-20210811-3.tgz; fi
curl -sf -o /data/lucene/namematching.tgz -L https://archives.ala.org.au/archives/nameindexes/20210811-3/namematching-20210811-3.tgz; fi
#COPY namematching.tgz /data/lucene/namematching.tgz

RUN tar zxf /data/lucene/namematching.tgz -C /data/lucene
Expand Down
2 changes: 1 addition & 1 deletion ala-sensitive-data-server/docker/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ server:
- type: http
port: 9190
conservation:
index: /data/lucene/namematching-20210811-3
index: /data/lucene/namematching
speciesUrl: file:///data/sds/sensitive-species-data.xml
zonesUrl: file:///data/sds/sensitivity-zones.xml
categoriesUrl: file:///data/sds/sensitivity-categories.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import au.org.ala.sds.validation.ValidationOutcome;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -34,17 +35,21 @@ public ApiTranslator(boolean useLibrary) {
/**
* Generate a sensitivity report from the SDS outcome.
*/
public SensitivityReport buildSensitivityReport(ValidationOutcome outcome) {
public SensitivityReport buildSensitivityReport(ValidationOutcome outcome, boolean allInstances) {
if (outcome == null)
return null;
Map<String, Object> original = this.useLibrary && outcome.getResult() != null ? (Map<String, Object>) outcome.getResult().remove(ORIGINAL_VALUES) : new HashMap<>();
Map<String, Object> result = this.useLibrary ? outcome.getResult() : new HashMap<>();
List<au.org.ala.sds.model.SensitivityInstance> instances = outcome.getInstances();
if (allInstances && outcome.getReport() instanceof SdsValidationReport) {
instances = ((SdsValidationReport) outcome.getReport()).getSpecies().getInstances();
}
return SensitivityReport.builder()
.valid(outcome.isValid())
.sensitive(outcome.isSensitive())
.loadable(outcome.isLoadable())
.accessControl(outcome.isControlledAccess())
.report(this.buildValidationReport(outcome.getReport()))
.report(this.buildValidationReport(outcome.getReport(), instances))
.original(original)
.updated(result)
.build();
Expand All @@ -53,15 +58,15 @@ public SensitivityReport buildSensitivityReport(ValidationOutcome outcome) {
/**
* Generate a validation report from an SDS validation report.
*/
public ValidationReport buildValidationReport(au.org.ala.sds.validation.ValidationReport report) {
public ValidationReport buildValidationReport(au.org.ala.sds.validation.ValidationReport report, List<au.org.ala.sds.model.SensitivityInstance> instances) {
if (report == null)
return null;
ValidationReport.ValidationReportBuilder builder = ValidationReport.builder()
.messages(report.getMessages().stream().map(m -> this.buildMessage(m)).collect(Collectors.toList()))
.category(report.getCategory())
.assertion(report.getAssertion());
if (report instanceof SdsValidationReport)
builder.taxon(this.buildSensitiveTaxon(((SdsValidationReport) report).getSpecies()));
builder.taxon(this.buildSensitiveTaxon(((SdsValidationReport) report).getSpecies(), instances));
return builder.build();
}

Expand All @@ -77,7 +82,7 @@ public ValidationReport.Message buildMessage(Message message) {
/**
* Generate a sensitive taxon description from the SDS
*/
public SensitiveTaxon buildSensitiveTaxon(au.org.ala.sds.model.SensitiveTaxon taxon) {
public SensitiveTaxon buildSensitiveTaxon(au.org.ala.sds.model.SensitiveTaxon taxon, List<au.org.ala.sds.model.SensitivityInstance> instances) {
if (taxon == null)
return null;
return SensitiveTaxon.builder()
Expand All @@ -87,7 +92,10 @@ public SensitiveTaxon buildSensitiveTaxon(au.org.ala.sds.model.SensitiveTaxon ta
.acceptedName(taxon.getAcceptedName())
.commonName(taxon.getCommonName())
.taxonRank(taxon.getRank() == null ? null : taxon.getRank().getRank())
.instances(taxon.getInstances().stream().map(i -> this.buildSensitivityInstance(i)).collect(Collectors.toList()))
.instances(instances.stream()
.map(i -> this.buildSensitivityInstance(i))
.collect(Collectors.toList())
)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ public SensitivityReport report(
@ApiParam(value = "The source data resource identifier", example = "dr1654") @QueryParam("dataResourceUid")String dataResourceUid,
@ApiParam(value = "The state or province zone identifier", example = "NSW") @QueryParam("stateProvince") String stateProvince,
@ApiParam(value = "The country zone identifier", example = "AUS") @QueryParam("country") String country,
@ApiParam(value = "The zone identifiers ", allowMultiple = true, example = "FFEZ") @QueryParam("zone") List<String> zones) {
@ApiParam(value = "The zone identifiers ", allowMultiple = true, example = "FFEZ") @QueryParam("zone") List<String> zones
) {
Map<String, String> properties = new HashMap<>();
properties.put("samplesProvided", "yes");
if (dataResourceUid != null && !dataResourceUid.isEmpty())
Expand All @@ -291,7 +292,7 @@ public SensitivityReport report(
scientificName,
taxonId
);
SensitivityReport report = this.translator.buildSensitivityReport(outcome);
SensitivityReport report = this.translator.buildSensitivityReport(outcome, false);
return report;
}

Expand Down Expand Up @@ -324,7 +325,7 @@ public SensitivityReport process(ProcessQuery query) {
query.getTaxonId()
);
this.amendOutcome(query, outcome);
SensitivityReport report = this.translator.buildSensitivityReport(outcome);
SensitivityReport report = this.translator.buildSensitivityReport(outcome, false);
for (Generalisation generalisation: this.generalisations) {
generalisation.process(query, report);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ModelResource(SDSConfiguration configuration){
.collect(Collectors.toList());
sis = new URL(sdsConfig.getSpeciesUrl()).openStream();
this.sensitive = new SensitiveSpeciesXmlDao(sis).getAll().stream()
.map(taxon -> translator.buildSensitiveTaxon(taxon))
.map(taxon -> translator.buildSensitiveTaxon(taxon, taxon.getInstances()))
.collect(Collectors.toList());
this.layers = sdsConfig.getGeospatialLayers();
} catch (Exception e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,64 @@ public void testReport4() throws Exception {
}


// Test taxon with multiple instances
@Test
public void testReport5() throws Exception {
SensitivityReport report = resource.report("Pandion haliaetus", null, "dr8359", "Western Australia", "Australia", null);
assertNotNull(report);
assertTrue(report.isValid());
assertTrue(report.isSensitive());
assertTrue(report.isLoadable());
assertFalse(report.isAccessControl());
ValidationReport vr = report.getReport();
assertNotNull(vr);
SensitiveTaxon taxon = vr.getTaxon();
assertNotNull(taxon);
assertEquals("Pandion haliaetus", taxon.getScientificName());
assertEquals("https://biodiversity.org.au/afd/taxa/23a8017a-3a2b-4a52-8ca6-d168bf52659c", taxon.getTaxonId());
assertNotNull(taxon.getInstances());
assertEquals(1, taxon.getInstances().size());
SensitivityInstance instance = taxon.getInstances().get(0);
assertEquals("WA DEC", instance.getAuthority());
assertEquals(SensitivityInstance.SensitivityType.CONSERVATION, instance.getType());
assertEquals("10km", instance.getGeneralisation().getGeneralisation());
assertNotNull(instance.getCategory());
assertEquals("Sensitive", instance.getCategory().getId());
assertEquals("dr467", instance.getDataResourceId());
assertNotNull(instance.getZone());
assertEquals("WA", instance.getZone().getId());
}


// Test taxon with multiple instances - only one requested
@Test
public void testReport6() throws Exception {
SensitivityReport report = resource.report("Pandion haliaetus", null, "dr8359", "South Australia", "Australia", null);
assertNotNull(report);
assertTrue(report.isValid());
assertTrue(report.isSensitive());
assertTrue(report.isLoadable());
assertFalse(report.isAccessControl());
ValidationReport vr = report.getReport();
assertNotNull(vr);
SensitiveTaxon taxon = vr.getTaxon();
assertNotNull(taxon);
assertEquals("Pandion haliaetus", taxon.getScientificName());
assertEquals("https://biodiversity.org.au/afd/taxa/23a8017a-3a2b-4a52-8ca6-d168bf52659c", taxon.getTaxonId());
assertNotNull(taxon.getInstances());
assertEquals(1, taxon.getInstances().size());
SensitivityInstance instance = taxon.getInstances().get(0);
assertEquals("SA DEWNR", instance.getAuthority());
assertEquals(SensitivityInstance.SensitivityType.CONSERVATION, instance.getType());
assertEquals("10km", instance.getGeneralisation().getGeneralisation());
assertNotNull(instance.getCategory());
assertEquals("C2", instance.getCategory().getId());
assertEquals("dr884", instance.getDataResourceId());
assertNotNull(instance.getZone());
assertEquals("SA", instance.getZone().getId());
}


@Test
public void testProcess1() throws Exception {
Map<String, String> properties = new HashMap<>();
Expand Down Expand Up @@ -285,16 +343,7 @@ public void testProcess3() throws Exception {
assertEquals("https://biodiversity.org.au/afd/taxa/2edc1a0c-b74a-49da-b053-b7e1328a54a7", taxon.getTaxonId());
assertEquals("little waterfall frog", taxon.getCommonName());
assertNotNull(taxon.getInstances());
assertEquals(1, taxon.getInstances().size());
SensitivityInstance instance = taxon.getInstances().get(0);
assertEquals("Qld DEHP", instance.getAuthority());
assertEquals(SensitivityInstance.SensitivityType.CONSERVATION, instance.getType());
assertEquals("10km", instance.getGeneralisation().getGeneralisation());
assertNotNull(instance.getCategory());
assertEquals("Sensitive", instance.getCategory().getId());
assertEquals("dr493", instance.getDataResourceId());
assertNotNull(instance.getZone());
assertEquals("QLD", instance.getZone().getId());
assertEquals(0, taxon.getInstances().size());
Map<String, Object> result = report.getUpdated();
assertNotNull(result);
assertNull(result.get("decimalLongitude"));
Expand Down
Loading

0 comments on commit d4531af

Please sign in to comment.