summaryrefslogtreecommitdiffstats
path: root/gui-server
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2022-11-08 20:06:23 +0000
committerliamfallon <liam.fallon@est.tech>2022-11-08 20:07:20 +0000
commitb972ede6f59bb057505e000178364c24fa85f77c (patch)
tree900f0193c243e088ac24ca59fc01eb8f4c3a2a82 /gui-server
parent4d04e56e297cb9808a7e5378480526ba44f6a0bd (diff)
Remove duplication in REST Controller Tests
Issue-ID: POLICY-4138 Change-Id: I796942a33031d572bac931861faa5dc736128c9b Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'gui-server')
-rw-r--r--gui-server/src/test/java/org/onap/policy/gui/server/rest/AcmRuntimeRestControllerTest.java132
-rw-r--r--gui-server/src/test/java/org/onap/policy/gui/server/rest/BaseRestControllerTest.java169
-rw-r--r--gui-server/src/test/java/org/onap/policy/gui/server/rest/PolicyApiRestControllerTest.java132
3 files changed, 191 insertions, 242 deletions
diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/rest/AcmRuntimeRestControllerTest.java b/gui-server/src/test/java/org/onap/policy/gui/server/rest/AcmRuntimeRestControllerTest.java
index 56a805d..56368c2 100644
--- a/gui-server/src/test/java/org/onap/policy/gui/server/rest/AcmRuntimeRestControllerTest.java
+++ b/gui-server/src/test/java/org/onap/policy/gui/server/rest/AcmRuntimeRestControllerTest.java
@@ -20,33 +20,13 @@
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.security.cert.X509Certificate;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.onap.policy.gui.server.test.util.KeyStoreHelper;
+import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
+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.HttpStatus;
import org.springframework.test.web.client.MockRestServiceServer;
-import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.client.RestTemplate;
@SpringBootTest(
@@ -59,107 +39,17 @@ import org.springframework.web.client.RestTemplate;
"runtime-ui.acm.disable-ssl-validation=true"
})
@AutoConfigureMockMvc
-class AcmRuntimeRestControllerTest {
-
- @Autowired
- private MockMvc mvc;
-
+class AcmRuntimeRestControllerTest extends BaseRestControllerTest {
@Autowired
- @Qualifier("acmRuntimeRestTemplate")
- private RestTemplate restTemplate;
-
- private MockRestServiceServer mockServer;
-
- @BeforeEach
- public void init() {
- mockServer = MockRestServiceServer.createServer(restTemplate);
+ public void setBaseMockServer(@Qualifier("acmRuntimeRestTemplate") RestTemplate restTemplate) {
+ super.setMockServer(MockRestServiceServer.createServer(restTemplate));
}
- @Test
- void testStaticContentUrls() throws Exception {
- 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 {
- X509Certificate cert = KeyStoreHelper.loadValidCert();
-
- mockServer.expect(
- requestTo("https://runtime-acm:8443/junit/test"))
- .andExpect(header(SSL_CERT_HEADER_NAME, urlEncodeCert(cert)))
- .andRespond(withStatus(HttpStatus.OK).body("admin"));
-
- mvc.perform(
- get("/runtime-ui/acm/restservices/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 {
- // 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("https://runtime-acm:8443/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("/runtime-ui/acm/restservices/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 {
- final String errorMessage = "This appliance cannot brew coffee";
-
- mockServer.expect(
- requestTo("https://runtime-acm:8443/coffee"))
- .andRespond(withStatus(HttpStatus.I_AM_A_TEAPOT).body(errorMessage));
-
- mvc.perform(
- post("/runtime-ui/acm/restservices/coffee").secure(true))
- .andExpect(status().is(HttpStatus.I_AM_A_TEAPOT.value()))
- .andExpect(content().string(errorMessage));
-
- mockServer.verify();
+ @Autowired
+ public void setBaseMapping(
+ @Value("${runtime-ui.acm.mapping-path}") String mappingPath,
+ @Value("${runtime-ui.acm.url}") URI url) {
+ super.setMappingPath(mappingPath);
+ super.setUrl(url);
}
}
diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/rest/BaseRestControllerTest.java b/gui-server/src/test/java/org/onap/policy/gui/server/rest/BaseRestControllerTest.java
new file mode 100644
index 0000000..10e04e3
--- /dev/null
+++ b/gui-server/src/test/java/org/onap/policy/gui/server/rest/BaseRestControllerTest.java
@@ -0,0 +1,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();
+ }
+}
diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/rest/PolicyApiRestControllerTest.java b/gui-server/src/test/java/org/onap/policy/gui/server/rest/PolicyApiRestControllerTest.java
index e7c8db6..a862dc8 100644
--- a/gui-server/src/test/java/org/onap/policy/gui/server/rest/PolicyApiRestControllerTest.java
+++ b/gui-server/src/test/java/org/onap/policy/gui/server/rest/PolicyApiRestControllerTest.java
@@ -20,33 +20,13 @@
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.security.cert.X509Certificate;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.onap.policy.gui.server.test.util.KeyStoreHelper;
+import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
+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.HttpStatus;
import org.springframework.test.web.client.MockRestServiceServer;
-import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.client.RestTemplate;
@SpringBootTest(
@@ -59,107 +39,17 @@ import org.springframework.web.client.RestTemplate;
"runtime-ui.acm.disable-ssl-validation=true"
})
@AutoConfigureMockMvc
-class PolicyApiRestControllerTest {
-
- @Autowired
- private MockMvc mvc;
-
+class PolicyApiRestControllerTest extends BaseRestControllerTest {
@Autowired
- @Qualifier("policyApiRestTemplate")
- private RestTemplate restTemplate;
-
- private MockRestServiceServer mockServer;
-
- @BeforeEach
- public void init() {
- mockServer = MockRestServiceServer.createServer(restTemplate);
+ public void setBaseMockServer(@Qualifier("policyApiRestTemplate") RestTemplate restTemplate) {
+ super.setMockServer(MockRestServiceServer.createServer(restTemplate));
}
- @Test
- void testStaticContentUrls() throws Exception {
- 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 {
- X509Certificate cert = KeyStoreHelper.loadValidCert();
-
- mockServer.expect(
- requestTo("https://policy-api:9876/junit/test"))
- .andExpect(header(SSL_CERT_HEADER_NAME, urlEncodeCert(cert)))
- .andRespond(withStatus(HttpStatus.OK).body("admin"));
-
- mvc.perform(
- get("/runtime-ui/policy/restservices/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 {
- // 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("https://policy-api:9876/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("/runtime-ui/policy/restservices/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 {
- final String errorMessage = "This appliance cannot brew coffee";
-
- mockServer.expect(
- requestTo("https://policy-api:9876/coffee"))
- .andRespond(withStatus(HttpStatus.I_AM_A_TEAPOT).body(errorMessage));
-
- mvc.perform(
- post("/runtime-ui/policy/restservices/coffee").secure(true))
- .andExpect(status().is(HttpStatus.I_AM_A_TEAPOT.value()))
- .andExpect(content().string(errorMessage));
-
- mockServer.verify();
+ @Autowired
+ public void setBaseMapping(
+ @Value("${runtime-ui.policy.mapping-path}") String mappingPath,
+ @Value("${runtime-ui.policy.url}") URI url) {
+ super.setMappingPath(mappingPath);
+ super.setUrl(url);
}
}