Add test base parent class
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / FeedServletTest.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 import org.apache.commons.lang3.reflect.FieldUtils;
26 import org.json.JSONObject;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
32 import org.onap.dmaap.datarouter.authz.Authorizer;
33 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
34 import org.onap.dmaap.datarouter.provisioning.beans.Updateable;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
37 import org.powermock.modules.junit4.PowerMockRunner;
38
39 import javax.servlet.ServletInputStream;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import java.util.HashSet;
44 import java.util.Set;
45
46 import static org.hamcrest.Matchers.notNullValue;
47 import static org.mockito.Mockito.*;
48 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
49
50
51 @RunWith(PowerMockRunner.class)
52 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Feed")
53 public class FeedServletTest extends DrServletTestBase {
54
55     private static FeedServlet feedServlet;
56
57     @Mock
58     private HttpServletRequest request;
59     @Mock
60     private HttpServletResponse response;
61
62     @Before
63     public void setUp() throws Exception {
64         super.setUp();
65         feedServlet = new FeedServlet();
66         setAuthoriserToReturnRequestIsAuthorized();
67         setPokerToNotCreateTimersWhenDeleteFeedIsCalled();
68         setUpValidAuthorisedRequest();
69         setUpValidSecurityOnHttpRequest();
70     }
71
72     @Test
73     public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
74         throws Exception {
75         when(request.isSecure()).thenReturn(false);
76         feedServlet.doDelete(request, response);
77         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
78     }
79
80
81     @Test
82     public void Given_Request_Is_HTTP_DELETE_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated()
83         throws Exception {
84         setBehalfHeader(null);
85         feedServlet.doDelete(request, response);
86         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
87     }
88
89
90     @Test
91     public void Given_Request_Is_HTTP_DELETE_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated()
92         throws Exception {
93         when(request.getPathInfo()).thenReturn(null);
94         feedServlet.doDelete(request, response);
95         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
96     }
97
98
99     @Test
100     public void Given_Request_Is_HTTP_DELETE_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated()
101         throws Exception {
102         setFeedToReturnInvalidFeedIdSupplied();
103         feedServlet.doDelete(request, response);
104         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
105     }
106
107
108     @Test
109     public void Given_Request_Is_HTTP_DELETE_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
110         throws Exception {
111         setAuthoriserToReturnRequestNotAuthorized();
112         feedServlet.doDelete(request, response);
113         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
114     }
115
116
117     @Test
118     public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Fails_An_Internal_Server_Error_Is_Reported()
119         throws Exception {
120         FeedServlet feedServlet = new FeedServlet() {
121             protected boolean doUpdate(Updateable bean) {
122                 return false;
123             }
124         };
125         feedServlet.doDelete(request, response);
126         verify(response)
127             .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
128     }
129
130
131     @Test
132     public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Succeeds_A_NO_CONTENT_Response_Is_Generated()
133         throws Exception {
134         FeedServlet feedServlet = new FeedServlet() {
135             protected boolean doUpdate(Updateable bean) {
136                 return true;
137             }
138         };
139         feedServlet.doDelete(request, response);
140         verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
141     }
142
143     @Test
144     public void Given_Request_Is_HTTP_GET_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
145         throws Exception {
146         when(request.isSecure()).thenReturn(false);
147         feedServlet.doGet(request, response);
148         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
149     }
150
151     @Test
152     public void Given_Request_Is_HTTP_GET_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated()
153         throws Exception {
154         setBehalfHeader(null);
155         feedServlet.doGet(request, response);
156         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
157     }
158
159
160     @Test
161     public void Given_Request_Is_HTTP_GET_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated()
162         throws Exception {
163         when(request.getPathInfo()).thenReturn(null);
164         feedServlet.doGet(request, response);
165         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
166     }
167
168
169     @Test
170     public void Given_Request_Is_HTTP_GET_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated()
171         throws Exception {
172         setFeedToReturnInvalidFeedIdSupplied();
173         feedServlet.doGet(request, response);
174         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
175     }
176
177
178     @Test
179     public void Given_Request_Is_HTTP_GET_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
180         throws Exception {
181         setAuthoriserToReturnRequestNotAuthorized();
182         feedServlet.doGet(request, response);
183         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
184     }
185
186
187     @Test
188     public void Given_Request_Is_HTTP_GET_And_Request_Succeeds() throws Exception {
189         ServletOutputStream outStream = mock(ServletOutputStream.class);
190         when(response.getOutputStream()).thenReturn(outStream);
191         feedServlet.doGet(request, response);
192         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
193     }
194
195
196     @Test
197     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
198         throws Exception {
199         when(request.isSecure()).thenReturn(false);
200         feedServlet.doPut(request, response);
201         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
202     }
203
204     @Test
205     public void Given_Request_Is_HTTP_PUT_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated()
206         throws Exception {
207         setBehalfHeader(null);
208         feedServlet.doPut(request, response);
209         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
210     }
211
212
213     @Test
214     public void Given_Request_Is_HTTP_PUT_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated()
215         throws Exception {
216         when(request.getPathInfo()).thenReturn(null);
217         feedServlet.doPut(request, response);
218         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
219     }
220
221
222     @Test
223     public void Given_Request_Is_HTTP_PUT_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated()
224         throws Exception {
225         setFeedToReturnInvalidFeedIdSupplied();
226         feedServlet.doPut(request, response);
227         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
228     }
229
230     @Test
231     public void Given_Request_Is_HTTP_PUT_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated()
232         throws Exception {
233         when(request.getContentType()).thenReturn("stub_contentType");
234         feedServlet.doPut(request, response);
235         verify(response)
236             .sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class)));
237     }
238
239     @Test
240     public void Given_Request_Is_HTTP_PUT_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated()
241         throws Exception {
242         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.feed; version=1.0");
243         ServletInputStream inStream = mock(ServletInputStream.class);
244         when(request.getInputStream()).thenReturn(inStream);
245         feedServlet.doPut(request, response);
246         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
247     }
248
249     private void setUpValidSecurityOnHttpRequest() throws Exception {
250         when(request.isSecure()).thenReturn(true);
251         Set<String> authAddressesAndNetworks = new HashSet<String>();
252         authAddressesAndNetworks.add(("127.0.0.1"));
253         FieldUtils
254             .writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks,
255                 true);
256         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
257     }
258
259     private void setBehalfHeader(String headerValue) {
260         when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
261     }
262
263     private void setValidPathInfoInHttpHeader() {
264         when(request.getPathInfo()).thenReturn("/123");
265     }
266
267     private void setFeedToReturnInvalidFeedIdSupplied() {
268         PowerMockito.mockStatic(Feed.class);
269         PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
270     }
271
272     private void setFeedToReturnValidFeedForSuppliedId() {
273         PowerMockito.mockStatic(Feed.class);
274         Feed feed = mock(Feed.class);
275         PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
276         when(feed.isDeleted()).thenReturn(false);
277         when(feed.asJSONObject(true)).thenReturn(mock(JSONObject.class));
278     }
279
280     private void setAuthoriserToReturnRequestNotAuthorized() throws IllegalAccessException {
281         AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
282         Authorizer authorizer = mock(Authorizer.class);
283         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
284         when(authorizer.decide(request)).thenReturn(authResponse);
285         when(authResponse.isAuthorized()).thenReturn(false);
286     }
287
288     private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
289         AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
290         Authorizer authorizer = mock(Authorizer.class);
291         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
292         when(authorizer.decide(request)).thenReturn(authResponse);
293         when(authResponse.isAuthorized()).thenReturn(true);
294     }
295
296     private void setPokerToNotCreateTimersWhenDeleteFeedIsCalled() throws Exception {
297         Poker poker = mock(Poker.class);
298         FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
299     }
300
301     private void setUpValidAuthorisedRequest() throws Exception {
302         setUpValidSecurityOnHttpRequest();
303         setBehalfHeader("Stub_Value");
304         setValidPathInfoInHttpHeader();
305         setFeedToReturnValidFeedForSuppliedId();
306     }
307 }