aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/test/java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-01-21 12:42:10 -0500
committerJim Hahn <jrh3@att.com>2019-01-23 18:43:43 -0500
commit18914b033f67d25cc9377b2e381648a9e3184968 (patch)
tree973bfdd2a119c19d11e3770e2a4a82d898279284 /policy-endpoints/src/test/java
parent9f15e1987dc9fc6bb8196c415b817b3f1cf6bd5f (diff)
Add gson support to policy-endpoints
Added "provider" property to both http client and server builders. The provider defaults to jackson, to maintain backward compatibility until other policy code has been converted to gson. Removed commented item from pom. Added some comments and re-arranged a few pieces of code. Fixed a few typos and removed spacing at the end of some lines. Reordered imports. Added comments about limitations when using jersey-media-json-jackson. Address ridiculous checkstyle complaint. Support comma-separated list of serialization providers in jersey client. Disabled metainf discovery from jersey client and server so that the media-json dependencies could be re-instated in the pom. Address another ridiculous checkstyle complaint. Change-Id: Ic5a93b475d0ee9b435352b3516de6b865b00a86a Issue-ID: POLICY-1428 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-endpoints/src/test/java')
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/GsonMessageBodyHandlerTest.java156
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java133
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java307
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyGsonProvider.java76
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyJacksonProvider.java76
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoReqResp.java59
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java11
7 files changed, 770 insertions, 48 deletions
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/GsonMessageBodyHandlerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/GsonMessageBodyHandlerTest.java
new file mode 100644
index 00000000..9c6ec80d
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/GsonMessageBodyHandlerTest.java
@@ -0,0 +1,156 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import javax.ws.rs.core.MediaType;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.endpoints.http.server.internal.GsonMessageBodyHandler;
+
+public class GsonMessageBodyHandlerTest {
+ private static final String GEN_TYPE = "some-type";
+ private static final String[] subtypes = {"json", "jSoN", "hello+json", "javascript", "x-javascript", "x-json"};
+
+ @SuppressWarnings("rawtypes")
+ private static final Class GEN_CLASS = MyObject.class;
+
+ @SuppressWarnings("unchecked")
+ private static final Class<Object> CLASS_OBJ = GEN_CLASS;
+
+ private GsonMessageBodyHandler hdlr;
+
+ @Before
+ public void setUp() {
+ hdlr = new GsonMessageBodyHandler();
+ }
+
+ @Test
+ public void testIsWriteable() {
+ // null media type
+ assertTrue(hdlr.isWriteable(null, null, null, null));
+
+ for (String subtype : subtypes) {
+ assertTrue("writeable " + subtype, hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, subtype)));
+
+ }
+
+ // the remaining should be FALSE
+
+ // null subtype
+ assertFalse(hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, null)));
+
+ // text subtype
+ assertFalse(hdlr.isWriteable(null, null, null, MediaType.TEXT_HTML_TYPE));
+ }
+
+ @Test
+ public void testGetSize() {
+ assertEquals(-1, hdlr.getSize(null, null, null, null, null));
+ }
+
+ @Test
+ public void testWriteTo_testReadFrom() throws Exception {
+ ByteArrayOutputStream outstr = new ByteArrayOutputStream();
+ MyObject obj1 = new MyObject(10);
+ hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
+
+ Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
+ new ByteArrayInputStream(outstr.toByteArray()));
+ assertEquals(obj1.toString(), obj2.toString());
+ }
+
+ @Test
+ public void testWriteTo_DifferentTypes() throws Exception {
+ ByteArrayOutputStream outstr = new ByteArrayOutputStream();
+
+ // use a derived type, but specify the base type when writing
+ MyObject obj1 = new MyObject(10) {};
+ hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
+
+ Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
+ new ByteArrayInputStream(outstr.toByteArray()));
+ assertEquals(obj1.toString(), obj2.toString());
+ }
+
+ @Test
+ public void testIsReadable() {
+ // null media type
+ assertTrue(hdlr.isReadable(null, null, null, null));
+
+ // null subtype
+ assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
+
+ for (String subtype : subtypes) {
+ assertTrue("readable " + subtype, hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, subtype)));
+
+ }
+
+ // the remaining should be FALSE
+
+ // null subtype
+ assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
+
+ // text subtype
+ assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_HTML_TYPE));
+ }
+
+ @Test
+ public void testReadFrom_DifferentTypes() throws Exception {
+ ByteArrayOutputStream outstr = new ByteArrayOutputStream();
+ MyObject obj1 = new MyObject(10);
+ hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
+
+ // use a derived type, but specify the base type when reading
+ @SuppressWarnings("rawtypes")
+ Class clazz = new MyObject() {}.getClass();
+
+ @SuppressWarnings("unchecked")
+ Class<Object> objclazz = clazz;
+
+ Object obj2 = hdlr.readFrom(objclazz, CLASS_OBJ, null, null, null,
+ new ByteArrayInputStream(outstr.toByteArray()));
+ assertEquals(obj1.toString(), obj2.toString());
+ }
+
+ public static class MyObject {
+ private int id;
+
+ public MyObject() {
+ super();
+ }
+
+ public MyObject(int id) {
+ this.id = id;
+ }
+
+ @Override
+ public String toString() {
+ return "MyObject [id=" + id + "]";
+ }
+ }
+
+}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java
index f15d68e9..1f1f117e 100644
--- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java
@@ -22,6 +22,8 @@
package org.onap.policy.common.endpoints.http.server.test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.security.KeyManagementException;
@@ -34,6 +36,7 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.junit.AfterClass;
+import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
@@ -53,7 +56,7 @@ public class HttpClientTest {
* @throws IOException can have an IO exception
*/
@BeforeClass
- public static void setUp() throws InterruptedException, IOException {
+ public static void setUpBeforeClass() throws InterruptedException, IOException {
/* echo server - http + no auth */
final HttpServletServer echoServerNoAuth =
@@ -113,10 +116,21 @@ public class HttpClientTest {
}
/**
+ * Clear https clients and reset providers.
+ */
+ @Before
+ public void setUp() {
+ HttpClient.factory.destroy();
+
+ MyGsonProvider.resetSome();
+ MyJacksonProvider.resetSome();
+ }
+
+ /**
* After the class is created method.
*/
@AfterClass
- public static void tearDown() {
+ public static void tearDownAfterClass() {
HttpServletServer.factory.destroy();
HttpClient.factory.destroy();
@@ -227,6 +241,44 @@ public class HttpClientTest {
}
@Test
+ public void testHttpPutAuthClient_JacksonProvider() throws Exception {
+ final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient")
+ .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667)
+ .basePath("junit/echo").userName("x").password("y").managed(true)
+ .serializationProvider(MyJacksonProvider.class.getCanonicalName()).build());
+
+ Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON);
+ final Response response = client.put("hello", entity, Collections.emptyMap());
+ final String body = HttpClient.getBody(response, String.class);
+
+ assertEquals(200, response.getStatus());
+ assertEquals("PUT:hello:{myParameter=myValue}", body);
+
+ assertTrue(MyJacksonProvider.hasWrittenSome());
+
+ assertFalse(MyGsonProvider.hasWrittenSome());
+ }
+
+ @Test
+ public void testHttpPutAuthClient_GsonProvider() throws Exception {
+ final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient")
+ .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667)
+ .basePath("junit/echo").userName("x").password("y").managed(true)
+ .serializationProvider(MyGsonProvider.class.getCanonicalName()).build());
+
+ Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON);
+ final Response response = client.put("hello", entity, Collections.emptyMap());
+ final String body = HttpClient.getBody(response, String.class);
+
+ assertEquals(200, response.getStatus());
+ assertEquals("PUT:hello:{myParameter=myValue}", body);
+
+ assertTrue(MyGsonProvider.hasWrittenSome());
+
+ assertFalse(MyJacksonProvider.hasWrittenSome());
+ }
+
+ @Test
public void testHttpAuthClient401() throws Exception {
final HttpClient client = getNoAuthHttpClient("testHttpAuthClient401", true,
6667);
@@ -321,16 +373,89 @@ public class HttpClientTest {
final HttpClient clientPdp = HttpClient.factory.get("PDP");
final Response response2 = clientPdp.get("test");
assertEquals(500, response2.getStatus());
+
+ assertFalse(MyJacksonProvider.hasWrittenSome());
+ assertFalse(MyGsonProvider.hasWrittenSome());
+ }
+
+ @Test
+ public void testHttpAuthClientProps_MixedProviders() throws Exception {
+ final Properties httpProperties = new Properties();
+
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "GSON,JACKSON");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "GSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "GSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "6666");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "GSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "junit/echo");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "GSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "GSON"
+ + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
+ httpProperties.setProperty(
+ PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "GSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
+ MyGsonProvider.class.getCanonicalName());
+
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "JACKSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "JACKSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "6666");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "JACKSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "junit/echo");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "JACKSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false");
+ httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "JACKSON"
+ + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
+ httpProperties.setProperty(
+ PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "JACKSON"
+ + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
+ MyJacksonProvider.class.getCanonicalName());
+
+ final List<HttpClient> clients = HttpClient.factory.build(httpProperties);
+ assertEquals(2, clients.size());
+
+ Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON);
+
+ // use gson client
+ MyGsonProvider.resetSome();
+ MyJacksonProvider.resetSome();
+ HttpClient client = HttpClient.factory.get("GSON");
+
+ Response response = client.put("hello", entity, Collections.emptyMap());
+ String body = HttpClient.getBody(response, String.class);
+
+ assertEquals(200, response.getStatus());
+ assertEquals("PUT:hello:{myParameter=myValue}", body);
+
+ assertTrue(MyGsonProvider.hasWrittenSome());
+ assertFalse(MyJacksonProvider.hasWrittenSome());
+
+ // use jackson client
+ MyGsonProvider.resetSome();
+ MyJacksonProvider.resetSome();
+ client = HttpClient.factory.get("JACKSON");
+
+ response = client.put("hello", entity, Collections.emptyMap());
+ body = HttpClient.getBody(response, String.class);
+
+ assertEquals(200, response.getStatus());
+ assertEquals("PUT:hello:{myParameter=myValue}", body);
+
+ assertTrue(MyJacksonProvider.hasWrittenSome());
+ assertFalse(MyGsonProvider.hasWrittenSome());
}
- private HttpClient getAuthHttpClient() throws KeyManagementException, NoSuchAlgorithmException {
+ private HttpClient getAuthHttpClient()
+ throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
return HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient")
.useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo")
.userName("x").password("y").managed(true).build());
}
private HttpClient getNoAuthHttpClient(String clientName, boolean https, int port)
- throws KeyManagementException, NoSuchAlgorithmException {
+ throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
return HttpClient.factory.build(BusTopicParams.builder().clientName(clientName)
.useHttps(https).allowSelfSignedCerts(https).hostname("localhost").port(port).basePath("junit/echo")
.userName(null).password(null).managed(true).build());
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 084847ce..3d80793e 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
@@ -2,14 +2,14 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 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.
@@ -24,14 +24,19 @@ 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
+import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
+import java.net.URLConnection;
import java.util.UUID;
-
+import org.apache.commons.io.IOUtils;
+import org.junit.AfterClass;
+import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.slf4j.Logger;
@@ -47,69 +52,251 @@ public class HttpServerTest {
*/
private static Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
+ private static final String LOCALHOST_PREFIX = "http://localhost:";
+
+ private static final Gson gson = new Gson();
+
+ /**
+ * Server port. Incremented by 10 with each test.
+ */
+ private static int port = 5608;
+
+ private String portUrl;
+
+ /**
+ * Increments the port number, clears the servers, and resets the providers.
+ */
+ @Before
+ public void setUp() {
+ port += 10;
+ portUrl = LOCALHOST_PREFIX + port;
+
+ HttpServletServer.factory.destroy();
+
+ MyJacksonProvider.resetSome();
+ MyGsonProvider.resetSome();
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() {
+ HttpServletServer.factory.destroy();
+ }
+
+ @Test
+ public void testDefaultPackageServer() throws Exception {
+ logger.info("-- testDefaultPackageServer() --");
+
+ HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", port, "/", false, true);
+ server.addServletPackage("/*", this.getClass().getPackage().getName());
+ server.addFilterClass("/*", TestFilter.class.getCanonicalName());
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+
+ RestEchoReqResp request = new RestEchoReqResp();
+ request.setRequestId(100);
+ request.setText("some text");
+ String reqText = gson.toJson(request);
+
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/full/request", reqText);
+ assertEquals(reqText, response);
+ }
+
+ @Test
+ public void testJacksonPackageServer() throws Exception {
+ logger.info("-- testJacksonPackageServer() --");
+
+ HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", port, "/", false, true);
+
+ server.setSerializationProvider(MyJacksonProvider.class.getCanonicalName());
+ server.addServletPackage("/*", this.getClass().getPackage().getName());
+ server.addFilterClass("/*", TestFilter.class.getCanonicalName());
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+
+ RestEchoReqResp request = new RestEchoReqResp();
+ request.setRequestId(100);
+ request.setText("some text");
+ String reqText = gson.toJson(request);
+
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/full/request", reqText);
+ assertEquals(reqText, response);
+
+ assertTrue(MyJacksonProvider.hasReadSome());
+ assertTrue(MyJacksonProvider.hasWrittenSome());
+
+ assertFalse(MyGsonProvider.hasReadSome());
+ assertFalse(MyGsonProvider.hasWrittenSome());
+ }
+
+ @Test
+ public void testGsonPackageServer() throws Exception {
+ logger.info("-- testGsonPackageServer() --");
+
+ HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", port, "/", false, true);
+
+ server.setSerializationProvider(MyGsonProvider.class.getCanonicalName());
+ server.addServletPackage("/*", this.getClass().getPackage().getName());
+ server.addFilterClass("/*", TestFilter.class.getCanonicalName());
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+
+ RestEchoReqResp request = new RestEchoReqResp();
+ request.setRequestId(100);
+ request.setText("some text");
+ String reqText = gson.toJson(request);
+
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/full/request", reqText);
+ assertEquals(reqText, response);
+
+ assertTrue(MyGsonProvider.hasReadSome());
+ assertTrue(MyGsonProvider.hasWrittenSome());
+
+ assertFalse(MyJacksonProvider.hasReadSome());
+ assertFalse(MyJacksonProvider.hasWrittenSome());
+ }
+
+ @Test
+ public void testDefaultClassServer() throws Exception {
+ logger.info("-- testDefaultClassServer() --");
+
+ HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", port, "/", false, true);
+ server.addServletClass("/*", RestEchoService.class.getCanonicalName());
+ server.addFilterClass("/*", TestFilter.class.getCanonicalName());
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+
+ RestEchoReqResp request = new RestEchoReqResp();
+ request.setRequestId(100);
+ request.setText("some text");
+ String reqText = gson.toJson(request);
+
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/full/request", reqText);
+ assertEquals(reqText, response);
+ }
+
+ @Test
+ public void testJacksonClassServer() throws Exception {
+ logger.info("-- testJacksonClassServer() --");
+
+ HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", port, "/", false, true);
+ server.setSerializationProvider(MyJacksonProvider.class.getCanonicalName());
+ server.addServletClass("/*", RestEchoService.class.getCanonicalName());
+ server.addFilterClass("/*", TestFilter.class.getCanonicalName());
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+
+ RestEchoReqResp request = new RestEchoReqResp();
+ request.setRequestId(100);
+ request.setText("some text");
+ String reqText = gson.toJson(request);
+
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/full/request", reqText);
+ assertEquals(reqText, response);
+
+ assertTrue(MyJacksonProvider.hasReadSome());
+ assertTrue(MyJacksonProvider.hasWrittenSome());
+
+ assertFalse(MyGsonProvider.hasReadSome());
+ assertFalse(MyGsonProvider.hasWrittenSome());
+ }
+
+ @Test
+ public void testGsonClassServer() throws Exception {
+ logger.info("-- testGsonClassServer() --");
+
+ HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", port, "/", false, true);
+ server.setSerializationProvider(MyGsonProvider.class.getCanonicalName());
+ server.addServletClass("/*", RestEchoService.class.getCanonicalName());
+ server.addFilterClass("/*", TestFilter.class.getCanonicalName());
+ server.waitedStart(5000);
+
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+
+ RestEchoReqResp request = new RestEchoReqResp();
+ request.setRequestId(100);
+ request.setText("some text");
+ String reqText = gson.toJson(request);
+
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/full/request", reqText);
+ assertEquals(reqText, response);
+
+ assertTrue(MyGsonProvider.hasReadSome());
+ assertTrue(MyGsonProvider.hasWrittenSome());
+
+ assertFalse(MyJacksonProvider.hasReadSome());
+ assertFalse(MyJacksonProvider.hasWrittenSome());
+ }
+
@Test
public void testSingleServer() throws Exception {
logger.info("-- testSingleServer() --");
- HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 5678, "/", false, true);
+ HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", port, "/", false, true);
server.addServletPackage("/*", this.getClass().getPackage().getName());
server.addFilterClass("/*", TestFilter.class.getCanonicalName());
server.waitedStart(5000);
- assertTrue(HttpServletServer.factory.get(5678).isAlive());
- assertFalse(HttpServletServer.factory.get(5678).isAaf());
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+ assertFalse(HttpServletServer.factory.get(port).isAaf());
- String response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/junit/echo/hello");
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/hello");
assertTrue("hello".equals(response));
response = null;
try {
- response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/swagger.json");
+ response = http(HttpServletServer.factory.get(port), portUrl + "/swagger.json");
} catch (IOException e) {
// Expected
}
assertTrue(response == null);
- response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/junit/echo/hello?block=true");
+ response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/hello?block=true");
assertEquals("FILTERED", response);
- assertTrue(HttpServletServer.factory.get(5678).isAlive());
- assertTrue(HttpServletServer.factory.inventory().size() == 1);
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+ assertEquals(1, HttpServletServer.factory.inventory().size());
server.setAafAuthentication("/*");
- assertTrue(HttpServletServer.factory.get(5678).isAaf());
+ assertTrue(HttpServletServer.factory.get(port).isAaf());
- HttpServletServer.factory.destroy(5678);
- assertTrue(HttpServletServer.factory.inventory().size() == 0);
+ HttpServletServer.factory.destroy(port);
+ assertEquals(0, HttpServletServer.factory.inventory().size());
}
@Test
public void testMultipleServers() throws Exception {
logger.info("-- testMultipleServers() --");
- HttpServletServer server1 = HttpServletServer.factory.build("echo-1", false,"localhost", 5688, "/", true, true);
+ HttpServletServer server1 = HttpServletServer.factory.build("echo-1", false,"localhost", port, "/", true, true);
server1.addServletPackage("/*", this.getClass().getPackage().getName());
server1.waitedStart(5000);
- HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", 5689, "/", false, true);
+ int port2 = port + 1;
+
+ HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", port2, "/", false, true);
server2.addServletPackage("/*", this.getClass().getPackage().getName());
server2.waitedStart(5000);
- assertTrue(HttpServletServer.factory.get(5688).isAlive());
- assertTrue(HttpServletServer.factory.get(5689).isAlive());
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
+ assertTrue(HttpServletServer.factory.get(port2).isAlive());
- String response = http(HttpServletServer.factory.get(5688), "http://localhost:5688/junit/echo/hello");
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/hello");
assertTrue("hello".equals(response));
- response = http(HttpServletServer.factory.get(5688), "http://localhost:5688/swagger.json");
+ response = http(HttpServletServer.factory.get(port), portUrl + "/swagger.json");
assertTrue(response != null);
- response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/junit/echo/hello");
+ response = http(HttpServletServer.factory.get(port2), LOCALHOST_PREFIX + port2 + "/junit/echo/hello");
assertTrue("hello".equals(response));
response = null;
try {
- response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/swagger.json");
+ response = http(HttpServletServer.factory.get(port2), LOCALHOST_PREFIX + port2 + "/swagger.json");
} catch (IOException e) {
// Expected
}
@@ -125,16 +312,16 @@ public class HttpServerTest {
String randomName = UUID.randomUUID().toString();
- HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5668, "/", false, true);
+ HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", port, "/", false, true);
server.addServletPackage("/*", this.getClass().getPackage().getName());
server.waitedStart(5000);
- assertTrue(HttpServletServer.factory.get(5668).isAlive());
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
- String response = http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/echo/hello");
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/hello");
assertTrue("hello".equals(response));
- response = http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/endpoints/http/servers");
+ response = http(HttpServletServer.factory.get(port), portUrl + "/junit/endpoints/http/servers");
assertTrue(response.contains(randomName));
HttpServletServer.factory.destroy();
@@ -146,13 +333,13 @@ public class HttpServerTest {
logger.info("-- testServiceClass() --");
String randomName = UUID.randomUUID().toString();
- HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5658, "/", false, true);
+ HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", port, "/", false, true);
server.addServletClass("/*", RestEchoService.class.getCanonicalName());
server.waitedStart(5000);
- assertTrue(HttpServletServer.factory.get(5658).isAlive());
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
- String response = http(HttpServletServer.factory.get(5658), "http://localhost:5658/junit/echo/hello");
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/hello");
assertTrue("hello".equals(response));
HttpServletServer.factory.destroy();
@@ -165,17 +352,17 @@ public class HttpServerTest {
String randomName = UUID.randomUUID().toString();
- HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5648, "/", false, true);
+ HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", port, "/", false, true);
server.addServletClass("/*", RestEchoService.class.getCanonicalName());
server.addServletClass("/*", RestEndpoints.class.getCanonicalName());
server.waitedStart(5000);
- assertTrue(HttpServletServer.factory.get(5648).isAlive());
+ assertTrue(HttpServletServer.factory.get(port).isAlive());
- String response = http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/echo/hello");
+ String response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/hello");
assertTrue("hello".equals(response));
- response = http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/endpoints/http/servers");
+ response = http(HttpServletServer.factory.get(port), portUrl + "/junit/endpoints/http/servers");
assertTrue(response.contains(randomName));
HttpServletServer.factory.destroy();
@@ -184,7 +371,7 @@ public class HttpServerTest {
/**
* performs an http request.
- *
+ *
* @throws MalformedURLException make sure URL is good
* @throws IOException thrown is IO exception occurs
* @throws InterruptedException thrown if thread interrupted occurs
@@ -197,7 +384,8 @@ public class HttpServerTest {
int maxNumberRetries = 5;
while (numRetries <= maxNumberRetries) {
try {
- response = response(url);
+ URLConnection conn = url.openConnection();
+ response = response(conn);
break;
} catch (ConnectException e) {
logger.warn("http server {} @ {} ({}) - cannot connect yet ..", server, urlString, numRetries, e);
@@ -212,15 +400,50 @@ public class HttpServerTest {
}
/**
+ * Performs an http request.
+ *
+ * @throws MalformedURLException make sure URL is good
+ * @throws IOException thrown is IO exception occurs
+ * @throws InterruptedException thrown if thread interrupted occurs
+ */
+ protected String http(HttpServletServer server, String urlString, String post)
+ throws MalformedURLException, IOException, InterruptedException {
+ URL url = new URL(urlString);
+ String response = null;
+ int numRetries = 1;
+ int maxNumberRetries = 5;
+ while (numRetries <= maxNumberRetries) {
+ try {
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod("POST");
+ conn.setDoOutput(true);
+ conn.setRequestProperty("Content-Type", "application/json");
+ IOUtils.write(post, conn.getOutputStream());
+ response = response(conn);
+ break;
+ } catch (ConnectException e) {
+ logger.warn("http server {} @ {} ({}) - cannot connect yet ..", server, urlString, numRetries, e);
+ numRetries++;
+ Thread.sleep(10000L);
+ } catch (Exception e) {
+ logger.error("http error", e);
+ throw e;
+ }
+ }
+
+ return response;
+ }
+
+ /**
* gets http response.
- *
- * @param url url
- *
+ *
+ * @param conn connection from which to read
+ *
* @throws IOException if an I/O error occurs
*/
- protected String response(URL url) throws IOException {
+ protected String response(URLConnection conn) throws IOException {
String response = "";
- try (BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
+ try (BufferedReader ioReader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
String line;
while ((line = ioReader.readLine()) != null) {
response += line;
@@ -229,6 +452,4 @@ public class HttpServerTest {
return response;
}
-
-
}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyGsonProvider.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyGsonProvider.java
new file mode 100644
index 00000000..037f6c6f
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyGsonProvider.java
@@ -0,0 +1,76 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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 java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import org.onap.policy.common.endpoints.http.server.internal.GsonMessageBodyHandler;
+
+/**
+ * GsonMessageBodyHandler that tracks activities.
+ */
+public class MyGsonProvider extends GsonMessageBodyHandler {
+ private static boolean readSome = false;
+ private static boolean wroteSome = false;
+
+ /**
+ * Constructs the object and resets the variables to indicate that no activity has
+ * occurred yet.
+ */
+ public MyGsonProvider() {
+ super();
+ }
+
+ @Override
+ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
+ MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
+
+ readSome = true;
+ return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
+ }
+
+ @Override
+ public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
+ MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
+
+ wroteSome = true;
+ super.writeTo(object, type, genericType, annotations, mediaType, httpHeaders, entityStream);
+ }
+
+ public static boolean hasReadSome() {
+ return readSome;
+ }
+
+ public static boolean hasWrittenSome() {
+ return wroteSome;
+ }
+
+ public static void resetSome() {
+ MyGsonProvider.readSome = false;
+ MyGsonProvider.wroteSome = false;
+ }
+
+}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyJacksonProvider.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyJacksonProvider.java
new file mode 100644
index 00000000..07062451
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/MyJacksonProvider.java
@@ -0,0 +1,76 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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 com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
+/**
+ * JacksonJsonProvider that tracks activities.
+ */
+public class MyJacksonProvider extends JacksonJsonProvider {
+ private static boolean readSome = false;
+ private static boolean wroteSome = false;
+
+ /**
+ * Constructs the object and resets the variables to indicate that no activity has
+ * occurred yet.
+ */
+ public MyJacksonProvider() {
+ super();
+ }
+
+ @Override
+ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
+ MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
+
+ readSome = true;
+ return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
+ }
+
+ @Override
+ public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
+ MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
+
+ wroteSome = true;
+ super.writeTo(object, type, genericType, annotations, mediaType, httpHeaders, entityStream);
+ }
+
+ public static boolean hasReadSome() {
+ return readSome;
+ }
+
+ public static boolean hasWrittenSome() {
+ return wroteSome;
+ }
+
+ public static void resetSome() {
+ MyJacksonProvider.readSome = false;
+ MyJacksonProvider.wroteSome = false;
+ }
+
+}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoReqResp.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoReqResp.java
new file mode 100644
index 00000000..5778eeeb
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoReqResp.java
@@ -0,0 +1,59 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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 com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * "ECHO" request and response supporting serialization and de-serialization via
+ * both jackson and gson.
+ */
+public class RestEchoReqResp {
+ @JsonProperty("reqId")
+ @SerializedName("reqId")
+ private int requestId;
+
+ @JsonProperty("textValue")
+ @SerializedName("textValue")
+ private String text;
+
+ public int getRequestId() {
+ return requestId;
+ }
+
+ public void setRequestId(int requestId) {
+ this.requestId = requestId;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ @Override
+ public String toString() {
+ return "RestEchoReqResp [requestId=" + requestId + ", text=" + text + "]";
+ }
+}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java
index 5d9b14d3..9db1053f 100644
--- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java
@@ -22,7 +22,6 @@ package org.onap.policy.common.endpoints.http.server.test;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
-
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
@@ -31,6 +30,8 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
@Api(value = "echo")
@@ -54,6 +55,14 @@ public class RestEchoService {
}
@POST
+ @Path("/full/request")
+ @Produces(MediaType.APPLICATION_JSON)
+ @ApiOperation(value = "echoes back the request structure", response = RestEchoReqResp.class)
+ public Response echoFullyPost(RestEchoReqResp reqResp) {
+ return Response.status(Status.OK).entity(reqResp).build();
+ }
+
+ @POST
@Path("{word}")
@Produces(MediaType.TEXT_PLAIN)
@ApiOperation(value = "echoes back whatever received")