Merge "Add Route Servlet and Log Servlet Tests"
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / PublishServletTest.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
24 package org.onap.dmaap.datarouter.provisioning;
25
26 import org.apache.commons.lang3.reflect.FieldUtils;
27 import org.json.JSONObject;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Matchers;
32 import org.mockito.Mock;
33 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
34 import org.onap.dmaap.datarouter.authz.Authorizer;
35 import org.onap.dmaap.datarouter.provisioning.beans.*;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
38 import org.powermock.modules.junit4.PowerMockRunner;
39
40 import javax.servlet.ServletConfig;
41 import javax.servlet.ServletContext;
42 import javax.servlet.ServletOutputStream;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45 import java.util.*;
46
47 import static org.hamcrest.Matchers.notNullValue;
48 import static org.mockito.Matchers.*;
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.verify;
51 import static org.mockito.Mockito.when;
52 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
53
54 /**
55  * Created by ezcoxem on 21/08/2018.
56  */
57
58 @RunWith(PowerMockRunner.class)
59 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Feed")
60 public class PublishServletTest extends DrServletTestBase {
61     private PublishServlet publishServlet;
62
63     private static String START_JSON_STRING = "{";
64     private static String END_JSON_STRING = "}";
65     private static String START_JSON_ARRAY = "[";
66     private static String END_JSON_ARRAY = "]";
67     private static String COMMA = ",";
68
69     @Mock
70     private HttpServletRequest request;
71
72     @Mock
73     private HttpServletResponse response;
74
75     @Before
76     public void setUp() throws Exception {
77         super.setUp();
78         publishServlet = new PublishServlet();
79     }
80
81     @Test
82     public void Given_Request_Is_HTTP_DELETE_And_There_Are_No_Nodes_Then_Service_Unavailable_Error_Is_Returned()
83             throws Exception {
84         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[0], true);
85         publishServlet.doDelete(request, response);
86         verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE), argThat(notNullValue(String.class)));
87     }
88
89     @Test
90     public void Given_Request_Is_HTTP_DELETE_And_Path_Is_Null_Then_Not_Found_Error_Is_Returned()
91             throws Exception {
92         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true);
93         publishServlet.doDelete(request, response);
94         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
95     }
96
97     @Test
98     public void Given_Request_Is_HTTP_DELETE_And_Ix_Is_Null_Then_Not_Found_Error_Is_Returned()
99             throws Exception {
100         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true);
101         when(request.getPathInfo()).thenReturn("/123/");
102         publishServlet.doDelete(request, response);
103         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
104     }
105
106     @Test
107     public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_Valid_Then_Not_Found_Error_Is_Returned()
108             throws Exception {
109         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true);
110         when(request.getPathInfo()).thenReturn("/123/fileName.txt");
111         PowerMockito.mockStatic(Feed.class);
112         PowerMockito.when(Feed.isFeedValid(anyInt())).thenReturn(false);
113         publishServlet.doDelete(request, response);
114         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
115     }
116
117     @Test
118     public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_A_Number_Then_Not_Found_Error_Is_Returned()
119             throws Exception {
120         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true);
121         when(request.getPathInfo()).thenReturn("/abc/fileName.txt");
122         PowerMockito.mockStatic(Feed.class);
123         PowerMockito.when(Feed.isFeedValid(anyInt())).thenReturn(false);
124         publishServlet.doDelete(request, response);
125         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
126     }
127
128
129     @Test
130     public void Given_Request_Is_HTTP_DELETE_And_All_Ok_Then_Request_succeeds()
131             throws Exception {
132         setConditionsForPositiveSuccessFlow();
133         when(request.getHeader(anyString())).thenReturn("Basic dXNlcg==");
134         publishServlet.doDelete(request, response);
135         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
136     }
137
138     @Test
139     public void Given_Request_Is_HTTP_PUT_And_Request_succeeds()
140             throws Exception {
141         setConditionsForPositiveSuccessFlow();
142
143         publishServlet.doPut(request, response);
144         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
145     }
146
147     @Test
148     public void Given_Request_Is_HTTP_POST_And_Request_succeeds()
149             throws Exception {
150         setConditionsForPositiveSuccessFlow();
151
152         publishServlet.doPost(request, response);
153         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
154     }
155
156     @Test
157     public void Given_Request_Is_HTTP_GET_And_Request_succeeds()
158             throws Exception {
159         setConditionsForPositiveSuccessFlow();
160
161         publishServlet.doGet(request, response);
162         verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
163     }
164
165     private void setConditionsForPositiveSuccessFlow() throws Exception {
166         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true);
167         FieldUtils.writeDeclaredField(publishServlet, "next_node", 0, true);
168         FieldUtils.writeDeclaredField(publishServlet, "provstring", "", true);
169         FieldUtils.writeDeclaredField(publishServlet, "irt", new ArrayList<IngressRoute>(), true);
170         FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", new HashMap<String,String>(), true);
171         when(request.getPathInfo()).thenReturn("/123/fileName.txt");
172         PowerMockito.mockStatic(Feed.class);
173         PowerMockito.when(Feed.isFeedValid(anyInt())).thenReturn(true);
174         setPokerToNotCreateTimersWhenDeleteFeedIsCalled();
175     }
176
177     private void setPokerToNotCreateTimersWhenDeleteFeedIsCalled() throws Exception {
178         Poker poker = mock(Poker.class);
179         FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
180         when(poker.getProvisioningString()).thenReturn(buildProvisioningString());
181     }
182
183
184     private String buildProvisioningString(){
185         StringBuffer provisionString = new StringBuffer();
186         provisionString.append(START_JSON_STRING);
187         provisionString.append("'ingress':");
188         provisionString.append(START_JSON_ARRAY);
189         provisionString.append(buildIngressRoute());
190         provisionString.append(END_JSON_ARRAY);
191         provisionString.append(END_JSON_STRING);
192         return provisionString.toString();
193     }
194
195     private StringBuffer buildIngressRoute(){
196         StringBuffer provisionString = new StringBuffer();
197         provisionString.append(START_JSON_STRING);
198         provisionString.append("'seq':1");
199         provisionString.append(COMMA);
200         provisionString.append("'feedid':123");
201         provisionString.append(COMMA);
202         provisionString.append("'user':'user'");
203         provisionString.append(COMMA);
204         provisionString.append("'subnet':'127.0.0.1'");
205         provisionString.append(COMMA);
206         provisionString.append("'nodelist':-1");
207         provisionString.append(COMMA);
208         provisionString.append("'node':['1','2']");
209         provisionString.append(END_JSON_STRING);
210         return provisionString;
211     }
212
213
214 }