From e7849e0abf8e7adf8b77f64ea7b24a0cf26dc040 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Fri, 15 Dec 2023 14:45:37 -0700 Subject: [PATCH] Add ncss tests for ncml dataset and dataset scan --- tds/src/test/content/thredds/catalog.xml | 11 +- .../server/ncss/controller/grid/NcmlTest.java | 108 ++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tds/src/test/java/thredds/server/ncss/controller/grid/NcmlTest.java diff --git a/tds/src/test/content/thredds/catalog.xml b/tds/src/test/content/thredds/catalog.xml index 02522d1c23..bbd6796026 100644 --- a/tds/src/test/content/thredds/catalog.xml +++ b/tds/src/test/content/thredds/catalog.xml @@ -173,9 +173,8 @@ - - - odap + + all Grid @@ -185,6 +184,9 @@ + + + @@ -246,6 +248,9 @@ + + + diff --git a/tds/src/test/java/thredds/server/ncss/controller/grid/NcmlTest.java b/tds/src/test/java/thredds/server/ncss/controller/grid/NcmlTest.java new file mode 100644 index 0000000000..c63aa39738 --- /dev/null +++ b/tds/src/test/java/thredds/server/ncss/controller/grid/NcmlTest.java @@ -0,0 +1,108 @@ +package thredds.server.ncss.controller.grid; + +import java.util.List; +import java.util.Optional; +import org.jdom2.Document; +import org.jdom2.Element; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; +import thredds.mock.web.MockTdsContextLoader; +import thredds.util.xml.XmlUtil; +import ucar.nc2.Attribute; +import ucar.nc2.NetcdfFile; +import ucar.nc2.NetcdfFiles; +import ucar.nc2.Variable; +import java.io.IOException; + +import static com.google.common.truth.Truth.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(locations = {"/WEB-INF/applicationContext.xml", "/WEB-INF/spring-servlet.xml"}, + loader = MockTdsContextLoader.class) +public class NcmlTest { + private static final String NCML_DATASET = "/ncss/grid/ExampleNcML/Modified.nc"; + private static final String NCML_DATASET_SCAN = "/ncss/grid/ModifyDatasetScan/example1.nc"; + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + @Before + public void setUp() throws IOException { + mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + } + + @Test + public void shouldShowNcmlModifiedVariableOnDatasetPage() throws Exception { + assertShowsNcmlModifiedVariableOnDatasetPage(NCML_DATASET + "/dataset.xml"); + } + + @Test + public void shouldShowNcmlModifiedVariableOnDatasetPageForDatasetScan() throws Exception { + assertShowsNcmlModifiedVariableOnDatasetPage(NCML_DATASET_SCAN + "/dataset.xml"); + } + + @Test + public void shouldReturnNcmlModifiedVariable() throws Exception { + assertReturnsNcmlModifiedVariable(NCML_DATASET); + } + + @Test + public void shouldReturnNcmlModifiedVariableForDatasetScan() throws Exception { + assertReturnsNcmlModifiedVariable(NCML_DATASET_SCAN); + } + + private void assertShowsNcmlModifiedVariableOnDatasetPage(String servletPath) throws Exception { + final RequestBuilder requestBuilder = MockMvcRequestBuilders.get(servletPath).servletPath(servletPath); + final MvcResult mvcResult = + mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk()).andReturn(); + + final Document doc = XmlUtil.getStringResponseAsDoc(mvcResult.getResponse()); + final List grids = XmlUtil.evaluateXPath(doc, "//grid"); + assertThat(grids).isNotNull(); + + final Optional orgiVar = + grids.stream().filter(e -> e.getAttribute("name").getValue().equals("rh")).findFirst(); + assertThat(orgiVar.isPresent()).isFalse(); + + final Optional modifiedVar = + grids.stream().filter(e -> e.getAttribute("name").getValue().equals("ReletiveHumidity")).findFirst(); + assertThat(modifiedVar.isPresent()).isTrue(); + assertThat(modifiedVar.get().getAttribute("name").getValue()).isEqualTo("ReletiveHumidity"); + assertThat(modifiedVar.get().getAttribute("desc").getValue()).isEqualTo("relatively humid"); + } + + private void assertReturnsNcmlModifiedVariable(String servletPath) throws Exception { + final RequestBuilder requestBuilder = + MockMvcRequestBuilders.get(servletPath).servletPath(servletPath).param("var", "all"); + final MvcResult mvcResult = + mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk()).andReturn(); + + try (NetcdfFile ncf = NetcdfFiles.openInMemory("ncmlTest.nc", mvcResult.getResponse().getContentAsByteArray())) { + final Variable origVar = ncf.findVariable("rh"); + assertThat((Object) origVar).isNull(); + + final Variable modifiedVar = ncf.findVariable("ReletiveHumidity"); + assertThat((Object) modifiedVar).isNotNull(); + + final Attribute att = modifiedVar.findAttribute("long_name"); + assertThat(att).isNotNull(); + assertThat(att.getStringValue()).isEqualTo("relatively humid"); + } + } +} +