diff options
author | Hengye <yehui.wang@est.tech> | 2020-02-29 20:39:45 +0800 |
---|---|---|
committer | Hengye <yehui.wang@est.tech> | 2020-03-02 10:25:56 +0800 |
commit | 2fda610a5b5c4b1b906eaf72405e225df19dbc02 (patch) | |
tree | f3ba075a5685247aaa566fb8f406c7e06ea88df1 /policy-endpoints/src/test | |
parent | d59c619324f5813127e5150e2b3fd5d446cf3a48 (diff) |
Add JettyStaticResourceServer to policy-endpoints
Using DefaultServlet to manage static resources
on Jetty Server.
Issue-ID: POLICY-2311
Signed-off-by: Hengye <yehui.wang@est.tech>
Change-Id: Ife0eda06334eb0b6350110254945975050181f20
Diffstat (limited to 'policy-endpoints/src/test')
3 files changed, 157 insertions, 0 deletions
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java index 0aec411b..e202b11b 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,12 +21,15 @@ package org.onap.policy.common.endpoints.http.server.test; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.catchThrowable; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import com.google.gson.Gson; + import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; @@ -476,6 +480,103 @@ public class HttpServerTest { assertTrue(HttpServletServerFactoryInstance.getServerFactory().inventory().isEmpty()); } + @Test + public void testSingleStaticResourceServer() throws Exception { + logger.info("-- testSingleStaticResourceServer() --"); + + HttpServletServer staticServer = HttpServletServerFactoryInstance.getServerFactory() + .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true); + Throwable thrown = catchThrowable(() -> staticServer.addServletResource("/*", null)); + assertThat(thrown).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("No resourceBase provided"); + + staticServer.addServletResource(null, + HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm()); + + thrown = catchThrowable(() -> staticServer.addServletClass("/*", RestEchoService.class.getName())); + assertThat(thrown).isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("is not supported on this type of jetty server"); + + thrown = catchThrowable(() -> staticServer.addServletPackage("/api/*", this.getClass().getPackage().getName())); + assertThat(thrown).isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("is not supported on this type of jetty server"); + + thrown = catchThrowable(() -> staticServer.setSerializationProvider(MyGsonProvider.class.getName())); + assertThat(thrown).isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("is not supported on this type of jetty server"); + + staticServer.waitedStart(5000); + + assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); + assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + + String response = http(portUrl); + assertThat(response).contains("Test Jetty Static Resources Root"); + + HttpServletServerFactoryInstance.getServerFactory().destroy(port); + assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + } + + @Test + public void testMultiStaticResourceServer() throws Exception { + logger.info("-- testMultiStaticResourceServer() --"); + + HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory() + .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true); + staticResourceServer.addServletResource("/root/*", + HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm()); + staticResourceServer.addServletResource("/alt-root/*", + HttpServerTest.class.getClassLoader().getResource("webapps/alt-root").toExternalForm()); + staticResourceServer.waitedStart(5000); + + assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); + assertEquals(1, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + + String response = http(portUrl + "/root/"); + assertThat(response).contains("Test Jetty Static Resources Root"); + + response = http(portUrl + "/alt-root/"); + assertThat(response).contains("Test Jetty Static Resources Alt-Root"); + + HttpServletServerFactoryInstance.getServerFactory().destroy(port); + assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + } + + @Test + public void testMultiTypesServer() throws Exception { + logger.info("-- testMultiTypesServer() --"); + + HttpServletServer staticResourceServer = HttpServletServerFactoryInstance.getServerFactory() + .buildStaticResourceServer("Static Resources Server", false, LOCALHOST, port, "/", true); + staticResourceServer.addServletResource("/root/*", + HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm()); + staticResourceServer.waitedStart(5000); + + int port2 = port + 1; + HttpServletServer jerseyServer = + HttpServletServerFactoryInstance.getServerFactory().build("echo", LOCALHOST, port2, "/", false, true); + jerseyServer.addServletPackage("/api/*", this.getClass().getPackage().getName()); + + Throwable thrown = catchThrowable(() -> jerseyServer.addServletResource("/root/*", + HttpServerTest.class.getClassLoader().getResource("webapps/root").toExternalForm())); + assertThat(thrown).isInstanceOf(UnsupportedOperationException.class) + .hasMessageContaining("is not supported on this type of jetty server"); + + jerseyServer.waitedStart(5000); + + assertTrue(HttpServletServerFactoryInstance.getServerFactory().get(port).isAlive()); + assertEquals(2, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + + String response = http(portUrl + "/root/"); + assertThat(response).contains("Test Jetty Static Resources Root"); + + response = http(LOCALHOST_PREFIX + port2 + "/api" + JUNIT_ECHO_HELLO); + assertEquals(HELLO, response); + + HttpServletServerFactoryInstance.getServerFactory().destroy(); + assertEquals(0, HttpServletServerFactoryInstance.getServerFactory().inventory().size()); + } + /** * performs an http request. * diff --git a/policy-endpoints/src/test/resources/webapps/alt-root/index.html b/policy-endpoints/src/test/resources/webapps/alt-root/index.html new file mode 100644 index 00000000..0948cebd --- /dev/null +++ b/policy-endpoints/src/test/resources/webapps/alt-root/index.html @@ -0,0 +1,28 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2020 Nordix Foundation. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> + +<html> + <head> + <title>Hello World</title> + </head> + <body> + <h4>Test Jetty Static Resources Alt-Root</h4> + </body> +</html>
\ No newline at end of file diff --git a/policy-endpoints/src/test/resources/webapps/root/index.html b/policy-endpoints/src/test/resources/webapps/root/index.html new file mode 100644 index 00000000..a7cc120b --- /dev/null +++ b/policy-endpoints/src/test/resources/webapps/root/index.html @@ -0,0 +1,28 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2020 Nordix Foundation. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> + +<html> + <head> + <title>Hello World</title> + </head> + <body> + <h4>Test Jetty Static Resources Root</h4> + </body> +</html>
\ No newline at end of file |