Create on boarding docker
[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 static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import java.net.URI;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpSession;
36
37 import org.eclipse.jetty.client.api.Request;
38 import org.eclipse.jetty.http.HttpFields;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.openecomp.sdc.common.api.Constants;
43 import org.openecomp.sdc.fe.config.Configuration;
44 import org.openecomp.sdc.fe.config.ConfigurationManager;
45 import org.openecomp.sdc.fe.servlets.FeProxyServlet;
46
47 public class FeProxyServletTest {
48         /*
49          * Example Url Mappings:
50          * http://localhost:8080/sdc1/feProxy/rest/services/MichaelTest2/0.0.1/csar
51          * --> http://localhost:8090/sdc2/rest/services/MichaelTest2/0.0.1/csar
52          * http://localhost:8080/sdc1/feProxy/dummy/not/working -->
53          * http://localhost:8090/sdc2/dummy/not/working
54          */
55         FeProxyServletForTest feProxy = new FeProxyServletForTest();
56         final static HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
57         final static HttpSession httpSession = Mockito.mock(HttpSession.class);
58         final static ServletContext servletContext = Mockito.mock(ServletContext.class);
59         final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
60         final static Configuration configuration = Mockito.mock(Configuration.class);
61     final static Configuration.OnboardingConfig onboardingConfiguration = Mockito.mock(Configuration.OnboardingConfig.class);
62         final static Request proxyRequest = Mockito.spy(Request.class);
63         final static HttpFields httpFields = Mockito.mock(HttpFields.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 HEADER_1 = "Header1";
72         final static String HEADER_2 = "Header2";
73         final static String HEADER_3 = "Header3";
74         final static String HEADER_1_VAL = "Header1_Val";
75         final static String HEADER_2_VAL = "Header2_Val";
76         final static String HEADER_3_VAL = "Header3_Val";
77         final static String REQUEST_ID_VAL = "4867495a-5ed7-49e4-8be2-cc8d66fdd52b";
78
79         @BeforeClass
80         public static void beforeClass() {
81                 when(servletRequest.getSession()).thenReturn(httpSession);
82                 when(httpSession.getServletContext()).thenReturn(servletContext);
83                 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
84                 when(configurationManager.getConfiguration()).thenReturn(configuration);
85                 when(configuration.getBeProtocol()).thenReturn(BE_PROTOCOL);
86                 when(configuration.getBeHost()).thenReturn(BE_HOST);
87                 when(configuration.getBeHttpPort()).thenReturn(BE_PORT);
88                 when(configuration.getOnboarding()).thenReturn(onboardingConfiguration);
89                 when(configuration.getOnboarding().getProtocolBe()).thenReturn(ONBOARDING_BE_PROTOCOL);
90                 when(configuration.getOnboarding().getHostBe()).thenReturn(ONBOARDING_BE_HOST);
91                 when(configuration.getOnboarding().getPortBe()).thenReturn(ONBOARDING_BE_PORT);
92
93                 List<String> strList = new ArrayList<String>();
94                 strList.add(HEADER_1);
95                 strList.add(HEADER_2);
96                 strList.add(HEADER_3);
97
98                 when(servletRequest.getHeaderNames()).thenReturn(Collections.enumeration(strList));
99                 when(servletRequest.getHeader(HEADER_1)).thenReturn(HEADER_1_VAL);
100                 when(servletRequest.getHeader(HEADER_2)).thenReturn(HEADER_2_VAL);
101                 when(servletRequest.getHeader(HEADER_3)).thenReturn(HEADER_3_VAL);
102                 when(servletRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID_VAL);
103
104                 when(proxyRequest.getHeaders()).thenReturn(httpFields);
105                 when(httpFields.containsKey(HEADER_1)).thenReturn(true);
106                 when(httpFields.containsKey(HEADER_2)).thenReturn(true);
107                 when(httpFields.containsKey(HEADER_3)).thenReturn(false);
108
109         }
110
111         @Test
112         public void testRewriteURI_APIRequest() {
113                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/dummyBeAPI");
114                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/dummyBeAPI";
115                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/dummyBeAPI";
116                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
117
118                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
119                 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/dummyBeAPI");
120
121                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
122
123                 assertTrue(rewriteURI.equals(expectedChangedUrl));
124         }
125
126         @Test
127         public void testRewriteURIWithOnboardingAPIRequest() {
128                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF");
129                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF";
130                 String expectedChangedUrl = ONBOARDING_BE_PROTOCOL + "://" + ONBOARDING_BE_HOST + ":" + ONBOARDING_BE_PORT + "/onboarding-api/gg%20g?subtype=VF";
131                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
132
133                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
134                 when(servletRequest.getServletPath()).thenReturn("/feProxy/onboarding-api/gg%20g?subtype=VF");
135
136                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
137
138                 assertTrue(rewriteURI.equals(expectedChangedUrl));
139         }
140
141
142         @Test
143         public void testRewriteURIWithQureyParam_APIRequest() {
144                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/dcae-api/gg%20g?subtype=VF");
145                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/dcae-api/gg%20g?subtype=VF";
146                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/dcae-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/dcae-api/gg%20g?subtype=VF");
151
152                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
153
154                 assertTrue(rewriteURI.equals(expectedChangedUrl));
155         }
156
157         @Test
158         public void testRewriteTargetWithRedeirectAPIRequest() {
159                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/gg%20g?subtype=VF");
160                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/gg%20g?subtype=VF";
161                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/gg%20g?subtype=VF";
162                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
163
164                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
165                 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/gg%20g?subtype=VF");
166
167                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
168
169                 assertTrue(rewriteURI.equals(expectedChangedUrl));
170         }
171
172
173
174         @Test
175         public void testCustomizeProxyRequest() {
176                 feProxy.customizeProxyRequest(proxyRequest, servletRequest);
177                 verify(proxyRequest).header(HEADER_3, HEADER_3_VAL);
178                 verify(proxyRequest, times(1)).header(Mockito.anyString(), Mockito.anyString());
179
180         }
181
182         /**
183          * class for testing only exposes the protected method.
184          */
185         public static class FeProxyServletForTest extends FeProxyServlet{
186
187                 @Override
188                 public String rewriteTarget(HttpServletRequest request) {
189                         return super.rewriteTarget(request);
190                 }
191         }
192 }