From c67ef762f1fd5faf39421aef99149c282e4c4df2 Mon Sep 17 00:00:00 2001 From: Sam Leeflang Date: Wed, 20 Nov 2024 15:23:48 +0100 Subject: [PATCH 1/6] Add sanitize logic to location meter field --- .../terms/BaseDigitalObjectDirector.java | 55 ++++++++++++++----- .../specimen/location/AbstractMeterTerm.java | 25 +++++++++ .../location/MaximumDepthInMeters.java | 16 +++++- .../MaximumDistanceAboveSurfaceInMeters.java | 16 +++++- .../location/MaximumElevationInMeters.java | 16 +++++- .../location/MinimumDepthInMeters.java | 16 +++++- .../MinimumDistanceAboveSurfaceInMeters.java | 16 +++++- .../location/MinimumElevationInMeters.java | 16 +++++- .../location/MaximumDepthInMetersTest.java | 40 ++++++++++++-- ...ximumDistanceAboveSurfaceInMetersTest.java | 42 +++++++++++--- .../MaximumElevationInMetersTest.java | 41 ++++++++++++-- .../location/MinimumDepthInMetersTest.java | 41 ++++++++++++-- ...nimumDistanceAboveSurfaceInMetersTest.java | 42 +++++++++++--- .../MinimumElevationInMetersTest.java | 41 ++++++++++++-- 14 files changed, 355 insertions(+), 68 deletions(-) create mode 100644 src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java diff --git a/src/main/java/eu/dissco/core/translator/terms/BaseDigitalObjectDirector.java b/src/main/java/eu/dissco/core/translator/terms/BaseDigitalObjectDirector.java index 117cf8b..5d00258 100644 --- a/src/main/java/eu/dissco/core/translator/terms/BaseDigitalObjectDirector.java +++ b/src/main/java/eu/dissco/core/translator/terms/BaseDigitalObjectDirector.java @@ -174,6 +174,7 @@ import eu.dissco.core.translator.terms.specimen.identification.taxonomy.Tribe; import eu.dissco.core.translator.terms.specimen.identification.taxonomy.VerbatimTaxonRank; import eu.dissco.core.translator.terms.specimen.identification.taxonomy.VernacularName; +import eu.dissco.core.translator.terms.specimen.location.AbstractMeterTerm; import eu.dissco.core.translator.terms.specimen.location.Continent; import eu.dissco.core.translator.terms.specimen.location.Country; import eu.dissco.core.translator.terms.specimen.location.CountryCode; @@ -252,6 +253,17 @@ public abstract class BaseDigitalObjectDirector { private final FdoProperties fdoProperties; private final List identifierTerms; + private static Double stringToDouble(Term term, String value) { + try { + if (value != null) { + return Double.valueOf(value); + } + } catch (NumberFormatException ex) { + log.warn("Unable to parse value: {} to a double for term: {}", value, term.getTerm()); + } + return null; + } + public DigitalSpecimen assembleDigitalSpecimenTerm(JsonNode data, boolean dwc) throws OrganisationException, UnknownPhysicalSpecimenIdType { var ds = assembleDigitalSpecimenTerms(data, dwc); @@ -455,7 +467,6 @@ protected Identification createIdentification(JsonNode data, boolean dwc) { return identification; } - private List assembleEventTerms(JsonNode data, boolean dwc) { var geoReference = new Georeference() .withType("ods:Georeference") @@ -531,14 +542,10 @@ private List assembleEventTerms(JsonNode data, boolean dwc) { .withDwcStateProvince(termMapper.retrieveTerm(new StateProvince(), data, dwc)) .withDwcWaterBody(termMapper.retrieveTerm(new WaterBody(), data, dwc)) .withDwcHigherGeography(termMapper.retrieveTerm(new HigherGeography(), data, dwc)) - .withDwcMaximumDepthInMeters(parseToDouble(new MaximumDepthInMeters(), data, dwc)) .withDwcMaximumDistanceAboveSurfaceInMeters( parseToDouble(new MaximumDistanceAboveSurfaceInMeters(), data, dwc)) - .withDwcMaximumElevationInMeters(parseToDouble(new MaximumElevationInMeters(), data, dwc)) - .withDwcMinimumDepthInMeters(parseToDouble(new MinimumDepthInMeters(), data, dwc)) .withDwcMinimumDistanceAboveSurfaceInMeters( parseToDouble(new MinimumDistanceAboveSurfaceInMeters(), data, dwc)) - .withDwcMinimumElevationInMeters(parseToDouble(new MinimumElevationInMeters(), data, dwc)) .withDwcVerbatimDepth(termMapper.retrieveTerm(new VerbatimDepth(), data, dwc)) .withDwcVerbatimElevation(termMapper.retrieveTerm(new VerbatimElevation(), data, dwc)) .withDwcVerticalDatum(termMapper.retrieveTerm(new VerticalDatum(), data, dwc)) @@ -546,6 +553,10 @@ private List assembleEventTerms(JsonNode data, boolean dwc) { .withDwcLocationRemarks(termMapper.retrieveTerm(new LocationRemarks(), data, dwc)) .withOdsHasGeoreference(geoReference) .withOdsHasGeologicalContext(geologicalContext); + setMinMaxMeterField(new MinimumElevationInMeters(), location, data, dwc); + setMinMaxMeterField(new MaximumElevationInMeters(), location, data, dwc); + setMinMaxMeterField(new MinimumDepthInMeters(), location, data, dwc); + setMinMaxMeterField(new MaximumDepthInMeters(), location, data, dwc); var assertions = new EventAssertions().gatherEventAssertions(mapper, data, dwc); var event = new Event() .withType("ods:Event") @@ -585,6 +596,31 @@ private List assembleEventTerms(JsonNode data, boolean dwc) { return List.of(event); } + private void setMinMaxMeterField(AbstractMeterTerm term, Location location, JsonNode data, + boolean dwc) { + var sanitizedValue = termMapper.retrieveTerm(term, data, dwc); + var doubleElevation = stringToDouble(term, sanitizedValue); + if (doubleElevation == null && sanitizedValue != null) { + if (term instanceof MaximumElevationInMeters) { + location.setDwcVerbatimElevation( + location.getDwcVerbatimElevation() + "| unparseable value maximumElevationInMeters: " + + sanitizedValue); + } else if (term instanceof MinimumElevationInMeters) { + location.setDwcVerbatimElevation( + location.getDwcVerbatimElevation() + "| unparseable value minimumElevationInMeters: " + + sanitizedValue); + } else if (term instanceof MaximumDepthInMeters) { + location.setDwcVerbatimDepth( + location.getDwcVerbatimDepth() + "| unparseable value maximumDepthInMeters: " + + sanitizedValue); + } else if (term instanceof MinimumDepthInMeters) { + location.setDwcVerbatimDepth( + location.getDwcVerbatimDepth() + "| unparseable value minimumDepthInMeters: " + + sanitizedValue); + } + } + } + private > T retrieveEnum(Term term, JsonNode data, boolean dwc, java.lang.Class enumClass) { var value = termMapper.retrieveTerm(term, data, dwc); @@ -612,14 +648,7 @@ private Integer parseToInteger(Term term, JsonNode data, boolean dwc) { private Double parseToDouble(Term term, JsonNode data, boolean dwc) { var value = termMapper.retrieveTerm(term, data, dwc); - try { - if (value != null) { - return Double.valueOf(value); - } - } catch (NumberFormatException ex) { - log.warn("Unable to parse value: {} to a double for term: {}", value, term.getTerm()); - } - return null; + return stringToDouble(term, value); } private Boolean parseToBoolean(Term term, JsonNode data, boolean dwc) { diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java new file mode 100644 index 0000000..f854767 --- /dev/null +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java @@ -0,0 +1,25 @@ +package eu.dissco.core.translator.terms.specimen.location; + +import eu.dissco.core.translator.terms.Term; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public abstract class AbstractMeterTerm extends Term { + + private static final Pattern M_PATTERN = Pattern.compile("((-\\s?)?\\d+([.,]\\d+)?)\\s*m\\.?(eter)?(tr)?(\\sm.?)?"); + + protected String sanitizeInput(String input) { + input = input.trim().toLowerCase(); + Matcher matcher = M_PATTERN.matcher(input); + if (matcher.matches()) { + log.debug("Parsed Number: {}", matcher.group(1)); + return matcher.group(1); + } else { + log.debug("Input string does not match the expected format: {}", input); + return input; + } + } + +} diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java index 87cd617..e33b296 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java @@ -4,7 +4,7 @@ import eu.dissco.core.translator.terms.Term; import java.util.List; -public class MaximumDepthInMeters extends Term { +public class MaximumDepthInMeters extends AbstractMeterTerm { public static final String TERM = DWC_PREFIX + "maximumDepthInMeters"; @@ -15,12 +15,22 @@ public class MaximumDepthInMeters extends Term { @Override public String retrieveFromDWCA(JsonNode unit) { - return super.searchJsonForTerm(unit, dwcaTerms); + var rawResult = super.searchJsonForTerm(unit, dwcaTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override public String retrieveFromABCD(JsonNode unit) { - return super.searchJsonForTerm(unit, abcdTerms); + var rawResult = super.searchJsonForTerm(unit, abcdTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java index 9bc29d6..aa544fb 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java @@ -4,7 +4,7 @@ import eu.dissco.core.translator.terms.Term; import java.util.List; -public class MaximumDistanceAboveSurfaceInMeters extends Term { +public class MaximumDistanceAboveSurfaceInMeters extends AbstractMeterTerm { public static final String TERM = DWC_PREFIX + "maximumDistanceAboveSurfaceInMeters"; @@ -14,12 +14,22 @@ public class MaximumDistanceAboveSurfaceInMeters extends Term { @Override public String retrieveFromDWCA(JsonNode unit) { - return super.searchJsonForTerm(unit, dwcaTerms); + var rawResult = super.searchJsonForTerm(unit, dwcaTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override public String retrieveFromABCD(JsonNode unit) { - return super.searchJsonForTerm(unit, abcdTerms); + var rawResult = super.searchJsonForTerm(unit, abcdTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java index a5e4b6c..a498318 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java @@ -4,7 +4,7 @@ import eu.dissco.core.translator.terms.Term; import java.util.List; -public class MaximumElevationInMeters extends Term { +public class MaximumElevationInMeters extends AbstractMeterTerm { public static final String TERM = DWC_PREFIX + "maximumElevationInMeters"; @@ -15,12 +15,22 @@ public class MaximumElevationInMeters extends Term { @Override public String retrieveFromDWCA(JsonNode unit) { - return super.searchJsonForTerm(unit, dwcaTerms); + var rawResult = super.searchJsonForTerm(unit, dwcaTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override public String retrieveFromABCD(JsonNode unit) { - return super.searchJsonForTerm(unit, abcdTerms); + var rawResult = super.searchJsonForTerm(unit, abcdTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java index 3d865be..d254c2f 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java @@ -4,7 +4,7 @@ import eu.dissco.core.translator.terms.Term; import java.util.List; -public class MinimumDepthInMeters extends Term { +public class MinimumDepthInMeters extends AbstractMeterTerm { public static final String TERM = DWC_PREFIX + "minimumDepthInMeters"; @@ -15,12 +15,22 @@ public class MinimumDepthInMeters extends Term { @Override public String retrieveFromDWCA(JsonNode unit) { - return super.searchJsonForTerm(unit, dwcaTerms); + var rawResult = super.searchJsonForTerm(unit, dwcaTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override public String retrieveFromABCD(JsonNode unit) { - return super.searchJsonForTerm(unit, abcdTerms); + var rawResult = super.searchJsonForTerm(unit, abcdTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java index d52b51f..7054e66 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java @@ -4,7 +4,7 @@ import eu.dissco.core.translator.terms.Term; import java.util.List; -public class MinimumDistanceAboveSurfaceInMeters extends Term { +public class MinimumDistanceAboveSurfaceInMeters extends AbstractMeterTerm { public static final String TERM = DWC_PREFIX + "minimumDistanceAboveSurfaceInMeters"; @@ -14,12 +14,22 @@ public class MinimumDistanceAboveSurfaceInMeters extends Term { @Override public String retrieveFromDWCA(JsonNode unit) { - return super.searchJsonForTerm(unit, dwcaTerms); + var rawResult = super.searchJsonForTerm(unit, dwcaTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override public String retrieveFromABCD(JsonNode unit) { - return super.searchJsonForTerm(unit, abcdTerms); + var rawResult = super.searchJsonForTerm(unit, abcdTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java index 03a82f2..25328f7 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java @@ -4,7 +4,7 @@ import eu.dissco.core.translator.terms.Term; import java.util.List; -public class MinimumElevationInMeters extends Term { +public class MinimumElevationInMeters extends AbstractMeterTerm { public static final String TERM = DWC_PREFIX + "minimumElevationInMeters"; @@ -15,12 +15,22 @@ public class MinimumElevationInMeters extends Term { @Override public String retrieveFromDWCA(JsonNode unit) { - return super.searchJsonForTerm(unit, dwcaTerms); + var rawResult = super.searchJsonForTerm(unit, dwcaTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override public String retrieveFromABCD(JsonNode unit) { - return super.searchJsonForTerm(unit, abcdTerms); + var rawResult = super.searchJsonForTerm(unit, abcdTerms); + if (rawResult != null) { + return sanitizeInput(rawResult); + } else { + return null; + } } @Override diff --git a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java index 8e46381..5a20394 100644 --- a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java +++ b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) @@ -13,11 +15,12 @@ class MaximumDepthInMetersTest { private static final String MAXIMUM_DEPTH_IN_METERS_STRING = "-350"; private final MaximumDepthInMeters maximumDepthInMeters = new MaximumDepthInMeters(); - @Test - void testRetrieveFromDWCA() { + @ParameterizedTest + @ValueSource(strings = {"-350m.", "-350meter", "-350 m", "-350 MTR"}) + void testRetrieveFromDWCA(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("dwc:maximumDepthInMeters", MAXIMUM_DEPTH_IN_METERS_STRING); + unit.put("dwc:maximumDepthInMeters", input); // When var result = maximumDepthInMeters.retrieveFromDWCA(unit); @@ -27,11 +30,25 @@ void testRetrieveFromDWCA() { } @Test - void testRetrieveFromABCD() { + void testRetrieveFromDWCANull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("dwc:maximumDepthInMeters"); + + // When + var result = maximumDepthInMeters.retrieveFromDWCA(unit); + + // Then + assertThat(result).isNull(); + } + + @ParameterizedTest + @ValueSource(strings = {"-350", "-350.0", "-350.00", "-350.000"}) + void testRetrieveFromABCD(String input) { // Given var unit = MAPPER.createObjectNode(); unit.put("abcd:gathering/depth/measurementOrFactAtomised/upperValue/value", - MAXIMUM_DEPTH_IN_METERS_STRING); + input); // When var result = maximumDepthInMeters.retrieveFromABCD(unit); @@ -40,6 +57,19 @@ void testRetrieveFromABCD() { assertThat(result).isEqualTo(MAXIMUM_DEPTH_IN_METERS_STRING); } + @Test + void testRetrieveFromABCDNull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("abcd:gathering/depth/measurementOrFactAtomised/upperValue/value"); + + // When + var result = maximumDepthInMeters.retrieveFromABCD(unit); + + // Then + assertThat(result).isNull(); + } + @Test void testGetTerm() { // When diff --git a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMetersTest.java b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMetersTest.java index 1ba68df..a656dc6 100644 --- a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMetersTest.java +++ b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMetersTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) @@ -13,12 +15,12 @@ class MaximumDistanceAboveSurfaceInMetersTest { private static final String MAXIMUM_DISTANCE_ABOVE_SURFACE_IN_METERS_STRING = "350"; private final MaximumDistanceAboveSurfaceInMeters maximumDistanceAboveSurfaceInMeters = new MaximumDistanceAboveSurfaceInMeters(); - @Test - void testRetrieveFromDWCA() { + @ParameterizedTest + @ValueSource(strings = {"350m.", "350meter", "350 m m", "350 mtr"}) + void testRetrieveFromDWCA(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("dwc:maximumDistanceAboveSurfaceInMeters", - MAXIMUM_DISTANCE_ABOVE_SURFACE_IN_METERS_STRING); + unit.put("dwc:maximumDistanceAboveSurfaceInMeters", input); // When var result = maximumDistanceAboveSurfaceInMeters.retrieveFromDWCA(unit); @@ -28,11 +30,24 @@ void testRetrieveFromDWCA() { } @Test - void testRetrieveFromABCD() { + void testRetrieveFromDWCANull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("dwc:maximumDistanceAboveSurfaceInMeters"); + + // When + var result = maximumDistanceAboveSurfaceInMeters.retrieveFromDWCA(unit); + + // Then + assertThat(result).isNull(); + } + + @ParameterizedTest + @ValueSource(strings = {"350m.", "350meter", "350 m m", "350 mtr"}) + void testRetrieveFromABCD(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("abcd:gathering/height/measurementOrFactAtomised/upperValue/value", - MAXIMUM_DISTANCE_ABOVE_SURFACE_IN_METERS_STRING); + unit.put("abcd:gathering/height/measurementOrFactAtomised/upperValue/value", input); // When var result = maximumDistanceAboveSurfaceInMeters.retrieveFromABCD(unit); @@ -41,6 +56,19 @@ void testRetrieveFromABCD() { assertThat(result).isEqualTo(MAXIMUM_DISTANCE_ABOVE_SURFACE_IN_METERS_STRING); } + @Test + void testRetrieveFromABCDNull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("abcd:gathering/height/measurementOrFactAtomised/upperValue/value"); + + // When + var result = maximumDistanceAboveSurfaceInMeters.retrieveFromABCD(unit); + + // Then + assertThat(result).isNull(); + } + @Test void testGetTerm() { // When diff --git a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMetersTest.java b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMetersTest.java index 4b0781a..1c29773 100644 --- a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMetersTest.java +++ b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMetersTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) @@ -13,11 +15,12 @@ class MaximumElevationInMetersTest { private static final String MAXIMUM_ELEVATION_IN_METERS_STRING = "350"; private final MaximumElevationInMeters maximumElevationInMeters = new MaximumElevationInMeters(); - @Test - void testRetrieveFromDWCA() { + @ParameterizedTest + @ValueSource(strings = {"350m.", "350meter", "350 m", "350 MTR"}) + void testRetrieveFromDWCA(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("dwc:maximumElevationInMeters", MAXIMUM_ELEVATION_IN_METERS_STRING); + unit.put("dwc:maximumElevationInMeters", input); // When var result = maximumElevationInMeters.retrieveFromDWCA(unit); @@ -27,11 +30,24 @@ void testRetrieveFromDWCA() { } @Test - void testRetrieveFromABCD() { + void testRetrieveFromDWCANull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("dwc:maximumElevationInMeters"); + + // When + var result = maximumElevationInMeters.retrieveFromDWCA(unit); + + // Then + assertThat(result).isNull(); + } + + @ParameterizedTest + @ValueSource(strings = {"350m.", "350meter", "350 m", "350 MTR"}) + void testRetrieveFromABCD(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("abcd:gathering/altitude/measurementOrFactAtomised/upperValue/value", - MAXIMUM_ELEVATION_IN_METERS_STRING); + unit.put("abcd:gathering/altitude/measurementOrFactAtomised/upperValue/value", input); // When var result = maximumElevationInMeters.retrieveFromABCD(unit); @@ -40,6 +56,19 @@ void testRetrieveFromABCD() { assertThat(result).isEqualTo(MAXIMUM_ELEVATION_IN_METERS_STRING); } + @Test + void testRetrieveFromABCDNull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("abcd:gathering/altitude/measurementOrFactAtomised/upperValue/value"); + + // When + var result = maximumElevationInMeters.retrieveFromABCD(unit); + + // Then + assertThat(result).isNull(); + } + @Test void testGetTerm() { // When diff --git a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMetersTest.java b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMetersTest.java index 9e664c0..0041417 100644 --- a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMetersTest.java +++ b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMetersTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) @@ -13,11 +15,12 @@ class MinimumDepthInMetersTest { private static final String MINIMUM_DEPTH_IN_METERS_STRING = "-50"; private final MinimumDepthInMeters minimumDepthInMeters = new MinimumDepthInMeters(); - @Test - void testRetrieveFromDWCA() { + @ParameterizedTest + @ValueSource(strings = {"-50m.", "-50meter", "-50 m", "-50 MTR"}) + void testRetrieveFromDWCA(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("dwc:minimumDepthInMeters", MINIMUM_DEPTH_IN_METERS_STRING); + unit.put("dwc:minimumDepthInMeters", input); // When var result = minimumDepthInMeters.retrieveFromDWCA(unit); @@ -27,11 +30,24 @@ void testRetrieveFromDWCA() { } @Test - void testRetrieveFromABCD() { + void testRetrieveFromDWCANull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("dwc:minimumDepthInMeters"); + + // When + var result = minimumDepthInMeters.retrieveFromDWCA(unit); + + // Then + assertThat(result).isNull(); + } + + @ParameterizedTest + @ValueSource(strings = {"-50m.", "-50meter", "-50 m", "-50 MTR"}) + void testRetrieveFromABCD(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("abcd:gathering/depth/measurementOrFactAtomised/lowerValue/value", - MINIMUM_DEPTH_IN_METERS_STRING); + unit.put("abcd:gathering/depth/measurementOrFactAtomised/lowerValue/value", input); // When var result = minimumDepthInMeters.retrieveFromABCD(unit); @@ -40,6 +56,19 @@ void testRetrieveFromABCD() { assertThat(result).isEqualTo(MINIMUM_DEPTH_IN_METERS_STRING); } + @Test + void testRetrieveFromNull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("abcd:gathering/depth/measurementOrFactAtomised/lowerValue/value"); + + // When + var result = minimumDepthInMeters.retrieveFromABCD(unit); + + // Then + assertThat(result).isNull(); + } + @Test void testGetTerm() { // When diff --git a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMetersTest.java b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMetersTest.java index d4353e9..a50e598 100644 --- a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMetersTest.java +++ b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMetersTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) @@ -13,12 +15,12 @@ class MinimumDistanceAboveSurfaceInMetersTest { private static final String MINIMUM_DISTANCE_ABOVE_SURFACE_IN_METERS_STRING = "250"; private final MinimumDistanceAboveSurfaceInMeters minimumDistanceAboveSurfaceInMeters = new MinimumDistanceAboveSurfaceInMeters(); - @Test - void testRetrieveFromDWCA() { + @ParameterizedTest + @ValueSource(strings = {"250m. ", "250 meter", "250 m", "250 MTR"}) + void testRetrieveFromDWCA(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("dwc:minimumDistanceAboveSurfaceInMeters", - MINIMUM_DISTANCE_ABOVE_SURFACE_IN_METERS_STRING); + unit.put("dwc:minimumDistanceAboveSurfaceInMeters", input); // When var result = minimumDistanceAboveSurfaceInMeters.retrieveFromDWCA(unit); @@ -28,11 +30,24 @@ void testRetrieveFromDWCA() { } @Test - void testRetrieveFromABCD() { + void testRetrieveFromDWCANull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("dwc:minimumDistanceAboveSurfaceInMeters"); + + // When + var result = minimumDistanceAboveSurfaceInMeters.retrieveFromDWCA(unit); + + // Then + assertThat(result).isNull(); + } + + @ParameterizedTest + @ValueSource(strings = {"250m. ", "250 meter", "250 m", "250 MTR"}) + void testRetrieveFromABCD(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("abcd:gathering/height/measurementOrFactAtomised/lowerValue/value", - MINIMUM_DISTANCE_ABOVE_SURFACE_IN_METERS_STRING); + unit.put("abcd:gathering/height/measurementOrFactAtomised/lowerValue/value", input); // When var result = minimumDistanceAboveSurfaceInMeters.retrieveFromABCD(unit); @@ -41,6 +56,19 @@ void testRetrieveFromABCD() { assertThat(result).isEqualTo(MINIMUM_DISTANCE_ABOVE_SURFACE_IN_METERS_STRING); } + @Test + void testRetrieveFromABCDNull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("abcd:gathering/height/measurementOrFactAtomised/lowerValue/value"); + + // When + var result = minimumDistanceAboveSurfaceInMeters.retrieveFromABCD(unit); + + // Then + assertThat(result).isNull(); + } + @Test void testGetTerm() { // When diff --git a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMetersTest.java b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMetersTest.java index e587ff2..ecac64c 100644 --- a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMetersTest.java +++ b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMetersTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) @@ -13,11 +15,12 @@ class MinimumElevationInMetersTest { private static final String MINIMUM_ELEVATION_IN_METERS_STRING = "250"; private final MinimumElevationInMeters minimumElevationInMeters = new MinimumElevationInMeters(); - @Test - void testRetrieveFromDWCA() { + @ParameterizedTest + @ValueSource(strings = {"250m. ", "250 meter", "250 m", "250 MTR"}) + void testRetrieveFromDWCA(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("dwc:minimumElevationInMeters", MINIMUM_ELEVATION_IN_METERS_STRING); + unit.put("dwc:minimumElevationInMeters", input); // When var result = minimumElevationInMeters.retrieveFromDWCA(unit); @@ -27,11 +30,24 @@ void testRetrieveFromDWCA() { } @Test - void testRetrieveFromABCD() { + void testRetrieveFromDWCANull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("dwc:minimumElevationInMeters"); + + // When + var result = minimumElevationInMeters.retrieveFromDWCA(unit); + + // Then + assertThat(result).isNull(); + } + + @ParameterizedTest + @ValueSource(strings = {"250m. ", "250 meter", "250 m", "250 MTR"}) + void testRetrieveFromABCD(String input) { // Given var unit = MAPPER.createObjectNode(); - unit.put("abcd:gathering/altitude/measurementOrFactAtomised/lowerValue/value", - MINIMUM_ELEVATION_IN_METERS_STRING); + unit.put("abcd:gathering/altitude/measurementOrFactAtomised/lowerValue/value", input); // When var result = minimumElevationInMeters.retrieveFromABCD(unit); @@ -40,6 +56,19 @@ void testRetrieveFromABCD() { assertThat(result).isEqualTo(MINIMUM_ELEVATION_IN_METERS_STRING); } + @Test + void testRetrieveFromABCDNull() { + // Given + var unit = MAPPER.createObjectNode(); + unit.putNull("abcd:gathering/altitude/measurementOrFactAtomised/lowerValue/value"); + + // When + var result = minimumElevationInMeters.retrieveFromABCD(unit); + + // Then + assertThat(result).isNull(); + } + @Test void testGetTerm() { // When From 908ee1105b1da49867f7234cf0afd6e3420d03ea Mon Sep 17 00:00:00 2001 From: Sam Leeflang Date: Wed, 20 Nov 2024 15:29:22 +0100 Subject: [PATCH 2/6] Fix test --- .../terms/specimen/location/MaximumDepthInMetersTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java index 5a20394..5b760b5 100644 --- a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java +++ b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java @@ -41,9 +41,8 @@ void testRetrieveFromDWCANull() { // Then assertThat(result).isNull(); } - @ParameterizedTest - @ValueSource(strings = {"-350", "-350.0", "-350.00", "-350.000"}) + @ValueSource(strings = {"-350m.", "-350meter", "-350 m", "-350 MTR"}) void testRetrieveFromABCD(String input) { // Given var unit = MAPPER.createObjectNode(); From 618c2f61290ff878dbc48fd2042d5e8b418b1063 Mon Sep 17 00:00:00 2001 From: Sam Leeflang Date: Wed, 20 Nov 2024 15:58:09 +0100 Subject: [PATCH 3/6] fix imports --- .../terms/specimen/location/MaximumDepthInMeters.java | 1 - .../location/MaximumDistanceAboveSurfaceInMeters.java | 1 - .../terms/specimen/location/MaximumElevationInMeters.java | 1 - .../terms/specimen/location/MinimumDepthInMeters.java | 1 - .../location/MinimumDistanceAboveSurfaceInMeters.java | 1 - .../terms/specimen/location/MinimumElevationInMeters.java | 1 - .../core/translator/terms/utils/IdentifierUtils.java | 7 ++++++- 7 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java index e33b296..ebe0613 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java @@ -1,7 +1,6 @@ package eu.dissco.core.translator.terms.specimen.location; import com.fasterxml.jackson.databind.JsonNode; -import eu.dissco.core.translator.terms.Term; import java.util.List; public class MaximumDepthInMeters extends AbstractMeterTerm { diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java index aa544fb..1390619 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java @@ -1,7 +1,6 @@ package eu.dissco.core.translator.terms.specimen.location; import com.fasterxml.jackson.databind.JsonNode; -import eu.dissco.core.translator.terms.Term; import java.util.List; public class MaximumDistanceAboveSurfaceInMeters extends AbstractMeterTerm { diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java index a498318..919555e 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java @@ -1,7 +1,6 @@ package eu.dissco.core.translator.terms.specimen.location; import com.fasterxml.jackson.databind.JsonNode; -import eu.dissco.core.translator.terms.Term; import java.util.List; public class MaximumElevationInMeters extends AbstractMeterTerm { diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java index d254c2f..6b3a651 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java @@ -1,7 +1,6 @@ package eu.dissco.core.translator.terms.specimen.location; import com.fasterxml.jackson.databind.JsonNode; -import eu.dissco.core.translator.terms.Term; import java.util.List; public class MinimumDepthInMeters extends AbstractMeterTerm { diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java index 7054e66..c46ef7e 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java @@ -1,7 +1,6 @@ package eu.dissco.core.translator.terms.specimen.location; import com.fasterxml.jackson.databind.JsonNode; -import eu.dissco.core.translator.terms.Term; import java.util.List; public class MinimumDistanceAboveSurfaceInMeters extends AbstractMeterTerm { diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java index 25328f7..7f46e7e 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java @@ -1,7 +1,6 @@ package eu.dissco.core.translator.terms.specimen.location; import com.fasterxml.jackson.databind.JsonNode; -import eu.dissco.core.translator.terms.Term; import java.util.List; public class MinimumElevationInMeters extends AbstractMeterTerm { diff --git a/src/main/java/eu/dissco/core/translator/terms/utils/IdentifierUtils.java b/src/main/java/eu/dissco/core/translator/terms/utils/IdentifierUtils.java index f69489b..db97dcd 100644 --- a/src/main/java/eu/dissco/core/translator/terms/utils/IdentifierUtils.java +++ b/src/main/java/eu/dissco/core/translator/terms/utils/IdentifierUtils.java @@ -1,6 +1,11 @@ package eu.dissco.core.translator.terms.utils; -import static eu.dissco.core.translator.schema.Identifier.DctermsType.*; +import static eu.dissco.core.translator.schema.Identifier.DctermsType.ARK; +import static eu.dissco.core.translator.schema.Identifier.DctermsType.DOI; +import static eu.dissco.core.translator.schema.Identifier.DctermsType.HANDLE; +import static eu.dissco.core.translator.schema.Identifier.DctermsType.PURL; +import static eu.dissco.core.translator.schema.Identifier.DctermsType.URL; +import static eu.dissco.core.translator.schema.Identifier.DctermsType.UUID; import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE; import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE; import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT; From 883a7322ae87317fa0ecf2f72cf6bcb7ec7d0b32 Mon Sep 17 00:00:00 2001 From: Sam Leeflang Date: Wed, 20 Nov 2024 16:06:24 +0100 Subject: [PATCH 4/6] Remove some duplication --- .../terms/specimen/location/AbstractMeterTerm.java | 3 +++ .../specimen/location/MaximumDepthInMeters.java | 12 ++---------- .../MaximumDistanceAboveSurfaceInMeters.java | 12 ++---------- .../specimen/location/MaximumElevationInMeters.java | 12 ++---------- .../specimen/location/MinimumDepthInMeters.java | 12 ++---------- .../MinimumDistanceAboveSurfaceInMeters.java | 12 ++---------- .../specimen/location/MinimumElevationInMeters.java | 12 ++---------- 7 files changed, 15 insertions(+), 60 deletions(-) diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java index f854767..fb76ed3 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java @@ -11,6 +11,9 @@ public abstract class AbstractMeterTerm extends Term { private static final Pattern M_PATTERN = Pattern.compile("((-\\s?)?\\d+([.,]\\d+)?)\\s*m\\.?(eter)?(tr)?(\\sm.?)?"); protected String sanitizeInput(String input) { + if (input == null) { + return null; + } input = input.trim().toLowerCase(); Matcher matcher = M_PATTERN.matcher(input); if (matcher.matches()) { diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java index ebe0613..c937460 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMeters.java @@ -15,21 +15,13 @@ public class MaximumDepthInMeters extends AbstractMeterTerm { @Override public String retrieveFromDWCA(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, dwcaTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override public String retrieveFromABCD(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, abcdTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java index 1390619..b9f2275 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumDistanceAboveSurfaceInMeters.java @@ -14,21 +14,13 @@ public class MaximumDistanceAboveSurfaceInMeters extends AbstractMeterTerm { @Override public String retrieveFromDWCA(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, dwcaTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override public String retrieveFromABCD(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, abcdTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java index 919555e..1999609 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MaximumElevationInMeters.java @@ -15,21 +15,13 @@ public class MaximumElevationInMeters extends AbstractMeterTerm { @Override public String retrieveFromDWCA(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, dwcaTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override public String retrieveFromABCD(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, abcdTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java index 6b3a651..b0c220f 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDepthInMeters.java @@ -15,21 +15,13 @@ public class MinimumDepthInMeters extends AbstractMeterTerm { @Override public String retrieveFromDWCA(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, dwcaTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override public String retrieveFromABCD(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, abcdTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java index c46ef7e..17f4315 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumDistanceAboveSurfaceInMeters.java @@ -14,21 +14,13 @@ public class MinimumDistanceAboveSurfaceInMeters extends AbstractMeterTerm { @Override public String retrieveFromDWCA(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, dwcaTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override public String retrieveFromABCD(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, abcdTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java index 7f46e7e..15ae95c 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/MinimumElevationInMeters.java @@ -15,21 +15,13 @@ public class MinimumElevationInMeters extends AbstractMeterTerm { @Override public String retrieveFromDWCA(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, dwcaTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override public String retrieveFromABCD(JsonNode unit) { var rawResult = super.searchJsonForTerm(unit, abcdTerms); - if (rawResult != null) { - return sanitizeInput(rawResult); - } else { - return null; - } + return sanitizeInput(rawResult); } @Override From d0e2490e3e81f0db9f148f0365eed4dcd920f1ca Mon Sep 17 00:00:00 2001 From: Sam Leeflang Date: Wed, 20 Nov 2024 16:13:15 +0100 Subject: [PATCH 5/6] Add test --- .../location/MaximumDepthInMetersTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java index 5b760b5..6bbed1d 100644 --- a/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java +++ b/src/test/java/eu/dissco/core/translator/terms/specimen/location/MaximumDepthInMetersTest.java @@ -41,6 +41,20 @@ void testRetrieveFromDWCANull() { // Then assertThat(result).isNull(); } + + @Test + void testRetrieveFromDWCAUnSanitizable() { + // Given + var unit = MAPPER.createObjectNode(); + unit.put("dwc:maximumDepthInMeters", "235 ft"); + + // When + var result = maximumDepthInMeters.retrieveFromDWCA(unit); + + // Then + assertThat(result).isEqualTo("235 ft"); + } + @ParameterizedTest @ValueSource(strings = {"-350m.", "-350meter", "-350 m", "-350 MTR"}) void testRetrieveFromABCD(String input) { From a6b0820a3dbd853446c8eee1e7f554cc16b6a3ab Mon Sep 17 00:00:00 2001 From: Sam Leeflang Date: Wed, 20 Nov 2024 16:27:44 +0100 Subject: [PATCH 6/6] Add additional cases to regEx --- .../translator/terms/specimen/location/AbstractMeterTerm.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java b/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java index fb76ed3..8c60b18 100644 --- a/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java +++ b/src/main/java/eu/dissco/core/translator/terms/specimen/location/AbstractMeterTerm.java @@ -8,7 +8,7 @@ @Slf4j public abstract class AbstractMeterTerm extends Term { - private static final Pattern M_PATTERN = Pattern.compile("((-\\s?)?\\d+([.,]\\d+)?)\\s*m\\.?(eter)?(tr)?(\\sm.?)?"); + private static final Pattern M_PATTERN = Pattern.compile("((-\\s?)?\\d+([.,]\\d+)?)\\s*m\\.?(eter)?(tr?.?)?(\\sm.?)?"); protected String sanitizeInput(String input) { if (input == null) {