1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning;
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;
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;
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;
55 * Created by ezcoxem on 21/08/2018.
58 @RunWith(PowerMockRunner.class)
59 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Feed")
60 public class PublishServletTest extends DrServletTestBase {
61 private PublishServlet publishServlet;
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 = ",";
70 private HttpServletRequest request;
73 private HttpServletResponse response;
76 public void setUp() throws Exception {
78 publishServlet = new PublishServlet();
82 public void Given_Request_Is_HTTP_DELETE_And_There_Are_No_Nodes_Then_Service_Unavailable_Error_Is_Returned()
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)));
90 public void Given_Request_Is_HTTP_DELETE_And_Path_Is_Null_Then_Not_Found_Error_Is_Returned()
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)));
98 public void Given_Request_Is_HTTP_DELETE_And_Ix_Is_Null_Then_Not_Found_Error_Is_Returned()
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)));
107 public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_Valid_Then_Not_Found_Error_Is_Returned()
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)));
118 public void Given_Request_Is_HTTP_DELETE_And_Feed_Is_Not_A_Number_Then_Not_Found_Error_Is_Returned()
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)));
130 public void Given_Request_Is_HTTP_DELETE_And_All_Ok_Then_Request_succeeds()
132 setConditionsForPositiveSuccessFlow();
133 when(request.getHeader(anyString())).thenReturn("Basic dXNlcg==");
134 publishServlet.doDelete(request, response);
135 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
139 public void Given_Request_Is_HTTP_PUT_And_Request_succeeds()
141 setConditionsForPositiveSuccessFlow();
143 publishServlet.doPut(request, response);
144 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
148 public void Given_Request_Is_HTTP_POST_And_Request_succeeds()
150 setConditionsForPositiveSuccessFlow();
152 publishServlet.doPost(request, response);
153 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
157 public void Given_Request_Is_HTTP_GET_And_Request_succeeds()
159 setConditionsForPositiveSuccessFlow();
161 publishServlet.doGet(request, response);
162 verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY));
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();
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());
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();
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;