summaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src
diff options
context:
space:
mode:
authorefiacor <fiachra.corcoran@est.tech>2019-07-25 11:05:54 +0000
committerefiacor <fiachra.corcoran@est.tech>2019-07-25 11:05:54 +0000
commitf09604a09059d6c3ecf05b253c9058c1a2e81ca2 (patch)
treead5c6e1abffdc7cf30404df8af8b9c11f8d71a0e /datarouter-prov/src
parentf20778ffa99aa9c6f30a0f84112a5392b259ea63 (diff)
FINAL unit test covergae commit
Change-Id: Ieb9e9d49931c253b89026ed4884edbdc8d6c6024 Issue-ID: DMAAP-1226 Signed-off-by: efiacor <fiachra.corcoran@est.tech>
Diffstat (limited to 'datarouter-prov/src')
-rwxr-xr-xdatarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java38
-rw-r--r--datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/ProxyServletTest.java184
2 files changed, 205 insertions, 17 deletions
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java
index 75423602..e47f9fbb 100755
--- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java
+++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java
@@ -24,6 +24,8 @@
package org.onap.dmaap.datarouter.provisioning;
+import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;
+
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -54,8 +56,6 @@ import org.apache.http.impl.client.DefaultHttpClient;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
-import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;
-
/**
* This class is the base class for those servlets that need to proxy their requests from the standby to active server.
* Its methods perform the proxy function to the active server. If the active server is not reachable, a 503
@@ -96,7 +96,7 @@ public class ProxyServlet extends BaseServlet {
// We are connecting with the node name, but the certificate will have the CNAME
// So we need to accept a non-matching certificate name
SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore,
- props.getProperty(Main.KEYSTORE_PASS_PROPERTY), trustStore);
+ props.getProperty(Main.KEYSTORE_PASS_PROPERTY), trustStore);
socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
sch = new Scheme("https", 443, socketFactory);
inited = true;
@@ -125,7 +125,7 @@ public class ProxyServlet extends BaseServlet {
* @param req the HTTP request
* @return true or false
*/
- protected boolean isProxyOK(final HttpServletRequest req) {
+ boolean isProxyOK(final HttpServletRequest req) {
String t = req.getQueryString();
if (t != null) {
t = t.replaceAll("&amp;", "&");
@@ -144,7 +144,7 @@ public class ProxyServlet extends BaseServlet {
*
* @return true if this server is the standby (and hence a proxy server).
*/
- public boolean isProxyServer() {
+ boolean isProxyServer() {
SynchronizerTask st = SynchronizerTask.getSynchronizer();
return st.getPodState() == SynchronizerTask.STANDBY_POD;
}
@@ -187,7 +187,7 @@ public class ProxyServlet extends BaseServlet {
*
* @return true if the proxy succeeded
*/
- public boolean doGetWithFallback(HttpServletRequest req, HttpServletResponse resp) {
+ boolean doGetWithFallback(HttpServletRequest req, HttpServletResponse resp) {
boolean rv = false;
if (inited) {
String url = buildUrl(req);
@@ -234,12 +234,8 @@ public class ProxyServlet extends BaseServlet {
// Copy request headers and request body
copyRequestHeaders(req, proxy);
- if ("POST".equals(method) || "PUT".equals(method)) {
- BasicHttpEntity body = new BasicHttpEntity();
- body.setContent(req.getInputStream());
- body.setContentLength(-1); // -1 = unknown
- proxy.setEntity(body);
- }
+
+ handlePutOrPost(req, method, proxy);
// Execute the request
HttpResponse pxyResponse = httpclient.execute(proxy);
@@ -263,6 +259,15 @@ public class ProxyServlet extends BaseServlet {
}
}
+ private void handlePutOrPost(HttpServletRequest req, String method, ProxyHttpRequest proxy) throws IOException {
+ if ("POST".equals(method) || "PUT".equals(method)) {
+ BasicHttpEntity body = new BasicHttpEntity();
+ body.setContent(req.getInputStream());
+ body.setContentLength(-1); // -1 = unknown
+ proxy.setEntity(body);
+ }
+ }
+
private String buildUrl(HttpServletRequest req) {
StringBuilder sb = new StringBuilder("https://");
sb.append(URLUtilities.getPeerPodName());
@@ -275,7 +280,6 @@ public class ProxyServlet extends BaseServlet {
}
private void copyRequestHeaders(HttpServletRequest from, HttpRequestBase to) {
- @SuppressWarnings("unchecked")
List<String> list = Collections.list(from.getHeaderNames());
for (String name : list) {
// Proxy code will add this one
@@ -285,7 +289,7 @@ public class ProxyServlet extends BaseServlet {
}
}
- private void copyResponseHeaders(HttpResponse from, HttpServletResponse to) {
+ void copyResponseHeaders(HttpResponse from, HttpServletResponse to) {
for (Header hdr : from.getAllHeaders()) {
// Don't copy Date: our Jetty will add another Date header
if (!"Date".equals(hdr.getName())) {
@@ -294,7 +298,7 @@ public class ProxyServlet extends BaseServlet {
}
}
- private void copyEntityContent(HttpResponse pxyResponse, HttpServletResponse resp) {
+ void copyEntityContent(HttpResponse pxyResponse, HttpServletResponse resp) {
HttpEntity entity = pxyResponse.getEntity();
if (entity != null) {
try (InputStream in = entity.getContent()) {
@@ -305,11 +309,11 @@ public class ProxyServlet extends BaseServlet {
}
}
- public class ProxyHttpRequest extends HttpEntityEnclosingRequestBase {
+ public static class ProxyHttpRequest extends HttpEntityEnclosingRequestBase {
private final String method;
- public ProxyHttpRequest(final String method, final String uri) {
+ ProxyHttpRequest(final String method, final String uri) {
super();
this.method = method;
setURI(URI.create(uri));
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/ProxyServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/ProxyServletTest.java
new file mode 100644
index 00000000..a0442c13
--- /dev/null
+++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/ProxyServletTest.java
@@ -0,0 +1,184 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dmaap.datarouter.provisioning;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.powermock.api.mockito.PowerMockito.when;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import javax.servlet.ServletConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicHeader;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PowerMockIgnore("javax.net.ssl.*")
+@PrepareForTest({URLUtilities.class, BaseServlet.class, Scheme.class})
+public class ProxyServletTest {
+ private static ProxyServlet proxyServlet;
+ private static EntityManagerFactory emf;
+ private static EntityManager em;
+
+ @Mock
+ private DefaultHttpClient httpClient;
+ @Mock
+ private HttpServletRequest request;
+ @Mock
+ private HttpServletResponse servletResponse;
+ @Mock
+ private CloseableHttpResponse response;
+ @Mock
+ private StatusLine statusLine;
+
+ @BeforeClass
+ public static void init() {
+ emf = Persistence.createEntityManagerFactory("dr-unit-tests");
+ em = emf.createEntityManager();
+ System.setProperty(
+ "org.onap.dmaap.datarouter.provserver.properties",
+ "src/test/resources/h2Database.properties");
+ }
+
+ @AfterClass
+ public static void tearDownClass() {
+ em.clear();
+ em.close();
+ emf.close();
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ proxyServlet = new ProxyServlet();
+ FieldUtils.writeDeclaredField(proxyServlet, "inited", true, true);
+
+ org.apache.http.conn.ssl.SSLSocketFactory sslSocketFactory = PowerMockito.mock(org.apache.http.conn.ssl.SSLSocketFactory.class);
+ Mockito.doNothing().when(sslSocketFactory).setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+ Scheme scheme = new Scheme("http", 80, sslSocketFactory);
+ FieldUtils.writeDeclaredField(proxyServlet, "sch", scheme, true);
+
+ PowerMockito.mockStatic(BaseServlet.class);
+ PowerMockito.mockStatic(URLUtilities.class);
+ when(URLUtilities.getPeerPodName()).thenReturn("localhost");
+
+ SynchronizerTask synchronizerTask = SynchronizerTask.getSynchronizer();
+ FieldUtils.writeDeclaredField(synchronizerTask, "podState", 2, true);
+ FieldUtils.writeDeclaredStaticField(BaseServlet.class, "synctask", synchronizerTask, true);
+
+ proxyServlet.init(mock(ServletConfig.class));
+ setHeadersForValidRequest();
+ mockHttpClientForGetRequest();
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_POST_Exception_Is_Thrown() {
+ proxyServlet.doPost(request, servletResponse);
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_DELETE_Exception_Is_Thrown() {
+ proxyServlet.doDelete(request, servletResponse);
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_GET_Exception_Is_Thrown() {
+ proxyServlet.doGet(request, servletResponse);
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_PUT_Exception_Is_Thrown() {
+ proxyServlet.doPut(request, servletResponse);
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_GetWithFallBack_Exception_Is_Thrown() {
+ proxyServlet.doGetWithFallback(request, servletResponse);
+ }
+
+ @Test
+ public void Veirfy_Headers_Are_Copied() {
+ HttpResponse proxyResponse = PowerMockito.mock(HttpResponse.class);
+ Header[] headersArray = new Header[2];
+ headersArray[0] = new BasicHeader("cache-control", "public, max-age=86400");
+ headersArray[1] = new BasicHeader("content-type", "text/plain");
+ PowerMockito.when(proxyResponse.getAllHeaders()).thenReturn(headersArray);
+ proxyServlet.copyResponseHeaders(proxyResponse, servletResponse);
+ }
+
+ @Test
+ public void Veirfy_EntitiyContent_Copied() throws IOException {
+ HttpResponse proxyResponse = PowerMockito.mock(HttpResponse.class);
+ HttpEntity entity = PowerMockito.mock(HttpEntity.class);
+ PowerMockito.when(proxyResponse.getEntity()).thenReturn(entity);
+ PowerMockito.when(entity.getContent()).thenReturn(new ByteArrayInputStream("blah".getBytes()));
+ proxyServlet.copyEntityContent(proxyResponse, servletResponse);
+ }
+
+ private void setHeadersForValidRequest() {
+ List<String> headers = new ArrayList<>();
+ headers.add("Content-Type");
+ headers.add("X-DMAAP-DR-ON-BEHALF-OF");
+ headers.add("X-DMAAP-DR-META");
+ Enumeration<String> headerNames = Collections.enumeration(headers);
+ Mockito.when(request.getHeaderNames()).thenReturn(headerNames);
+ Enumeration<String> contentTypeHeader = Collections.enumeration(Collections.singletonList("text/plain"));
+ Enumeration<String> behalfHeader = Collections.enumeration(Collections.singletonList("User1"));
+ Enumeration<String> metaDataHeader = Collections.enumeration(Collections.singletonList("?#@><"));
+ Mockito.when(request.getHeaders("Content-Type")).thenReturn(contentTypeHeader);
+ Mockito.when(request.getHeaders("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn(behalfHeader);
+ Mockito.when(request.getHeaders("X-DMAAP-DR-META")).thenReturn(metaDataHeader);
+ }
+
+ private void mockHttpClientForGetRequest() throws Exception {
+ PowerMockito.when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
+ Mockito.when(request.getRequestURI()).thenReturn("/api");
+ Mockito.when(response.getStatusLine()).thenReturn(statusLine);
+ }
+}