aboutsummaryrefslogtreecommitdiffstats
path: root/gui-server/src/test/java/org/onap/policy/gui/server/rest/BaseRestControllerTest.java
blob: 10e04e39ea5e1c6eb6b966a08224248348578cd9 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.gui.server.rest;

import static org.onap.policy.gui.server.filters.ClientSslHeaderFilter.SSL_CERT_HEADER_NAME;
import static org.onap.policy.gui.server.test.util.X509RequestPostProcessor.x509;
import static org.onap.policy.gui.server.util.X509CertificateEncoder.urlEncodeCert;
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.withStatus;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.net.URI;
import java.security.cert.X509Certificate;
import lombok.Setter;
import org.junit.jupiter.api.Test;
import org.onap.policy.gui.server.test.util.KeyStoreHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.servlet.MockMvc;

class BaseRestControllerTest {
    @Autowired
    private MockMvc mvc;

    @Setter
    private MockRestServiceServer mockServer;

    @Setter
    private String mappingPath;

    @Setter
    private URI url;

    @Test
    void testStaticContentUrls() throws Exception {
        // Don't run test if this base class is called directly form JUnit
        if (mockServer == null) {
            return;
        }

        mvc.perform(get("/runtime-ui/"))
            .andExpect(status().isOk())
            .andExpect(forwardedUrl("/runtime-ui/index.html"));

        mvc.perform(get("/runtime-ui"))
            .andExpect(status().is3xxRedirection())
            .andExpect(redirectedUrl("/runtime-ui/"));
    }

    /*
     * This is a happy path test to verify that calls to <mapping-path>/**
     * are relayed to the server, and that the server receives the
     * client certificate encoded in a header. More extensive tests of the
     * certificate cert filter are in ClientSslHeaderFilterTest.
     */
    @Test
    void testServerProxyWithClientCert() throws Exception {
        // Don't run test if this base class is called directly form JUnit
        if (mockServer == null) {
            return;
        }

        X509Certificate cert = KeyStoreHelper.loadValidCert();

        mockServer.expect(
            requestTo(url + "junit/test"))
            .andExpect(header(SSL_CERT_HEADER_NAME, urlEncodeCert(cert)))
            .andRespond(withStatus(HttpStatus.OK).body("admin"));

        mvc.perform(
            get(mappingPath + "junit/test")
                .with(x509(cert)))
            .andExpect(status().isOk())
            .andExpect(content().string("admin"));

        mockServer.verify();
    }

    /*
     * This test verifies that HTTP headers are preserved for requests to the
     * server (including multi-value headers).
     */
    @Test
    void verifyServerProxyPassesHeaders() throws Exception {
        // Don't run test if this base class is called directly form JUnit
        if (mockServer == null) {
            return;
        }

        // Single value header
        final String userAgent = "User-Agent";
        final String userAgentValue = "JUnit";
        // Multi-value header
        final String acceptLanguage = "Accept-Language";
        final String enUs = "en-US";
        final String enIe = "en-IE";

        mockServer.expect(
            requestTo(url + "junit/test"))
            .andExpect(method(HttpMethod.GET))
            .andExpect(header(userAgent, userAgentValue))
            .andExpect(header(acceptLanguage, enUs, enIe))
            .andRespond(withStatus(HttpStatus.OK));

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set(userAgent, userAgentValue);
        requestHeaders.add(acceptLanguage, enUs);
        requestHeaders.add(acceptLanguage, enIe);
        mvc.perform(
            get(mappingPath + "junit/test")
                .headers(requestHeaders))
            .andExpect(status().isOk());

        mockServer.verify();
    }

    /*
     * This test verifies that error messages from the server are
     * delivered to the client (as opposed to 500 "Internal Server Error").
     */
    @Test
    void verifyServerProxyReturnsBackendErrorCode() throws Exception {
        // Don't run test if this base class is called directly form JUnit
        if (mockServer == null) {
            return;
        }

        final String errorMessage = "This appliance cannot brew coffee";

        mockServer.expect(
            requestTo(url + "coffee"))
            .andRespond(withStatus(HttpStatus.I_AM_A_TEAPOT).body(errorMessage));

        mvc.perform(
            post(mappingPath + "coffee").secure(true))
            .andExpect(status().is(HttpStatus.I_AM_A_TEAPOT.value()))
            .andExpect(content().string(errorMessage));

        mockServer.verify();
    }
}