2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.dcae;
24 import io.vavr.collection.HashMap;
25 import org.junit.jupiter.api.Nested;
26 import org.junit.jupiter.api.Test;
27 import org.onap.dcae.common.configuration.AuthMethodType;
28 import org.springframework.context.annotation.Import;
29 import org.springframework.http.HttpStatus;
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.mockito.Mockito.when;
34 import static org.onap.dcae.TLSTest.HttpsConfiguration.USERNAME;
35 import static org.onap.dcae.TLSTest.HttpsConfiguration.PASSWORD;
37 public class TLSTest extends TLSTestBase {
40 @Import(HttpConfiguration.class)
41 class HttpTest extends TestClassBase {
44 public void shouldHttpRequestSucceed() {
45 assertEquals(HttpStatus.OK, makeHttpRequest().getStatusCode());
49 public void shouldHttpsRequestFail() {
50 assertThrows(Exception.class, this::makeHttpsRequest);
55 @Import(HttpsConfiguration.class)
56 class HttpsTest extends TestClassBase {
60 public void shouldHttpsRequestWithoutBasicAuthFail() {
61 assertThrows(Exception.class, this::makeHttpsRequest);
65 public void shouldHttpsRequestWithBasicAuthSucceed() {
66 assertEquals(HttpStatus.OK, makeHttpsRequestWithBasicAuth(USERNAME, PASSWORD).getStatusCode());
71 @Import(HttpsConfigurationWithTLSAuthentication.class)
72 class HttpsWithTLSAuthenticationTest extends TestClassBase {
75 public void shouldHttpsRequestWithoutCertificateFail() {
76 assertThrows(Exception.class, this::makeHttpsRequest);
80 public void shouldHttpsRequestWithCertificateSucceed() {
81 assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCert().getStatusCode());
86 @Import(HttpsConfigurationWithTLSAuthenticationAndBasicAuth.class)
87 class HttpsWithTLSAuthenticationAndBasicAuthTest extends TestClassBase {
90 public void shouldHttpsRequestWithoutBasicAuthSucceed() {
91 assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCert().getStatusCode());
95 public void shouldHttpsRequestWithBasicAuthSucceed() {
96 assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCertAndBasicAuth(USERNAME, PASSWORD).getStatusCode());
100 // ApplicationSettings configurations
101 static class HttpConfiguration extends TLSTestBase.ConfigurationBase {
103 protected void configureSettings(ApplicationSettings settings) {
104 when(settings.authMethod()).thenReturn(AuthMethodType.NO_AUTH.value());
108 static class HttpsConfiguration extends TLSTestBase.ConfigurationBase {
109 public static final String USERNAME = "TestUser";
110 public static final String PASSWORD = "TestPassword";
113 protected void configureSettings(ApplicationSettings settings) {
114 when(settings.keystoreFileLocation()).thenReturn(KEYSTORE.toString());
115 when(settings.keystorePasswordFileLocation()).thenReturn(KEYSTORE_PASSWORD_FILE.toString());
116 when(settings.authMethod()).thenReturn(AuthMethodType.BASIC_AUTH.value());
117 when(settings.validAuthorizationCredentials()).thenReturn(HashMap.of(USERNAME, "$2a$10$51tDgG2VNLde5E173Ay/YO.Fq.aD.LR2Rp8pY3QAKriOSPswvGviy"));
121 static class HttpsConfigurationWithTLSAuthentication extends HttpsConfiguration {
123 protected void configureSettings(ApplicationSettings settings) {
124 super.configureSettings(settings);
125 when(settings.authMethod()).thenReturn(AuthMethodType.CERT_ONLY.value());
126 when(settings.truststoreFileLocation()).thenReturn(TRUSTSTORE.toString());
127 when(settings.truststorePasswordFileLocation()).thenReturn(TRUSTSTORE_PASSWORD_FILE.toString());
131 static class HttpsConfigurationWithTLSAuthenticationAndBasicAuth extends HttpsConfigurationWithTLSAuthentication {
133 protected void configureSettings(ApplicationSettings settings) {
134 super.configureSettings(settings);
135 when(settings.authMethod()).thenReturn(AuthMethodType.CERT_BASIC_AUTH.value());