Update FProxy to separate truststore and keystore
[aaf/cadi.git] / sidecar / fproxy / src / test / java / org / onap / aaf / cadi / sidecar / fproxy / test / 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.cadi.sidecar.fproxy.test;
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 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;
47
48 @RunWith(SpringRunner.class)
49 @SpringBootTest
50 @AutoConfigureMockMvc
51 public class FProxyServiceTest {
52
53     static {
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"));
58     }
59
60     @Value("${transactionid.header.name}")
61     private String transactionIdHeaderName;
62
63     @Autowired
64     private MockMvc mvc;
65
66     @Autowired
67     private RestTemplate restTemplate;
68
69     private MockRestServiceServer mockServer;
70
71     @Before
72     public void setUp() {
73         mockServer = MockRestServiceServer.createServer(restTemplate);
74     }
75
76     @Test
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";
81
82         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
83                 .andExpect(header(transactionIdHeaderName, testTransactionId))
84                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
85
86         mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
87                 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
88                 .andExpect(content().string(equalTo(testResponse)));
89
90         mockServer.verify();
91     }
92
93     @Test
94     public void testCredentialCacheEndpoint() throws Exception {
95         populateCredentialCache("tx1", "headername", "headervalue", CredentialType.HEADER.toString());
96     }
97
98     @Test
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";
104
105         String testResponse = "Response from MockRestService";
106
107         // Populate the cache with header credentials
108         populateCredentialCache(testTransactionId, headerName, headerValue, CredentialType.HEADER.toString());
109
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));
115
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)));
120
121         mockServer.verify();
122     }
123
124     @Test
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";
131
132         String testResponse = "Response from MockRestService";
133
134         // Populate the cache with header credentials using a new value
135         populateCredentialCache(testTransactionId, headerName, newHeaderValue, CredentialType.HEADER.toString());
136
137         // Expect mock server to be called with request containing the original header credential value, not the cached
138         // new header value
139         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
140                 .andExpect(header(headerName, headerValue))
141                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
142
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)));
147
148         mockServer.verify();
149     }
150
151     @Test
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";
158
159         // Populate the cache with cookie credentials
160         populateCredentialCache(testTransactionId, cookieName, cookieValue, CredentialType.COOKIE.toString());
161
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));
166
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)));
171
172         mockServer.verify();
173     }
174
175     @Test
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";
182
183         String testResponse = "Response from MockRestService";
184
185         // Populate the cache with cookie credentials using a new value
186         populateCredentialCache(testTransactionId, cookieName, newCookieValue, CredentialType.COOKIE.toString());
187
188         // Expect mock server to be called with request containing the original cookie credential value, not the cached
189         // new cookie value
190         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
191                 .andExpect(header(HttpHeaders.COOKIE, cookieName + "=" + cookieValue))
192                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
193
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)));
198
199         mockServer.verify();
200     }
201
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 + "\" }";
207
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)));
212     }
213 }