summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJorge Hernandez <jorge.hernandez-herrero@att.com>2019-01-15 13:28:16 -0600
committerJorge Hernandez <jorge.hernandez-herrero@att.com>2019-01-16 09:36:37 -0600
commit9f15e1987dc9fc6bb8196c415b817b3f1cf6bd5f (patch)
tree0aab018cb4a9186f7ae2efc2a88ba3200c58f6d4
parent2a191bf0d0a5cb3beb436a025d2f4efa419bb354 (diff)
Add post and delete http client methods
Additional clean up for sonars have also been added for the classes that have been modified Issue-ID: POLICY-1367 Change-Id: Ie97d9057273e89850420a7c1b5b2d275709bdfd0 Signed-off-by: Jorge Hernandez <jorge.hernandez-herrero@att.com>
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java156
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java17
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java2
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java32
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java114
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java23
6 files changed, 246 insertions, 98 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java
index 72847b18..2e3b9afb 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * policy-endpoints
+ * 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.
@@ -27,36 +27,136 @@ import javax.ws.rs.core.Response;
import org.onap.policy.common.capabilities.Startable;
+/**
+ * Http Client interface.
+ */
public interface HttpClient extends Startable {
- public static final HttpClientFactory factory = new IndexedHttpClientFactory();
-
- public Response get(String path);
-
- public Response get();
-
- public Response put(String path, Entity<?> entity, Map<String, Object> headers);
-
- public static <T> T getBody(Response response, Class<T> entityType) {
+ /**
+ * Factory.
+ */
+ HttpClientFactory factory = new IndexedHttpClientFactory();
+
+ /**
+ * GET request.
+ *
+ * @param path context uri path.
+ * @return response
+ */
+ Response get(String path);
+
+ /**
+ * GET request.
+ *
+ * @return response
+ */
+ Response get();
+
+ /**
+ * PUT request.
+ *
+ * @param path context uri path
+ * @param entity body
+ * @param headers headers
+ *
+ * @return response.
+ */
+ Response put(String path, Entity<?> entity, Map<String, Object> headers);
+
+ /**
+ * POST request.
+ *
+ * @param path context uri path
+ * @param entity body
+ * @param headers headers
+ *
+ * @return response.
+ */
+ Response post(String path, Entity<?> entity, Map<String, Object> headers);
+
+ /**
+ * DELETE request.
+ *
+ * @param path context uri path
+ * @param headers headers
+ *
+ * @return response.
+ */
+ Response delete(String path, Map<String, Object> headers);
+
+ /**
+ * Retrieve the body from the HTTP transaction.
+ *
+ * @param response response.
+ * @param entityType body type.
+ * @param <T> body class.
+ *
+ * @return response.
+ */
+ static <T> T getBody(Response response, Class<T> entityType) {
return response.readEntity(entityType);
}
- public String getName();
-
- public boolean isHttps();
-
- public boolean isSelfSignedCerts();
-
- public String getHostname();
-
- public int getPort();
-
- public String getBasePath();
-
- public String getUserName();
-
- public String getPassword();
-
- public String getBaseUrl();
+ /**
+ * Get the client name.
+ * @return name
+ */
+ String getName();
+
+ /**
+ * HTTPS support.
+ *
+ * @return if the client uses https
+ */
+ boolean isHttps();
+
+ /**
+ * Self-signed certificates.
+ *
+ * @return if the self-signed certificates are allowed
+ */
+ boolean isSelfSignedCerts();
+
+ /**
+ * Get the host name.
+ *
+ * @return host name
+ */
+ String getHostname();
+
+ /**
+ * Get the port.
+ *
+ * @return port
+ */
+ int getPort();
+
+ /**
+ * Get the base path.
+ *
+ * @return base path
+ */
+ String getBasePath();
+
+ /**
+ * Get the user name.
+ *
+ * @return the user name
+ */
+ String getUserName();
+
+ /**
+ * Get the password.
+ *
+ * @return the password
+ */
+ String getPassword();
+
+ /**
+ * Get the base URL.
+ *
+ * @return the base URL
+ */
+ String getBaseUrl();
}
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java
index 133dfaea..f482eb01 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java
@@ -1,6 +1,6 @@
/*-
* ============LICENSE_START=======================================================
- * policy-endpoints
+ * ONAP
* ================================================================================
* Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
@@ -35,13 +35,13 @@ public interface HttpClientFactory {
/**
* Build and http client with the following parameters.
*/
- public HttpClient build(BusTopicParams busTopicParams)
+ HttpClient build(BusTopicParams busTopicParams)
throws KeyManagementException, NoSuchAlgorithmException;
/**
* Build http client from properties.
*/
- public List<HttpClient> build(Properties properties) throws KeyManagementException, NoSuchAlgorithmException;
+ List<HttpClient> build(Properties properties) throws KeyManagementException, NoSuchAlgorithmException;
/**
* Get http client.
@@ -49,21 +49,24 @@ public interface HttpClientFactory {
* @param name the name
* @return the http client
*/
- public HttpClient get(String name);
+ HttpClient get(String name);
/**
* List of http clients.
*
* @return http clients
*/
- public List<HttpClient> inventory();
+ List<HttpClient> inventory();
/**
* Destroy by name.
*
* @param name name
*/
- public void destroy(String name);
+ void destroy(String name);
- public void destroy();
+ /**
+ * destroy the factory.
+ */
+ void destroy();
}
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java
index d4d4a281..9aef09e4 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java
@@ -1,6 +1,6 @@
/*
* ============LICENSE_START=======================================================
- * ONAP Policy Engine - Common Modules
+ * ONAP
* ================================================================================
* Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java
index 94635615..b55a7bb2 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java
@@ -1,8 +1,8 @@
/*
* ============LICENSE_START=======================================================
- * policy-endpoints
+ * ONAP
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -46,6 +46,9 @@ import org.onap.policy.common.endpoints.http.client.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * Http Client implementation using a Jersey Client.
+ */
public class JerseyClient implements HttpClient {
/**
@@ -164,11 +167,17 @@ public class JerseyClient implements HttpClient {
@Override
public Response put(String path, Entity<?> entity, Map<String, Object> headers) {
- Builder builder = this.client.target(this.baseUrl).path(path).request();
- for (Entry<String, Object> header : headers.entrySet()) {
- builder.header(header.getKey(), header.getValue());
- }
- return builder.put(entity);
+ return getBuilder(path, headers).put(entity);
+ }
+
+ @Override
+ public Response post(String path, Entity<?> entity, Map<String, Object> headers) {
+ return getBuilder(path, headers).post(entity);
+ }
+
+ @Override
+ public Response delete(String path, Map<String, Object> headers) {
+ return getBuilder(path, headers).delete();
}
@Override
@@ -274,4 +283,13 @@ public class JerseyClient implements HttpClient {
return builder.toString();
}
+ private Builder getBuilder(String path, Map<String, Object> headers) {
+ Builder builder = this.client.target(this.baseUrl).path(path).request();
+ for (Entry<String, Object> header : headers.entrySet()) {
+ builder.header(header.getKey(), header.getValue());
+ }
+ return builder;
+ }
+
+
}
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 c5204ae3..f15d68e9 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
@@ -2,7 +2,7 @@
* ============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.
* Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,18 +22,17 @@
package org.onap.policy.common.endpoints.http.server.test;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
-
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.BeforeClass;
import org.junit.Test;
@@ -43,12 +42,8 @@ import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.onap.policy.common.utils.network.NetworkUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
public class HttpClientTest {
- private static final Logger logger = LoggerFactory.getLogger(HttpClientTest.class);
-
private static final HashMap<String, String> savedValuesMap = new HashMap<>();
/**
@@ -59,8 +54,6 @@ public class HttpClientTest {
*/
@BeforeClass
public static void setUp() throws InterruptedException, IOException {
- logger.info("-- setup() --");
-
/* echo server - http + no auth */
final HttpServletServer echoServerNoAuth =
@@ -124,8 +117,6 @@ public class HttpClientTest {
*/
@AfterClass
public static void tearDown() {
- logger.info("-- tearDown() --");
-
HttpServletServer.factory.destroy();
HttpClient.factory.destroy();
@@ -166,80 +157,85 @@ public class HttpClientTest {
@Test
public void testHttpGetNoAuthClient() throws Exception {
- logger.info("-- testHttpNoAuthClient() --");
-
- final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpNoAuthClient")
- .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6666).basePath("junit/echo")
- .userName(null).password(null).managed(true).build());
+ final HttpClient client = getNoAuthHttpClient("testHttpNoAuthClient", false,
+ 6666);
final Response response = client.get("hello");
final String body = HttpClient.getBody(response, String.class);
- assertTrue(response.getStatus() == 200);
- assertTrue(body.equals("hello"));
+ assertEquals(200, response.getStatus());
+ assertEquals("hello", body);
}
@Test
public void testHttpPutNoAuthClient() throws Exception {
- logger.info("-- testHttpNoAuthClient() --");
-
- final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpNoAuthClient")
- .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6666).basePath("junit/echo")
- .userName(null).password(null).managed(true).build());
+ final HttpClient client = getNoAuthHttpClient("testHttpNoAuthClient", false, 6666);
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);
- assertTrue(response.getStatus() == 200);
- assertTrue(body.equals("hello:{myParameter=myValue}"));
+ assertEquals(200, response.getStatus());
+ assertEquals("PUT:hello:{myParameter=myValue}", body);
}
@Test
- public void testHttpGetAuthClient() throws Exception {
- logger.info("-- testHttpAuthClient() --");
+ public void testHttpPostNoAuthClient() throws Exception {
+ final HttpClient client = getNoAuthHttpClient("testHttpNoAuthClient", false,
+ 6666);
- 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).build());
+ Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON);
+ final Response response = client.post("hello", entity, Collections.emptyMap());
+ final String body = HttpClient.getBody(response, String.class);
+
+ assertEquals(200, response.getStatus());
+ assertEquals("POST:hello:{myParameter=myValue}", body);
+ }
+
+ @Test
+ public void testHttpDeletetNoAuthClient() throws Exception {
+ final HttpClient client = getNoAuthHttpClient("testHttpNoAuthClient", false,
+ 6666);
+
+ final Response response = client.delete("hello", Collections.emptyMap());
+ final String body = HttpClient.getBody(response, String.class);
+
+ assertEquals(200, response.getStatus());
+ assertEquals("DELETE:hello", body);
+ }
+
+ @Test
+ public void testHttpGetAuthClient() throws Exception {
+ final HttpClient client = getAuthHttpClient();
final Response response = client.get("hello");
final String body = HttpClient.getBody(response, String.class);
- assertTrue(response.getStatus() == 200);
- assertTrue(body.equals("hello"));
+ assertEquals(200, response.getStatus());
+ assertEquals("hello", body);
}
@Test
public void testHttpPutAuthClient() throws Exception {
- logger.info("-- testHttpAuthClient() --");
-
- 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).build());
+ final HttpClient client = getAuthHttpClient();
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);
- assertTrue(response.getStatus() == 200);
- assertTrue(body.equals("hello:{myParameter=myValue}"));
+ assertEquals(200, response.getStatus());
+ assertEquals("PUT:hello:{myParameter=myValue}", body);
}
@Test
public void testHttpAuthClient401() throws Exception {
- logger.info("-- testHttpAuthClient401() --");
-
- final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient401")
- .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo")
- .userName(null).password(null).managed(true).build());
+ final HttpClient client = getNoAuthHttpClient("testHttpAuthClient401", true,
+ 6667);
final Response response = client.get("hello");
- assertEquals(new Integer(response.getStatus()).toString(), response.getStatus(), 401);
+ assertEquals(401, response.getStatus());
}
@Test
public void testHttpAuthClientProps() throws Exception {
- logger.info("-- testHttpAuthClientProps() --");
-
final Properties httpProperties = new Properties();
httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "PAP,PDP");
@@ -309,10 +305,10 @@ public class HttpClientTest {
+ PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
final List<HttpServletServer> servers = HttpServletServer.factory.build(httpProperties);
- assertTrue(servers.size() == 2);
+ assertEquals(2, servers.size());
final List<HttpClient> clients = HttpClient.factory.build(httpProperties);
- assertTrue(clients.size() == 2);
+ assertEquals(2, clients.size());
for (final HttpServletServer server : servers) {
server.waitedStart(10000);
@@ -320,13 +316,27 @@ public class HttpClientTest {
final HttpClient clientPap = HttpClient.factory.get("PAP");
final Response response = clientPap.get();
- assertTrue(response.getStatus() == 200);
+ assertEquals(200, response.getStatus());
final HttpClient clientPdp = HttpClient.factory.get("PDP");
final Response response2 = clientPdp.get("test");
- assertTrue(response2.getStatus() == 500);
+ assertEquals(500, response2.getStatus());
+ }
+
+ private HttpClient getAuthHttpClient() throws KeyManagementException, NoSuchAlgorithmException {
+ 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 {
+ 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());
+ }
+
+
class MyEntity {
private String myParameter;
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 56ed8933..5d9b14d3 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
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * policy-endpoints
+ * ONAP
* ================================================================================
- * Copyright (C) 2017 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.
@@ -23,7 +23,9 @@ 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;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@@ -48,7 +50,22 @@ public class RestEchoService {
@Produces(MediaType.TEXT_PLAIN)
@ApiOperation(value = "echoes back whatever received")
public String echoPut(@PathParam("word") String word, Object entity) {
- return word + ":" + entity.toString();
+ return "PUT:" + word + ":" + entity.toString();
}
+ @POST
+ @Path("{word}")
+ @Produces(MediaType.TEXT_PLAIN)
+ @ApiOperation(value = "echoes back whatever received")
+ public String echoPost(@PathParam("word") String word, Object entity) {
+ return "POST:" + word + ":" + entity.toString();
+ }
+
+ @DELETE
+ @Path("{word}")
+ @Produces(MediaType.TEXT_PLAIN)
+ @ApiOperation(value = "echoes back whatever received")
+ public String echoDelete(@PathParam("word") String word) {
+ return "DELETE:" + word;
+ }
}