aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java
diff options
context:
space:
mode:
authormmis <michael.morris@ericsson.com>2018-07-20 15:28:25 +0100
committermmis <michael.morris@ericsson.com>2018-07-25 13:16:51 +0100
commitb40acf2d244058c162a8597968e59f2708e6abf4 (patch)
treed30890a123305c50e3280ebbe02742b72f226c90 /policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java
parent02bf7de575b246c63188988072aabb36ac3b697b (diff)
Copy policy-endpoints from drools-pdp to common
Issue-ID: POLICY-967 Change-Id: I374c155ee102c3e157c60d0a22d7191544abb76a Signed-off-by: mmis <michael.morris@ericsson.com>
Diffstat (limited to 'policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java')
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java227
1 files changed, 227 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
new file mode 100644
index 00000000..eba96208
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java
@@ -0,0 +1,227 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-endpoints
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.endpoints.http.server.test;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.ConnectException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.UUID;
+
+import org.junit.Test;
+import org.onap.policy.common.endpoints.http.server.HttpServletServer;
+import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory;
+import org.onap.policy.common.endpoints.http.server.impl.IndexedHttpServletServerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * HttpServletServer JUNIT tests
+ */
+public class HttpServerTest {
+
+ HttpServletServerFactory httpServletServerFactory = IndexedHttpServletServerFactory.getInstance();
+
+ /**
+ * Logger
+ */
+ private static Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
+
+ @Test
+ public void testSingleServer() throws Exception {
+ logger.info("-- testSingleServer() --");
+
+ HttpServletServer server = httpServletServerFactory.build("echo", "localhost", 5678, "/", false, true);
+ server.addServletPackage("/*", this.getClass().getPackage().getName());
+ server.waitedStart(5000);
+
+ assertTrue(httpServletServerFactory.get(5678).isAlive());
+
+ String response = http(httpServletServerFactory.get(5678), "http://localhost:5678/junit/echo/hello");
+ assertTrue("hello".equals(response));
+
+ response = null;
+ try {
+ response = http(httpServletServerFactory.get(5678), "http://localhost:5678/swagger.json");
+ } catch (IOException e) {
+ // Expected
+ }
+ assertTrue(response == null);
+
+ assertTrue(httpServletServerFactory.get(5678).isAlive());
+ assertTrue(httpServletServerFactory.inventory().size() == 1);
+
+ httpServletServerFactory.destroy(5678);
+ assertTrue(httpServletServerFactory.inventory().size() == 0);
+ }
+
+ @Test
+ public void testMultipleServers() throws Exception {
+ logger.info("-- testMultipleServers() --");
+
+ HttpServletServer server1 = httpServletServerFactory.build("echo-1", "localhost", 5688, "/", true, true);
+ server1.addServletPackage("/*", this.getClass().getPackage().getName());
+ server1.waitedStart(5000);
+
+ HttpServletServer server2 = httpServletServerFactory.build("echo-2", "localhost", 5689, "/", false, true);
+ server2.addServletPackage("/*", this.getClass().getPackage().getName());
+ server2.waitedStart(5000);
+
+ assertTrue(httpServletServerFactory.get(5688).isAlive());
+ assertTrue(httpServletServerFactory.get(5689).isAlive());
+
+ String response = http(httpServletServerFactory.get(5688), "http://localhost:5688/junit/echo/hello");
+ assertTrue("hello".equals(response));
+
+ response = http(httpServletServerFactory.get(5688), "http://localhost:5688/swagger.json");
+ assertTrue(response != null);
+
+ response = http(httpServletServerFactory.get(5689), "http://localhost:5689/junit/echo/hello");
+ assertTrue("hello".equals(response));
+
+ response = null;
+ try {
+ response = http(httpServletServerFactory.get(5689), "http://localhost:5689/swagger.json");
+ } catch (IOException e) {
+ // Expected
+ }
+ assertTrue(response == null);
+
+ httpServletServerFactory.destroy();
+ assertTrue(httpServletServerFactory.inventory().size() == 0);
+ }
+
+ @Test
+ public void testMultiServicePackage() throws Exception {
+ logger.info("-- testMultiServicePackage() --");
+
+ String randomName = UUID.randomUUID().toString();
+
+ HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5668, "/", false, true);
+ server.addServletPackage("/*", this.getClass().getPackage().getName());
+ server.waitedStart(5000);
+
+ assertTrue(httpServletServerFactory.get(5668).isAlive());
+
+ String response = http(httpServletServerFactory.get(5668), "http://localhost:5668/junit/echo/hello");
+ assertTrue("hello".equals(response));
+
+ response = http(httpServletServerFactory.get(5668), "http://localhost:5668/junit/endpoints/http/servers");
+ assertTrue(response.contains(randomName));
+
+ httpServletServerFactory.destroy();
+ assertTrue(httpServletServerFactory.inventory().size() == 0);
+ }
+
+ @Test
+ public void testServiceClass() throws Exception {
+ logger.info("-- testServiceClass() --");
+ String randomName = UUID.randomUUID().toString();
+
+ HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5658, "/", false, true);
+ server.addServletClass("/*", RestEchoService.class.getCanonicalName());
+ server.waitedStart(5000);
+
+ assertTrue(httpServletServerFactory.get(5658).isAlive());
+
+ String response = http(httpServletServerFactory.get(5658), "http://localhost:5658/junit/echo/hello");
+ assertTrue("hello".equals(response));
+
+ httpServletServerFactory.destroy();
+ assertTrue(httpServletServerFactory.inventory().size() == 0);
+ }
+
+ @Test
+ public void testMultiServiceClass() throws Exception {
+ logger.info("-- testMultiServiceClass() --");
+
+ String randomName = UUID.randomUUID().toString();
+
+ HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5648, "/", false, true);
+ server.addServletClass("/*", RestEchoService.class.getCanonicalName());
+ server.addServletClass("/*", RestEndpoints.class.getCanonicalName());
+ server.waitedStart(5000);
+
+ assertTrue(httpServletServerFactory.get(5648).isAlive());
+
+ String response = http(httpServletServerFactory.get(5648), "http://localhost:5648/junit/echo/hello");
+ assertTrue("hello".equals(response));
+
+ response = http(httpServletServerFactory.get(5648), "http://localhost:5648/junit/endpoints/http/servers");
+ assertTrue(response.contains(randomName));
+
+ httpServletServerFactory.destroy();
+ assertTrue(httpServletServerFactory.inventory().size() == 0);
+ }
+
+ /**
+ * performs an http request
+ *
+ * @throws MalformedURLException
+ * @throws IOException
+ * @throws InterruptedException
+ */
+ protected String http(HttpServletServer server, String aUrl)
+ throws MalformedURLException, IOException, InterruptedException {
+ URL url = new URL(aUrl);
+ String response = null;
+ int numRetries = 1, maxNumberRetries = 5;
+ while (numRetries <= maxNumberRetries) {
+ try {
+ response = response(url);
+ break;
+ } catch (ConnectException e) {
+ logger.warn("http server {} @ {} ({}) - cannot connect yet ..", server, aUrl, numRetries, e);
+ numRetries++;
+ Thread.sleep(10000L);
+ } catch (Exception e) {
+ throw e;
+ }
+ }
+
+ return response;
+ }
+
+ /**
+ * gets http response
+ *
+ * @param url url
+ *
+ * @throws IOException
+ */
+ protected String response(URL url) throws IOException {
+ String response = "";
+ try (BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
+ String line;
+ while ((line = ioReader.readLine()) != null) {
+ response += line;
+ }
+ }
+ return response;
+ }
+
+
+
+}