Skip to content

Commit

Permalink
Value or Marker parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
smithkm committed Jan 10, 2024
1 parent 2b0c4c0 commit 07f2f29
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<StreamingParserFactory<Map<LayerType, FipLayer>>, String> {
public class FipLayerParser
implements ControlMapValueReplacer<StreamingParserFactory<Map<LayerType, FipLayer>>, String> {

public static final String CONTROL_KEY = "FIP_LAYERS";

Expand Down Expand Up @@ -103,8 +104,8 @@ protected ValueOrMarker<Optional<FipLayer>, EndOfRecord> convert(Map<String, Obj
return builder.value(
Optional.of(
new FipLayer(
polygonId, LayerType.VETERAN, ageTotal, height, siteIndex, crownClosure,
siteSp0.get(), siteSp64.get(), yearsToBreastHeight,
polygonId, LayerType.VETERAN, ageTotal, height, siteIndex,
crownClosure, siteSp0.get(), siteSp64.get(), yearsToBreastHeight,
inventoryTypeGroup, breastHeightAge
)
)
Expand Down Expand Up @@ -141,7 +142,8 @@ protected boolean stop(ValueOrMarker<Optional<FipLayer>, EndOfRecord> nextChild)
}

@Override
protected Map<LayerType, FipLayer> convert(List<ValueOrMarker<Optional<FipLayer>, EndOfRecord>> children) {
protected Map<LayerType, FipLayer>
convert(List<ValueOrMarker<Optional<FipLayer>, EndOfRecord>> children) {
return children.stream().map(ValueOrMarker::getValue).map(Optional::get) // Should never be empty as
// we've filtered out
// markers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,9 +42,9 @@ public class FipLayer {
Map<String, FipSpecies> species = Collections.emptyMap();

public FipLayer(
String polygonIdentifier, LayerType layer, float ageTotal, float height, float siteIndex, float crownClosure,
String siteGenus, String siteSpecies, float yearsToBreastHeight, Optional<Integer> inventoryTypeGroup,
Optional<Float> breastHeightAge
String polygonIdentifier, LayerType layer, float ageTotal, float height, float siteIndex,
float crownClosure, String siteGenus, String siteSpecies, float yearsToBreastHeight,
Optional<Integer> inventoryTypeGroup, Optional<Float> breastHeightAge
) {
super();
this.polygonIdentifier = polygonIdentifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -26,7 +26,8 @@ public class FipSpecies {
Map<String, Float> speciesPercent; // Map from

public FipSpecies(
String polygonIdentifier, LayerType layer, String genus, float percentGenus, Map<String, Float> speciesPercent
String polygonIdentifier, LayerType layer, String genus, float percentGenus,
Map<String, Float> speciesPercent
) {
super();
this.polygonIdentifier = polygonIdentifier;
Expand Down

0 comments on commit 07f2f29

Please sign in to comment.