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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.integration;
22 import java.util.HashMap;
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;
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.
36 public class KafkaTestContainer extends KafkaContainer {
38 private static final String IMAGE_NAME_AND_VERSION = "registry.nordix.org/onaptest/confluentinc/cp-kafka:6.2.1";
40 private static KafkaTestContainer kafkaTestContainer;
42 private KafkaTestContainer() {
43 super(DockerImageName.parse(IMAGE_NAME_AND_VERSION).asCompatibleSubstituteFor("confluentinc/cp-kafka"));
47 * Provides an instance of Kafka test container wrapper.
48 * This will allow to initialize Kafka messaging support before any integration test run.
50 * @return KafkaTestContainer the unique Kafka instance
52 public static KafkaTestContainer getInstance() {
53 if (kafkaTestContainer == null) {
54 kafkaTestContainer = new KafkaTestContainer();
55 Runtime.getRuntime().addShutdownHook(new Thread(kafkaTestContainer::close));
57 return kafkaTestContainer;
60 public static KafkaConsumer getConsumer(final String consumerGroupId, final Object valueDeserializer) {
61 return new KafkaConsumer<>(consumerProperties(consumerGroupId, valueDeserializer));
67 System.setProperty("spring.kafka.properties.bootstrap.servers", kafkaTestContainer.getBootstrapServers());
72 // Method intentionally left blank
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);