update link to upper-constraints.txt
[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 jakarta.servlet.ServletOutputStream;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.AfterClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.powermock.core.classloader.annotations.PowerMockIgnore;
36 import org.powermock.modules.junit4.PowerMockRunner;
37
38 import jakarta.servlet.http.HttpServletRequest;
39 import jakarta.servlet.http.HttpServletResponse;
40 import javax.persistence.EntityManager;
41 import javax.persistence.EntityManagerFactory;
42 import javax.persistence.Persistence;
43
44 import static org.mockito.ArgumentMatchers.anyString;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.mockito.ArgumentMatchers.matches;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.times;
49 import static org.mockito.Mockito.verify;
50 import static org.powermock.api.mockito.PowerMockito.when;
51
52
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
55 public class LogServletTest extends DrServletTestBase {
56
57     private static EntityManagerFactory emf;
58     private static EntityManager em;
59     private static LogServlet logServlet;
60
61     @Mock
62     private HttpServletRequest request;
63     @Mock
64     private HttpServletResponse response;
65
66     @Mock
67     private ServletOutputStream servletOutputStream;
68
69     private ListAppender<ILoggingEvent> listAppender;
70
71     @BeforeClass
72     public static void init() {
73         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
74         em = emf.createEntityManager();
75         System.setProperty(
76                 "org.onap.dmaap.datarouter.provserver.properties",
77                 "src/test/resources/h2Database.properties");
78     }
79
80     @AfterClass
81     public static void tearDownClass() {
82         em.clear();
83         em.close();
84         emf.close();
85     }
86
87     @Before
88     public void setUp() throws Exception {
89         listAppender = setTestLogger(LogServlet.class);
90         logServlet = new LogServlet(true);
91         setUpValidParameterValuesForMap();
92     }
93
94     @Test
95     public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
96             throws Exception {
97         logServlet.doDelete(request, response);
98         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), anyString());
99         verifyEnteringExitCalled(listAppender);
100     }
101
102     @Test
103     public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
104             throws Exception {
105         when(request.getPathInfo()).thenReturn(null);
106         logServlet.doGet(request, response);
107         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
108         verifyEnteringExitCalled(listAppender);
109     }
110
111     @Test
112     public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type_Then_Bad_Request_Response_Is_Generated()
113             throws Exception {
114         when(request.getParameter("type")).thenReturn("bad_type");
115         logServlet.doGet(request, response);
116         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
117     }
118
119     @Test
120     public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID_Then_Bad_Request_Response_Is_Generated()
121             throws Exception {
122         when(request.getParameter("publishId")).thenReturn("bad_PublishID'");
123         logServlet.doGet(request, response);
124         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
125     }
126
127     @Test
128     public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode_Then_Bad_Request_Response_Is_Generated()
129             throws Exception {
130         when(request.getParameter("statusCode")).thenReturn("1'");
131         logServlet.doGet(request, response);
132         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
133     }
134
135     @Test
136     public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
137             throws Exception {
138         when(request.getParameter("expiryReason")).thenReturn("bad_ExpiryReason");
139         logServlet.doGet(request, response);
140         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
141     }
142
143     @Test
144     public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
145             throws Exception {
146         when(request.getParameter("start")).thenReturn("bad_startTime");
147         logServlet.doGet(request, response);
148         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
149     }
150
151     @Test
152     public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
153             throws Exception {
154         when(request.getParameter("end")).thenReturn("bad_endTime");
155         logServlet.doGet(request, response);
156         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
157     }
158
159     @Test
160     public void Given_Request_Is_HTTP_GET_And_Is_FeedLog_A_STATUS_OK_Response_Is_Generated() {
161         logServlet.doGet(request, response);
162         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
163         verifyEnteringExitCalled(listAppender);
164     }
165
166     @Test
167     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
168             throws Exception {
169         logServlet.doPut(request, response);
170         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), anyString());
171         verifyEnteringExitCalled(listAppender);
172     }
173
174     @Test
175     public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
176             throws Exception {
177         logServlet.doPost(request, response);
178         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), anyString());
179         verifyEnteringExitCalled(listAppender);
180     }
181
182     @Test
183     public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_A_STATUS_OK_Response_Is_Generated() {
184         when(request.getParameter("type")).thenReturn("pub");
185         when(request.getParameter("expiryReason")).thenReturn(null);
186         logServlet.doGet(request, response);
187         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
188     }
189
190     @Test
191     public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_With_Filename_That_exists_A_STATUS_OK_Response_Is_Generated_And_Correct_Value_Returned()
192             throws Exception {
193         when(request.getParameter("type")).thenReturn("pub");
194         when(request.getPathInfo()).thenReturn("/1");
195         when(request.getParameter("publishId")).thenReturn("ID");
196         when(request.getParameter("expiryReason")).thenReturn(null);
197         when(request.getParameter("statusCode")).thenReturn("204");
198         when(request.getParameter("filename")).thenReturn("file123");
199         logServlet.doGet(request, response);
200         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
201         verify(servletOutputStream, times(1)).print("[");
202         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\"}"));
203         verify(servletOutputStream, times(1)).print("[");
204     }
205
206     @Test
207     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()
208             throws Exception {
209         when(request.getParameter("type")).thenReturn("pub");
210         when(request.getPathInfo()).thenReturn("/1");
211         when(request.getParameter("publishId")).thenReturn("ID");
212         when(request.getParameter("expiryReason")).thenReturn(null);
213         when(request.getParameter("statusCode")).thenReturn("204");
214         when(request.getParameter("filename")).thenReturn("file456");
215         logServlet.doGet(request, response);
216         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
217         verify(servletOutputStream, times(1)).print("[");
218         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\"}"));
219         verify(servletOutputStream, times(1)).print("[");
220     }
221
222     @Test
223     public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
224             throws Exception {
225         when(request.getParameter("type")).thenReturn("del");
226         when(request.getParameter("expiryReason")).thenReturn(null);
227         logServlet.doGet(request, response);
228         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
229     }
230
231     @Test
232     public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
233             throws Exception {
234         when(request.getParameter("type")).thenReturn("exp");
235         when(request.getParameter("statusCode")).thenReturn(null);
236         when(request.getParameter("expiryReason")).thenReturn(null);
237         ServletOutputStream s = mock(ServletOutputStream.class);
238         when(response.getOutputStream()).thenReturn(s);
239         logServlet.doGet(request, response);
240         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
241     }
242
243     @Test
244     public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
245             throws Exception {
246         LogServlet logServletNotFeedlog = new LogServlet(false);
247         when(request.getParameter("type")).thenReturn("del");
248         when(request.getParameter("statusCode")).thenReturn(null);
249         when(request.getParameter("expiryReason")).thenReturn(null);
250         logServletNotFeedlog.doGet(request, response);
251         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
252     }
253
254     @Test
255     public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
256             throws Exception {
257         LogServlet logServletNotFeedlog = new LogServlet(false);
258         when(request.getParameter("type")).thenReturn("exp");
259         when(request.getParameter("statusCode")).thenReturn(null);
260         when(request.getParameter("expiryReason")).thenReturn(null);
261         logServletNotFeedlog.doGet(request, response);
262         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
263     }
264
265     private void setUpValidParameterValuesForMap() throws Exception {
266         when(request.getPathInfo()).thenReturn("123");
267         when(request.getParameter("type")).thenReturn("exp");
268         when(request.getParameter("publishId")).thenReturn("bad_PublishID");
269         when(request.getParameter("statusCode")).thenReturn("-1");
270         when(request.getParameter("expiryReason")).thenReturn("other");
271         when(request.getParameter("start")).thenReturn("2536159564422");
272         when(request.getParameter("end")).thenReturn("2536159564422");
273         servletOutputStream = mock(ServletOutputStream.class);
274         when(response.getOutputStream()).thenReturn(servletOutputStream);
275     }
276 }