Improve Test coverage of InfluxLogger
[nonrtric/plt/ranpm.git] / influxlogger / src / test / java / org / oran / pmlog / configuration / ApplicationConfigTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oran.pmlog.configuration;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24
25 import java.lang.reflect.Field;
26 import java.util.HashMap;
27 import java.util.Map;
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;
40
41 @ContextConfiguration(classes = {ApplicationConfig.class})
42 @ExtendWith(MockitoExtension.class)
43 class ApplicationConfigTest {
44
45     @InjectMocks
46     private ApplicationConfig appConfig;
47
48     @BeforeEach
49     void setup() {
50         MockitoAnnotations.initMocks(this);
51     }
52
53     @Test
54     void testGetS3LocksBucket_WhenEmptyLocksBucket_ReturnsS3Bucket() {
55         injectFieldValue(appConfig, "influxBucket", "test-bucket");
56
57         String result = appConfig.getInfluxBucket();
58         assertEquals("test-bucket", result);
59     }
60
61     @Test
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");
68
69         appConfig.addKafkaSecurityProps(props);
70
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));
75         assertEquals(
76             "org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required unsecuredLoginStringClaim_sub=\"alice\"; ",
77             props.get(SaslConfigs.SASL_JAAS_CONFIG));
78     }
79
80     @Test
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");
89
90         appConfig.addKafkaSecurityProps(props);
91
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));
98     }
99
100     private void injectFieldValue(Object target, String fieldName, Object value) {
101         try {
102             Field field = target.getClass().getDeclaredField(fieldName);
103             field.setAccessible(true);
104             field.set(target, value);
105         } catch (NoSuchFieldException | IllegalAccessException e) {
106             e.printStackTrace();
107         }
108     }
109 }
110