forked from Unidata/threddsIso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNcIsoIT.java
More file actions
143 lines (127 loc) · 5.87 KB
/
NcIsoIT.java
File metadata and controls
143 lines (127 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2023-2025 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package thredds.server.metadata;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;
import org.w3c.dom.Node;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Diff;
import org.xmlunit.util.Predicate;
import thredds.util.ContentType;
import ucar.httpservices.HTTPException;
import ucar.httpservices.HTTPFactory;
import ucar.httpservices.HTTPMethod;
import ucar.httpservices.HTTPSession;
@Category(Integration.class)
public class NcIsoIT {
private static final DockerImageName dockerImageName =
DockerImageName.parse("unidata/thredds-docker:" + getTdsVersion());
private static String basePath;
@ClassRule
public static final GenericContainer<?> tds = new GenericContainer<>(dockerImageName)
.withExposedPorts(8080)
.withCopyFileToContainer(MountableFile.forClasspathResource("/thredds/catalog.xml"),
"/usr/local/tomcat/content/thredds/catalog.xml")
.withCopyFileToContainer(MountableFile.forClasspathResource("/thredds/testgrid1.nc"),
"/usr/local/tomcat/content/thredds/public/testdata/testgrid1.nc")
// copy local tds-plugin jar
.withCopyFileToContainer(MountableFile.forHostPath(getJarPath("tds-plugin")),
"/usr/local/tomcat/webapps/thredds/WEB-INF/lib/1-tds-plugin.jar")
// make sure local nciso-common shows up before bundled nciso-common jar by
// prefixing it with 1-
.withCopyFileToContainer(MountableFile.forHostPath(getJarPath("../../nciso-common/target/nciso-common")),
"/usr/local/tomcat/webapps/thredds/WEB-INF/lib/1-nciso-common.jar");
@BeforeClass
public static void setUp() throws IOException, InterruptedException {
final String address = tds.getHost();
final Integer port = tds.getFirstMappedPort();
basePath = "http://" + address + ":" + port + "/thredds/";
// remove bundled tds-plugin classes from the classpath
tds.execInContainer("rm", "-rf", "/usr/local/tomcat/webapps/thredds/WEB-INF/classes/thredds/server/metadata");
}
@Test
public void shouldReturnNcml() {
final String path = basePath + "ncml/testAll/testgrid1.nc";
final String expectedOutput = "/thredds/testgrid1.ncml.xml";
final Predicate<Node> filter = node -> !(node.hasAttributes() && node.getAttributes().getNamedItem("name") != null
&& (node.getAttributes().getNamedItem("name").getNodeValue().equals("metadata_creation")
|| node.getAttributes().getNamedItem("name").getNodeValue().equals("nciso_version")));
compare(path, expectedOutput, ContentType.xml, filter);
}
@Test
public void shouldReturnIso() {
final String path = basePath + "iso/testAll/testgrid1.nc";
final String expectedOutput = "/thredds/testgrid1.iso.xml";
final Predicate<Node> filter =
node -> !node.getTextContent().startsWith("This record was translated from NcML using")
&& !node.getNodeName().startsWith("gco:Date");
compare(path, expectedOutput, ContentType.xml, filter);
}
@Test
public void shouldReturnUddc() {
final String path = basePath + "uddc/testAll/testgrid1.nc";
final String expectedOutput = "/thredds/testgrid1.uddc.html";
compare(path, expectedOutput, ContentType.html, node -> true);
}
private void compare(String endpoint, String expectedOutput, ContentType expectedType, Predicate<Node> filter) {
final byte[] response = getContent(endpoint, expectedType);
final Diff diff = DiffBuilder.compare(Input.fromStream(getClass().getResourceAsStream(expectedOutput)))
.withTest(Input.fromByteArray(response)).normalizeWhitespace()
// don't compare elements with e.g. version/ current datetime
.withNodeFilter(filter).build();
assertWithMessage(diff.toString()).that(diff.hasDifferences()).isFalse();
}
private byte[] getContent(String endpoint, ContentType expectedContentType) {
try (HTTPSession session = HTTPFactory.newSession(endpoint)) {
final HTTPMethod method = HTTPFactory.Get(session);
final int statusCode = method.execute();
assertThat(statusCode).isEqualTo(HttpServletResponse.SC_OK);
assertThat(method.getResponseHeaderValue(ContentType.HEADER).get())
.isEqualTo(expectedContentType.getContentHeader());
return method.getResponseAsBytes();
} catch (HTTPException e) {
fail("Problem with HTTP request:" + e);
return null;
}
}
private static String getPropertyValue(String propertyKey) {
try {
final Properties properties = new Properties();
properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("project.properties"));
return properties.getProperty(propertyKey);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static String getVersion() {
return getPropertyValue("version");
}
private static String getTdsVersion() {
return getPropertyValue("tds_version");
}
private static Path getJarPath(String name) {
try {
final Path targetPath = Paths.get(ClassLoader.getSystemClassLoader().getResource("").toURI()).getParent();
return Paths.get(targetPath.toRealPath().toString(), name + "-" + getVersion() + ".jar");
} catch (URISyntaxException | IOException e) {
throw new RuntimeException(e);
}
}
}