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.modules.junit4.PowerMockRunner;
36 import javax.servlet.ServletOutputStream;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import javax.persistence.EntityManager;
40 import javax.persistence.EntityManagerFactory;
41 import javax.persistence.Persistence;
43 import static org.hamcrest.CoreMatchers.notNullValue;
44 import static org.mockito.Matchers.argThat;
45 import static org.mockito.Matchers.eq;
46 import static org.mockito.Mockito.*;
47 import static org.powermock.api.mockito.PowerMockito.when;
50 @RunWith(PowerMockRunner.class)
51 public class LogServletTest extends DrServletTestBase {
53 private static EntityManagerFactory emf;
54 private static EntityManager em;
55 private static LogServlet logServlet;
58 private HttpServletRequest request;
60 private HttpServletResponse response;
63 private ServletOutputStream s;
65 ListAppender<ILoggingEvent> listAppender;
68 public static void init() {
69 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
70 em = emf.createEntityManager();
72 "org.onap.dmaap.datarouter.provserver.properties",
73 "src/test/resources/h2Database.properties");
77 public static void tearDownClass() {
84 public void setUp() throws Exception {
85 listAppender = setTestLogger(LogServlet.class);
86 logServlet = new LogServlet(true);
87 setUpValidParameterValuesForMap();
91 public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
93 logServlet.doDelete(request, response);
94 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
95 verifyEnteringExitCalled(listAppender);
99 public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
101 when(request.getPathInfo()).thenReturn(null);
102 logServlet.doGet(request, response);
103 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
104 verifyEnteringExitCalled(listAppender);
108 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type_Then_Bad_Request_Response_Is_Generated()
110 when(request.getParameter("type")).thenReturn("bad_type");
111 logServlet.doGet(request, response);
112 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
116 public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID_Then_Bad_Request_Response_Is_Generated()
118 when(request.getParameter("publishId")).thenReturn("bad_PublishID'");
119 logServlet.doGet(request, response);
120 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
124 public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode_Then_Bad_Request_Response_Is_Generated()
126 when(request.getParameter("statusCode")).thenReturn("1'");
127 logServlet.doGet(request, response);
128 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
132 public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
134 when(request.getParameter("expiryReason")).thenReturn("bad_ExpiryReason");
135 logServlet.doGet(request, response);
136 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
140 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
142 when(request.getParameter("start")).thenReturn("bad_startTime");
143 logServlet.doGet(request, response);
144 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
148 public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
150 when(request.getParameter("end")).thenReturn("bad_endTime");
151 logServlet.doGet(request, response);
152 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
156 public void Given_Request_Is_HTTP_GET_And_Is_FeedLog_A_STATUS_OK_Response_Is_Generated()
158 logServlet.doGet(request, response);
159 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
160 verifyEnteringExitCalled(listAppender);
164 public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
166 logServlet.doPut(request, response);
167 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
168 verifyEnteringExitCalled(listAppender);
172 public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
174 logServlet.doPost(request, response);
175 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
176 verifyEnteringExitCalled(listAppender);
180 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_A_STATUS_OK_Response_Is_Generated()
182 when(request.getParameter("type")).thenReturn("pub");
183 when(request.getParameter("expiryReason")).thenReturn(null);
184 logServlet.doGet(request, response);
185 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
189 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_With_Filename_That_exists_A_STATUS_OK_Response_Is_Generated_And_Correct_Value_Returned()
191 when(request.getParameter("type")).thenReturn("pub");
192 when(request.getPathInfo()).thenReturn("/1");
193 when(request.getParameter("publishId")).thenReturn("ID");
194 when(request.getParameter("expiryReason")).thenReturn(null);
195 when(request.getParameter("statusCode")).thenReturn("204");
196 when(request.getParameter("filename")).thenReturn("file123");
197 logServlet.doGet(request, response);
198 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
199 verify(s, times(1)).print("[");
200 verify(s, 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\"}"));
201 verify(s, times(1)).print("[");
205 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()
207 when(request.getParameter("type")).thenReturn("pub");
208 when(request.getPathInfo()).thenReturn("/1");
209 when(request.getParameter("publishId")).thenReturn("ID");
210 when(request.getParameter("expiryReason")).thenReturn(null);
211 when(request.getParameter("statusCode")).thenReturn("204");
212 when(request.getParameter("filename")).thenReturn("file456");
213 logServlet.doGet(request, response);
214 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
215 verify(s, times(1)).print("[");
216 verify(s, 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\"}"));
217 verify(s, times(1)).print("[");
221 public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
223 when(request.getParameter("type")).thenReturn("del");
224 when(request.getParameter("expiryReason")).thenReturn(null);
225 logServlet.doGet(request, response);
226 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
230 public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
232 when(request.getParameter("type")).thenReturn("exp");
233 when(request.getParameter("statusCode")).thenReturn(null);
234 when(request.getParameter("expiryReason")).thenReturn(null);
235 ServletOutputStream s = mock(ServletOutputStream.class);
236 when(response.getOutputStream()).thenReturn(s);
237 logServlet.doGet(request, response);
238 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
242 public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
244 LogServlet logServletNotFeedlog = new LogServlet(false);
245 when(request.getParameter("type")).thenReturn("del");
246 when(request.getParameter("statusCode")).thenReturn(null);
247 when(request.getParameter("expiryReason")).thenReturn(null);
248 logServletNotFeedlog.doGet(request, response);
249 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
253 public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
255 LogServlet logServletNotFeedlog = new LogServlet(false);
256 when(request.getParameter("type")).thenReturn("exp");
257 when(request.getParameter("statusCode")).thenReturn(null);
258 when(request.getParameter("expiryReason")).thenReturn(null);
259 logServletNotFeedlog.doGet(request, response);
260 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
263 private void setUpValidParameterValuesForMap() throws Exception {
264 when(request.getPathInfo()).thenReturn("123");
265 when(request.getParameter("type")).thenReturn("exp");
266 when(request.getParameter("publishId")).thenReturn("bad_PublishID");
267 when(request.getParameter("statusCode")).thenReturn("-1");
268 when(request.getParameter("expiryReason")).thenReturn("other");
269 when(request.getParameter("start")).thenReturn("2536159564422");
270 when(request.getParameter("end")).thenReturn("2536159564422");
271 s = mock(ServletOutputStream.class);
272 when(response.getOutputStream()).thenReturn(s);