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.fproxy;
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;
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;
49 @RunWith(SpringRunner.class)
52 public class FProxyServiceTest {
55 System.setProperty("server.ssl.key-store-password",
56 Password.deobfuscate("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10"));
59 @Value("${transactionid.header.name}")
60 private String transactionIdHeaderName;
66 private RestTemplate restTemplate;
68 private MockRestServiceServer mockServer;
72 mockServer = MockRestServiceServer.createServer(restTemplate);
76 public void testRequestFrowarding() throws Exception {
77 String testUrl = "https://localhost:80/testurl";
78 String testResponse = "Response from MockRestService";
80 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
81 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
83 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
84 .andExpect(content().string(equalTo(testResponse)));
90 public void testCredentialCacheEndpoint() throws Exception {
91 populateCredentialCache("tx1", "headername", "headervalue", CredentialType.HEADER.toString());
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";
101 String testResponse = "Response from MockRestService";
103 // Populate the cache with header credentials
104 populateCredentialCache(testTransactionId, headerName, headerValue, CredentialType.HEADER.toString());
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));
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)));
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";
127 String testResponse = "Response from MockRestService";
129 // Populate the cache with header credentials using a new value
130 populateCredentialCache(testTransactionId, headerName, newHeaderValue, CredentialType.HEADER.toString());
132 // Expect mock server to be called with request containing the original header credential value, not the cached
134 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
135 .andExpect(header(headerName, headerValue))
136 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
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)));
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";
154 // Populate the cache with cookie credentials
155 populateCredentialCache(testTransactionId, cookieName, cookieValue, CredentialType.COOKIE.toString());
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));
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)));
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";
178 String testResponse = "Response from MockRestService";
180 // Populate the cache with cookie credentials using a new value
181 populateCredentialCache(testTransactionId, cookieName, newCookieValue, CredentialType.COOKIE.toString());
183 // Expect mock server to be called with request containing the original cookie credential value, not the cached
185 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
186 .andExpect(header(HttpHeaders.COOKIE, cookieName + "=" + cookieValue))
187 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
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)));
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 + "\" }";
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)));