Add RequestId and InvocationId to DR
[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.mock;
47 import static org.mockito.Mockito.verify;
48 import static org.powermock.api.mockito.PowerMockito.when;
49
50
51 @RunWith(PowerMockRunner.class)
52 public class LogServletTest extends DrServletTestBase {
53
54     private static EntityManagerFactory emf;
55     private static EntityManager em;
56     private static LogServlet logServlet;
57
58     @Mock
59     private HttpServletRequest request;
60     @Mock
61     private HttpServletResponse response;
62
63     ListAppender<ILoggingEvent> listAppender;
64
65     @BeforeClass
66     public static void init() {
67         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
68         em = emf.createEntityManager();
69         System.setProperty(
70                 "org.onap.dmaap.datarouter.provserver.properties",
71                 "src/test/resources/h2Database.properties");
72     }
73
74     @AfterClass
75     public static void tearDownClass() {
76         em.clear();
77         em.close();
78         emf.close();
79     }
80
81     @Before
82     public void setUp() throws Exception {
83         listAppender = setTestLogger(LogServlet.class);
84         logServlet = new LogServlet(true);
85         setUpValidParameterValuesForMap();
86     }
87
88     @Test
89     public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
90             throws Exception {
91         logServlet.doDelete(request, response);
92         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
93         verifyEnteringExitCalled(listAppender);
94     }
95
96     @Test
97     public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
98             throws Exception {
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);
103     }
104
105     @Test
106     public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type_Then_Bad_Request_Response_Is_Generated()
107             throws Exception {
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)));
111     }
112
113     @Test
114     public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID_Then_Bad_Request_Response_Is_Generated()
115             throws Exception {
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)));
119     }
120
121     @Test
122     public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode_Then_Bad_Request_Response_Is_Generated()
123             throws Exception {
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)));
127     }
128
129     @Test
130     public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
131             throws Exception {
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)));
135     }
136
137     @Test
138     public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
139             throws Exception {
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)));
143     }
144
145     @Test
146     public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
147             throws Exception {
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)));
151     }
152
153     @Test
154     public void Given_Request_Is_HTTP_GET_And_Is_FeedLog_A_STATUS_OK_Response_Is_Generated()
155             throws Exception {
156         logServlet.doGet(request, response);
157         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
158         verifyEnteringExitCalled(listAppender);
159     }
160
161     @Test
162     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
163             throws Exception {
164         logServlet.doPut(request, response);
165         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
166         verifyEnteringExitCalled(listAppender);
167     }
168
169     @Test
170     public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
171             throws Exception {
172         logServlet.doPost(request, response);
173         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
174         verifyEnteringExitCalled(listAppender);
175     }
176
177     @Test
178     public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_A_STATUS_OK_Response_Is_Generated()
179             throws Exception {
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_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
188             throws Exception {
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));
193     }
194
195     @Test
196     public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
197             throws Exception {
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));
205     }
206
207     @Test
208     public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
209             throws Exception {
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));
216     }
217
218     @Test
219     public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
220             throws Exception {
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));
227     }
228
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);
239     }
240 }