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