aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/test
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-06-13 12:34:17 -0400
committerJim Hahn <jrh3@att.com>2019-06-13 16:10:38 -0400
commitfe5d78724f723a451ddc0d7cc41d6fc60092b314 (patch)
tree9913cc94014b3e99774e9ec5e39a7211046a765a /policy-endpoints/src/test
parentea262e6da52fd4da0733f02998f87aebaf502ddb (diff)
More sonar fixes in policy/common
Note: this does not increase code coverage, but should fix other code issues. Resolved cyclomatic complexity issue in ParameterValidationResult. Refactored duplicate code in GroupValidationResult. Removed IOException from NetworkUtil "throws". Replaced null/empty string tests with StringUtils.isBlank(). Added @FunctionalInterface where needed. Replaced anonymous classes with lambda expressions. Replaced duplicate strings with a constant. Added private constructors for utility classes. Removed sleep() from tests. Removed unused parameter from method call. Made some protected methods private. Compute integrity monitor's state-transition table once. Use for-loop instead of iterator. Moved constructors. Fixed some checkstyle issues (tabs => spaces, trailing spaces). Change-Id: I9a962ca45c4ff3f212c6014da799d06f07b232ef Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-endpoints/src/test')
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java94
1 files changed, 33 insertions, 61 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 3ae39ebd..34e35a2e 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
@@ -28,7 +28,6 @@ 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;
@@ -40,6 +39,7 @@ import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.utils.gson.GsonTestUtils;
+import org.onap.policy.common.utils.network.NetworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -105,7 +105,7 @@ public class HttpServerTest {
request.setText(SOME_TEXT);
String reqText = gson.toJson(request);
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
+ String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
assertEquals(reqText, response);
}
@@ -127,7 +127,7 @@ public class HttpServerTest {
request.setText(SOME_TEXT);
String reqText = gson.toJson(request);
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
+ String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
assertEquals(reqText, response);
assertTrue(MyJacksonProvider.hasReadSome());
@@ -155,7 +155,7 @@ public class HttpServerTest {
request.setText(SOME_TEXT);
String reqText = gson.toJson(request);
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
+ String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
assertEquals(reqText, response);
assertTrue(MyGsonProvider.hasReadSome());
@@ -181,7 +181,7 @@ public class HttpServerTest {
request.setText(SOME_TEXT);
String reqText = gson.toJson(request);
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
+ String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
assertEquals(reqText, response);
}
@@ -202,7 +202,7 @@ public class HttpServerTest {
request.setText(SOME_TEXT);
String reqText = gson.toJson(request);
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
+ String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
assertEquals(reqText, response);
assertTrue(MyJacksonProvider.hasReadSome());
@@ -229,7 +229,7 @@ public class HttpServerTest {
request.setText(SOME_TEXT);
String reqText = gson.toJson(request);
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
+ String response = http(portUrl + JUNIT_ECHO_FULL_REQUEST, reqText);
assertEquals(reqText, response);
assertTrue(MyGsonProvider.hasReadSome());
@@ -261,18 +261,18 @@ public class HttpServerTest {
assertTrue(HttpServletServer.factory.get(port).isAlive());
assertFalse(HttpServletServer.factory.get(port).isAaf());
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_HELLO);
+ String response = http(portUrl + JUNIT_ECHO_HELLO);
assertEquals(HELLO, response);
response = null;
try {
- response = http(HttpServletServer.factory.get(port), portUrl + SWAGGER_JSON);
+ response = http(portUrl + SWAGGER_JSON);
} catch (IOException e) {
// Expected
}
assertTrue(response == null);
- response = http(HttpServletServer.factory.get(port), portUrl + "/junit/echo/hello?block=true");
+ response = http(portUrl + "/junit/echo/hello?block=true");
assertEquals("FILTERED", response);
assertTrue(HttpServletServer.factory.get(port).isAlive());
@@ -302,18 +302,18 @@ public class HttpServerTest {
assertTrue(HttpServletServer.factory.get(port).isAlive());
assertTrue(HttpServletServer.factory.get(port2).isAlive());
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_HELLO);
+ String response = http(portUrl + JUNIT_ECHO_HELLO);
assertTrue(HELLO.equals(response));
- response = http(HttpServletServer.factory.get(port), portUrl + SWAGGER_JSON);
+ response = http(portUrl + SWAGGER_JSON);
assertTrue(response != null);
- response = http(HttpServletServer.factory.get(port2), LOCALHOST_PREFIX + port2 + JUNIT_ECHO_HELLO);
+ response = http(LOCALHOST_PREFIX + port2 + JUNIT_ECHO_HELLO);
assertTrue(HELLO.equals(response));
response = null;
try {
- response = http(HttpServletServer.factory.get(port2), LOCALHOST_PREFIX + port2 + SWAGGER_JSON);
+ response = http(LOCALHOST_PREFIX + port2 + SWAGGER_JSON);
} catch (IOException e) {
// Expected
}
@@ -335,10 +335,10 @@ public class HttpServerTest {
assertTrue(HttpServletServer.factory.get(port).isAlive());
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_HELLO);
+ String response = http(portUrl + JUNIT_ECHO_HELLO);
assertTrue(HELLO.equals(response));
- response = http(HttpServletServer.factory.get(port), portUrl + "/junit/endpoints/http/servers");
+ response = http(portUrl + "/junit/endpoints/http/servers");
assertTrue(response.contains(randomName));
HttpServletServer.factory.destroy();
@@ -356,7 +356,7 @@ public class HttpServerTest {
assertTrue(HttpServletServer.factory.get(port).isAlive());
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_HELLO);
+ String response = http(portUrl + JUNIT_ECHO_HELLO);
assertTrue(HELLO.equals(response));
HttpServletServer.factory.destroy();
@@ -376,10 +376,10 @@ public class HttpServerTest {
assertTrue(HttpServletServer.factory.get(port).isAlive());
- String response = http(HttpServletServer.factory.get(port), portUrl + JUNIT_ECHO_HELLO);
+ String response = http(portUrl + JUNIT_ECHO_HELLO);
assertTrue(HELLO.equals(response));
- response = http(HttpServletServer.factory.get(port), portUrl + "/junit/endpoints/http/servers");
+ response = http(portUrl + "/junit/endpoints/http/servers");
assertTrue(response.contains(randomName));
HttpServletServer.factory.destroy();
@@ -393,27 +393,13 @@ public class HttpServerTest {
* @throws IOException thrown is IO exception occurs
* @throws InterruptedException thrown if thread interrupted occurs
*/
- protected String http(HttpServletServer server, String urlString)
+ private String http(String urlString)
throws IOException, InterruptedException {
URL url = new URL(urlString);
- String response = null;
- int numRetries = 1;
- int maxNumberRetries = 5;
- while (numRetries <= maxNumberRetries) {
- try {
- URLConnection conn = url.openConnection();
- 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) {
- throw e;
- }
+ if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 2)) {
+ throw new IllegalStateException("port never opened: " + url);
}
-
- return response;
+ return response(url.openConnection());
}
/**
@@ -423,32 +409,18 @@ public class HttpServerTest {
* @throws IOException thrown is IO exception occurs
* @throws InterruptedException thrown if thread interrupted occurs
*/
- protected String http(HttpServletServer server, String urlString, String post)
+ private String http(String urlString, String post)
throws 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;
- }
+ if (!NetworkUtil.isTcpPortOpen(url.getHost(), url.getPort(), 25, 2)) {
+ throw new IllegalStateException("port never opened: " + url);
}
-
- return response;
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod("POST");
+ conn.setDoOutput(true);
+ conn.setRequestProperty("Content-Type", "application/json");
+ IOUtils.write(post, conn.getOutputStream());
+ return response(conn);
}
/**
@@ -458,7 +430,7 @@ public class HttpServerTest {
*
* @throws IOException if an I/O error occurs
*/
- protected String response(URLConnection conn) throws IOException {
+ private String response(URLConnection conn) throws IOException {
StringBuilder response = new StringBuilder();
try (BufferedReader ioReader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
String line;