Merge "Code coverage for policy/gui gui-server"
diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/GuiServerAppMainTest.java b/gui-server/src/test/java/org/onap/policy/gui/server/GuiServerAppMainTest.java
new file mode 100644
index 0000000..d0f6598
--- /dev/null
+++ b/gui-server/src/test/java/org/onap/policy/gui/server/GuiServerAppMainTest.java
@@ -0,0 +1,41 @@
+/*-
+ * ============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;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * In this test, we check that application can start via main() method.
+ */
+class GuiServerAppMainTest {
+
+ @Test
+ void whenMainIsCalled_thenNoExceptions() {
+ String[] args = {
+ "--server.port=0", // use random available port
+ "--clamp.url=https://clamp-backend:8443/",
+ "--clamp.disable-ssl-validation=true"
+ };
+ assertDoesNotThrow(() -> GuiServerApplication.main(args));
+ }
+}
diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/config/ClampRestTemplateConfig6Test.java b/gui-server/src/test/java/org/onap/policy/gui/server/config/ClampRestTemplateConfig6Test.java
new file mode 100644
index 0000000..d1d3072
--- /dev/null
+++ b/gui-server/src/test/java/org/onap/policy/gui/server/config/ClampRestTemplateConfig6Test.java
@@ -0,0 +1,57 @@
+/*-
+ * ============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.config;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+import org.junit.jupiter.api.Test;
+import org.onap.policy.gui.server.test.util.hello.HelloWorldApplication;
+import org.springframework.beans.factory.BeanCreationException;
+import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.util.ReflectionTestUtils;
+
+/**
+ * In this test, server.ssl.trust-store is unset while SSL validation is enabled.
+ * An BeanCreationException should be thrown on application startup.
+ */
+@SpringBootTest(
+ classes = { HelloWorldApplication.class }
+)
+class ClampRestTemplateConfig6Test {
+
+ @Test
+ void expectExceptionWithNoTrustStore(ApplicationContext context) {
+ // Manually autowire the bean so we can test PostConstruct logic.
+ ClampRestTemplateConfig restTemplateConfig = new ClampRestTemplateConfig();
+ AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
+ factory.autowireBean(restTemplateConfig);
+
+ // Enable SSL validation, but provide no trust store.
+ ReflectionTestUtils.setField(restTemplateConfig, "disableSslValidation", false);
+
+ // Expect exception when creating bean.
+ assertThatExceptionOfType(BeanCreationException.class)
+ .isThrownBy(() -> factory.initializeBean(restTemplateConfig, "clampRestTemplate"))
+ .withMessageContaining("server.ssl.trust-store must be set");
+ }
+}
diff --git a/gui-server/src/test/java/org/onap/policy/gui/server/filters/ClientSslHeaderFilterTest.java b/gui-server/src/test/java/org/onap/policy/gui/server/filters/ClientSslHeaderFilterTest.java
index 5fc026d..fb56fbc 100644
--- a/gui-server/src/test/java/org/onap/policy/gui/server/filters/ClientSslHeaderFilterTest.java
+++ b/gui-server/src/test/java/org/onap/policy/gui/server/filters/ClientSslHeaderFilterTest.java
@@ -27,12 +27,14 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.onap.policy.gui.server.filters.ClientSslHeaderFilter.SSL_CERT_HEADER_NAME;
import static org.onap.policy.gui.server.filters.ClientSslHeaderFilter.X509_ATTRIBUTE_NAME;
import static org.onap.policy.gui.server.util.X509CertificateEncoder.urlDecodeCert;
import java.io.IOException;
+import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.Enumeration;
@@ -118,6 +120,29 @@
}
/*
+ * If there is a CertificateEncodingException, the filter should not set
+ * the X-SSL-Cert header.
+ */
+ @Test
+ void testInvalidClientCert_noHeader() throws Exception {
+ // Create an invalid cert.
+ X509Certificate invalidCert = mock(X509Certificate.class);
+ doThrow(CertificateEncodingException.class).when(invalidCert).getEncoded();
+
+ // Create a request with an invalid client SSL cert.
+ MockHttpServletRequest inRequest = new MockHttpServletRequest();
+ inRequest.setAttribute(X509_ATTRIBUTE_NAME, new X509Certificate[] { invalidCert });
+
+ // Apply the filter.
+ HttpServletRequest outRequest = applyRequestFilter(inRequest);
+
+ // The modified request should not contain a cert header.
+ assertFalse(containsCertHeader(outRequest.getHeaderNames()));
+ assertNull(outRequest.getHeader(SSL_CERT_HEADER_NAME));
+ assertEquals(Collections.emptyEnumeration(), outRequest.getHeaders(SSL_CERT_HEADER_NAME));
+ }
+
+ /*
* This test is needed to prevent a security vulnerability where a
* malicious user does not authenticate using client cert, but defines the
* X-SSL-Cert header themselves, thus gaining access without having the