5ddf2b2a68c919957207f0614b8db761b118714c
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. 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.netconfsimulator.kafka;
22
23
24 import java.util.Map;
25 import org.apache.kafka.clients.consumer.ConsumerConfig;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.context.annotation.Bean;
28 import org.springframework.context.annotation.Configuration;
29 import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
30 import org.springframework.kafka.core.ConsumerFactory;
31 import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
32 import org.springframework.kafka.core.DefaultKafkaProducerFactory;
33 import org.springframework.kafka.core.KafkaTemplate;
34 import org.springframework.kafka.core.ProducerFactory;
35 import org.springframework.kafka.test.utils.KafkaTestUtils;
36
37 import static org.onap.netconfsimulator.kafka.StoreServiceTest.embeddedKafka;
38
39 @Configuration
40 class EmbeddedKafkaConfig {
41
42     @Bean
43     KafkaTemplate<String, String> kafkaTemplate(){
44         return new KafkaTemplate<>(producerFactory());
45     }
46
47     @Bean
48     @Autowired
49     ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(ConsumerFactory<String, String> consumerFactory){
50         ConcurrentKafkaListenerContainerFactory<String, String> containerFactory = new ConcurrentKafkaListenerContainerFactory<>();
51         containerFactory.setConsumerFactory(consumerFactory);
52         return containerFactory;
53     }
54
55     @Bean
56     ConsumerFactory<String, String> consumerFactory(){
57         Map<String, Object> consumerProperties =
58             KafkaTestUtils.consumerProps("sender", "false", embeddedKafka.getEmbeddedKafka());
59         consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
60         return new DefaultKafkaConsumerFactory<>(consumerProperties);
61     }
62
63     private ProducerFactory<String, String> producerFactory() {
64         Map<String, Object> senderProperties =
65             KafkaTestUtils.senderProps(embeddedKafka.getEmbeddedKafka().getBrokersAsString());
66         return new DefaultKafkaProducerFactory<>(senderProperties);
67     }
68
69 }