e411bdc02599752d79d3694a132593d30ef06be8
[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 org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.AfterClass;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 import javax.servlet.ServletOutputStream;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import javax.persistence.EntityManager;
38 import javax.persistence.EntityManagerFactory;
39 import javax.persistence.Persistence;
40
41 import static org.hamcrest.CoreMatchers.notNullValue;
42 import static org.mockito.Matchers.argThat;
43 import static org.mockito.Matchers.eq;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.verify;
46 import static org.powermock.api.mockito.PowerMockito.when;
47
48
49 @RunWith(PowerMockRunner.class)
50 public class LogServletTest extends DrServletTestBase {
51
52     private static EntityManagerFactory emf;
53     private static EntityManager em;
54     private static LogServlet logServlet;
55
56     @Mock
57     private HttpServletRequest request;
58     @Mock
59     private HttpServletResponse response;
60
61     @BeforeClass
62     public static void init() {
63         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
64         em = emf.createEntityManager();
65         System.setProperty(
66                 "org.onap.dmaap.datarouter.provserver.properties",
67                 "src/test/resources/h2Database.properties");
68     }
69
70     @AfterClass
71     public static void tearDownClass() {
72         em.clear();
73         em.close();
74         emf.close();
75     }
76
77     @Before
78     public void setUp() throws Exception {
79         logServlet = new LogServlet(true);
80         setUpValidParameterValuesForMap();
81     }
82
83     @Test
84     public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
85             throws Exception {
86         logServlet.doDelete(request, response);
87         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
88     }
89
90     @Test
91     public void Given_Request_Is_HTTP_GET_And_FeedID_Is_Invalid_Then_Bad_Request_Response_Is_Generated()
92             throws Exception {
93         when(request.getPathInfo()).thenReturn(null);
94         logServlet.doGet(request, response);
95         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
96     }
97
98     @Test
99     public void Given_Request_Is_HTTP_GET_And_Has_Bad_Type_Then_Bad_Request_Response_Is_Generated()
100             throws Exception {
101         when(request.getParameter("type")).thenReturn("bad_type");
102         logServlet.doGet(request, response);
103         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
104     }
105
106     @Test
107     public void Given_Request_Is_HTTP_GET_And_Has_Bad_PublishID_Then_Bad_Request_Response_Is_Generated()
108             throws Exception {
109         when(request.getParameter("publishId")).thenReturn("bad_PublishID'");
110         logServlet.doGet(request, response);
111         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
112     }
113
114     @Test
115     public void Given_Request_Is_HTTP_GET_And_Has_Bad_StatusCode_Then_Bad_Request_Response_Is_Generated()
116             throws Exception {
117         when(request.getParameter("statusCode")).thenReturn("1'");
118         logServlet.doGet(request, response);
119         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
120     }
121
122     @Test
123     public void Given_Request_Is_HTTP_GET_And_Has_Bad_ExpiryReason()
124             throws Exception {
125         when(request.getParameter("expiryReason")).thenReturn("bad_ExpiryReason");
126         logServlet.doGet(request, response);
127         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
128     }
129
130     @Test
131     public void Given_Request_Is_HTTP_GET_And_Has_Bad_Start()
132             throws Exception {
133         when(request.getParameter("start")).thenReturn("bad_startTime");
134         logServlet.doGet(request, response);
135         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
136     }
137
138     @Test
139     public void Given_Request_Is_HTTP_GET_And_Has_Bad_End()
140             throws Exception {
141         when(request.getParameter("end")).thenReturn("bad_endTime");
142         logServlet.doGet(request, response);
143         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
144     }
145
146     @Test
147     public void Given_Request_Is_HTTP_GET_And_Is_FeedLog_A_STATUS_OK_Response_Is_Generated()
148             throws Exception {
149         logServlet.doGet(request, response);
150         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
151     }
152
153     @Test
154     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
155             throws Exception {
156         logServlet.doPut(request, response);
157         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
158     }
159
160     @Test
161     public void Given_Request_Is_HTTP_POST_And_Is_Not_Allowed_Then_Forbidden_Response_Is_Generated()
162             throws Exception {
163         logServlet.doPost(request, response);
164         verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
165     }
166
167     @Test
168     public void Given_Request_Is_GetPublishRecordsForFeed_And_Type_Is_Publish_A_STATUS_OK_Response_Is_Generated()
169             throws Exception {
170         when(request.getParameter("type")).thenReturn("pub");
171         when(request.getParameter("expiryReason")).thenReturn(null);
172         logServlet.doGet(request, response);
173         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
174     }
175
176     @Test
177     public void Given_Request_Is_getDeliveryRecordsForFeed_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
178             throws Exception {
179         when(request.getParameter("type")).thenReturn("del");
180         when(request.getParameter("expiryReason")).thenReturn(null);
181         logServlet.doGet(request, response);
182         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
183     }
184
185     @Test
186     public void Given_Request_Is_getExpiryRecordsForFeed_And_Type_Is_Expire_A_STATUS_OK_Response_Is_Generated()
187             throws Exception {
188         when(request.getParameter("type")).thenReturn("exp");
189         when(request.getParameter("statusCode")).thenReturn(null);
190         when(request.getParameter("expiryReason")).thenReturn(null);
191         ServletOutputStream s = mock(ServletOutputStream.class);
192         when(response.getOutputStream()).thenReturn(s);
193         logServlet.doGet(request, response);
194         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
195     }
196
197     @Test
198     public void Given_Request_Is_getDeliveryRecordsForSubscription_And_Type_Is_Delivery_A_STATUS_OK_Response_Is_Generated()
199             throws Exception {
200         LogServlet logServletNotFeedlog = new LogServlet(false);
201         when(request.getParameter("type")).thenReturn("del");
202         when(request.getParameter("statusCode")).thenReturn(null);
203         when(request.getParameter("expiryReason")).thenReturn(null);
204         logServletNotFeedlog.doGet(request, response);
205         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
206     }
207
208     @Test
209     public void Given_Request_Is_getExpiryRecordsForSubscription_And_Type_Is_Expiry_A_STATUS_OK_Response_Is_Generated()
210             throws Exception {
211         LogServlet logServletNotFeedlog = new LogServlet(false);
212         when(request.getParameter("type")).thenReturn("exp");
213         when(request.getParameter("statusCode")).thenReturn(null);
214         when(request.getParameter("expiryReason")).thenReturn(null);
215         logServletNotFeedlog.doGet(request, response);
216         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
217     }
218
219     private void setUpValidParameterValuesForMap() throws Exception {
220         when(request.getPathInfo()).thenReturn("123");
221         when(request.getParameter("type")).thenReturn("exp");
222         when(request.getParameter("publishId")).thenReturn("bad_PublishID");
223         when(request.getParameter("statusCode")).thenReturn("-1");
224         when(request.getParameter("expiryReason")).thenReturn("other");
225         when(request.getParameter("start")).thenReturn(null);
226         when(request.getParameter("end")).thenReturn(null);
227         ServletOutputStream s = mock(ServletOutputStream.class);
228         when(response.getOutputStream()).thenReturn(s);
229     }
230 }