424ddf8b3f448dd538ffcc2d6f8cb199a154a474
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / TLSTest.java
1 /*-
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  * Copyright (C) 2019 Nokia. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dcae;
24
25 import io.vavr.collection.HashMap;
26 import org.junit.jupiter.api.Nested;
27 import org.junit.jupiter.api.Test;
28 import org.onap.dcae.common.configuration.AuthMethodType;
29 import org.springframework.context.annotation.Import;
30 import org.springframework.http.HttpStatus;
31
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.junit.jupiter.api.Assertions.assertThrows;
34 import static org.mockito.Mockito.when;
35 import static org.onap.dcae.TLSTest.HttpsConfigurationWithTLSAuthenticationAndBasicAuth.USERNAME;
36 import static org.onap.dcae.TLSTest.HttpsConfigurationWithTLSAuthenticationAndBasicAuth.PASSWORD;
37
38 public class TLSTest extends TLSTestBase {
39
40     private static final String MAPPING_FILE_LOCATION = "./etc/externalRepo/schema-map.json";
41     private static final String SCHEMA_FILES_LOCATION = "./etc/externalRepo";
42     private static final String STND_DEFINED_DATA_PATH = "/event/stndDefinedFields/data";
43     private static final String SCHEMA_REF_PATH = "/event/stndDefinedFields/schemaReference";
44
45     @Nested
46     @Import(HttpConfiguration.class)
47     class HttpTest extends TestClassBase {
48
49         @Test
50         public void shouldHttpRequestSucceed() {
51             assertEquals(HttpStatus.OK, makeHttpRequest().getStatusCode());
52         }
53
54         @Test
55         public void shouldHttpsRequestFail() {
56             assertThrows(Exception.class, this::makeHttpsRequest);
57         }
58     }
59
60     @Nested
61     @Import(HttpsConfigurationWithTLSAuthenticationAndBasicAuth.class)
62     class HttpsWithTLSAuthenticationAndBasicAuthTest extends TestClassBase {
63
64         @Test
65         public void shouldHttpsRequestWithoutBasicAuthSucceed() {
66             assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCert().getStatusCode());
67         }
68
69         @Test
70         public void shouldHttpsRequestWithBasicAuthSucceed() {
71             assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCertAndBasicAuth(USERNAME, PASSWORD).getStatusCode());
72         }
73     }
74
75     // ApplicationSettings configurations
76     static class HttpConfiguration extends TLSTestBase.ConfigurationBase {
77
78         @Override
79         protected void configureSettings(ApplicationSettings settings) {
80             when(settings.authMethod()).thenReturn(AuthMethodType.NO_AUTH.value());
81             when(settings.httpPort()).thenReturn(1111);
82             when(settings.getExternalSchemaMappingFileLocation()).thenReturn(MAPPING_FILE_LOCATION);
83             when(settings.getExternalSchemaSchemasLocation()).thenReturn(SCHEMA_FILES_LOCATION);
84             when(settings.getExternalSchemaSchemaRefPath()).thenReturn(SCHEMA_REF_PATH);
85             when(settings.getExternalSchemaStndDefinedDataPath()).thenReturn(STND_DEFINED_DATA_PATH);
86
87         }
88     }
89
90     static class HttpsConfigurationWithTLSAuthenticationAndBasicAuth extends TLSTestBase.ConfigurationBase {
91         public static final String USERNAME = "TestUser";
92         public static final String PASSWORD = "TestPassword";
93         @Override
94         protected void configureSettings(ApplicationSettings settings) {
95             when(settings.keystoreFileLocation()).thenReturn(KEYSTORE.toString());
96             when(settings.keystorePasswordFileLocation()).thenReturn(KEYSTORE_PASSWORD_FILE.toString());
97             when(settings.validAuthorizationCredentials()).thenReturn(HashMap.of(USERNAME, "$2a$10$51tDgG2VNLde5E173Ay/YO.Fq.aD.LR2Rp8pY3QAKriOSPswvGviy"));
98             when(settings.authMethod()).thenReturn(AuthMethodType.CERT_BASIC_AUTH.value());
99             when(settings.truststoreFileLocation()).thenReturn(TRUSTSTORE.toString());
100             when(settings.truststorePasswordFileLocation()).thenReturn(TRUSTSTORE_PASSWORD_FILE.toString());
101             when(settings.certSubjectMatcher()).thenReturn(CERT_SUBJECT_MATCHER.toString());
102             when(settings.httpPort()).thenReturn(1111);
103             when(settings.getExternalSchemaMappingFileLocation()).thenReturn(MAPPING_FILE_LOCATION);
104             when(settings.getExternalSchemaSchemasLocation()).thenReturn(SCHEMA_FILES_LOCATION);
105             when(settings.getExternalSchemaSchemaRefPath()).thenReturn(SCHEMA_REF_PATH);
106             when(settings.getExternalSchemaStndDefinedDataPath()).thenReturn(STND_DEFINED_DATA_PATH);
107         }
108     }
109 }