From 07f2f2945c1fb154cb797868e8ba55ef386380aa Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Wed, 10 Jan 2024 12:18:02 -0800 Subject: [PATCH] Value or Marker parser test --- .../ca/bc/gov/nrs/vdyp/model/LayerType.java | 6 +-- .../nrs/vdyp/io/parse/ValueParserTest.java | 45 ++++++++++++++----- .../bc/gov/nrs/vdyp/fip/FipLayerParser.java | 10 +++-- .../bc/gov/nrs/vdyp/fip/model/FipLayer.java | 10 ++--- .../bc/gov/nrs/vdyp/fip/model/FipSpecies.java | 9 ++-- 5 files changed, 54 insertions(+), 26 deletions(-) diff --git a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/LayerType.java b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/LayerType.java index 2a61eff50..8d813da1e 100644 --- a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/LayerType.java +++ b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/LayerType.java @@ -4,13 +4,13 @@ public enum LayerType { /** * The primary layer */ - PRIMARY, + PRIMARY, /** * The parser is aware of this but it is never implemented */ SECONDARY, - /** - * An older layer than the primary layer, also called the "overstory" + /** + * An older layer than the primary layer, also called the "overstory" */ VETERAN } diff --git a/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/ValueParserTest.java b/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/ValueParserTest.java index c098147ef..973174bcc 100644 --- a/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/ValueParserTest.java +++ b/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/ValueParserTest.java @@ -1,12 +1,18 @@ package ca.bc.gov.nrs.vdyp.io.parse; +import static ca.bc.gov.nrs.vdyp.test.VdypMatchers.notPresent; +import static ca.bc.gov.nrs.vdyp.test.VdypMatchers.present; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasProperty; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.Optional; + import org.junit.jupiter.api.Test; +import ca.bc.gov.nrs.vdyp.test.VdypMatchers; + class ValueParserTest { @Test @@ -54,24 +60,43 @@ void rangeParserTest() throws Exception { } static enum TestEnum { - VALUE1, - VALUE2 + VALUE1, VALUE2 } - + @Test void enumParserTest() throws Exception { var parser = ValueParser.enumParser(TestEnum.class); - + assertThat(parser.parse("VALUE1"), is(TestEnum.VALUE1)); assertThat(parser.parse("VALUE2"), is(TestEnum.VALUE2)); - - var ex = assertThrows(ValueParseException.class, ()->parser.parse("FAKE")); + + var ex = assertThrows(ValueParseException.class, () -> parser.parse("FAKE")); assertThat(ex.getMessage(), is("\"FAKE\" is not a valid TestEnum")); - - ex = assertThrows(ValueParseException.class, ()->parser.parse("")); + + ex = assertThrows(ValueParseException.class, () -> parser.parse("")); assertThat(ex.getMessage(), is("\"\" is not a valid TestEnum")); - - ex = assertThrows(ValueParseException.class, ()->parser.parse(" ")); + + ex = assertThrows(ValueParseException.class, () -> parser.parse(" ")); assertThat(ex.getMessage(), is("\"\" is not a valid TestEnum")); } + + @Test + void testValueOrMarkerParser() throws Exception { + var parser = ValueParser.valueOrMarker(ValueParser.INTEGER, (s) -> { + if ("MARK".equals(s)) { + return Optional.of("MARK"); + } + return Optional.empty(); + }); + + assertThat(parser.parse("MARK").getMarker(), present(is("MARK"))); + assertThat(parser.parse("MARK").getValue(), notPresent()); + + assertThat(parser.parse("1").getValue(), present(is(1))); + assertThat(parser.parse("1").getMarker(), notPresent()); + + var ex = assertThrows(ValueParseException.class, () -> parser.parse("X")); + assertThat(ex.getMessage(), is("\"X\" is not a valid Integer")); + } + } diff --git a/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/FipLayerParser.java b/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/FipLayerParser.java index cd0a04e2b..8615f384f 100644 --- a/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/FipLayerParser.java +++ b/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/FipLayerParser.java @@ -21,7 +21,8 @@ import ca.bc.gov.nrs.vdyp.io.parse.ValueParser; import ca.bc.gov.nrs.vdyp.model.LayerType; -public class FipLayerParser implements ControlMapValueReplacer>, String> { +public class FipLayerParser + implements ControlMapValueReplacer>, String> { public static final String CONTROL_KEY = "FIP_LAYERS"; @@ -103,8 +104,8 @@ protected ValueOrMarker, EndOfRecord> convert(Map, EndOfRecord> nextChild) } @Override - protected Map convert(List, EndOfRecord>> children) { + protected Map + convert(List, EndOfRecord>> children) { return children.stream().map(ValueOrMarker::getValue).map(Optional::get) // Should never be empty as // we've filtered out // markers diff --git a/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/model/FipLayer.java b/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/model/FipLayer.java index afaafc41c..fbbe81b00 100644 --- a/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/model/FipLayer.java +++ b/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/model/FipLayer.java @@ -22,8 +22,8 @@ public class FipLayer { String polygonIdentifier; // FIP_P/POLYDESC final LayerType layer; // This is also represents the distinction between data stored in FIPL_1(A) and - // FIP_V(A). Where VDYP7 stores both and looks at certain values to determine if - // a layer is "present". VDYP8 stores them in a map keyed by this value + // FIP_V(A). Where VDYP7 stores both and looks at certain values to determine if + // a layer is "present". VDYP8 stores them in a map keyed by this value float ageTotal; // FIPL_1/AGETOT_L1 or FIPL_V/AGETOT_V1 float height; // FIPL_1/HT_L1 or FIPL_V/HT_V1 float siteIndex; // FIPL_1/SI_L1 or FIPL_V/SI_V1 @@ -42,9 +42,9 @@ public class FipLayer { Map species = Collections.emptyMap(); public FipLayer( - String polygonIdentifier, LayerType layer, float ageTotal, float height, float siteIndex, float crownClosure, - String siteGenus, String siteSpecies, float yearsToBreastHeight, Optional inventoryTypeGroup, - Optional breastHeightAge + String polygonIdentifier, LayerType layer, float ageTotal, float height, float siteIndex, + float crownClosure, String siteGenus, String siteSpecies, float yearsToBreastHeight, + Optional inventoryTypeGroup, Optional breastHeightAge ) { super(); this.polygonIdentifier = polygonIdentifier; diff --git a/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/model/FipSpecies.java b/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/model/FipSpecies.java index f2a59dfe9..9aedca917 100644 --- a/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/model/FipSpecies.java +++ b/vdyp-fip/src/main/java/ca/bc/gov/nrs/vdyp/fip/model/FipSpecies.java @@ -11,9 +11,9 @@ public class FipSpecies { final String polygonIdentifier; // FIP_P/POLYDESC final LayerType layer; // This is also represents the distinction between data stored in - // FIPL_1(A) and FIP_V(A). Where VDYP7 stores both and looks at certain values - // to determine if a layer is "present". VDYP8 stores them in a map keyed by - // this value + // FIPL_1(A) and FIP_V(A). Where VDYP7 stores both and looks at certain values + // to determine if a layer is "present". VDYP8 stores them in a map keyed by + // this value final String genus; // FIPSA/SP0V @@ -26,7 +26,8 @@ public class FipSpecies { Map speciesPercent; // Map from public FipSpecies( - String polygonIdentifier, LayerType layer, String genus, float percentGenus, Map speciesPercent + String polygonIdentifier, LayerType layer, String genus, float percentGenus, + Map speciesPercent ) { super(); this.polygonIdentifier = polygonIdentifier;