From ef0960fc02938a5002838667b80c0b6873811d55 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 17 Sep 2024 12:47:28 -0600 Subject: [PATCH 1/5] Add test helper function to xml utils --- tds/src/test/java/thredds/util/xml/XmlUtil.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tds/src/test/java/thredds/util/xml/XmlUtil.java b/tds/src/test/java/thredds/util/xml/XmlUtil.java index 7617c360d1..d9095191d1 100644 --- a/tds/src/test/java/thredds/util/xml/XmlUtil.java +++ b/tds/src/test/java/thredds/util/xml/XmlUtil.java @@ -17,11 +17,13 @@ public class XmlUtil { private XmlUtil() {} public static Document getStringResponseAsDoc(MockHttpServletResponse response) throws JDOMException, IOException { + return getStringResponseAsDoc(response.getContentAsByteArray()); + } + public static Document getStringResponseAsDoc(byte[] response) throws JDOMException, IOException { SAXBuilder sb = new SAXBuilder(); sb.setExpandEntities(false); - String strResponse = response.getContentAsString(); - return sb.build(new ByteArrayInputStream(strResponse.getBytes(response.getCharacterEncoding()))); + return sb.build(new ByteArrayInputStream(response)); } public static List evaluateXPath(Document doc, String strXpath) { From 4336b0677315c7309eccec90b6b6d826d678b596 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 17 Sep 2024 12:48:07 -0600 Subject: [PATCH 2/5] Add tests for radar server queries --- .../server/radar/TestRadarServerQuery.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tds/src/integrationTests/java/thredds/server/radar/TestRadarServerQuery.java diff --git a/tds/src/integrationTests/java/thredds/server/radar/TestRadarServerQuery.java b/tds/src/integrationTests/java/thredds/server/radar/TestRadarServerQuery.java new file mode 100644 index 0000000000..a3fb8bd386 --- /dev/null +++ b/tds/src/integrationTests/java/thredds/server/radar/TestRadarServerQuery.java @@ -0,0 +1,116 @@ +package thredds.server.radar; + +import jakarta.servlet.http.HttpServletResponse; +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.Namespace; +import org.jdom2.filter.Filters; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import thredds.test.util.TestOnLocalServer; +import thredds.util.xml.XmlUtil; +import ucar.unidata.util.test.category.NeedsCdmUnitTest; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static com.google.common.truth.Truth.assertThat; + +@Category(NeedsCdmUnitTest.class) +public class TestRadarServerQuery { + private static final Namespace NS = + Namespace.getNamespace("ns", "http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0"); + private static final String L2_URL = "/radarServer/nexrad/level2/IDD"; + private static final String L3_URL = "/radarServer/nexrad/level3/IDD"; + + @Test + public void shouldReturnAllDatasetsForStation() throws IOException, JDOMException { + String endpoint = L2_URL + "?stn=KDGX&temporal=all"; + verifyNumberOfDatasets(endpoint, 3); + } + + @Test + public void shouldReturnZeroDatasetsForNonOverlappingTimeRange() throws IOException, JDOMException { + String endpoint = L2_URL + "?stn=KDGX&time_start=2000-01-01T12:00:00&time_end=2001-01-01T12:00:00"; + verifyNumberOfDatasets(endpoint, 0); + } + + @Test + public void shouldReturnOneDatasetForOverlappingTimeRange() throws IOException, JDOMException { + String endpoint = L2_URL + "?stn=KDGX&time_start=2014-06-02T23:52:00&time_end=2014-06-02T23:53:00"; + verifyNumberOfDatasets(endpoint, 1); + } + + @Test + public void shouldReturnOneDatasetForOverlappingTimeDuration() throws IOException, JDOMException { + String endpoint = L2_URL + "?stn=KDGX&time_start=2014-06-02T23:52:00&time_duration=PT1M"; + verifyNumberOfDatasets(endpoint, 1); + } + + @Test + public void shouldReturnOneDatasetForTime() throws IOException, JDOMException { + String endpoint = L2_URL + "?stn=KDGX&time=2014-06-02T23:52:00"; + verifyNumberOfDatasets(endpoint, 1); + } + + @Test + public void shouldReturnZeroDatasetsForNonExistentStation() throws IOException, JDOMException { + String endpoint = L2_URL + "?stn=ABCD&temporal=all"; + verifyNumberOfDatasets(endpoint, 0); + } + + @Test + public void shouldReturnErrorForNonOverlappingBox() throws IOException, JDOMException { + String endpoint = L2_URL + "?north=10&south=0&west=-100&east=-80&temporal=all"; + byte[] result = TestOnLocalServer.getContent(TestOnLocalServer.withHttpPath(endpoint), HttpServletResponse.SC_OK, + "text/plain;charset=iso-8859-1"); + assertThat(new String(result, StandardCharsets.UTF_8)).isEqualTo("No stations found for specified coordinates."); + } + + @Test + public void shouldReturnAllDatasetsForOverlappingBox() throws IOException, JDOMException { + String endpoint = L2_URL + "?north=50&south=30&west=-100&east=-80&temporal=all"; + verifyNumberOfDatasets(endpoint, 3); + } + + @Test + public void shouldReturnAllDatasetsForLonLat() throws IOException, JDOMException { + String endpoint = L2_URL + "?latitude=30&longitude=-90&temporal=all"; + verifyNumberOfDatasets(endpoint, 3); + } + + @Test + public void shouldReturnAllLevel3Datasets() throws IOException, JDOMException { + String endpoint = L3_URL + "?temporal=all&var=N0R&stn=UDX"; + verifyNumberOfDatasets(endpoint, 329); + } + + @Test + public void shouldReturnErrorWithoutVar() throws IOException, JDOMException { + String endpoint = L3_URL + "?temporal=all&stn=UDX"; + byte[] result = TestOnLocalServer.getContent(TestOnLocalServer.withHttpPath(endpoint), HttpServletResponse.SC_OK, + "text/plain;charset=iso-8859-1"); + assertThat(new String(result, StandardCharsets.UTF_8)).isEqualTo("One or more variables required."); + } + + private static void verifyNumberOfDatasets(String endpoint, int expectedNumber) throws IOException, JDOMException { + byte[] result = TestOnLocalServer.getContent(TestOnLocalServer.withHttpPath(endpoint), HttpServletResponse.SC_OK, + "application/xml"); + + Document doc = XmlUtil.getStringResponseAsDoc(result); + + XPathExpression xpathDoc = XPathFactory.instance() + .compile("ns:catalog/ns:dataset/ns:metadata/ns:documentation", Filters.element(), null, NS); + Element documentation = xpathDoc.evaluateFirst(doc); + assertThat(documentation.getTextTrim()).isEqualTo(expectedNumber + " datasets found for query"); + + XPathExpression xpathDatasets = + XPathFactory.instance().compile("ns:catalog/ns:dataset/ns:dataset", Filters.element(), null, NS); + List datasets = xpathDatasets.evaluate(doc); + assertThat(datasets.size()).isEqualTo(expectedNumber); + } +} From a976989b823aafdbd1adb180fe8c1f40ac6cee04 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 17 Sep 2024 13:00:44 -0600 Subject: [PATCH 3/5] Add station for level 2 radar data --- tds/src/test/content/thredds/radar/CS039_L2_stations.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tds/src/test/content/thredds/radar/CS039_L2_stations.xml b/tds/src/test/content/thredds/radar/CS039_L2_stations.xml index ecf667c66a..1e94283f58 100644 --- a/tds/src/test/content/thredds/radar/CS039_L2_stations.xml +++ b/tds/src/test/content/thredds/radar/CS039_L2_stations.xml @@ -6,4 +6,7 @@ + + + \ No newline at end of file From 7df4ff531f3cdcb2a0bb158d734731162c033087 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 17 Sep 2024 13:01:09 -0600 Subject: [PATCH 4/5] Fix date format and regex to match radar data --- tds/src/test/content/thredds/radar/radarCollections.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tds/src/test/content/thredds/radar/radarCollections.xml b/tds/src/test/content/thredds/radar/radarCollections.xml index e4f3e1d682..e0e14e35f0 100644 --- a/tds/src/test/content/thredds/radar/radarCollections.xml +++ b/tds/src/test/content/thredds/radar/radarCollections.xml @@ -9,7 +9,7 @@ - + Radial NEXRAD2 From 4e0df541602cc22e240315af48bea5fdec93dfc9 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Tue, 17 Sep 2024 13:17:39 -0600 Subject: [PATCH 5/5] Use the AliasTranslator when getting the datascan location, so that tests with ${cdmUnitTest} variable have correct path for the radar server --- .../java/thredds/server/radarServer2/RadarServerConfig.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tds/src/main/java/thredds/server/radarServer2/RadarServerConfig.java b/tds/src/main/java/thredds/server/radarServer2/RadarServerConfig.java index 18be093e34..cd5559f8be 100644 --- a/tds/src/main/java/thredds/server/radarServer2/RadarServerConfig.java +++ b/tds/src/main/java/thredds/server/radarServer2/RadarServerConfig.java @@ -14,6 +14,8 @@ import ucar.nc2.units.DateRange; import ucar.nc2.units.DateType; import ucar.nc2.units.TimeDuration; +import ucar.nc2.util.AliasTranslator; + import java.io.File; import java.io.IOException; import java.net.URI; @@ -59,7 +61,7 @@ static public List readXML(String filename) { Element meta = dataset.getChild("metadata", catNS); conf.name = dataset.getAttributeValue("name"); conf.urlPath = dataset.getAttributeValue("path"); - conf.dataPath = getPath(dataset.getAttributeValue("location")); + conf.dataPath = getPath(AliasTranslator.translateAlias(dataset.getAttributeValue("location"))); conf.dataFormat = meta.getChild("dataFormat", catNS).getValue(); conf.stationFile = meta.getChild("stationFile", catNS).getAttributeValue("path"); conf.doc = meta.getChild("documentation", catNS).getValue();