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";
79 String testTransactionId = "63f88b50-6345-4a61-bc59-3a48cabb60a4";
81 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
82 .andExpect(header(transactionIdHeaderName, testTransactionId))
83 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
85 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
86 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
87 .andExpect(content().string(equalTo(testResponse)));
93 public void testCredentialCacheEndpoint() throws Exception {
94 populateCredentialCache("tx1", "headername", "headervalue", CredentialType.HEADER.toString());
98 public void testPopulateHeaderFromCache() throws Exception {
99 String testTransactionId = "tx1";
100 String testUrl = "https://localhost:80/testurl";
101 String headerName = "headername";
102 String headerValue = "headervalue";
104 String testResponse = "Response from MockRestService";
106 // Populate the cache with header credentials
107 populateCredentialCache(testTransactionId, headerName, headerValue, CredentialType.HEADER.toString());
109 // Expect mock server to be called with request containing cached header
110 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
111 .andExpect(header(transactionIdHeaderName, testTransactionId))
112 .andExpect(header(headerName, headerValue))
113 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
115 // Send request to mock server with transaction Id
116 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
117 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
118 .andExpect(content().string(equalTo(testResponse)));
124 public void testHeaderAlreadyExists() throws Exception {
125 String testTransactionId = "tx1";
126 String testUrl = "https://localhost:80/testurl";
127 String headerName = "headername";
128 String headerValue = "headervalue";
129 String newHeaderValue = "newheadervalue";
131 String testResponse = "Response from MockRestService";
133 // Populate the cache with header credentials using a new value
134 populateCredentialCache(testTransactionId, headerName, newHeaderValue, CredentialType.HEADER.toString());
136 // Expect mock server to be called with request containing the original header credential value, not the cached
138 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
139 .andExpect(header(headerName, headerValue))
140 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
142 // Send request to mock server that already contains a header with same name as the one that has been cached
143 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
144 .header(transactionIdHeaderName, testTransactionId).header(headerName, headerValue))
145 .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));
151 public void testPopulateCookieFromCache() throws Exception {
152 String testTransactionId = "tx1";
153 String testUrl = "https://localhost:80/testurl";
154 String cookieName = "testcookie";
155 String cookieValue = "testcookie=testvalue";
156 String testResponse = "Response from MockRestService";
158 // Populate the cache with cookie credentials
159 populateCredentialCache(testTransactionId, cookieName, cookieValue, CredentialType.COOKIE.toString());
161 // Expect mock server to be called with request containing cached header
162 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
163 .andExpect(header(HttpHeaders.COOKIE, cookieValue))
164 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
166 // Send request to mock server with transaction Id
167 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
168 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
169 .andExpect(content().string(equalTo(testResponse)));
175 public void testCookieAlreadyExists() throws Exception {
176 String testTransactionId = "tx1";
177 String testUrl = "https://localhost:80/testurl";
178 String cookieName = "testcookie";
179 String cookieValue = "testvalue";
180 String newCookieValue = "newtestvalue";
182 String testResponse = "Response from MockRestService";
184 // Populate the cache with cookie credentials using a new value
185 populateCredentialCache(testTransactionId, cookieName, newCookieValue, CredentialType.COOKIE.toString());
187 // Expect mock server to be called with request containing the original cookie credential value, not the cached
189 mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
190 .andExpect(header(HttpHeaders.COOKIE, cookieName + "=" + cookieValue))
191 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
193 // Send request to mock server that already contains a cookie with same name as the one that has been cached
194 mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
195 .header(transactionIdHeaderName, testTransactionId).cookie(new Cookie(cookieName, cookieValue)))
196 .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));
201 private void populateCredentialCache(String transactionId, String credentialName, String credentialValue,
202 String credentialType) throws Exception {
203 String cacheUrl = "https://localhost:80/credential-cache/" + transactionId;
204 String requestBody = "{ \"credentialName\":\"" + credentialName + "\", \"credentialValue\":\"" + credentialValue
205 + "\", \"credentialType\":\"" + credentialType + "\" }";
207 // Populate the cache with credentials
208 mvc.perform(MockMvcRequestBuilders.post(cacheUrl).content(requestBody).contentType(MediaType.APPLICATION_JSON)
209 .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
210 .andExpect(content().string(equalTo(transactionId)));