Remove certOnly and basicAuth from authentication methods
[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     @Nested
41     @Import(HttpConfiguration.class)
42     class HttpTest extends TestClassBase {
43
44         @Test
45         public void shouldHttpRequestSucceed() {
46             assertEquals(HttpStatus.OK, makeHttpRequest().getStatusCode());
47         }
48
49         @Test
50         public void shouldHttpsRequestFail() {
51             assertThrows(Exception.class, this::makeHttpsRequest);
52         }
53     }
54
55     @Nested
56     @Import(HttpsConfigurationWithTLSAuthenticationAndBasicAuth.class)
57     class HttpsWithTLSAuthenticationAndBasicAuthTest extends TestClassBase {
58
59         @Test
60         public void shouldHttpsRequestWithoutBasicAuthSucceed() {
61             assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCert().getStatusCode());
62         }
63
64         @Test
65         public void shouldHttpsRequestWithBasicAuthSucceed() {
66             assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCertAndBasicAuth(USERNAME, PASSWORD).getStatusCode());
67         }
68     }
69
70     // ApplicationSettings configurations
71     static class HttpConfiguration extends TLSTestBase.ConfigurationBase {
72         @Override
73         protected void configureSettings(ApplicationSettings settings) {
74             when(settings.authMethod()).thenReturn(AuthMethodType.NO_AUTH.value());
75             when(settings.httpPort()).thenReturn(1111);
76         }
77     }
78
79     static class HttpsConfigurationWithTLSAuthenticationAndBasicAuth extends TLSTestBase.ConfigurationBase {
80         public static final String USERNAME = "TestUser";
81         public static final String PASSWORD = "TestPassword";
82         @Override
83         protected void configureSettings(ApplicationSettings settings) {
84             when(settings.keystoreFileLocation()).thenReturn(KEYSTORE.toString());
85             when(settings.keystorePasswordFileLocation()).thenReturn(KEYSTORE_PASSWORD_FILE.toString());
86             when(settings.validAuthorizationCredentials()).thenReturn(HashMap.of(USERNAME, "$2a$10$51tDgG2VNLde5E173Ay/YO.Fq.aD.LR2Rp8pY3QAKriOSPswvGviy"));
87             when(settings.authMethod()).thenReturn(AuthMethodType.CERT_BASIC_AUTH.value());
88             when(settings.truststoreFileLocation()).thenReturn(TRUSTSTORE.toString());
89             when(settings.truststorePasswordFileLocation()).thenReturn(TRUSTSTORE_PASSWORD_FILE.toString());
90             when(settings.certSubjectMatcher()).thenReturn(CERT_SUBJECT_MATCHER.toString());
91             when(settings.httpPort()).thenReturn(1111);
92         }
93     }
94 }