Skip to content

Commit

Permalink
vdyp-common unit tests + code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mjunkin committed Feb 28, 2025
1 parent 1c6162d commit 089a2ed
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private void close(OutputStream os) {
try {
os.close();
} catch (IOException e) {
logger.error("Failed to close OutputStream", e);
throw new RuntimeException("Failed to close a given output stream");
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions lib/vdyp-common/src/main/java/ca/bc/gov/nrs/vdyp/common/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ca.bc.gov.nrs.vdyp.common;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -19,6 +21,8 @@
import javax.annotation.Nullable;

import org.apache.commons.math3.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.bc.gov.nrs.vdyp.model.BecDefinition;
import ca.bc.gov.nrs.vdyp.model.BecLookup;
Expand All @@ -34,6 +38,8 @@

public class Utils {

private static final Logger logger = LoggerFactory.getLogger(Utils.class);

private Utils() {
}

Expand Down Expand Up @@ -184,6 +190,16 @@ public static <T> Comparator<T> compareWithFallback(Comparator<T>... comparators
};
}

public static void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
logger.error("Failed to close OutputStream", e);
}
}
}

/**
* Compares two Optionals of comparables of the same type, treating "empty" as equal to "empty" and before
* "present".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public class VdypOutputWriter implements Closeable {
protected final OutputStream polygonFile;
protected final OutputStream speciesFile;
protected final OutputStream utilizationFile;
@SuppressWarnings("unused")
private Optional<OutputStream> compatibilityVariablesFile;
protected Optional<OutputStream> compatibilityVariablesFile;

private ResolvedControlMap controlMap;

Expand Down Expand Up @@ -272,8 +271,9 @@ static void calculateCuVolumeLessDecayWastageBreakage(
}
}

private static OutputStream getOutputStream(Map<String, Object> controlMap, ConcreteFileResolver resolver, String key)
throws IOException {
private static OutputStream
getOutputStream(Map<String, Object> controlMap, ConcreteFileResolver resolver, String key)
throws IOException {
String fileName = Utils.expectParsedControl(controlMap, key, String.class);
return resolver.resolveForOutput(fileName);
}
Expand Down Expand Up @@ -468,8 +468,9 @@ void writeFormat(OutputStream os, String format, Object... params) throws IOExce

@Override
public void close() throws IOException {
polygonFile.close();
speciesFile.close();
utilizationFile.close();
Utils.close(polygonFile);
Utils.close(speciesFile);
Utils.close(utilizationFile);
compatibilityVariablesFile.ifPresent(x -> Utils.close(x));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ void testInitOneControlFile() throws IOException, ResourceParseException {
app.close();
}
}

@Test
void testDebugGetSet() throws IOException {
try (var app = new TestStartApplication(controlMap, true)) {

for (int i = 0; i < 25; i++) {
app.setDebugMode(0, i + 1);
assertThat(app.getDebugMode(0), equalTo(i + 1));
}

assertThrows(ArrayIndexOutOfBoundsException.class, () -> app.setDebugMode(-1, 1));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> app.setDebugMode(25, 1));
}
}

@Test
void testApplicationExceptions() throws IOException, ResourceParseException {
try (var app = new TestStartApplication(controlMap, true)) {

assertThrows(VdypApplicationInitializationException.class, () -> TestStartApplication.doMain(app));
assertThrows(VdypApplicationInitializationException.class, () -> TestStartApplication.doMain(app, "bad path"));
}
}

private MockFileResolver dummyIo() {
controlMap.put(ControlKey.VDYP_OUTPUT_VDYP_POLYGON.name(), "DUMMY1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class FileSystemFileResolverTest {
void testWorkingDirGetPath() throws Exception {
var unit = new FileSystemFileResolver();

assertThat(unit.getInputFileResolver(), equalTo(unit));
assertThat(unit.getOutputFileResolver(), equalTo(unit));
assertThat(unit.toString(), equalTo("FileSystemFileResolver (absolute)"));

var result = unit.toPath("test");

assertThat(result.toAbsolutePath().toString(), equalTo(System.getProperty("user.dir") + "/test"));
Expand All @@ -29,6 +33,8 @@ void testSpecifiedDirGetPath() throws Exception {

var result = unit.toPath("test");

assertThat(unit.toString(), equalTo("FileSystemFileResolver (relative [" + result.getParent().toAbsolutePath() + "])"));

assertThat(result.toAbsolutePath().toString(), equalTo(testDir.resolve("test").toString()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void testZipOutputFileResolver() throws IOException {

ZipOutputFileResolver resolver = new ZipOutputFileResolver();

assertThat(resolver.toString(), equalTo("ZipOutputFileResolver"));

assertThat(resolver.getOutputFileResolver(), equalTo(resolver));
assertThrows(UnsupportedOperationException.class, () -> resolver.getInputFileResolver());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class VdypOutputWriterTest {
MockOutputStream polyStream;
MockOutputStream specStream;
MockOutputStream utilStream;
MockOutputStream cvarsStream;

MockFileResolver fileResolver;

Expand All @@ -44,37 +45,43 @@ class VdypOutputWriterTest {
@BeforeEach
void initStreams() {
controlMap = new HashMap<String, Object>();

TestUtils.populateControlMapBecReal(controlMap);
TestUtils.populateControlMapGenusReal(controlMap);

polyStream = new TestUtils.MockOutputStream("polygons");
specStream = new TestUtils.MockOutputStream("species");
utilStream = new TestUtils.MockOutputStream("utilization");
cvarsStream = new TestUtils.MockOutputStream("cvars");

controlMap.put(ControlKey.VDYP_OUTPUT_VDYP_POLYGON.name(), "testPolygonFile");
controlMap.put(ControlKey.VDYP_OUTPUT_VDYP_LAYER_BY_SPECIES.name(), "testSpeciesFile");
controlMap.put(ControlKey.VDYP_OUTPUT_VDYP_LAYER_BY_SP0_BY_UTIL.name(), "testUtilizationFile");
controlMap.put(ControlKey.VDYP_OUTPUT_COMPATIBILITY_VARIABLES.name(), "testCompatibilityVariables");

fileResolver = new MockFileResolver("TEST");
fileResolver.addStream("testPolygonFile", polyStream);
fileResolver.addStream("testSpeciesFile", specStream);
fileResolver.addStream("testUtilizationFile", utilStream);
fileResolver.addStream("testCompatibilityVariables", cvarsStream);
}

@Test
void testClosesGivenStreams() throws IOException {

var unit = new VdypOutputWriter(controlMap, polyStream, specStream, utilStream);
var unit = new VdypOutputWriter(controlMap, polyStream, specStream, utilStream, Optional.of(cvarsStream));

unit.close();

polyStream.assertClosed();
specStream.assertClosed();
utilStream.assertClosed();
cvarsStream.assertClosed();

polyStream.assertContent(emptyString());
specStream.assertContent(emptyString());
utilStream.assertContent(emptyString());
cvarsStream.assertContent(emptyString());
}

@Test
Expand All @@ -87,10 +94,12 @@ void testClosesOpenedStreams() throws IOException {
polyStream.assertClosed();
specStream.assertClosed();
utilStream.assertClosed();
cvarsStream.assertClosed();

polyStream.assertContent(emptyString());
specStream.assertContent(emptyString());
utilStream.assertContent(emptyString());
cvarsStream.assertContent(emptyString());
}

@Test
Expand All @@ -101,6 +110,7 @@ void testCreateFromControlMap() throws IOException {
controlMap.put(ControlKey.VDYP_OUTPUT_VDYP_POLYGON.name(), fileResolver.toPath("testPolygonFile"));
controlMap.put(ControlKey.VDYP_OUTPUT_VDYP_LAYER_BY_SPECIES.name(), fileResolver.toPath("testSpeciesFile"));
controlMap.put(ControlKey.VDYP_OUTPUT_VDYP_LAYER_BY_SP0_BY_UTIL.name(), fileResolver.toPath("testUtilizationFile"));
controlMap.put(ControlKey.VDYP_OUTPUT_COMPATIBILITY_VARIABLES.name(), fileResolver.toPath("testCompatibilityVariables"));

try (var unit = new VdypOutputWriter(controlMap)) {
var polygon = buildTestPolygonAndChildren();
Expand All @@ -119,6 +129,10 @@ void testCreateFromControlMap() throws IOException {
try (var utilizationReader = Files.newBufferedReader(fileResolver.toPath("testUtilizationFile"))) {
assertTrue(utilizationReader.read() >= 0);
}

try (var cVarsReader = Files.newBufferedReader(fileResolver.toPath("testCompatibilityVariables"))) {
assertTrue(cVarsReader.read() == -1);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
package ca.bc.gov.nrs.vdyp.math;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import org.junit.jupiter.api.Test;

public class VdypMathTest {

@Test
void testClampInteger() {
assertThat(VdypMath.clamp(5, 0, 10), equalTo(5));
assertThat(VdypMath.clamp(-5, 0, 10), equalTo(0));
assertThat(VdypMath.clamp(15, 0, 10), equalTo(10));
}

@Test
void testClampDouble() {
assertThat(VdypMath.clamp(5.1, 0.5, 10.5), equalTo(5.1));
assertThat(VdypMath.clamp(-5, 0.5, 10.5), equalTo(0.5));
assertThat(VdypMath.clamp(15.3, 0.5, 10.5), equalTo(10.5));
}

@Test
void testMax3() {
assertThat(VdypMath.max(5.1, 0.5, 10.5), equalTo(10.5));
assertThat(VdypMath.max(15.1, 0.5, 10.5), equalTo(15.1));
assertThat(VdypMath.max(5, 0, 10), equalTo(10));
assertThat(VdypMath.max(15, 0, 10), equalTo(15));
}

@Test
void testMax4() {
assertThat(VdypMath.max(5.1, 0.5, 10.5, 15.5), equalTo(15.5));
assertThat(VdypMath.max(15.1, 10.5, 0.5, 3.2), equalTo(15.5));
assertThat(VdypMath.max(3, 5, 0, 10), equalTo(0));
assertThat(VdypMath.max(15.1, 10.5, 0.5, 3.2), equalTo(15.1));
assertThat(VdypMath.max(3, 5, 0, 10), equalTo(10));
assertThat(VdypMath.max(20, 15, 10, 0), equalTo(20));
}
}

0 comments on commit 089a2ed

Please sign in to comment.