Skip to content
Snippets Groups Projects

NSHM Unit tests

1 file
+ 3
0
Compare changes
  • Side-by-side
  • Inline
@@ -13,9 +13,11 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.JsonNode;
@@ -40,6 +42,8 @@ import io.swagger.v3.core.util.Yaml;
* Utilities to run tests on a NSHM.
*/
class NshmTestUtils {
private static Logger LOGGER = Logger.getLogger(NshmTestUtils.class.getName());
private static final Path DATA_PATH = Paths.get("src/test/resources/e2e");
private static final double TOLERANCE = 1e-12;
@@ -78,11 +82,12 @@ class NshmTestUtils {
*
* @param nshm The NSHM to test
*/
static void testNshm(Nshm nshm) {
static void testNshm(Nshm nshm, Optional<Path> dataPath) {
NshmModel nshmModel = loadModel(nshm);
for (NamedLocation location : nshm.locations()) {
compareCurves(nshmModel, location);
LOGGER.info("Location: " + location.toString());
compareCurves(nshmModel, location, dataPath);
}
nshmModel.exec.shutdown();
@@ -93,11 +98,11 @@ class NshmTestUtils {
*
* @param nshmModel The NSHM model
*/
static void writeExpecteds(NshmModel nshmModel) throws IOException {
static void writeExpecteds(NshmModel nshmModel, Optional<Path> dataPath) throws IOException {
for (NamedLocation location : nshmModel.nshm.locations()) {
Map<String, XySequence> xyMap = generateActual(nshmModel, location);
String json = GSON.toJson(xyMap);
writeExpected(nshmModel.nshm, location, json);
writeExpected(nshmModel.nshm, location, json, dataPath);
}
}
@@ -128,9 +133,11 @@ class NshmTestUtils {
Double.valueOf(expected).equals(Double.valueOf(actual));
}
private static void compareCurves(NshmModel nshmModel, NamedLocation location) {
Map<String, XySequence> actual = generateActual(nshmModel, location);
Map<String, XySequence> expected = readExpected(nshmModel, location);
private static void compareCurves(NshmModel nshmModel, NamedLocation location,
Optional<Path> dataPath) {
Map<String, XySequence> actual = dataPath.isPresent()
? readExpected(nshmModel, location, dataPath) : generateActual(nshmModel, location);
Map<String, XySequence> expected = readExpected(nshmModel, location, Optional.empty());
for (String key : actual.keySet()) {
assertCurveEquals(expected.get(key), actual.get(key), TOLERANCE);
@@ -163,8 +170,9 @@ class NshmTestUtils {
return xyMap;
}
private static Map<String, XySequence> readExpected(NshmModel nshmModel, NamedLocation loc) {
Path resultPath = DATA_PATH
private static Map<String, XySequence> readExpected(NshmModel nshmModel, NamedLocation loc,
Optional<Path> dataPath) {
Path resultPath = dataPath.orElse(DATA_PATH)
.resolve(nshmModel.nshm.modelName())
.resolve(nshmModel.nshm.resultFilename(loc));
@@ -187,8 +195,9 @@ class NshmTestUtils {
private static void writeExpected(
Nshm nshm,
NamedLocation loc,
String json) throws IOException {
Path modelDir = DATA_PATH.resolve(nshm.modelName());
String json,
Optional<Path> dataPath) throws IOException {
Path modelDir = dataPath.orElse(DATA_PATH).resolve(nshm.modelName());
if (!Files.exists(modelDir)) {
Files.createDirectories(modelDir);
}
Loading