Config fetch for VESCollector through DCAE-SDK (CBS Client)
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / TLSTestBase.java
1 /*-
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.dcae;
23
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;
29
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;
47
48 @Configuration
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");
57
58     protected static abstract class ConfigurationBase {
59         protected final ApplicationSettings settings = Mockito.mock(ApplicationSettings.class);
60
61         @Bean
62         @Primary
63         public ApplicationSettings settings() {
64             configureSettings(settings);
65             return settings;
66         }
67
68         protected abstract void configureSettings(final ApplicationSettings settings);
69     }
70
71     @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
72     protected abstract class TestClassBase {
73
74         @MockBean
75         @Qualifier("eventSender")
76         protected EventSender eventSender;
77
78         @LocalServerPort
79         private int port;
80
81         private final String keyStorePassword;
82         private final String trustStorePassword;
83
84         public TestClassBase() {
85             keyStorePassword = readFile(KEYSTORE_PASSWORD_FILE);
86             trustStorePassword = readFile(TRUSTSTORE_PASSWORD_FILE);
87         }
88
89         private String getURL(final String protocol, final String uri) {
90             return protocol + "://localhost:" + port + uri;
91         }
92
93         private RestTemplate addBasicAuth(final RestTemplate template, final String username, final String password) {
94             template.getInterceptors()
95                     .add(new BasicAuthenticationInterceptor(username, password));
96
97             return template;
98         }
99
100         public String createHttpURL(String uri) {
101             return getURL("http", uri);
102         }
103
104         public String createHttpsURL(String uri) {
105             return getURL("https", uri);
106         }
107
108         public RestTemplate createHttpRestTemplate() {
109             return new RestTemplate();
110         }
111
112         public RestTemplate createHttpsRestTemplate() {
113             return rethrow(() ->
114                     createRestTemplateWithSsl(
115                             sslBuilderWithTrustStore(KEYSTORE, keyStorePassword).build()
116                     ));
117         }
118
119         public RestTemplate createHttpsRestTemplateWithKeyStore() {
120             return rethrow(() ->
121                     createRestTemplateWithSsl(
122                             configureKeyStore(
123                                     sslBuilderWithTrustStore(KEYSTORE, keyStorePassword),
124                                     TRUSTSTORE,
125                                     trustStorePassword
126                             ).build())
127             );
128         }
129
130         public ResponseEntity<String> makeHttpRequest() {
131             return createHttpRestTemplate().getForEntity(createHttpURL("/"), String.class);
132         }
133
134         public ResponseEntity<String> makeHttpsRequest() {
135             return createHttpsRestTemplate().getForEntity(createHttpsURL("/"), String.class);
136         }
137
138         public ResponseEntity<String> makeHttpsRequestWithClientCert() {
139             return createHttpsRestTemplateWithKeyStore().getForEntity(createHttpsURL("/"), String.class);
140         }
141
142         public ResponseEntity<String> makeHttpsRequestWithClientCertAndBasicAuth(
143                 final String username,
144                 final String password) {
145             return addBasicAuth(createHttpsRestTemplateWithKeyStore(), username, password)
146                     .getForEntity(createHttpsURL("/"), String.class);
147         }
148     }
149 }