diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/fault/FaultSectionsTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/fault/FaultSectionsTests.java deleted file mode 100644 index 970551bc245480b477d889cd01d2199498cdeb53..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/fault/FaultSectionsTests.java +++ /dev/null @@ -1,67 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.fault; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.Test; - -import gov.usgs.earthquake.nshmp.model.SourceFeature; - -class FaultSectionsTests { - private static final Map<NshmFaultSection, FaultSections> FAULT_SECTIONS = FaultSections - .getFaultSections(); - - @Test - final void faultSectionLoadTests() { - assertDoesNotThrow(() -> FaultSections.getFaultSections()); - } - - @Test - final void hasNshmFaultSections() { - for (NshmFaultSection nshm : NshmFaultSection.values()) { - assertTrue(FAULT_SECTIONS.keySet().contains(nshm)); - } - } - - @Test - final void conus2018Tests() { - FaultSections conus2018 = FAULT_SECTIONS.get(NshmFaultSection.CONUS_2018); - List<SourceFeature> sourceFeatures = conus2018.features(); - - doestNotContainState(sourceFeatures, "AK"); - doestNotContainState(sourceFeatures, "AS"); - doestNotContainState(sourceFeatures, "GU"); - doestNotContainState(sourceFeatures, "HI"); - doestNotContainState(sourceFeatures, "PR"); - } - - @Test - final void hawaii2021Tests() { - FaultSections hawaii2021 = FAULT_SECTIONS.get(NshmFaultSection.HAWAII_2021); - List<SourceFeature> sourceFeatures = hawaii2021.features(); - - isState(sourceFeatures, "HI"); - } - - private void doestNotContainState(List<SourceFeature> sourceFeatures, String state) { - assertFalse(states(sourceFeatures).contains(state)); - } - - private void isState(List<SourceFeature> sourceFeatures, String state) { - assertEquals(states(sourceFeatures).size(), 1); - assertEquals(Set.of(state), states(sourceFeatures)); - } - - private Set<String> states(List<SourceFeature> sourceFeatures) { - return sourceFeatures.stream() - .map(feature -> feature.state.get()) - .collect(Collectors.toSet()); - } -} diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/fault/NshmFaultSectionTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/fault/NshmFaultSectionTests.java deleted file mode 100644 index 40d432f7417b3e2e3c04646a9b9f4889f3114654..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/fault/NshmFaultSectionTests.java +++ /dev/null @@ -1,33 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.fault; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.net.URISyntaxException; -import java.nio.file.Path; - -import org.junit.jupiter.api.Test; - -import gov.usgs.earthquake.nshmp.www.NshmRegion; -import gov.usgs.earthquake.nshmp.www.Utils; - -class NshmFaultSectionTests { - - @Test - final void conus2018Tests() throws URISyntaxException { - testNshmFaultSection(NshmFaultSection.CONUS_2018, NshmRegion.CONUS); - } - - @Test - final void hawaii2021Tests() throws URISyntaxException { - testNshmFaultSection(NshmFaultSection.HAWAII_2021, NshmRegion.HAWAII); - } - - private void testNshmFaultSection(NshmFaultSection nshm, NshmRegion region) - throws URISyntaxException { - assertEquals(nshm.region(), region); - - Path path = Path.of(Utils.FAULT_SECTIONS_RESOURCES).resolve(nshm.directory()); - assertTrue(path.toFile().isDirectory()); - } -} diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataSetTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataSetTests.java deleted file mode 100644 index 57fddf0464a3fa187cdc7467bd9ab2daafd020ea..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataSetTests.java +++ /dev/null @@ -1,53 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.gps; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.net.URL; - -import org.junit.jupiter.api.Test; - -import com.google.common.io.Resources; - -import gov.usgs.earthquake.nshmp.www.Utils; - -class GpsDataSetTests { - - @Test - public final void fromIdTests() { - for (GpsDataSet gpsDataSet : GpsDataSet.values()) { - assertEquals(gpsDataSet, GpsDataSet.fromId(gpsDataSet.id)); - assertEquals(gpsDataSet, GpsDataSet.fromId(gpsDataSet.toId())); - assertTrue(gpsDataSet.equals(GpsDataSet.fromId(gpsDataSet.id))); - } - } - - @Test - public final void fromIdTestIAE() { - assertThrows(IllegalArgumentException.class, () -> { - GpsDataSet.fromId("bad-id"); - }); - } - - @Test - public final void assertEqualsTests() { - for (GpsDataSet gpsDataSet : GpsDataSet.values()) { - assertEquals(gpsDataSet.id, gpsDataSet.toId()); - } - } - - @Test - public final void resourceFileExistsTest() { - assertThrows(IllegalArgumentException.class, () -> { - Resources.getResource(GpsDataSet.class, Utils.GPS_RESOURCES + "not_a_file"); - }); - - for (GpsDataSet gpsDataSet : GpsDataSet.values()) { - URL dataUrl = - Resources.getResource(GpsDataSet.class, Utils.GPS_RESOURCES + gpsDataSet.filename); - assertTrue(dataUrl != null); - } - } - -} diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataSetsTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataSetsTests.java deleted file mode 100644 index 2c60513351eea681973f3d0c2410ea47f4e5b1fc..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataSetsTests.java +++ /dev/null @@ -1,184 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.gps; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; - -import com.google.common.io.Resources; - -import gov.usgs.earthquake.nshmp.geo.Location; -import gov.usgs.earthquake.nshmp.geo.json.Feature; -import gov.usgs.earthquake.nshmp.geo.json.GeoJson; -import gov.usgs.earthquake.nshmp.www.Utils; -import gov.usgs.earthquake.nshmp.www.gps.GpsData.Field; - -class GpsDataSetsTests { - - private static final GpsDataSets dataSets = GpsDataSets.loadGpsDataSets(); - - private static final Map<GpsDataSet, Map<Object, Object>> expected; - - private static List<String> propertiesToCheck = - Arrays.asList("UNITS", "POSITIVE_UP", Field.SITETYPE, "NOT_A_PROPERTY"); - private static List<String> doublesToCheck = - Arrays.asList(Field.VEL_E, Field.SIG_N, Field.VEL_V, Field.CORRELATION); - - static { - Map<GpsDataSet, Map<Object, Object>> expectedMap = new HashMap<>(); - - // AK_2020 - expectedMap.put(GpsDataSet.AK_2020, Map.of( - "count", 321, - "first", Map.of( - "id", "2437", - "coords", Location.create(-135.44, 58.42), - Field.SITETYPE, "Campaign", - Field.VEL_E, -1.6, - Field.SIG_N, 0.7), - "last", Map.of( - "id", "AC42", - "coords", Location.create(-162.78, 54.47), - Field.SITETYPE, "cGPS", - Field.VEL_E, -5.8, - Field.SIG_N, 0.5))); - - // HI_2020 - expectedMap.put(GpsDataSet.HI_2020, Map.of( - "count", 115, - "first", Map.of( - "id", "0", - "coords", Location.create(-155.26610, 19.37911), - Field.VEL_E, 22.8000, - Field.VEL_V, -22.7000, - Field.SIG_N, 0.0800), - "last", Map.of( - "id", "114", - "coords", Location.create(-157.92083, 21.31299), - Field.VEL_E, 0.2000, - Field.VEL_V, -0.3000, - Field.SIG_N, 0.0200))); - - // CONUS_2014_WUS - expectedMap.put(GpsDataSet.CONUS_2014_WUS, Map.of( - "count", 3168, - "first", Map.of( - "id", "0", - "coords", Location.create(-118.14610, 34.75410), - Field.VEL_E, -13.5440, - Field.SIG_N, 0.6900, - Field.CORRELATION, 0.1700), - "last", Map.of( - "id", "3167", - "coords", Location.create(-111.02040, 39.29330), - Field.VEL_E, -0.3720, - Field.SIG_N, 0.3000, - Field.CORRELATION, 0.0310))); - - // CONUS_2014_UCERF - expectedMap.put(GpsDataSet.CONUS_2014_UCERF, Map.of( - "count", 1867, - "first", Map.of( - "id", "0", - "coords", Location.create(-118.14600, 34.75400), - Field.VEL_E, -12.6980, - Field.SIG_N, 0.8000, - Field.CORRELATION, 0.0010), - "last", Map.of( - "id", "1866", - "coords", Location.create(-114.00000, 33.00000), - Field.VEL_E, 0.0000, - Field.SIG_N, 1.0000, - Field.CORRELATION, 0.0000))); - - // CONUS_2023 - expectedMap.put(GpsDataSet.CONUS_2023, Map.of( - "count", 4979, - "first", Map.of( - "id", "0", - "coords", Location.create(-117.09320, 34.11640), - Field.VEL_E, -15.1890, - Field.SIG_N, 0.4960, - Field.CORRELATION, 0.0010), - "last", Map.of( - "id", "4978", - "coords", Location.create(-104.21150, 36.49130), - Field.VEL_E, -0.7700, - Field.SIG_N, 0.1700, - Field.CORRELATION, 0.0350))); - - expected = Collections.unmodifiableMap(expectedMap); - } - - /* - * TODO: Is there a better way? This test current tests that the method - * functions and validates a small subset of the JSON results, better would be - * to use test resource files and run comprehensive tests against those, - * possibly with equivalent JSON files for comparison to the test CSV files. - */ - @SuppressWarnings("unchecked") - @ParameterizedTest - @EnumSource(value = GpsDataSet.class, - names = { "AK_2020", "HI_2020", "CONUS_2014_WUS", "CONUS_2014_UCERF", "CONUS_2023" }) - final void getJsonTest(GpsDataSet gpsDataSet) { - String json = dataSets.getJsonString(gpsDataSet); - List<Feature> features = GeoJson.from(json).toFeatureCollection().features(); - int n = (int) expected.get(gpsDataSet).get("count"); - assertEquals(n, features.size()); - if (expected.get(gpsDataSet).containsKey("first")) { - checkFeature( - (Map<String, Object>) expected.get(gpsDataSet).get("first"), - features.get(0)); - } - if (expected.get(gpsDataSet).containsKey("last")) { - checkFeature( - (Map<String, Object>) expected.get(gpsDataSet).get("last"), - features.get(n - 1)); - } - } - - private static void checkFeature(Map<String, Object> expected, Feature f) { - assertEquals(expected.get("id"), f.idAsString().orElseThrow()); - assertEquals(expected.get("id"), - f.properties().get(Field.STATIONID, String.class).orElseThrow()); - - assertEquals(expected.get("coords"), f.asPoint()); - - for (String key : propertiesToCheck) { - if (expected.containsKey(key)) { - assertEquals(expected.get(key), f.properties().get(key, String.class).orElseThrow()); - } else { - assertEquals(Optional.empty(), f.properties().get(key, String.class)); - } - } - - for (String key : doublesToCheck) { - if (expected.containsKey(key)) { - assertEquals(expected.get(key), f.properties().get(key, Double.class).orElseThrow()); - } else { - assertEquals(Optional.empty(), f.properties().get(key, Double.class)); - } - } - } - - @ParameterizedTest - @EnumSource(value = GpsDataSet.class) - final void getCsvTest(GpsDataSet gpsDataSet) throws IOException { - - String actualCsv = Resources.toString( - Resources.getResource(GpsDataSet.class, Utils.GPS_RESOURCES + gpsDataSet.filename), - StandardCharsets.UTF_8); - - assertEquals(actualCsv, dataSets.getCsv(gpsDataSet)); - } - -} diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataTests.java deleted file mode 100644 index 68f131f8c1a6f8090449b2253e5a1c3564151f10..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/gps/GpsDataTests.java +++ /dev/null @@ -1,54 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.gps; - -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.CORRELATION; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.LATITUDE; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.LONGITUDE; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.SIG_E; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.SIG_N; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.SIG_V; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.SITETYPE; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.STATIONID; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.VEL_E; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.VEL_N; -import static gov.usgs.earthquake.nshmp.www.gps.GpsData.Field.VEL_V; -import static gov.usgs.earthquake.nshmp.www.gps.GpsDataSet.AK_2020; -import static gov.usgs.earthquake.nshmp.www.gps.GpsDataSet.CONUS_2014_UCERF; -import static gov.usgs.earthquake.nshmp.www.gps.GpsDataSet.CONUS_2014_WUS; -import static gov.usgs.earthquake.nshmp.www.gps.GpsDataSet.CONUS_2023; -import static gov.usgs.earthquake.nshmp.www.gps.GpsDataSet.HI_2020; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; - -class GpsDataTests { - - private static final Map<GpsDataSet, List<String>> fieldMap = - Map.of( - HI_2020, - Arrays.asList(LONGITUDE, LATITUDE, VEL_E, VEL_N, VEL_V, SIG_E, SIG_N, SIG_V), - AK_2020, - Arrays.asList(STATIONID, SITETYPE, LONGITUDE, LATITUDE, VEL_E, VEL_N, SIG_E, SIG_N), - CONUS_2014_UCERF, - Arrays.asList(LONGITUDE, LATITUDE, VEL_E, VEL_N, SIG_E, SIG_N, CORRELATION), - CONUS_2014_WUS, - Arrays.asList(LONGITUDE, LATITUDE, VEL_E, VEL_N, SIG_E, SIG_N, CORRELATION), - CONUS_2023, - Arrays.asList(LONGITUDE, LATITUDE, VEL_E, VEL_N, SIG_E, SIG_N, CORRELATION)); - - @ParameterizedTest - @EnumSource(value = GpsDataSet.class, - names = { "AK_2020", "HI_2020", "CONUS_2014_WUS", "CONUS_2014_UCERF", "CONUS_2023" }) - final void getFieldsTests(GpsDataSet gpsDataSet) { - GpsData data = GpsData.readGpsData(gpsDataSet); - List<String> expected = fieldMap.get(gpsDataSet).stream().sorted().collect(Collectors.toList()); - List<String> actual = data.getFields().stream().sorted().collect(Collectors.toList()); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/gulf/GulfDataTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/gulf/GulfDataTests.java deleted file mode 100644 index 6a7f1570df2d1dba1a7a4736b10d06db31349133..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/gulf/GulfDataTests.java +++ /dev/null @@ -1,152 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.gulf; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.ArgumentsProvider; - -import com.google.common.io.Resources; - -import gov.usgs.earthquake.nshmp.Text; -import gov.usgs.earthquake.nshmp.Text.Delimiter; -import gov.usgs.earthquake.nshmp.data.DelimitedData; -import gov.usgs.earthquake.nshmp.geo.Location; -import gov.usgs.earthquake.nshmp.geo.json.Feature; -import gov.usgs.earthquake.nshmp.www.Utils; -import gov.usgs.earthquake.nshmp.www.gulf.GulfData.GulfDataResponse; - -public class GulfDataTests { - - @Test - final void gulfDataTest() throws IOException { - GulfData gulfData = GulfData.readGulfData(); - - List<String> lines = readLinesToList(); - DelimitedData data = DelimitedData.comma(lines); - - data.records() - .forEach(record -> { - double lon = record.getDouble("longitude"); - double lat = record.getDouble("latitude"); - double expectedDepth = record.getDouble("depth"); - Location loc = Location.create(lon, lat); - double actualDepth = gulfData.getGulfData(loc); - assertEquals(expectedDepth, actualDepth); - - GulfDataResponse actual = gulfData.getGulfDataResponse(loc); - assertEquals(expectedDepth, actual.sedimentDepth); - assertEquals(lon, actual.longitude); - assertEquals(lat, actual.latitude); - - Feature actualFeature = gulfData.getGulfDataFeature(loc); - assertEquals(expectedDepth, - actualFeature.properties().getDouble("sedimentDepth").getAsDouble()); - assertEquals(lon, actualFeature.asPoint().longitude); - assertEquals(lat, actualFeature.asPoint().latitude); - }); - } - - private static List<String> readLinesToList() { - List<String> lines = null; - try { - lines = Resources.readLines( - Resources.getResource(GulfData.class, Utils.GULF_RESOURCES + - GulfData.GULF_DATA_FILENAME), - StandardCharsets.UTF_8); - } catch (IOException e) { - e.printStackTrace(); - } - return lines; - } - - public static class GulfParameterizedTests implements ArgumentsProvider { - @Override - public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception { - - List<String> lines = readLinesToList(); - - List<Object[]> list = new ArrayList<>(); - - list = lines.stream().skip(3).map(line -> { - Iterator<String> it = Text.split(line, Delimiter.COMMA).iterator(); - return new Object[] { - Double.valueOf(it.next()), - Double.valueOf(it.next()), - Double.valueOf(it.next()) - }; - - }).collect(Collectors.toList()); - - return list.stream().map(Arguments::of); - } - - } - - @Test - final void roundToGridTest() { - double tol = 1e-6; - double delta = 0.05; - double x = delta / 5; - // Rounding the halfway value appears to be handled differently for positive - // and negative numbers, so a step of delta/2 is not tested here now - // i.e. test passes for longitude test values including a step of delta / 2, - // but test fails with latitude values with this step - // Positive numbers appear to be rounded using the round-to-even strategy - // for mid-points, while negative numbers seem to use the round towards zero - // strategy for mid-points - List<Double> steps = - Arrays.asList(0.00, x, 2 * x, delta * 0.49, delta * 0.51, 3 * x, 4 * x, - delta * 0.99); // add `delta / 2` to test mid-point rounding - - List<Double> values = Arrays.asList(-101.50, -101.45, -101.40, -101.35, -101.30, -101.25, - -101.20, -101.15, -101.10, -101.05, -101.00, -100.95, -100.90); - testRoundToGrid(values, steps, delta, tol); - - values = Arrays.asList(32.45, 32.50, 32.55, 32.60, 32.65, 32.70, 32.75, 32.80, 32.85, 32.90, - 32.95, 33.00, 33.05, 33.10, 33.15, 33.20, 33.25); - testRoundToGrid(values, steps, delta, tol); - - } - - private void testRoundToGrid(List<Double> minimums, List<Double> steps, double delta, - double tol) { - for (double min : minimums) { - - double max = min + delta; - - for (double step : steps) { - double value = min + step; - double rounded = GulfData.roundToGrid(value, delta); - if (Math.abs(step - delta / 2) < tol) { - System.out.printf("Min, max, target, rounded: %.3f, %.3f, %.3f, %.3f\n", - min, max, value, rounded); - } - // assume rounding method - midpoint rounds up - if (step < delta / 2) { - // target value is less than 1/2 delta above the minimum, rounds down - assertEquals(min, rounded, tol); - assertTrue(rounded - min < delta / 2); - assertTrue(max - rounded > delta / 2); - } else { - // target value is greater than or equal to 1/2 delta above minimum, - // rounds up - assertEquals(max, rounded, tol); - assertTrue(rounded - min >= delta / 2); - assertTrue(max - rounded <= delta / 2); - } - } - } - } -} diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/services/FaultSectionsControllerTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/services/FaultSectionsControllerTests.java deleted file mode 100644 index ac7e26647dfb43b40c63a0924b37cfbd59c0eb86..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/services/FaultSectionsControllerTests.java +++ /dev/null @@ -1,79 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.services; - -import static gov.usgs.earthquake.nshmp.www.Utils.GSON; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; - -import java.util.Map; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; - -import gov.usgs.earthquake.nshmp.geo.json.FeatureCollection; -import gov.usgs.earthquake.nshmp.geo.json.GeoJson; -import gov.usgs.earthquake.nshmp.www.fault.FaultSections; -import gov.usgs.earthquake.nshmp.www.fault.NshmFaultSection; -import gov.usgs.earthquake.nshmp.www.meta.Status; -import gov.usgs.earthquake.nshmp.www.services.FaultSectionsController.Response; -import gov.usgs.earthquake.nshmp.www.services.FaultSectionsService.RequestData; - -import io.micronaut.http.HttpRequest; -import io.micronaut.test.extensions.junit5.annotation.MicronautTest; -import jakarta.inject.Inject; - -@MicronautTest -class FaultSectionsControllerTests { - private static final Map<NshmFaultSection, FaultSections> FAULT_SECTIONS = FaultSections - .getFaultSections(); - - private HttpRequest<?> request; - - @Inject - FaultSectionsController controller; - - @BeforeEach - final void beforeEach() { - request = HttpRequest.GET(""); - } - - @ParameterizedTest - @EnumSource(value = NshmFaultSection.class) - final void NshmFaultSectionTests(NshmFaultSection nshmFaultSection) { - // Slash delimited HTTP GET test with raw result - var httpSlashRaw = controller.doGetFaultsSlashRaw(request, nshmFaultSection, true); - httpTests(httpSlashRaw.body(), nshmFaultSection, true); - - // Slash delimited HTTP GET test - var httpSlash = controller.doGetFaultsSlash(request, nshmFaultSection); - httpTests(httpSlash.body(), nshmFaultSection, false); - - // Query based HTTP GET test with raw result - var httpRaw = controller.doGetFaults(request, nshmFaultSection, true); - httpTests(httpRaw.body(), nshmFaultSection, true); - - // Query based HTTP GET test with raw result - var http = controller.doGetFaults(request, nshmFaultSection, false); - httpTests(http.body(), nshmFaultSection, false); - } - - private void featureCollectionTests(NshmFaultSection nshm, FeatureCollection fc) { - assertEquals(FAULT_SECTIONS.get(nshm).toFeatureCollection().toJson(), fc.toJson()); - } - - private void httpTests(String http, NshmFaultSection nshmFaultSection, boolean raw) { - if (raw) { - featureCollectionTests(nshmFaultSection, GeoJson.from(http).toFeatureCollection()); - } else { - Response response = GSON.fromJson(http, Response.class); - RequestData request = response.getRequest(); - - assertEquals(Status.SUCCESS, response.getStatus()); - assertNotEquals(Status.ERROR, response.getStatus()); - - assertEquals(nshmFaultSection, request.nshm()); - assertEquals(raw, request.raw()); - featureCollectionTests(nshmFaultSection, response.getResponse()); - } - } -} diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/services/GpsControllerTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/services/GpsControllerTests.java deleted file mode 100644 index a4481ccbdef02d14b462804eed73d5228e187423..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/services/GpsControllerTests.java +++ /dev/null @@ -1,139 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.services; - -import static gov.usgs.earthquake.nshmp.www.Utils.GSON; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - -import java.lang.reflect.Type; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; - -import com.google.common.reflect.TypeToken; -import com.google.gson.JsonElement; - -import gov.usgs.earthquake.nshmp.geo.json.Feature; -import gov.usgs.earthquake.nshmp.geo.json.FeatureCollection; -import gov.usgs.earthquake.nshmp.geo.json.GeoJson; -import gov.usgs.earthquake.nshmp.geo.json.Properties; -import gov.usgs.earthquake.nshmp.www.ResponseBody; -import gov.usgs.earthquake.nshmp.www.gps.GpsDataSet; -import gov.usgs.earthquake.nshmp.www.gps.GpsDataSets; -import gov.usgs.earthquake.nshmp.www.meta.Status; -import gov.usgs.earthquake.nshmp.www.services.GpsService.GpsFormat; -import gov.usgs.earthquake.nshmp.www.services.GpsService.RequestData; - -import io.micronaut.http.HttpRequest; -import io.micronaut.test.extensions.junit5.annotation.MicronautTest; -import jakarta.inject.Inject; - -@MicronautTest -class GpsControllerTests { - - private static final GpsDataSets DATA = GpsDataSets.loadGpsDataSets(); - - @SuppressWarnings("serial") - private static final Type REQUEST_DATA_TYPE = - new TypeToken<ResponseBody<RequestData, JsonElement>>(ResponseBody.class) {}.getType(); - - @Inject - GpsController controller; - - @ParameterizedTest - @EnumSource(value = GpsDataSet.class) - final void gpsDataTests(GpsDataSet model) { - var request = HttpRequest.GET(""); - - // loop over available formats - for (GpsFormat format : GpsService.GpsFormat.values()) { - var http = controller.doGetGpsData(request, model, format); - httpTests(http.body(), model, format); - } - } - - private static void httpTests(String http, GpsDataSet model, GpsFormat format) { - if (format.equals(GpsFormat.JSON)) { - ResponseBody<RequestData, JsonElement> response = GSON.fromJson(http, REQUEST_DATA_TYPE); - var actual = GeoJson.from(response.getResponse().toString()).toFeatureCollection(); - - assertEquals(Status.SUCCESS, response.getStatus()); - assertTrue(Status.SUCCESS.equals(response.getStatus())); - assertNotEquals(Status.ERROR, response.getStatus()); - - testDataResponse(model, actual); - - } else if (format.equals(GpsFormat.RAW_JSON)) { - var actual = GeoJson.from(http).toFeatureCollection(); - - testDataResponse(model, actual); - - } else if (format.equals(GpsFormat.CSV)) { - var actual = http; - var expected = DATA.getCsv(model); - - assertEquals(expected, actual); - assertTrue(expected.equals(actual)); - - } else { - fail("Unexpected GpsFormat [" + format.name() + "]"); - } - } - - private static void testDataResponse(GpsDataSet model, FeatureCollection actual) { - var features = actual.features(); - - var expected = GeoJson.from(DATA.getJsonString(model)).toFeatureCollection().features(); - - assertEquals(expected.size(), features.size()); - - for (int i = 0; i < features.size(); i++) { - - Feature actualFeature = features.get(i); - Properties actualProps = actualFeature.properties(); - - Feature expectedFeature = expected.get(i); - Properties expectedProps = expectedFeature.properties(); - - assertEquals(expectedFeature.asPoint().longitude, actualFeature.asPoint().longitude); - assertEquals(expectedFeature.asPoint().latitude, actualFeature.asPoint().latitude); - assertEquals(expectedFeature.asPoint(), actualFeature.asPoint()); - - assertEquals(expectedProps.map(), actualProps.map()); - testProperties(expectedProps, actualProps); - - } - - } - - private static void testProperties(Properties expected, Properties actual) { - assertEquals(expected.map().keySet().size(), actual.map().keySet().size()); - for (String field : expected.map().keySet()) { - switch (field) { - case "lon": - case "lat": - break; - case "stationId": - case "siteType": - if (expected.getString(field).isPresent()) { - assertTrue(actual.getString(field).isPresent()); - assertEquals( - expected.getString(field).orElseThrow(), - actual.getString(field).orElseThrow()); - } else { - assertTrue(actual.getString(field).isEmpty()); - } - default: - if (expected.getDouble(field).isPresent()) { - assertTrue(actual.getDouble(field).isPresent()); - assertEquals(expected.getDouble(field).getAsDouble(), - actual.getDouble(field).getAsDouble()); - } else { - assertTrue(actual.getDouble(field).isEmpty()); - } - } - } - } - -} diff --git a/src/test/java/gov/usgs/earthquake/nshmp/www/services/GulfControllerTests.java b/src/test/java/gov/usgs/earthquake/nshmp/www/services/GulfControllerTests.java deleted file mode 100644 index 88f618fb7fc89c64d012b507db486ab73e5ab092..0000000000000000000000000000000000000000 --- a/src/test/java/gov/usgs/earthquake/nshmp/www/services/GulfControllerTests.java +++ /dev/null @@ -1,68 +0,0 @@ -package gov.usgs.earthquake.nshmp.www.services; - -import static gov.usgs.earthquake.nshmp.www.Utils.GSON; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ArgumentsSource; - -import gov.usgs.earthquake.nshmp.geo.Location; -import gov.usgs.earthquake.nshmp.www.ResponseBody; -import gov.usgs.earthquake.nshmp.www.gulf.GulfData.GulfDataResponse; -import gov.usgs.earthquake.nshmp.www.gulf.GulfDataTests.GulfParameterizedTests; -import gov.usgs.earthquake.nshmp.www.meta.Status; -import gov.usgs.earthquake.nshmp.www.services.GulfController.Response; -import gov.usgs.earthquake.nshmp.www.services.GulfService.RequestData; - -import io.micronaut.http.HttpRequest; -import io.micronaut.test.extensions.junit5.annotation.MicronautTest; -import jakarta.inject.Inject; - -@MicronautTest -class GulfControllerTests { - - private HttpRequest<?> request; - - @Inject - GulfController controller; - - @BeforeEach - public final void beforeEach() { - request = HttpRequest.GET(""); - } - - @ParameterizedTest - @ArgumentsSource(GulfParameterizedTests.class) - final void httpGulfDataTests(double lon, Double lat, Double depth) { - var loc = Location.create(lon, lat); - - // Slash delimited HTTP GET test - var httpSlash = controller.doGetGulfSlash(request, lon, lat); - httpTests(httpSlash.body(), loc, depth); - - // Query based HTTP GET test - var http = controller.doGetGulf(request, lon, lat); - httpTests(http.body(), loc, depth); - } - - private void httpTests(String http, Location loc, Double depth) { - ResponseBody<RequestData, GulfDataResponse> response = GSON.fromJson(http, Response.class); - var request = response.getRequest(); - - var actual = response.getResponse(); - - assertEquals(Status.SUCCESS, response.getStatus()); - assertTrue(Status.SUCCESS.equals(response.getStatus())); - assertNotEquals(Status.ERROR, response.getStatus()); - - // requests are for grid points, so no rounding is performed - assertEquals(loc.longitude, request.longitude); - assertEquals(loc.latitude, request.latitude); - assertEquals(loc.longitude, actual.longitude()); - assertEquals(loc.latitude, actual.latitude()); - assertEquals(depth, actual.sedimentDepth()); - } -}