diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2020-03-03 15:10:50 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-03-03 15:10:50 +0000 |
commit | 361e714960f444d9319dd54186af0e8efede4011 (patch) | |
tree | 0ec60bb548ff2894b6f043fbddeef728ac97b3d2 /policy-endpoints/src/test/java | |
parent | 95f16eb44ac773576836ed15bc65960f3e38e8a9 (diff) | |
parent | 2fda610a5b5c4b1b906eaf72405e225df19dbc02 (diff) |
Merge "Add JettyStaticResourceServer to policy-endpoints"
Diffstat (limited to 'policy-endpoints/src/test/java')
-rw-r--r-- | policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java | 101 |
1 files changed, 101 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. * |