Improve Test coverage of DFC
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / configuration / AppConfigTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.oran.datafile.configuration;
22
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;
27
28 import java.lang.reflect.Field;
29 import java.util.HashMap;
30 import java.util.Map;
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;
43
44 @ContextConfiguration(classes = {AppConfig.class})
45 @ExtendWith(MockitoExtension.class)
46 class AppConfigTest {
47
48     @InjectMocks
49     private AppConfig appConfig;
50
51     @BeforeEach
52     void setup() {
53         MockitoAnnotations.initMocks(this);
54     }
55     @Test
56     void testGetS3LocksBucket_WhenEmptyLocksBucket_ReturnsS3Bucket() {
57         injectFieldValue(appConfig, "s3Bucket", "test-bucket");
58         injectFieldValue(appConfig, "s3LocksBucket", "");
59
60         String result = appConfig.getS3LocksBucket();
61         assertEquals("test-bucket", result);
62     }
63
64     @Test
65     void testGetS3LocksBucket_WhenNonEmptyLocksBucket_ReturnsLocksBucket() {
66         injectFieldValue(appConfig, "s3Bucket", "test-bucket");
67         injectFieldValue(appConfig, "s3LocksBucket", "locks");
68
69         String result = appConfig.getS3LocksBucket();
70         assertEquals("locks", result);
71     }
72
73     @Test
74     void testIsS3Enabled_WhenS3EndpointAndBucketSet_ReturnsTrue() {
75         injectFieldValue(appConfig, "s3Bucket", "test-bucket");
76         injectFieldValue(appConfig, "s3EndpointOverride", "s3.endpoint");
77         boolean result = appConfig.isS3Enabled();
78         assertTrue(result);
79     }
80
81     @Test
82     void testIsS3Enabled_WhenS3EndpointNotSet_ReturnsFalse() {
83         injectFieldValue(appConfig, "s3Bucket", "test-bucket");
84         injectFieldValue(appConfig, "s3EndpointOverride", "");
85         boolean result = appConfig.isS3Enabled();
86         assertFalse(result);
87     }
88
89     @Test
90     void testGetKafkaBootStrapServers() {
91         assertNull((new AppConfig()).getKafkaBootStrapServers());
92     }
93
94     @Test
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");
101
102         appConfig.addKafkaSecurityProps(props);
103
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));
108         assertEquals(
109             "org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required unsecuredLoginStringClaim_sub=\"alice\"; ",
110             props.get(SaslConfigs.SASL_JAAS_CONFIG));
111     }
112
113     @Test
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");
122
123         appConfig.addKafkaSecurityProps(props);
124
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));
131     }
132
133     @Test
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");
139
140         CertificateConfig certificateConfig = appConfig.getCertificateConfiguration();
141
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);
146     }
147
148     @Test
149     void testGetSftpConfiguration() {
150         injectFieldValue(appConfig, "knownHostsFilePath", "/path/to/known_hosts");
151         injectFieldValue(appConfig, "strictHostKeyChecking", true);
152
153         SftpConfig sftpConfig = appConfig.getSftpConfiguration();
154
155         assertEquals("/path/to/known_hosts", sftpConfig.knownHostsFilePath);
156         assertTrue(sftpConfig.strictHostKeyChecking);
157     }
158
159     private void injectFieldValue(Object target, String fieldName, Object value) {
160         try {
161             Field field = target.getClass().getDeclaredField(fieldName);
162             field.setAccessible(true);
163             field.set(target, value);
164         } catch (NoSuchFieldException | IllegalAccessException e) {
165             e.printStackTrace();
166         }
167     }
168 }