1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
23 package org.onap.dmaap.datarouter.node;
25 import org.apache.commons.lang3.reflect.FieldUtils;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.powermock.api.mockito.PowerMockito;
31 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
32 import org.powermock.modules.junit4.PowerMockRunner;
34 import javax.servlet.ServletOutputStream;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
40 import static org.hamcrest.Matchers.notNullValue;
41 import static org.mockito.Matchers.eq;
42 import static org.mockito.Mockito.*;
44 @RunWith(PowerMockRunner.class)
45 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
46 public class NodeServletTest {
48 private NodeServlet nodeServlet;
51 private HttpServletRequest request;
54 private HttpServletResponse response;
57 public void setUp() throws Exception{
58 nodeServlet = new NodeServlet();
59 setBehalfHeader("Stub_Value");
60 when(request.getPathInfo()).thenReturn("2");
61 when(request.isSecure()).thenReturn(true);
63 setUpNodeMainDelivery();
64 when(request.getHeader("Authorization")).thenReturn("User1");
65 when(request.getHeader("X-ATT-DR-PUBLISH-ID")).thenReturn("User1");
69 public void Given_Request_Is_HTTP_GET_And_Config_Is_Down_Then_Service_Unavailable_Response_Is_Generated() throws Exception {
70 setNodeConfigManagerIsConfiguredToReturnFalse();
71 nodeServlet.doGet(request, response);
72 verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE));
76 public void Given_Request_Is_HTTP_GET_And_Endpoint_Is_Internal_FetchProv_Then_No_Content_Response_Is_Generated() throws Exception {
77 when(request.getPathInfo()).thenReturn("/internal/fetchProv");
78 nodeServlet.doGet(request, response);
79 verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
83 public void Given_Request_Is_HTTP_GET_And_Endpoint_Is_ResetSubscription_Then_No_Content_Response_Is_Generated() throws Exception {
84 when(request.getPathInfo()).thenReturn("/internal/resetSubscription/1");
85 nodeServlet.doGet(request, response);
86 verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
90 public void Given_Request_Is_HTTP_GET_And_Endpoint_Is_Internal_Logs_And_File_Does_Not_Exist_Then_Not_Found_Response_Is_Generated() throws Exception {
91 when(request.getPathInfo()).thenReturn("/internal/logs/fileName");
92 when(request.getRemoteAddr()).thenReturn("135.207.136.128");
93 nodeServlet.doGet(request, response);
94 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND));
98 public void Given_Request_Is_HTTP_GET_And_Endpoint_Is_Internal_Rtt_And_Error_Connecting_To_Socket_Occurs_Then_Ok_Response_Is_Generated() throws Exception {
99 when(request.getPathInfo()).thenReturn("/internal/rtt/0.0.0.0");
100 when(request.getRemoteAddr()).thenReturn("135.207.136.128");
101 ServletOutputStream outStream = mock(ServletOutputStream.class);
102 when(response.getOutputStream()).thenReturn(outStream);
103 nodeServlet.doGet(request, response);
104 verify(response).setStatus(eq(200));
108 public void Given_Request_Is_HTTP_GET_To_Invalid_Endpoint_Then_Not_Found_Response_Is_Generated() throws Exception {
109 when(request.getPathInfo()).thenReturn("/incorrect");
110 nodeServlet.doGet(request, response);
111 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND));
115 public void Given_Request_Is_HTTP_PUT_And_Config_Is_Down_Then_Service_Unavailable_Response_Is_Generated() throws Exception {
116 setNodeConfigManagerIsConfiguredToReturnFalse();
117 nodeServlet.doPut(request, response);
118 verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE));
122 public void Given_Request_Is_HTTP_PUT_And_Endpoint_Is_Incorrect_Then_Not_Found_Response_Is_Generated() throws Exception {
123 when(request.getPathInfo()).thenReturn("/incorrect/");
124 nodeServlet.doPut(request, response);
125 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
129 public void Given_Request_Is_HTTP_PUT_And_Request_Is_Not_Secure_Then_Forbidden_Response_Is_Generated() throws Exception {
130 when(request.isSecure()).thenReturn(false);
131 nodeServlet.doPut(request, response);
132 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
136 public void Given_Request_Is_HTTP_PUT_And_File_Id_Is_Null_Then_Not_Found_Response_Is_Generated() throws Exception {
137 when(request.getPathInfo()).thenReturn(null);
138 nodeServlet.doPut(request, response);
139 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
143 public void Given_Request_Is_HTTP_PUT_And_Authorization_Is_Null_Then_Forbidden_Response_Is_Generated() throws Exception {
144 when(request.getHeader("Authorization")).thenReturn(null);
145 nodeServlet.doPut(request, response);
146 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
150 public void Given_Request_Is_HTTP_PUT_And_Publish_Does_Not_Include_File_Id_Then_Not_Found_Response_Is_Generated() throws Exception {
151 when(request.getPathInfo()).thenReturn("/publish/");
152 nodeServlet.doPut(request, response);
153 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
157 public void Given_Request_Is_HTTP_PUT_And_Publish_Not_Permitted_Then_Forbidden_Response_Is_Generated() throws Exception {
158 when(request.getPathInfo()).thenReturn("/publish/1/fileName");
159 setNodeConfigManagerIsPublishPermittedToReturnAReason();
160 nodeServlet.doPut(request, response);
161 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
165 public void Given_Request_Is_HTTP_PUT_And_Internal_Publish_On_Same_Node_Then_Forbidden_Response_Is_Generated() throws Exception {
166 when(request.getPathInfo()).thenReturn("/internal/publish/1/fileName");
167 setNodeConfigManagerIsPublishPermittedToReturnAReason();
168 nodeServlet.doPut(request, response);
169 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN));
173 public void Given_Request_Is_HTTP_PUT_And_Internal_Publish_But_Invalid_File_Id_Then_Not_Found_Response_Is_Generated() throws Exception {
174 when(request.getPathInfo()).thenReturn("/internal/publish/1/");
175 nodeServlet.doPut(request, response);
176 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
180 public void Given_Request_Is_HTTP_PUT_On_Publish_And_Ingress_Node_Is_Provided_Then_Request_Is_Redirected() throws Exception {
181 when(request.getPathInfo()).thenReturn("/publish/1/fileName");
182 setNodeConfigManagerToAllowRedirectOnIngressNode();
183 nodeServlet.doPut(request, response);
184 verify(response).sendRedirect(anyString());
188 public void Given_Request_Is_HTTP_PUT_On_Publish_With_Meta_Data_Too_Long_Then_Bad_Request_Response_Is_Generated() throws Exception {
189 when(request.getPathInfo()).thenReturn("/publish/1/fileName");
190 setHeadersForValidRequest(true);
191 nodeServlet.doPut(request, response);
192 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
196 public void Given_Request_Is_HTTP_PUT_On_Publish_With_Meta_Data_Malformed_Then_Bad_Request_Response_Is_Generated() throws Exception {
197 when(request.getPathInfo()).thenReturn("/publish/1/fileName");
198 setHeadersForValidRequest(false);
199 nodeServlet.doPut(request, response);
200 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
204 public void Given_Request_Is_HTTP_DELETE_On_Publish_With_Meta_Data_Malformed_Then_Bad_Request_Response_Is_Generated() throws Exception {
205 when(request.getPathInfo()).thenReturn("/publish/1/fileName");
206 setHeadersForValidRequest(false);
207 nodeServlet.doDelete(request, response);
208 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
212 private void setBehalfHeader(String headerValue) {
213 when(request.getHeader("X-ATT-DR-ON-BEHALF-OF")).thenReturn(headerValue);
216 private void setUpConfig() throws IllegalAccessException{
217 NodeConfigManager config = mock(NodeConfigManager.class);
218 PowerMockito.mockStatic(NodeConfigManager.class);
219 when(config.isShutdown()).thenReturn(false);
220 when(config.isConfigured()).thenReturn(true);
221 when(config.getSpoolDir()).thenReturn("spool/dir");
222 when(config.getLogDir()).thenReturn("log/dir");
223 when(config.getPublishId()).thenReturn("User1");
224 when(config.isAnotherNode(anyString(), anyString())).thenReturn(true);
225 when(config.getEventLogInterval()).thenReturn("40");
226 FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true);
227 FieldUtils.writeDeclaredStaticField(NodeMain.class, "nodeConfigManager", config, true);
228 PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
232 private void setUpNodeMainDelivery() throws IllegalAccessException{
233 Delivery delivery = mock(Delivery.class);
234 doNothing().when(delivery).resetQueue(anyObject());
235 FieldUtils.writeDeclaredStaticField(NodeMain.class, "delivery", delivery, true);
238 private void setNodeConfigManagerIsConfiguredToReturnFalse() throws IllegalAccessException{
239 NodeConfigManager config = mock(NodeConfigManager.class);
240 when(config.isConfigured()).thenReturn(false);
241 FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true);
244 private void setNodeConfigManagerIsPublishPermittedToReturnAReason() throws IllegalAccessException{
245 NodeConfigManager config = mock(NodeConfigManager.class);
246 when(config.isShutdown()).thenReturn(false);
247 when(config.isConfigured()).thenReturn(true);
248 when(config.getSpoolDir()).thenReturn("spool/dir");
249 when(config.getLogDir()).thenReturn("log/dir");
250 when(config.isPublishPermitted(anyString(), anyString(), anyString())).thenReturn("Not Permitted");
251 when(config.isAnotherNode(anyString(), anyString())).thenReturn(false);
252 FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true);
255 private void setNodeConfigManagerToAllowRedirectOnIngressNode() throws IllegalAccessException{
256 NodeConfigManager config = mock(NodeConfigManager.class);
257 when(config.isShutdown()).thenReturn(false);
258 when(config.isConfigured()).thenReturn(true);
259 when(config.getSpoolDir()).thenReturn("spool/dir");
260 when(config.getLogDir()).thenReturn("log/dir");
261 when(config.getPublishId()).thenReturn("User1");
262 when(config.isAnotherNode(anyString(), anyString())).thenReturn(true);
263 when(config.getAuthUser(anyString(), anyString())).thenReturn("User1");
264 when(config.getIngressNode(anyString(), anyString(), anyString())).thenReturn("NewNode");
265 when(config.getExtHttpsPort()).thenReturn(8080);
266 FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true);
269 private String createLargeMetaDataString() {
270 StringBuilder myString = new StringBuilder("meta");
271 for (int i = 0; i <= 4098; ++i) {
272 myString.append('x');
274 return myString.toString();
277 private void setHeadersForValidRequest(boolean isMetaTooLong) {
278 String metaDataString;
280 metaDataString = createLargeMetaDataString();
282 metaDataString = "?#@><";
284 List<String> headers = new ArrayList<>();
285 headers.add("Content-Type");
286 headers.add("X-ATT-DR-ON-BEHALF-OF");
287 headers.add("X-ATT-DR-META");
288 Enumeration<String> headerNames = Collections.enumeration(headers);
289 when(request.getHeaderNames()).thenReturn(headerNames);
290 Enumeration<String> contentTypeHeader = Collections.enumeration(Arrays.asList("text/plain"));
291 Enumeration<String> behalfHeader = Collections.enumeration(Arrays.asList("User1"));
292 Enumeration<String> metaDataHeader = Collections.enumeration(Arrays.asList(metaDataString));
293 when(request.getHeaders("Content-Type")).thenReturn(contentTypeHeader);
294 when(request.getHeaders("X-ATT-DR-ON-BEHALF-OF")).thenReturn(behalfHeader);
295 when(request.getHeaders("X-ATT-DR-META")).thenReturn(metaDataHeader);