|
| 1 | +package ca.bc.gov.nrs.vdyp.io.parse.model; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.*; |
| 5 | +import static org.junit.jupiter.api.Assertions.*; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.NoSuchElementException; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import ca.bc.gov.nrs.vdyp.io.parse.common.ResourceParseException; |
| 14 | +import ca.bc.gov.nrs.vdyp.model.LayerType; |
| 15 | +import ca.bc.gov.nrs.vdyp.model.UtilizationClass; |
| 16 | +import ca.bc.gov.nrs.vdyp.test.MockFileResolver; |
| 17 | +import ca.bc.gov.nrs.vdyp.test.TestUtils; |
| 18 | +import ca.bc.gov.nrs.vdyp.test.VdypMatchers; |
| 19 | + |
| 20 | +import static ca.bc.gov.nrs.vdyp.test.VdypMatchers.*; |
| 21 | + |
| 22 | +class VdypSpeciesParserTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + void testEmpty() throws IOException, ResourceParseException { |
| 26 | + var controlMap = TestUtils.loadControlMap(); |
| 27 | + var resolver = new MockFileResolver("testResolver"); |
| 28 | + |
| 29 | + try (var is = TestUtils.makeInputStream("")) { |
| 30 | + |
| 31 | + resolver.addStream("test.dat", is); |
| 32 | + |
| 33 | + var parser = new VdypSpeciesParser().map("test.dat", resolver, controlMap); |
| 34 | + |
| 35 | + var stream = parser.get(); |
| 36 | + |
| 37 | + assertTrue(!stream.hasNext(), "stream is not empty"); |
| 38 | + |
| 39 | + assertThrows(NoSuchElementException.class, () -> stream.next()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testOnePoly() throws IOException, ResourceParseException { |
| 45 | + var controlMap = TestUtils.loadControlMap(); |
| 46 | + var resolver = new MockFileResolver("testResolver"); |
| 47 | + |
| 48 | + try ( |
| 49 | + var is = TestUtils.makeInputStream( |
| 50 | + "01002 S000001 00 1970 P 3 B B 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 51 | + "01002 S000001 00 1970 P 4 C C 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 52 | + "01002 S000001 00 1970 P 5 D D 100.0 0.0 0.0 0.0 35.00 35.30 55.0 54.0 1.0 1 13", |
| 53 | + "01002 S000001 00 1970 P 8 H H 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 54 | + "01002 S000001 00 1970 P 15 S S 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 55 | + "01002 S000001 00 1970 " |
| 56 | + ) |
| 57 | + ) { |
| 58 | + |
| 59 | + resolver.addStream("test.dat", is); |
| 60 | + |
| 61 | + var parser = new VdypSpeciesParser().map("test.dat", resolver, controlMap); |
| 62 | + |
| 63 | + var stream = parser.get(); |
| 64 | + |
| 65 | + assertTrue(stream.hasNext(), "stream is empty"); |
| 66 | + |
| 67 | + var result = new ArrayList<>(stream.next()); // Array list makes it indexable so we can easily sample |
| 68 | + // entries to test. Checking everything would be excessive. |
| 69 | + |
| 70 | + assertThat(result, hasSize(5)); |
| 71 | + |
| 72 | + assertThat( |
| 73 | + result.get(0), |
| 74 | + allOf( |
| 75 | + hasProperty("polygonIdentifier", VdypMatchers.isPolyId("01002 S000001 00", 1970)), |
| 76 | + hasProperty("layerType", is(LayerType.PRIMARY)), hasProperty("genus", is("B")) |
| 77 | + ) |
| 78 | + ); |
| 79 | + |
| 80 | + assertTrue(!stream.hasNext(), "stream is not empty"); |
| 81 | + |
| 82 | + assertThrows(NoSuchElementException.class, () -> stream.next()); |
| 83 | + |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testMultiplePoly() throws IOException, ResourceParseException { |
| 89 | + var controlMap = TestUtils.loadControlMap(); |
| 90 | + var resolver = new MockFileResolver("testResolver"); |
| 91 | + |
| 92 | + try ( |
| 93 | + var is = TestUtils.makeInputStream( |
| 94 | + "01002 S000001 00 1970 P 3 B B 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 95 | + "01002 S000001 00 1970 P 4 C C 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 96 | + "01002 S000001 00 1970 P 5 D D 100.0 0.0 0.0 0.0 35.00 35.30 55.0 54.0 1.0 1 13", |
| 97 | + "01002 S000001 00 1970 P 8 H H 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 98 | + "01002 S000001 00 1970 P 15 S S 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 99 | + "01002 S000001 00 1970 ", |
| 100 | + "01002 S000002 00 1970 P 3 B B 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 101 | + "01002 S000002 00 1970 P 5 D D 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 102 | + "01002 S000002 00 1970 P 8 H H 100.0 0.0 0.0 0.0 28.70 24.30 45.0 39.6 5.4 1 34", |
| 103 | + "01002 S000002 00 1970 P 15 S S 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 104 | + "01002 S000002 00 1970 V 3 B B 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 105 | + "01002 S000002 00 1970 V 8 H H 100.0 0.0 0.0 0.0 16.70 26.20 105.0 97.9 7.1 1 -9", |
| 106 | + "01002 S000002 00 1970 V 15 S S 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9", |
| 107 | + "01002 S000002 00 1970 " |
| 108 | + |
| 109 | + ) |
| 110 | + ) { |
| 111 | + |
| 112 | + resolver.addStream("test.dat", is); |
| 113 | + |
| 114 | + var parser = new VdypSpeciesParser().map("test.dat", resolver, controlMap); |
| 115 | + |
| 116 | + var stream = parser.get(); |
| 117 | + |
| 118 | + assertTrue(stream.hasNext(), "stream is empty"); |
| 119 | + |
| 120 | + var result = new ArrayList<>(stream.next()); // Array list makes it indexable so we can easily sample |
| 121 | + // entries to test. Checking everything would be excessive. |
| 122 | + |
| 123 | + assertThat(result, hasSize(5)); |
| 124 | + |
| 125 | + assertThat( |
| 126 | + result.get(0), |
| 127 | + allOf( |
| 128 | + hasProperty("polygonIdentifier", VdypMatchers.isPolyId("01002 S000001 00", 1970)), |
| 129 | + hasProperty("layerType", is(LayerType.PRIMARY)), hasProperty("genus", is("B")) |
| 130 | + ) |
| 131 | + ); |
| 132 | + assertThat( |
| 133 | + result.get(4), |
| 134 | + allOf( |
| 135 | + hasProperty("polygonIdentifier", VdypMatchers.isPolyId("01002 S000001 00", 1970)), |
| 136 | + hasProperty("layerType", is(LayerType.PRIMARY)), hasProperty("genus", is("S")) |
| 137 | + ) |
| 138 | + ); |
| 139 | + assertTrue(stream.hasNext(), "stream is empty"); |
| 140 | + |
| 141 | + result = new ArrayList<>(stream.next()); // Array list makes it indexable so we can easily sample |
| 142 | + // entries to test. Checking everything would be excessive. |
| 143 | + |
| 144 | + assertThat(result, hasSize(7)); |
| 145 | + |
| 146 | + assertThat( |
| 147 | + result.get(0), |
| 148 | + allOf( |
| 149 | + hasProperty("polygonIdentifier", VdypMatchers.isPolyId("01002 S000002 00", 1970)), |
| 150 | + hasProperty("layerType", is(LayerType.PRIMARY)), hasProperty("genus", is("B")) |
| 151 | + ) |
| 152 | + ); |
| 153 | + assertThat( |
| 154 | + result.get(3), |
| 155 | + allOf( |
| 156 | + hasProperty("polygonIdentifier", VdypMatchers.isPolyId("01002 S000002 00", 1970)), |
| 157 | + hasProperty("layerType", is(LayerType.PRIMARY)), hasProperty("genus", is("S")) |
| 158 | + ) |
| 159 | + ); |
| 160 | + assertThat( |
| 161 | + result.get(4), |
| 162 | + allOf( |
| 163 | + hasProperty("polygonIdentifier", VdypMatchers.isPolyId("01002 S000002 00", 1970)), |
| 164 | + hasProperty("layerType", is(LayerType.VETERAN)), hasProperty("genus", is("B")) |
| 165 | + ) |
| 166 | + ); |
| 167 | + assertThat( |
| 168 | + result.get(6), |
| 169 | + allOf( |
| 170 | + hasProperty("polygonIdentifier", VdypMatchers.isPolyId("01002 S000002 00", 1970)), |
| 171 | + hasProperty("layerType", is(LayerType.VETERAN)), hasProperty("genus", is("S")) |
| 172 | + ) |
| 173 | + ); |
| 174 | + |
| 175 | + assertTrue(!stream.hasNext(), "stream is not empty"); |
| 176 | + |
| 177 | + assertThrows(NoSuchElementException.class, () -> stream.next()); |
| 178 | + |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments