2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oran.pmlog.configuration;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import java.lang.reflect.Field;
26 import java.util.HashMap;
28 import org.apache.kafka.clients.CommonClientConfigs;
29 import org.apache.kafka.common.config.SaslConfigs;
30 import org.apache.kafka.common.config.SslConfigs;
31 import org.apache.kafka.common.security.auth.SecurityProtocol;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.oran.pmlog.oauth2.OAuthKafkaAuthenticateLoginCallbackHandler;
39 import org.springframework.test.context.ContextConfiguration;
41 @ContextConfiguration(classes = {ApplicationConfig.class})
42 @ExtendWith(MockitoExtension.class)
43 class ApplicationConfigTest {
46 private ApplicationConfig appConfig;
50 MockitoAnnotations.initMocks(this);
54 void testGetS3LocksBucket_WhenEmptyLocksBucket_ReturnsS3Bucket() {
55 injectFieldValue(appConfig, "influxBucket", "test-bucket");
57 String result = appConfig.getInfluxBucket();
58 assertEquals("test-bucket", result);
62 void testAddKafkaSecurityProps_UseOAuthToken() {
63 Map<String, Object> props = new HashMap<>();
64 injectFieldValue(appConfig, "useOathToken", true);
65 injectFieldValue(appConfig, "kafkaKeyStoreLocation", "key-store-location");
66 injectFieldValue(appConfig, "kafkTrustStoreLocation", "trust-store-location");
67 injectFieldValue(appConfig, "kafkaKeyStorePassword", "key-store-password");
69 appConfig.addKafkaSecurityProps(props);
71 assertEquals(SecurityProtocol.SASL_SSL.name, props.get(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG));
72 assertEquals("OAUTHBEARER", props.get(SaslConfigs.SASL_MECHANISM));
73 assertEquals(OAuthKafkaAuthenticateLoginCallbackHandler.class.getName(),
74 props.get(SaslConfigs.SASL_LOGIN_CALLBACK_HANDLER_CLASS));
76 "org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required unsecuredLoginStringClaim_sub=\"alice\"; ",
77 props.get(SaslConfigs.SASL_JAAS_CONFIG));
81 void testAddKafkaSecurityProps_SslConfig() {
82 Map<String, Object> props = new HashMap<>();
83 injectFieldValue(appConfig, "useOathToken", false);
84 injectFieldValue(appConfig, "kafkaKeyStoreLocation", "key-store-location");
85 injectFieldValue(appConfig, "kafkaKeyStoreType", "JKS");
86 injectFieldValue(appConfig, "kafkaKeyStorePassword", "key-store-password");
87 injectFieldValue(appConfig, "kafkTrustStoreLocation", "trust-store-location");
88 injectFieldValue(appConfig, "kafkaTrustStoreType", "JKS");
90 appConfig.addKafkaSecurityProps(props);
92 assertEquals(SecurityProtocol.SASL_SSL.name, props.get(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG));
93 assertEquals("JKS", props.get(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG));
94 assertEquals("key-store-location", props.get(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG));
95 assertEquals("key-store-password", props.get(SslConfigs.SSL_KEY_PASSWORD_CONFIG));
96 assertEquals("JKS", props.get(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG));
97 assertEquals("trust-store-location", props.get(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG));
100 private void injectFieldValue(Object target, String fieldName, Object value) {
102 Field field = target.getClass().getDeclaredField(fieldName);
103 field.setAccessible(true);
104 field.set(target, value);
105 } catch (NoSuchFieldException | IllegalAccessException e) {