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.mock;
47 import static org.mockito.Mockito.verify;
48 import static org.powermock.api.mockito.PowerMockito.when;
51 @RunWith(PowerMockRunner.class)
52 public class LogServletTest extends DrServletTestBase {
54 private static EntityManagerFactory emf;
55 private static EntityManager em;
56 private static LogServlet logServlet;
59 private HttpServletRequest request;
61 private HttpServletResponse response;
63 ListAppender<ILoggingEvent> listAppender;
66 public static void init() {
67 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
68 em = emf.createEntityManager();
70 "org.onap.dmaap.datarouter.provserver.properties",
71 "src/test/resources/h2Database.properties");
75 public static void tearDownClass() {
82 public void setUp() throws Exception {
83 listAppender = setTestLogger(LogServlet.class);
84 logServlet = new LogServlet(true);
85 setUpValidParameterValuesForMap();
89 public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
91 logServlet.doDelete(request, response);
92 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
93 verifyEnteringExitCalled(listAppender);
97 public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
99 when(request.getPathInfo()).thenReturn(null);
100 logServlet.doGet(request, response);
101 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
102 verifyEnteringExitCalled(listAppender);
106 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type_Then_Bad_Request_Response_Is_Generated()
108 when(request.getParameter("type")).thenReturn("bad_type");
109 logServlet.doGet(request, response);
110 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
114 public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID_Then_Bad_Request_Response_Is_Generated()
116 when(request.getParameter("publishId")).thenReturn("bad_PublishID'");
117 logServlet.doGet(request, response);
118 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
122 public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode_Then_Bad_Request_Response_Is_Generated()
124 when(request.getParameter("statusCode")).thenReturn("1'");
125 logServlet.doGet(request, response);
126 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
130 public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
132 when(request.getParameter("expiryReason")).thenReturn("bad_ExpiryReason");
133 logServlet.doGet(request, response);
134 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
138 public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
140 when(request.getParameter("start")).thenReturn("bad_startTime");
141 logServlet.doGet(request, response);
142 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
146 public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
148 when(request.getParameter("end")).thenReturn("bad_endTime");
149 logServlet.doGet(request, response);
150 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
154 public void Given_Request_Is_HTTP_GET_And_Is_FeedLog_A_STATUS_OK_Response_Is_Generated()
156 logServlet.doGet(request, response);
157 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
158 verifyEnteringExitCalled(listAppender);
162 public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
164 logServlet.doPut(request, response);
165 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
166 verifyEnteringExitCalled(listAppender);
170 public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
172 logServlet.doPost(request, response);
173 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
174 verifyEnteringExitCalled(listAppender);
178 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_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
189 when(request.getParameter("type")).thenReturn("del");
190 when(request.getParameter("expiryReason")).thenReturn(null);
191 logServlet.doGet(request, response);
192 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
196 public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
198 when(request.getParameter("type")).thenReturn("exp");
199 when(request.getParameter("statusCode")).thenReturn(null);
200 when(request.getParameter("expiryReason")).thenReturn(null);
201 ServletOutputStream s = mock(ServletOutputStream.class);
202 when(response.getOutputStream()).thenReturn(s);
203 logServlet.doGet(request, response);
204 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
208 public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
210 LogServlet logServletNotFeedlog = new LogServlet(false);
211 when(request.getParameter("type")).thenReturn("del");
212 when(request.getParameter("statusCode")).thenReturn(null);
213 when(request.getParameter("expiryReason")).thenReturn(null);
214 logServletNotFeedlog.doGet(request, response);
215 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
219 public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
221 LogServlet logServletNotFeedlog = new LogServlet(false);
222 when(request.getParameter("type")).thenReturn("exp");
223 when(request.getParameter("statusCode")).thenReturn(null);
224 when(request.getParameter("expiryReason")).thenReturn(null);
225 logServletNotFeedlog.doGet(request, response);
226 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
229 private void setUpValidParameterValuesForMap() throws Exception {
230 when(request.getPathInfo()).thenReturn("123");
231 when(request.getParameter("type")).thenReturn("exp");
232 when(request.getParameter("publishId")).thenReturn("bad_PublishID");
233 when(request.getParameter("statusCode")).thenReturn("-1");
234 when(request.getParameter("expiryReason")).thenReturn("other");
235 when(request.getParameter("start")).thenReturn(null);
236 when(request.getParameter("end")).thenReturn(null);
237 ServletOutputStream s = mock(ServletOutputStream.class);
238 when(response.getOutputStream()).thenReturn(s);