2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.oran.datafile.configuration;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import java.lang.reflect.Field;
29 import java.util.HashMap;
31 import org.apache.kafka.clients.CommonClientConfigs;
32 import org.apache.kafka.common.config.SaslConfigs;
33 import org.apache.kafka.common.config.SslConfigs;
34 import org.apache.kafka.common.security.auth.SecurityProtocol;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.MockitoAnnotations;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.oran.datafile.oauth2.OAuthKafkaAuthenticateLoginCallbackHandler;
42 import org.springframework.test.context.ContextConfiguration;
44 @ContextConfiguration(classes = {AppConfig.class})
45 @ExtendWith(MockitoExtension.class)
49 private AppConfig appConfig;
53 MockitoAnnotations.initMocks(this);
56 void testGetS3LocksBucket_WhenEmptyLocksBucket_ReturnsS3Bucket() {
57 injectFieldValue(appConfig, "s3Bucket", "test-bucket");
58 injectFieldValue(appConfig, "s3LocksBucket", "");
60 String result = appConfig.getS3LocksBucket();
61 assertEquals("test-bucket", result);
65 void testGetS3LocksBucket_WhenNonEmptyLocksBucket_ReturnsLocksBucket() {
66 injectFieldValue(appConfig, "s3Bucket", "test-bucket");
67 injectFieldValue(appConfig, "s3LocksBucket", "locks");
69 String result = appConfig.getS3LocksBucket();
70 assertEquals("locks", result);
74 void testIsS3Enabled_WhenS3EndpointAndBucketSet_ReturnsTrue() {
75 injectFieldValue(appConfig, "s3Bucket", "test-bucket");
76 injectFieldValue(appConfig, "s3EndpointOverride", "s3.endpoint");
77 boolean result = appConfig.isS3Enabled();
82 void testIsS3Enabled_WhenS3EndpointNotSet_ReturnsFalse() {
83 injectFieldValue(appConfig, "s3Bucket", "test-bucket");
84 injectFieldValue(appConfig, "s3EndpointOverride", "");
85 boolean result = appConfig.isS3Enabled();
90 void testGetKafkaBootStrapServers() {
91 assertNull((new AppConfig()).getKafkaBootStrapServers());
95 void testAddKafkaSecurityProps_UseOAuthToken() {
96 Map<String, Object> props = new HashMap<>();
97 injectFieldValue(appConfig, "useOathToken", true);
98 injectFieldValue(appConfig, "kafkaKeyStoreLocation", "key-store-location");
99 injectFieldValue(appConfig, "kafkTrustStoreLocation", "trust-store-location");
100 injectFieldValue(appConfig, "kafkaKeyStorePassword", "key-store-password");
102 appConfig.addKafkaSecurityProps(props);
104 assertEquals(SecurityProtocol.SASL_SSL.name, props.get(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG));
105 assertEquals("OAUTHBEARER", props.get(SaslConfigs.SASL_MECHANISM));
106 assertEquals(OAuthKafkaAuthenticateLoginCallbackHandler.class.getName(),
107 props.get(SaslConfigs.SASL_LOGIN_CALLBACK_HANDLER_CLASS));
109 "org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required unsecuredLoginStringClaim_sub=\"alice\"; ",
110 props.get(SaslConfigs.SASL_JAAS_CONFIG));
114 void testAddKafkaSecurityProps_SslConfig() {
115 Map<String, Object> props = new HashMap<>();
116 injectFieldValue(appConfig, "useOathToken", false);
117 injectFieldValue(appConfig, "kafkaKeyStoreLocation", "key-store-location");
118 injectFieldValue(appConfig, "kafkaKeyStoreType", "JKS");
119 injectFieldValue(appConfig, "kafkaKeyStorePassword", "key-store-password");
120 injectFieldValue(appConfig, "kafkTrustStoreLocation", "trust-store-location");
121 injectFieldValue(appConfig, "kafkaTrustStoreType", "JKS");
123 appConfig.addKafkaSecurityProps(props);
125 assertEquals(SecurityProtocol.SASL_SSL.name, props.get(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG));
126 assertEquals("JKS", props.get(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG));
127 assertEquals("key-store-location", props.get(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG));
128 assertEquals("key-store-password", props.get(SslConfigs.SSL_KEY_PASSWORD_CONFIG));
129 assertEquals("JKS", props.get(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG));
130 assertEquals("trust-store-location", props.get(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG));
134 void testGetCertificateConfiguration() {
135 injectFieldValue(appConfig, "clientTrustStore", "trust-store");
136 injectFieldValue(appConfig, "clientTrustStorePassword", "trust-store-password");
137 injectFieldValue(appConfig, "clientKeyStore", "key-store");
138 injectFieldValue(appConfig, "clientKeyStorePassword", "key-store-password");
140 CertificateConfig certificateConfig = appConfig.getCertificateConfiguration();
142 assertEquals("trust-store", certificateConfig.trustedCa);
143 assertEquals("trust-store-password", certificateConfig.trustedCaPasswordPath);
144 assertEquals("key-store", certificateConfig.keyCert);
145 assertEquals("key-store-password", certificateConfig.keyPasswordPath);
149 void testGetSftpConfiguration() {
150 injectFieldValue(appConfig, "knownHostsFilePath", "/path/to/known_hosts");
151 injectFieldValue(appConfig, "strictHostKeyChecking", true);
153 SftpConfig sftpConfig = appConfig.getSftpConfiguration();
155 assertEquals("/path/to/known_hosts", sftpConfig.knownHostsFilePath);
156 assertTrue(sftpConfig.strictHostKeyChecking);
159 private void injectFieldValue(Object target, String fieldName, Object value) {
161 Field field = target.getClass().getDeclaredField(fieldName);
162 field.setAccessible(true);
163 field.set(target, value);
164 } catch (NoSuchFieldException | IllegalAccessException e) {