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.provisioning;
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.read.ListAppender;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.AfterClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.powermock.core.classloader.annotations.PowerMockIgnore;
35 import org.powermock.modules.junit4.PowerMockRunner;
37 import javax.servlet.ServletOutputStream;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.persistence.EntityManager;
41 import javax.persistence.EntityManagerFactory;
42 import javax.persistence.Persistence;
44 import static org.mockito.ArgumentMatchers.anyString;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.mockito.ArgumentMatchers.matches;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.times;
49 import static org.mockito.Mockito.verify;
50 import static org.powermock.api.mockito.PowerMockito.when;
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
55 public class LogServletTest extends DrServletTestBase {
57 private static EntityManagerFactory emf;
58 private static EntityManager em;
59 private static LogServlet logServlet;
62 private HttpServletRequest request;
64 private HttpServletResponse response;
67 private ServletOutputStream servletOutputStream;
69 private ListAppender<ILoggingEvent> listAppender;
72 public static void init() {
73 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
74 em = emf.createEntityManager();
76 "org.onap.dmaap.datarouter.provserver.properties",
77 "src/test/resources/h2Database.properties");
81 public static void tearDownClass() {
88 public void setUp() throws Exception {
89 listAppender = setTestLogger(LogServlet.class);
90 logServlet = new LogServlet(true);
91 setUpValidParameterValuesForMap();
95 public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
97 logServlet.doDelete(request, response);
98 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), anyString());
99 verifyEnteringExitCalled(listAppender);
103 public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
105 when(request.getPathInfo()).thenReturn(null);
106 logServlet.doGet(request, response);
107 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
108 verifyEnteringExitCalled(listAppender);
112 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type_Then_Bad_Request_Response_Is_Generated()
114 when(request.getParameter("type")).thenReturn("bad_type");
115 logServlet.doGet(request, response);
116 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
120 public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID_Then_Bad_Request_Response_Is_Generated()
122 when(request.getParameter("publishId")).thenReturn("bad_PublishID'");
123 logServlet.doGet(request, response);
124 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
128 public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode_Then_Bad_Request_Response_Is_Generated()
130 when(request.getParameter("statusCode")).thenReturn("1'");
131 logServlet.doGet(request, response);
132 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
136 public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
138 when(request.getParameter("expiryReason")).thenReturn("bad_ExpiryReason");
139 logServlet.doGet(request, response);
140 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
144 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
146 when(request.getParameter("start")).thenReturn("bad_startTime");
147 logServlet.doGet(request, response);
148 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
152 public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
154 when(request.getParameter("end")).thenReturn("bad_endTime");
155 logServlet.doGet(request, response);
156 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
160 public void Given_Request_Is_HTTP_GET_And_Is_FeedLog_A_STATUS_OK_Response_Is_Generated() {
161 logServlet.doGet(request, response);
162 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
163 verifyEnteringExitCalled(listAppender);
167 public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
169 logServlet.doPut(request, response);
170 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), anyString());
171 verifyEnteringExitCalled(listAppender);
175 public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
177 logServlet.doPost(request, response);
178 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), anyString());
179 verifyEnteringExitCalled(listAppender);
183 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_A_STATUS_OK_Response_Is_Generated() {
184 when(request.getParameter("type")).thenReturn("pub");
185 when(request.getParameter("expiryReason")).thenReturn(null);
186 logServlet.doGet(request, response);
187 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
191 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_With_Filename_That_exists_A_STATUS_OK_Response_Is_Generated_And_Correct_Value_Returned()
193 when(request.getParameter("type")).thenReturn("pub");
194 when(request.getPathInfo()).thenReturn("/1");
195 when(request.getParameter("publishId")).thenReturn("ID");
196 when(request.getParameter("expiryReason")).thenReturn(null);
197 when(request.getParameter("statusCode")).thenReturn("204");
198 when(request.getParameter("filename")).thenReturn("file123");
199 logServlet.doGet(request, response);
200 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
201 verify(servletOutputStream, times(1)).print("[");
202 verify(servletOutputStream, times(1)).print(matches("\n\\{\"statusCode\":204,\"publishId\":\"ID\",\"requestURI\":\"URL/file123\",\"sourceIP\":\"172.0.0.8\",\"method\":\"PUT\",\"contentType\":\"application/vnd.dmaap-dr.log-list; version=1.0\",\"endpointId\":\"user\",\"type\":\"pub\",\"date\":\"2050-05-14T1[6-7]:46:04.422Z\",\"contentLength\":100,\"fileName\":\"file123\"}"));
203 verify(servletOutputStream, times(1)).print("[");
207 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_With_Filename_That_Doesnt_exist_A_STATUS_OK_Response_Is_Generated_And_Empty_Array_Returned()
209 when(request.getParameter("type")).thenReturn("pub");
210 when(request.getPathInfo()).thenReturn("/1");
211 when(request.getParameter("publishId")).thenReturn("ID");
212 when(request.getParameter("expiryReason")).thenReturn(null);
213 when(request.getParameter("statusCode")).thenReturn("204");
214 when(request.getParameter("filename")).thenReturn("file456");
215 logServlet.doGet(request, response);
216 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
217 verify(servletOutputStream, times(1)).print("[");
218 verify(servletOutputStream, times(0)).print(matches("\n\\{\"statusCode\":204,\"publishId\":\"ID\",\"requestURI\":\"URL/file123\",\"sourceIP\":\"172.0.0.8\",\"method\":\"PUT\",\"contentType\":\"application/vnd.dmaap-dr.log-list; version=1.0\",\"endpointId\":\"user\",\"type\":\"pub\",\"date\":\"2050-05-14T1[6-7]:46:04.422Z\",\"contentLength\":100,\"fileName\":\"file123\"}"));
219 verify(servletOutputStream, times(1)).print("[");
223 public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
225 when(request.getParameter("type")).thenReturn("del");
226 when(request.getParameter("expiryReason")).thenReturn(null);
227 logServlet.doGet(request, response);
228 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
232 public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
234 when(request.getParameter("type")).thenReturn("exp");
235 when(request.getParameter("statusCode")).thenReturn(null);
236 when(request.getParameter("expiryReason")).thenReturn(null);
237 ServletOutputStream s = mock(ServletOutputStream.class);
238 when(response.getOutputStream()).thenReturn(s);
239 logServlet.doGet(request, response);
240 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
244 public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
246 LogServlet logServletNotFeedlog = new LogServlet(false);
247 when(request.getParameter("type")).thenReturn("del");
248 when(request.getParameter("statusCode")).thenReturn(null);
249 when(request.getParameter("expiryReason")).thenReturn(null);
250 logServletNotFeedlog.doGet(request, response);
251 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
255 public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
257 LogServlet logServletNotFeedlog = new LogServlet(false);
258 when(request.getParameter("type")).thenReturn("exp");
259 when(request.getParameter("statusCode")).thenReturn(null);
260 when(request.getParameter("expiryReason")).thenReturn(null);
261 logServletNotFeedlog.doGet(request, response);
262 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
265 private void setUpValidParameterValuesForMap() throws Exception {
266 when(request.getPathInfo()).thenReturn("123");
267 when(request.getParameter("type")).thenReturn("exp");
268 when(request.getParameter("publishId")).thenReturn("bad_PublishID");
269 when(request.getParameter("statusCode")).thenReturn("-1");
270 when(request.getParameter("expiryReason")).thenReturn("other");
271 when(request.getParameter("start")).thenReturn("2536159564422");
272 when(request.getParameter("end")).thenReturn("2536159564422");
273 servletOutputStream = mock(ServletOutputStream.class);
274 when(response.getOutputStream()).thenReturn(servletOutputStream);