Add forward proxy code
[aaf/cadi.git] / sidecar / fproxy / src / test / java / org / onap / aaf / fproxy / FProxyServiceTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aaf
4  * ================================================================================
5  * Copyright © 2018 European Software Marketing Ltd.
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 package org.onap.aaf.fproxy;
21
22 import static org.hamcrest.Matchers.equalTo;
23 import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
24 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
25 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
26 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29
30 import javax.servlet.http.Cookie;
31 import org.eclipse.jetty.util.security.Password;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.aaf.fproxy.data.CredentialCacheData.CredentialType;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.http.HttpMethod;
42 import org.springframework.http.MediaType;
43 import org.springframework.test.context.junit4.SpringRunner;
44 import org.springframework.test.web.client.MockRestServiceServer;
45 import org.springframework.test.web.servlet.MockMvc;
46 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
47 import org.springframework.web.client.RestTemplate;
48
49 @RunWith(SpringRunner.class)
50 @SpringBootTest
51 @AutoConfigureMockMvc
52 public class FProxyServiceTest {
53
54     static {
55         System.setProperty("server.ssl.key-store-password",
56                 Password.deobfuscate("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10"));
57     }
58
59     @Value("${transactionid.header.name}")
60     private String transactionIdHeaderName;
61
62     @Autowired
63     private MockMvc mvc;
64
65     @Autowired
66     private RestTemplate restTemplate;
67
68     private MockRestServiceServer mockServer;
69
70     @Before
71     public void setUp() {
72         mockServer = MockRestServiceServer.createServer(restTemplate);
73     }
74
75     @Test
76     public void testRequestFrowarding() throws Exception {
77         String testUrl = "https://localhost:80/testurl";
78         String testResponse = "Response from MockRestService";
79
80         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
81                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
82
83         mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
84                 .andExpect(content().string(equalTo(testResponse)));
85
86         mockServer.verify();
87     }
88
89     @Test
90     public void testCredentialCacheEndpoint() throws Exception {
91         populateCredentialCache("tx1", "headername", "headervalue", CredentialType.HEADER.toString());
92     }
93
94     @Test
95     public void testPopulateHeaderFromCache() throws Exception {
96         String testTransactionId = "tx1";
97         String testUrl = "https://localhost:80/testurl";
98         String headerName = "headername";
99         String headerValue = "headervalue";
100
101         String testResponse = "Response from MockRestService";
102
103         // Populate the cache with header credentials
104         populateCredentialCache(testTransactionId, headerName, headerValue, CredentialType.HEADER.toString());
105
106         // Expect mock server to be called with request containing cached header
107         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
108                 .andExpect(header(headerName, headerValue))
109                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
110
111         // Send request to mock server with transaction Id
112         mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
113                 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
114                 .andExpect(content().string(equalTo(testResponse)));
115
116         mockServer.verify();
117     }
118
119     @Test
120     public void testHeaderAlreadyExists() throws Exception {
121         String testTransactionId = "tx1";
122         String testUrl = "https://localhost:80/testurl";
123         String headerName = "headername";
124         String headerValue = "headervalue";
125         String newHeaderValue = "newheadervalue";
126
127         String testResponse = "Response from MockRestService";
128
129         // Populate the cache with header credentials using a new value
130         populateCredentialCache(testTransactionId, headerName, newHeaderValue, CredentialType.HEADER.toString());
131
132         // Expect mock server to be called with request containing the original header credential value, not the cached
133         // new header value
134         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
135                 .andExpect(header(headerName, headerValue))
136                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
137
138         // Send request to mock server that already contains a header with same name as the one that has been cached
139         mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
140                 .header(transactionIdHeaderName, testTransactionId).header(headerName, headerValue))
141                 .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));
142
143         mockServer.verify();
144     }
145
146     @Test
147     public void testPopulateCookieFromCache() throws Exception {
148         String testTransactionId = "tx1";
149         String testUrl = "https://localhost:80/testurl";
150         String cookieName = "testcookie";
151         String cookieValue = "testcookie=testvalue";
152         String testResponse = "Response from MockRestService";
153
154         // Populate the cache with cookie credentials
155         populateCredentialCache(testTransactionId, cookieName, cookieValue, CredentialType.COOKIE.toString());
156
157         // Expect mock server to be called with request containing cached header
158         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
159                 .andExpect(header(HttpHeaders.COOKIE, cookieValue))
160                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
161
162         // Send request to mock server with transaction Id
163         mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
164                 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
165                 .andExpect(content().string(equalTo(testResponse)));
166
167         mockServer.verify();
168     }
169
170     @Test
171     public void testCookieAlreadyExists() throws Exception {
172         String testTransactionId = "tx1";
173         String testUrl = "https://localhost:80/testurl";
174         String cookieName = "testcookie";
175         String cookieValue = "testvalue";
176         String newCookieValue = "newtestvalue";
177
178         String testResponse = "Response from MockRestService";
179
180         // Populate the cache with cookie credentials using a new value
181         populateCredentialCache(testTransactionId, cookieName, newCookieValue, CredentialType.COOKIE.toString());
182
183         // Expect mock server to be called with request containing the original cookie credential value, not the cached
184         // new cookie value
185         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
186                 .andExpect(header(HttpHeaders.COOKIE, cookieName + "=" + cookieValue))
187                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
188
189         // Send request to mock server that already contains a cookie with same name as the one that has been cached
190         mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
191                 .header(transactionIdHeaderName, testTransactionId).cookie(new Cookie(cookieName, cookieValue)))
192                 .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));
193
194         mockServer.verify();
195     }
196
197     private void populateCredentialCache(String transactionId, String credentialName, String credentialValue,
198             String credentialType) throws Exception {
199         String cacheUrl = "https://localhost:80/credential-cache/" + transactionId;
200         String requestBody = "{ \"credentialName\":\"" + credentialName + "\", \"credentialValue\":\"" + credentialValue
201                 + "\", \"credentialType\":\"" + credentialType + "\" }";
202
203         // Populate the cache with credentials
204         mvc.perform(MockMvcRequestBuilders.post(cacheUrl).content(requestBody).contentType(MediaType.APPLICATION_JSON)
205                 .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
206                 .andExpect(content().string(equalTo(transactionId)));
207     }
208 }