c2bccb718c636df2ff97cbfbaa78ce6cfb3e3272
[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 s;
64
65     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             throws Exception {
158         logServlet.doGet(request, response);
159         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
160         verifyEnteringExitCalled(listAppender);
161     }
162
163     @Test
164     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
165             throws Exception {
166         logServlet.doPut(request, response);
167         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
168         verifyEnteringExitCalled(listAppender);
169     }
170
171     @Test
172     public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
173             throws Exception {
174         logServlet.doPost(request, response);
175         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
176         verifyEnteringExitCalled(listAppender);
177     }
178
179     @Test
180     public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_A_STATUS_OK_Response_Is_Generated()
181             throws Exception {
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));
186     }
187
188     @Test
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()
190             throws Exception {
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("[");
202     }
203
204     @Test
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()
206             throws Exception {
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("[");
218     }
219
220     @Test
221     public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
222             throws Exception {
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));
227     }
228
229     @Test
230     public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
231             throws Exception {
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));
239     }
240
241     @Test
242     public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
243             throws Exception {
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));
250     }
251
252     @Test
253     public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
254             throws Exception {
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));
261     }
262
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);
273     }
274 }