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 servletOutputStream;
65 private 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() {
157 logServlet.doGet(request, response);
158 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
159 verifyEnteringExitCalled(listAppender);
163 public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
165 logServlet.doPut(request, response);
166 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
167 verifyEnteringExitCalled(listAppender);
171 public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
173 logServlet.doPost(request, response);
174 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
175 verifyEnteringExitCalled(listAppender);
179 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_A_STATUS_OK_Response_Is_Generated() {
180 when(request.getParameter("type")).thenReturn("pub");
181 when(request.getParameter("expiryReason")).thenReturn(null);
182 logServlet.doGet(request, response);
183 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
187 public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_With_Filename_That_exists_A_STATUS_OK_Response_Is_Generated_And_Correct_Value_Returned()
189 when(request.getParameter("type")).thenReturn("pub");
190 when(request.getPathInfo()).thenReturn("/1");
191 when(request.getParameter("publishId")).thenReturn("ID");
192 when(request.getParameter("expiryReason")).thenReturn(null);
193 when(request.getParameter("statusCode")).thenReturn("204");
194 when(request.getParameter("filename")).thenReturn("file123");
195 logServlet.doGet(request, response);
196 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
197 verify(servletOutputStream, times(1)).print("[");
198 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\"}"));
199 verify(servletOutputStream, times(1)).print("[");
203 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()
205 when(request.getParameter("type")).thenReturn("pub");
206 when(request.getPathInfo()).thenReturn("/1");
207 when(request.getParameter("publishId")).thenReturn("ID");
208 when(request.getParameter("expiryReason")).thenReturn(null);
209 when(request.getParameter("statusCode")).thenReturn("204");
210 when(request.getParameter("filename")).thenReturn("file456");
211 logServlet.doGet(request, response);
212 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
213 verify(servletOutputStream, times(1)).print("[");
214 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\"}"));
215 verify(servletOutputStream, times(1)).print("[");
219 public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
221 when(request.getParameter("type")).thenReturn("del");
222 when(request.getParameter("expiryReason")).thenReturn(null);
223 logServlet.doGet(request, response);
224 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
228 public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
230 when(request.getParameter("type")).thenReturn("exp");
231 when(request.getParameter("statusCode")).thenReturn(null);
232 when(request.getParameter("expiryReason")).thenReturn(null);
233 ServletOutputStream s = mock(ServletOutputStream.class);
234 when(response.getOutputStream()).thenReturn(s);
235 logServlet.doGet(request, response);
236 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
240 public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
242 LogServlet logServletNotFeedlog = new LogServlet(false);
243 when(request.getParameter("type")).thenReturn("del");
244 when(request.getParameter("statusCode")).thenReturn(null);
245 when(request.getParameter("expiryReason")).thenReturn(null);
246 logServletNotFeedlog.doGet(request, response);
247 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
251 public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
253 LogServlet logServletNotFeedlog = new LogServlet(false);
254 when(request.getParameter("type")).thenReturn("exp");
255 when(request.getParameter("statusCode")).thenReturn(null);
256 when(request.getParameter("expiryReason")).thenReturn(null);
257 logServletNotFeedlog.doGet(request, response);
258 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
261 private void setUpValidParameterValuesForMap() throws Exception {
262 when(request.getPathInfo()).thenReturn("123");
263 when(request.getParameter("type")).thenReturn("exp");
264 when(request.getParameter("publishId")).thenReturn("bad_PublishID");
265 when(request.getParameter("statusCode")).thenReturn("-1");
266 when(request.getParameter("expiryReason")).thenReturn("other");
267 when(request.getParameter("start")).thenReturn("2536159564422");
268 when(request.getParameter("end")).thenReturn("2536159564422");
269 servletOutputStream = mock(ServletOutputStream.class);
270 when(response.getOutputStream()).thenReturn(servletOutputStream);