aboutsummaryrefslogtreecommitdiffstats
path: root/sidecar/fproxy/src/test/java/org/onap/aaf/fproxy/FProxyServiceTest.java
blob: ccf13e9be7c01255deaa349edb1d86d4e418f26e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 * ============LICENSE_START=======================================================
 * org.onap.aaf
 * ================================================================================
 * Copyright © 2018 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aaf.fproxy;

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import javax.servlet.http.Cookie;
import org.eclipse.jetty.util.security.Password;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.aaf.fproxy.data.CredentialCacheData.CredentialType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class FProxyServiceTest {

    static {
        System.setProperty("server.ssl.key-store-password",
                Password.deobfuscate("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10"));
    }

    @Value("${transactionid.header.name}")
    private String transactionIdHeaderName;

    @Autowired
    private MockMvc mvc;

    @Autowired
    private RestTemplate restTemplate;

    private MockRestServiceServer mockServer;

    @Before
    public void setUp() {
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }

    @Test
    public void testRequestFrowarding() throws Exception {
        String testUrl = "https://localhost:80/testurl";
        String testResponse = "Response from MockRestService";

        mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
                .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));

        mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
                .andExpect(content().string(equalTo(testResponse)));

        mockServer.verify();
    }

    @Test
    public void testCredentialCacheEndpoint() throws Exception {
        populateCredentialCache("tx1", "headername", "headervalue", CredentialType.HEADER.toString());
    }

    @Test
    public void testPopulateHeaderFromCache() throws Exception {
        String testTransactionId = "tx1";
        String testUrl = "https://localhost:80/testurl";
        String headerName = "headername";
        String headerValue = "headervalue";

        String testResponse = "Response from MockRestService";

        // Populate the cache with header credentials
        populateCredentialCache(testTransactionId, headerName, headerValue, CredentialType.HEADER.toString());

        // Expect mock server to be called with request containing cached header
        mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
                .andExpect(header(headerName, headerValue))
                .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));

        // Send request to mock server with transaction Id
        mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
                .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
                .andExpect(content().string(equalTo(testResponse)));

        mockServer.verify();
    }

    @Test
    public void testHeaderAlreadyExists() throws Exception {
        String testTransactionId = "tx1";
        String testUrl = "https://localhost:80/testurl";
        String headerName = "headername";
        String headerValue = "headervalue";
        String newHeaderValue = "newheadervalue";

        String testResponse = "Response from MockRestService";

        // Populate the cache with header credentials using a new value
        populateCredentialCache(testTransactionId, headerName, newHeaderValue, CredentialType.HEADER.toString());

        // Expect mock server to be called with request containing the original header credential value, not the cached
        // new header value
        mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
                .andExpect(header(headerName, headerValue))
                .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));

        // Send request to mock server that already contains a header with same name as the one that has been cached
        mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
                .header(transactionIdHeaderName, testTransactionId).header(headerName, headerValue))
                .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));

        mockServer.verify();
    }

    @Test
    public void testPopulateCookieFromCache() throws Exception {
        String testTransactionId = "tx1";
        String testUrl = "https://localhost:80/testurl";
        String cookieName = "testcookie";
        String cookieValue = "testcookie=testvalue";
        String testResponse = "Response from MockRestService";

        // Populate the cache with cookie credentials
        populateCredentialCache(testTransactionId, cookieName, cookieValue, CredentialType.COOKIE.toString());

        // Expect mock server to be called with request containing cached header
        mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
                .andExpect(header(HttpHeaders.COOKIE, cookieValue))
                .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));

        // Send request to mock server with transaction Id
        mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
                .header(transactionIdHeaderName, testTransactionId)).andExpect(status().isOk())
                .andExpect(content().string(equalTo(testResponse)));

        mockServer.verify();
    }

    @Test
    public void testCookieAlreadyExists() throws Exception {
        String testTransactionId = "tx1";
        String testUrl = "https://localhost:80/testurl";
        String cookieName = "testcookie";
        String cookieValue = "testvalue";
        String newCookieValue = "newtestvalue";

        String testResponse = "Response from MockRestService";

        // Populate the cache with cookie credentials using a new value
        populateCredentialCache(testTransactionId, cookieName, newCookieValue, CredentialType.COOKIE.toString());

        // Expect mock server to be called with request containing the original cookie credential value, not the cached
        // new cookie value
        mockServer.expect(requestTo(testUrl)).andExpect(method(HttpMethod.GET))
                .andExpect(header(HttpHeaders.COOKIE, cookieName + "=" + cookieValue))
                .andRespond(withSuccess(testResponse, MediaType.APPLICATION_JSON));

        // Send request to mock server that already contains a cookie with same name as the one that has been cached
        mvc.perform(MockMvcRequestBuilders.get(testUrl).accept(MediaType.APPLICATION_JSON)
                .header(transactionIdHeaderName, testTransactionId).cookie(new Cookie(cookieName, cookieValue)))
                .andExpect(status().isOk()).andExpect(content().string(equalTo(testResponse)));

        mockServer.verify();
    }

    private void populateCredentialCache(String transactionId, String credentialName, String credentialValue,
            String credentialType) throws Exception {
        String cacheUrl = "https://localhost:80/credential-cache/" + transactionId;
        String requestBody = "{ \"credentialName\":\"" + credentialName + "\", \"credentialValue\":\"" + credentialValue
                + "\", \"credentialType\":\"" + credentialType + "\" }";

        // Populate the cache with credentials
        mvc.perform(MockMvcRequestBuilders.post(cacheUrl).content(requestBody).contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
                .andExpect(content().string(equalTo(transactionId)));
    }
}