81e939b929b0bfe3d0b44e6b65eaed6b8c4fbfa1
[sdc.git] / catalog-fe / src / test / java / org / openecomp / sdc / servlets / FeProxyServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.sdc.servlets;
22
23 import org.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.http.HttpFields;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.openecomp.sdc.common.api.Constants;
29 import org.openecomp.sdc.fe.config.Configuration;
30 import org.openecomp.sdc.fe.config.ConfigurationManager;
31 import org.openecomp.sdc.fe.config.PluginsConfiguration;
32 import org.openecomp.sdc.fe.servlets.FeProxyServlet;
33
34 import javax.servlet.ServletContext;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpSession;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertTrue;
43 import static org.mockito.Mockito.when;
44
45 public class FeProxyServletTest {
46         /*
47          * Example Url Mappings:
48          * http://localhost:8080/sdc1/feProxy/rest/services/MichaelTest2/0.0.1/csar
49          * --> http://localhost:8090/sdc2/rest/services/MichaelTest2/0.0.1/csar
50          * http://localhost:8080/sdc1/feProxy/dummy/not/working -->
51          * http://localhost:8090/sdc2/dummy/not/working
52          */
53         FeProxyServletForTest feProxy = new FeProxyServletForTest();
54         final static HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
55         final static HttpSession httpSession = Mockito.mock(HttpSession.class);
56         final static ServletContext servletContext = Mockito.mock(ServletContext.class);
57         final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
58         final static Configuration configuration = Mockito.mock(Configuration.class);
59         final static Configuration.OnboardingConfig onboardingConfiguration = Mockito.mock(Configuration.OnboardingConfig.class);
60         final static Request proxyRequest = Mockito.spy(Request.class);
61         final static HttpFields httpFields = Mockito.mock(HttpFields.class);
62         private static final PluginsConfiguration pluginsConfiguration = Mockito.mock(PluginsConfiguration.class);
63         private static final PluginsConfiguration.Plugin plugin = Mockito.mock(PluginsConfiguration.Plugin.class);
64
65         final static String BE_PROTOCOL = "http";
66         final static String BE_HOST = "172.20.43.124";
67         final static int BE_PORT = 8090;
68         final static String ONBOARDING_BE_PROTOCOL = "http";
69         final static String ONBOARDING_BE_HOST = "172.20.43.125";
70         final static int ONBOARDING_BE_PORT = 8091;
71         final static String WF_PROTOCOL = "http";
72         final static String WF_HOST = "172.20.43.126";
73         final static int WF_PORT = 8092;
74         final static String HEADER_1 = "Header1";
75         final static String HEADER_2 = "Header2";
76         final static String HEADER_3 = "Header3";
77         final static String HEADER_1_VAL = "Header1_Val";
78         final static String HEADER_2_VAL = "Header2_Val";
79         final static String HEADER_3_VAL = "Header3_Val";
80         final static String REQUEST_ID_VAL = "4867495a-5ed7-49e4-8be2-cc8d66fdd52b";
81
82         @BeforeClass
83         public static void beforeClass() {
84                 when(servletRequest.getSession()).thenReturn(httpSession);
85                 when(httpSession.getServletContext()).thenReturn(servletContext);
86                 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
87                 when(configurationManager.getConfiguration()).thenReturn(configuration);
88                 when(configuration.getBeProtocol()).thenReturn(BE_PROTOCOL);
89                 when(configuration.getBeHost()).thenReturn(BE_HOST);
90                 when(configuration.getBeHttpPort()).thenReturn(BE_PORT);
91                 when(configuration.getOnboarding()).thenReturn(onboardingConfiguration);
92                 when(configuration.getOnboarding().getProtocolBe()).thenReturn(ONBOARDING_BE_PROTOCOL);
93                 when(configuration.getOnboarding().getHostBe()).thenReturn(ONBOARDING_BE_HOST);
94                 when(configuration.getOnboarding().getPortBe()).thenReturn(ONBOARDING_BE_PORT);
95
96                 List<String> strList = new ArrayList<String>();
97                 strList.add(HEADER_1);
98                 strList.add(HEADER_2);
99                 strList.add(HEADER_3);
100
101                 when(servletRequest.getHeaderNames()).thenReturn(Collections.enumeration(strList));
102                 when(servletRequest.getHeader(HEADER_1)).thenReturn(HEADER_1_VAL);
103                 when(servletRequest.getHeader(HEADER_2)).thenReturn(HEADER_2_VAL);
104                 when(servletRequest.getHeader(HEADER_3)).thenReturn(HEADER_3_VAL);
105                 when(servletRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID_VAL);
106
107                 when(proxyRequest.getHeaders()).thenReturn(httpFields);
108                 when(httpFields.containsKey(HEADER_1)).thenReturn(true);
109                 when(httpFields.containsKey(HEADER_2)).thenReturn(true);
110                 when(httpFields.containsKey(HEADER_3)).thenReturn(false);
111
112                 List<PluginsConfiguration.Plugin> pluginList = new ArrayList<PluginsConfiguration.Plugin>();
113                 when(plugin.getPluginId()).thenReturn("WORKFLOW");
114                 when(plugin.getPluginSourceUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT);
115                 when(plugin.getPluginDiscoveryUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/workflows");
116                 when(plugin.getPluginFeProxyUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/workflows/wf/");
117                 when(plugin.getPluginProxyRedirectPath()).thenReturn("/wf/");
118                 pluginList.add(plugin);
119                 PluginsConfiguration.Plugin noConfigPlugin = new PluginsConfiguration.Plugin();
120                 noConfigPlugin.setPluginId("NO_CONFIG");
121                 pluginList.add(noConfigPlugin);
122                 when(configurationManager.getPluginsConfiguration()).thenReturn(pluginsConfiguration);
123                 when(pluginsConfiguration.getPluginsList()).thenReturn(pluginList);
124
125         }
126
127         @Test
128         public void testRewriteURI_APIRequest() {
129                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/dummyBeAPI");
130                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/dummyBeAPI";
131                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/dummyBeAPI";
132                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
133
134                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
135                 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/dummyBeAPI");
136
137                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
138
139                 assertTrue(rewriteURI.equals(expectedChangedUrl));
140         }
141
142         @Test
143         public void testRewriteURIWithOnboardingAPIRequest() {
144                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF");
145                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF";
146                 String expectedChangedUrl = ONBOARDING_BE_PROTOCOL + "://" + ONBOARDING_BE_HOST + ":" + ONBOARDING_BE_PORT + "/onboarding-api/gg%20g?subtype=VF";
147                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
148
149                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
150                 when(servletRequest.getServletPath()).thenReturn("/feProxy/onboarding-api/gg%20g?subtype=VF");
151
152                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
153
154                 assertTrue(rewriteURI.equals(expectedChangedUrl));
155         }
156
157
158         @Test
159         public void testRewriteURIWithQureyParam_APIRequest() {
160                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/dcae-api/gg%20g?subtype=VF");
161                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/dcae-api/gg%20g?subtype=VF";
162                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/dcae-api/gg%20g?subtype=VF";
163                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
164
165                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
166                 when(servletRequest.getServletPath()).thenReturn("/feProxy/dcae-api/gg%20g?subtype=VF");
167
168                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
169
170                 assertTrue(rewriteURI.equals(expectedChangedUrl));
171         }
172
173         @Test
174         public void testRewriteTargetWithRedeirectAPIRequest() {
175                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/gg%20g?subtype=VF");
176                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/gg%20g?subtype=VF";
177                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/gg%20g?subtype=VF";
178                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
179
180                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
181                 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/gg%20g?subtype=VF");
182
183                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
184
185                 assertTrue(rewriteURI.equals(expectedChangedUrl));
186         }
187
188         @Test
189         public void testRewriteURIWithWFAPIRequest() {
190                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/plugin/wf/workflows");
191                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/plugin/wf/workflows";
192                 String expectedChangedUrl = WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/workflows/wf/workflows";
193                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
194
195                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
196                 when(servletRequest.getServletPath()).thenReturn("/feProxy/plugin/wf/workflows");
197
198                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
199
200                 assertEquals(expectedChangedUrl, rewriteURI);
201
202                 // now test in case it did not go through the plugin
203                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/wf/workflows");
204                 requestResourceUrl = "http://localhost:8080/sdc1/feProxy/wf/workflows";
205                 expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/wf/workflows";
206                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
207
208                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
209                 when(servletRequest.getServletPath()).thenReturn("/feProxy/wf/workflows");
210
211                 rewriteURI = feProxy.rewriteTarget(servletRequest);
212
213                 assertEquals(expectedChangedUrl, rewriteURI);
214
215         }
216
217         /**
218          * class for testing only exposes the protected method.
219          */
220         public static class FeProxyServletForTest extends FeProxyServlet{
221
222                 @Override
223                 public String rewriteTarget(HttpServletRequest request) {
224                         return super.rewriteTarget(request);
225                 }
226         }
227 }