All to 2.1.7-SNAPSHOT
[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         String testTransactionId = "63f88b50-6345-4a61-bc59-3a48cabb60a4";
80
81         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
82                 .andExpect(header(transactionIdHeaderName, testTransactionId))
83                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
84
85         mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
86                 .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
87                 .andExpect(content().string(equalTo(testResponse)));
88
89         mockServer.verify();
90     }
91
92     @Test
93     public void testCredentialCacheEndpoint() throws Exception {
94         populateCredentialCache("tx1", "headername", "headervalue", CredentialType.HEADER.toString());
95     }
96
97     @Test
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";
103
104         String testResponse = "Response from MockRestService";
105
106         // Populate the cache with header credentials
107         populateCredentialCache(testTransactionId, headerName, headerValue, CredentialType.HEADER.toString());
108
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));
114
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)));
119
120         mockServer.verify();
121     }
122
123     @Test
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";
130
131         String testResponse = "Response from MockRestService";
132
133         // Populate the cache with header credentials using a new value
134         populateCredentialCache(testTransactionId, headerName, newHeaderValue, CredentialType.HEADER.toString());
135
136         // Expect mock server to be called with request containing the original header credential value, not the cached
137         // new header value
138         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
139                 .andExpect(header(headerName, headerValue))
140                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
141
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)));
146
147         mockServer.verify();
148     }
149
150     @Test
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";
157
158         // Populate the cache with cookie credentials
159         populateCredentialCache(testTransactionId, cookieName, cookieValue, CredentialType.COOKIE.toString());
160
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));
165
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)));
170
171         mockServer.verify();
172     }
173
174     @Test
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";
181
182         String testResponse = "Response from MockRestService";
183
184         // Populate the cache with cookie credentials using a new value
185         populateCredentialCache(testTransactionId, cookieName, newCookieValue, CredentialType.COOKIE.toString());
186
187         // Expect mock server to be called with request containing the original cookie credential value, not the cached
188         // new cookie value
189         mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
190                 .andExpect(header(HttpHeaders.COOKIE, cookieName + "=" + cookieValue))
191                 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
192
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)));
197
198         mockServer.verify();
199     }
200
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 + "\" }";
206
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)));
211     }
212 }