2 * ============LICENSE_START=======================================================
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
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 package org.onap.aaf.cadi.sidecar.fproxy.test;
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 import javax.servlet.http.Cookie;
30 import org.eclipse.jetty.util.security.Password;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.aaf.cadi.sidecar.fproxy.data.CredentialCacheData.CredentialType;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.MediaType;
42 import org.springframework.test.context.junit4.SpringRunner;
43 import org.springframework.test.web.client.MockRestServiceServer;
44 import org.springframework.test.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
46 import org.springframework.web.client.RestTemplate;
48 @RunWith(SpringRunner.class)
51 public class FProxyServiceTest {
54 System.setProperty("server.ssl.key-store-password",
55 Password.deobfuscate("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10"));
56 System.setProperty("server.ssl.trust-store-password",
57 Password.deobfuscate("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10"));
60 @Value("${transactionid.header.name}")
61 private String transactionIdHeaderName;
67 private RestTemplate restTemplate;
69 private MockRestServiceServer mockServer;
73 mockServer = MockRestServiceServer.createServer(restTemplate);
77 public void testRequestFrowarding() throws Exception {
78 String testUrl = "https://localhost:80/testurl";
79 String testResponse = "Response from MockRestService";
80 String testTransactionId = "63f88b50-6345-4a61-bc59-3a48cabb60a4";
82 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
83 .andExpect(header(transactionIdHeaderName, testTransactionId))
84 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
86 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
87 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
88 .andExpect(content().string(equalTo(testResponse)));
94 public void testCredentialCacheEndpoint() throws Exception {
95 populateCredentialCache("tx1", "headername", "headervalue", CredentialType.HEADER.toString());
99 public void testPopulateHeaderFromCache() throws Exception {
100 String testTransactionId = "tx1";
101 String testUrl = "https://localhost:80/testurl";
102 String headerName = "headername";
103 String headerValue = "headervalue";
105 String testResponse = "Response from MockRestService";
107 // Populate the cache with header credentials
108 populateCredentialCache(testTransactionId, headerName, headerValue, CredentialType.HEADER.toString());
110 // Expect mock server to be called with request containing cached header
111 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
112 .andExpect(header(transactionIdHeaderName, testTransactionId))
113 .andExpect(header(headerName, headerValue))
114 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
116 // Send request to mock server with transaction Id
117 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
118 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
119 .andExpect(content().string(equalTo(testResponse)));
125 public void testHeaderAlreadyExists() throws Exception {
126 String testTransactionId = "tx1";
127 String testUrl = "https://localhost:80/testurl";
128 String headerName = "headername";
129 String headerValue = "headervalue";
130 String newHeaderValue = "newheadervalue";
132 String testResponse = "Response from MockRestService";
134 // Populate the cache with header credentials using a new value
135 populateCredentialCache(testTransactionId, headerName, newHeaderValue, CredentialType.HEADER.toString());
137 // Expect mock server to be called with request containing the original header credential value, not the cached
139 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
140 .andExpect(header(headerName, headerValue))
141 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
143 // Send request to mock server that already contains a header with same name as the one that has been cached
144 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
145 .header(transactionIdHeaderName, testTransactionId).header(headerName, headerValue))
146 .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));
152 public void testPopulateCookieFromCache() throws Exception {
153 String testTransactionId = "tx1";
154 String testUrl = "https://localhost:80/testurl";
155 String cookieName = "testcookie";
156 String cookieValue = "testcookie=testvalue";
157 String testResponse = "Response from MockRestService";
159 // Populate the cache with cookie credentials
160 populateCredentialCache(testTransactionId, cookieName, cookieValue, CredentialType.COOKIE.toString());
162 // Expect mock server to be called with request containing cached header
163 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
164 .andExpect(header(HttpHeaders.COOKIE, cookieValue))
165 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
167 // Send request to mock server with transaction Id
168 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
169 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
170 .andExpect(content().string(equalTo(testResponse)));
176 public void testCookieAlreadyExists() throws Exception {
177 String testTransactionId = "tx1";
178 String testUrl = "https://localhost:80/testurl";
179 String cookieName = "testcookie";
180 String cookieValue = "testvalue";
181 String newCookieValue = "newtestvalue";
183 String testResponse = "Response from MockRestService";
185 // Populate the cache with cookie credentials using a new value
186 populateCredentialCache(testTransactionId, cookieName, newCookieValue, CredentialType.COOKIE.toString());
188 // Expect mock server to be called with request containing the original cookie credential value, not the cached
190 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
191 .andExpect(header(HttpHeaders.COOKIE, cookieName + "=" + cookieValue))
192 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
194 // Send request to mock server that already contains a cookie with same name as the one that has been cached
195 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
196 .header(transactionIdHeaderName, testTransactionId).cookie(new Cookie(cookieName, cookieValue)))
197 .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));
202 private void populateCredentialCache(String transactionId, String credentialName, String credentialValue,
203 String credentialType) throws Exception {
204 String cacheUrl = "https://localhost:80/credential-cache/" + transactionId;
205 String requestBody = "{ \"credentialName\":\"" + credentialName + "\", \"credentialValue\":\"" + credentialValue
206 + "\", \"credentialType\":\"" + credentialType + "\" }";
208 // Populate the cache with credentials
209 mvc.perform(MockMvcRequestBuilders.post(cacheUrl).content(requestBody).contentType(MediaType.APPLICATION_JSON)
210 .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
211 .andExpect(content().string(equalTo(transactionId)));