2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2018-2020 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 static org.onap.dcae.TestingUtilities.configureKeyStore;
25 import static org.onap.dcae.TestingUtilities.createRestTemplateWithSsl;
26 import static org.onap.dcae.TestingUtilities.readFile;
27 import static org.onap.dcae.TestingUtilities.rethrow;
28 import static org.onap.dcae.TestingUtilities.sslBuilderWithTrustStore;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mockito;
34 import org.onap.dcae.common.EventSender;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
38 import org.springframework.boot.test.mock.mockito.MockBean;
39 import org.springframework.boot.web.server.LocalServerPort;
40 import org.springframework.context.annotation.Bean;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.context.annotation.Primary;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.http.client.support.BasicAuthenticationInterceptor;
45 import org.springframework.test.context.junit.jupiter.SpringExtension;
46 import org.springframework.web.client.RestTemplate;
49 @ExtendWith(SpringExtension.class)
50 public class TLSTestBase {
51 protected static final Path RESOURCES = Paths.get("src", "test", "resources");
52 protected static final Path KEYSTORE = Paths.get(RESOURCES.toString(), "keystore");
53 protected static final Path KEYSTORE_PASSWORD_FILE = Paths.get(RESOURCES.toString(), "passwordfile");
54 protected static final Path TRUSTSTORE = Paths.get(RESOURCES.toString(), "truststore");
55 protected static final Path TRUSTSTORE_PASSWORD_FILE = Paths.get(RESOURCES.toString(), "trustpasswordfile");
56 protected static final Path CERT_SUBJECT_MATCHER = Paths.get(RESOURCES.toString(), "certSubjectMatcher");
58 protected static abstract class ConfigurationBase {
59 protected final ApplicationSettings settings = Mockito.mock(ApplicationSettings.class);
63 public ApplicationSettings settings() {
64 configureSettings(settings);
68 protected abstract void configureSettings(final ApplicationSettings settings);
71 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
72 protected abstract class TestClassBase {
75 @Qualifier("eventSender")
76 protected EventSender eventSender;
81 private final String keyStorePassword;
82 private final String trustStorePassword;
84 public TestClassBase() {
85 keyStorePassword = readFile(KEYSTORE_PASSWORD_FILE);
86 trustStorePassword = readFile(TRUSTSTORE_PASSWORD_FILE);
89 private String getURL(final String protocol, final String uri) {
90 return protocol + "://localhost:" + port + uri;
93 private RestTemplate addBasicAuth(final RestTemplate template, final String username, final String password) {
94 template.getInterceptors()
95 .add(new BasicAuthenticationInterceptor(username, password));
100 public String createHttpURL(String uri) {
101 return getURL("http", uri);
104 public String createHttpsURL(String uri) {
105 return getURL("https", uri);
108 public RestTemplate createHttpRestTemplate() {
109 return new RestTemplate();
112 public RestTemplate createHttpsRestTemplate() {
114 createRestTemplateWithSsl(
115 sslBuilderWithTrustStore(KEYSTORE, keyStorePassword).build()
119 public RestTemplate createHttpsRestTemplateWithKeyStore() {
121 createRestTemplateWithSsl(
123 sslBuilderWithTrustStore(KEYSTORE, keyStorePassword),
130 public ResponseEntity<String> makeHttpRequest() {
131 return createHttpRestTemplate().getForEntity(createHttpURL("/"), String.class);
134 public ResponseEntity<String> makeHttpsRequest() {
135 return createHttpsRestTemplate().getForEntity(createHttpsURL("/"), String.class);
138 public ResponseEntity<String> makeHttpsRequestWithClientCert() {
139 return createHttpsRestTemplateWithKeyStore().getForEntity(createHttpsURL("/"), String.class);
142 public ResponseEntity<String> makeHttpsRequestWithClientCertAndBasicAuth(
143 final String username,
144 final String password) {
145 return addBasicAuth(createHttpsRestTemplateWithKeyStore(), username, password)
146 .getForEntity(createHttpsURL("/"), String.class);