Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / LogServletTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning;
24
25
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;
35
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;
42
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;
48
49
50 @RunWith(PowerMockRunner.class)
51 public class LogServletTest extends DrServletTestBase {
52
53     private static EntityManagerFactory emf;
54     private static EntityManager em;
55     private static LogServlet logServlet;
56
57     @Mock
58     private HttpServletRequest request;
59     @Mock
60     private HttpServletResponse response;
61
62     @Mock
63     private ServletOutputStream servletOutputStream;
64
65     private ListAppender<ILoggingEvent> listAppender;
66
67     @BeforeClass
68     public static void init() {
69         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
70         em = emf.createEntityManager();
71         System.setProperty(
72                 "org.onap.dmaap.datarouter.provserver.properties",
73                 "src/test/resources/h2Database.properties");
74     }
75
76     @AfterClass
77     public static void tearDownClass() {
78         em.clear();
79         em.close();
80         emf.close();
81     }
82
83     @Before
84     public void setUp() throws Exception {
85         listAppender = setTestLogger(LogServlet.class);
86         logServlet = new LogServlet(true);
87         setUpValidParameterValuesForMap();
88     }
89
90     @Test
91     public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
92             throws Exception {
93         logServlet.doDelete(request, response);
94         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
95         verifyEnteringExitCalled(listAppender);
96     }
97
98     @Test
99     public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
100             throws Exception {
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);
105     }
106
107     @Test
108     public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type_Then_Bad_Request_Response_Is_Generated()
109             throws Exception {
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)));
113     }
114
115     @Test
116     public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID_Then_Bad_Request_Response_Is_Generated()
117             throws Exception {
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)));
121     }
122
123     @Test
124     public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode_Then_Bad_Request_Response_Is_Generated()
125             throws Exception {
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)));
129     }
130
131     @Test
132     public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
133             throws Exception {
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)));
137     }
138
139     @Test
140     public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
141             throws Exception {
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)));
145     }
146
147     @Test
148     public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
149             throws Exception {
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)));
153     }
154
155     @Test
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);
160     }
161
162     @Test
163     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
164             throws Exception {
165         logServlet.doPut(request, response);
166         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
167         verifyEnteringExitCalled(listAppender);
168     }
169
170     @Test
171     public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
172             throws Exception {
173         logServlet.doPost(request, response);
174         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
175         verifyEnteringExitCalled(listAppender);
176     }
177
178     @Test
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));
184     }
185
186     @Test
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()
188             throws Exception {
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("[");
200     }
201
202     @Test
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()
204             throws Exception {
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("[");
216     }
217
218     @Test
219     public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
220             throws Exception {
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));
225     }
226
227     @Test
228     public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
229             throws Exception {
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));
237     }
238
239     @Test
240     public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
241             throws Exception {
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));
248     }
249
250     @Test
251     public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
252             throws Exception {
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));
259     }
260
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);
271     }
272 }