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.rproxy;
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.annotation.Resource;
31 import org.eclipse.jetty.http.HttpHeader;
32 import org.eclipse.jetty.util.security.Password;
33 import org.hamcrest.Matchers;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.aaf.rproxy.config.ForwardProxyProperties;
38 import org.onap.aaf.rproxy.config.PrimaryServiceProperties;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
44 import org.springframework.http.HttpMethod;
45 import org.springframework.http.MediaType;
46 import org.springframework.test.context.ContextConfiguration;
47 import org.springframework.test.context.TestPropertySource;
48 import org.springframework.test.context.junit4.SpringRunner;
49 import org.springframework.test.web.client.MockRestServiceServer;
50 import org.springframework.test.web.client.match.MockRestRequestMatchers;
51 import org.springframework.test.web.servlet.MockMvc;
52 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
53 import org.springframework.web.client.RestTemplate;
55 @RunWith(SpringRunner.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
58 @TestPropertySource(locations = {"classpath:primary-service.properties", "classpath:forward-proxy.properties"})
59 @ContextConfiguration(classes = ReverseProxyTestConfig.class)
60 public class ReverseProxyApplicationTest {
63 System.setProperty("server.ssl.key-store-password",
64 Password.deobfuscate("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10"));
67 @Value("${transactionid.header.name}")
68 private String transactionIdHeaderName;
70 @Resource(name = "PrimaryServiceProperties")
71 private PrimaryServiceProperties primaryServiceProps;
73 @Resource(name = "ForwardProxyProperties")
74 private ForwardProxyProperties forwardProxyProps;
77 private MockMvc mockMvc;
80 private RestTemplate restTemplate;
82 private MockRestServiceServer mockServer;
84 private String primaryServiceBaseUrl;
85 private String forwardProxyBaseUrl;
88 public void setUp() throws Exception {
89 mockServer = MockRestServiceServer.createServer(restTemplate);
90 primaryServiceBaseUrl = primaryServiceProps.getProtocol() + "://" + primaryServiceProps.getHost() + ":"
91 + primaryServiceProps.getPort();
92 forwardProxyBaseUrl = forwardProxyProps.getProtocol() + "://" + forwardProxyProps.getHost() + ":"
93 + forwardProxyProps.getPort() + forwardProxyProps.getCacheurl() + "/";
97 public void checkForwardRequest() throws Exception {
99 String transactionId = "63f88b50-6345-4a61-bc59-3a48cabb60a4";
100 String testUrl = "/aai/v13/cloud-infrastructure/cloud-regions";
101 String testResponse = "Response from MockRestService";
103 mockServer.expect(requestTo(primaryServiceBaseUrl + testUrl)).andExpect(method(HttpMethod.GET))
104 .andExpect(header(transactionIdHeaderName, transactionId))
105 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
107 // Send request to mock server with transaction Id
108 mockMvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
109 .header(transactionIdHeaderName, transactionId)).andExpect(status().isOk())
110 .andExpect(content().string(equalTo(testResponse)));
116 public void checkTransactionIdIsSetIfEmptyInRequest() throws Exception {
118 String testUrl = "/aai/v13/cloud-infrastructure/cloud-regions";
119 String testResponse = "Response from MockRestService";
121 mockServer.expect(requestTo(primaryServiceBaseUrl + testUrl)).andExpect(method(HttpMethod.GET))
122 .andExpect(header(transactionIdHeaderName, Matchers.any(String.class)))
123 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
125 // Send request to mock server without transaction Id
126 mockMvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON))
127 .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));
133 public void checkBasicAuthCaching() throws Exception {
135 String transactionId = "63f88b50-6345-4a61-bc59-3a48cabb60a4";
136 String testUrl = "/aai/v13/cloud-infrastructure/cloud-regions";
137 String testResponse = "Response from MockRestService";
138 String testAuthValue = "testAuthValue";
139 String testCachePayload = "{ \"credentialName\":" + HttpHeader.AUTHORIZATION + ", \"credentialValue\":"
140 + testAuthValue + ", \"credentialType\":\"HEADER\" }";
142 mockServer.expect(requestTo(forwardProxyBaseUrl + transactionId)).andExpect(method(HttpMethod.POST))
143 .andExpect(MockRestRequestMatchers.content().json(testCachePayload))
144 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
146 mockServer.expect(requestTo(primaryServiceBaseUrl + testUrl)).andExpect(method(HttpMethod.GET))
147 .andExpect(header(transactionIdHeaderName, transactionId))
148 .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));
150 // Send request to mock server with transaction Id
151 mockMvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
152 .header(transactionIdHeaderName, transactionId)
153 .header(HttpHeader.AUTHORIZATION.asString(), testAuthValue)).andExpect(status().isOk())
154 .andExpect(content().string(equalTo(testResponse)));