[KAFKA] Adding new client code
[dmaap/kafka11aaf.git] / kafkaClient / src / test / java / org / onap / dmaap / kafka / TestConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * dmaap-kafka-client
4  * ================================================================================
5  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
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.onap.dmaap.kafka;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.Properties;
29 import lombok.SneakyThrows;
30
31 public class TestConfiguration implements org.onap.dmaap.kafka.IKafkaConfig {
32
33         private Properties loadProperties(String configFileName) throws IOException {
34                 Properties configuration = new Properties();
35                 try (InputStream inputStream = TestConfiguration.class
36                         .getClassLoader()
37                         .getResourceAsStream(configFileName)) {
38                         configuration.load(inputStream);
39                 }
40                 return configuration;
41         }
42
43         private final Properties testConfig;
44         private List<String> bootstrapServers;
45         private List<String> consumerTopics;
46
47         @SneakyThrows
48         public TestConfiguration(String configFilename) {
49                 testConfig = loadProperties(configFilename);
50                 bootstrapServers = new ArrayList<>(Arrays.asList(((String) testConfig.get("kafka.kafkaBootstrapServers")).split(",")));
51         }
52
53         @Override
54         public List<String> getKafkaBootstrapServers() {
55                 return bootstrapServers;
56         }
57
58         public void setBootstrapServers(List<String> newBootstrapList) {
59                 bootstrapServers = newBootstrapList;
60         }
61
62         @Override
63         public String getKafkaSaslMechanism() {
64                 return "PLAIN";
65         }
66
67         @Override
68         public String getKafkaSaslJaasConfig() {
69                 return "org.apache.kafka.common.security.plain.PlainLoginModule required username=admin password=admin-secret;";
70         }
71
72         @Override
73         public int getPollingTimeout() {
74                 return Integer.parseInt((String) testConfig.get("kafka.pollingTimeout"));
75         }
76
77         @Override
78         public String getConsumerGroup() {
79                 return (String) testConfig.get("kafka.consumerGroup");
80         }
81
82         @Override
83         public String getConsumerID() {
84                 return (String) testConfig.get("kafka.consumerID");
85         }
86
87         @Override
88         public List<String> getConsumerTopics() {
89                 consumerTopics = new ArrayList<>();
90                 String topicString = (String) testConfig.get("kafka.consumerTopics");
91                 if (topicString != null) {
92                         consumerTopics.addAll(Arrays.asList((topicString).split(",")));
93                 }
94                 return consumerTopics;
95         }
96
97         public void setConsumerTopics(List<String> newTopics) {
98                 this.consumerTopics = newTopics;
99         }
100
101         @Override
102         public List<String> getProducerTopics() {
103                 List<String> producerTopics = new ArrayList<>();
104                 String topicString = (String) testConfig.get("kafka.producerTopics");
105                 if (topicString != null) {
106                         producerTopics.addAll(Arrays.asList((topicString).split(",")));
107                 }
108                 return producerTopics;
109         }
110 }