Merge "Add kafka messaging support to integration test module"
[cps.git] / integration-test / src / test / java / org / onap / cps / integration / KafkaTestContainer.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.integration;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import org.apache.kafka.clients.consumer.ConsumerConfig;
25 import org.apache.kafka.clients.consumer.KafkaConsumer;
26 import org.apache.kafka.common.serialization.StringDeserializer;
27 import org.testcontainers.containers.KafkaContainer;
28 import org.testcontainers.utility.DockerImageName;
29
30 /**
31  * The Apache Kafka test container wrapper.
32  * Allow to use specific image and version with Singleton design pattern.
33  * This ensures only one instance of Kafka container across the integration tests.
34  * Avoid unnecessary resource and time consumption.
35  */
36 public class KafkaTestContainer extends KafkaContainer {
37
38     private static final String IMAGE_NAME_AND_VERSION = "registry.nordix.org/onaptest/confluentinc/cp-kafka:6.2.1";
39
40     private static KafkaTestContainer kafkaTestContainer;
41
42     private KafkaTestContainer() {
43         super(DockerImageName.parse(IMAGE_NAME_AND_VERSION).asCompatibleSubstituteFor("confluentinc/cp-kafka"));
44     }
45
46     /**
47      * Provides an instance of Kafka test container wrapper.
48      * This will allow to initialize Kafka messaging support before any integration test run.
49      *
50      * @return KafkaTestContainer the unique Kafka instance
51      */
52     public static KafkaTestContainer getInstance() {
53         if (kafkaTestContainer == null) {
54             kafkaTestContainer = new KafkaTestContainer();
55             Runtime.getRuntime().addShutdownHook(new Thread(kafkaTestContainer::close));
56         }
57         return kafkaTestContainer;
58     }
59
60     public static KafkaConsumer getConsumer(final String consumerGroupId, final Object valueDeserializer) {
61         return new KafkaConsumer<>(consumerProperties(consumerGroupId, valueDeserializer));
62     }
63
64     @Override
65     public void start() {
66         super.start();
67         System.setProperty("spring.kafka.properties.bootstrap.servers", kafkaTestContainer.getBootstrapServers());
68     }
69
70     @Override
71     public void stop() {
72         // Method intentionally left blank
73     }
74
75     private static Map<String, Object> consumerProperties(final String consumerGroupId,
76                                                           final Object valueDeserializer) {
77         final Map<String, Object> configProps = new HashMap<>();
78         configProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaTestContainer.getBootstrapServers());
79         configProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
80         configProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializer);
81         configProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
82         configProps.put(ConsumerConfig.GROUP_ID_CONFIG, consumerGroupId);
83         return configProps;
84     }
85
86 }