2 * Copyright © 2018-2019 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * 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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.message.service
19 import kotlinx.coroutines.delay
20 import kotlinx.coroutines.launch
21 import kotlinx.coroutines.runBlocking
22 import org.apache.kafka.common.serialization.Serdes
23 import org.apache.kafka.streams.Topology
24 import org.apache.kafka.streams.processor.Processor
25 import org.apache.kafka.streams.processor.ProcessorSupplier
26 import org.apache.kafka.streams.state.Stores
28 import org.junit.runner.RunWith
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
31 import org.onap.ccsdk.cds.blueprintsprocessor.message.BluePrintMessageLibConfiguration
32 import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsBasicAuthConsumerProperties
33 import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.annotation.DirtiesContext
36 import org.springframework.test.context.ContextConfiguration
37 import org.springframework.test.context.TestPropertySource
38 import org.springframework.test.context.junit4.SpringRunner
39 import kotlin.test.assertNotNull
41 @RunWith(SpringRunner::class)
43 @ContextConfiguration(
44 classes = [BluePrintMessageLibConfiguration::class,
45 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
50 "blueprintsprocessor.messageproducer.sample.type=kafka-scram-ssl-auth",
51 "blueprintsprocessor.messageproducer.sample.bootstrapServers=127.0.0.1:9092",
52 "blueprintsprocessor.messageproducer.sample.topic=default-stream-topic",
53 "blueprintsprocessor.messageproducer.sample.clientId=default-client-id",
54 "blueprintsprocessor.messageproducer.sample.truststore=/path/to/truststore.jks",
55 "blueprintsprocessor.messageproducer.sample.truststorePassword=secretpassword",
56 "blueprintsprocessor.messageproducer.sample.scramUsername=sample-user",
57 "blueprintsprocessor.messageproducer.sample.scramPassword=secretpassword",
59 "blueprintsprocessor.messageconsumer.stream-consumer.type=kafka-streams-scram-ssl-auth",
60 "blueprintsprocessor.messageconsumer.stream-consumer.bootstrapServers=127.0.0.1:9092",
61 "blueprintsprocessor.messageconsumer.stream-consumer.applicationId=test-streams-application",
62 "blueprintsprocessor.messageconsumer.stream-consumer.topic=default-stream-topic",
63 "blueprintsprocessor.messageproducer.stream-consumer.truststore=/path/to/truststore.jks",
64 "blueprintsprocessor.messageproducer.stream-consumer.truststorePassword=secretpassword",
65 "blueprintsprocessor.messageproducer.stream-consumer.scramUsername=sample-user",
66 "blueprintsprocessor.messageproducer.stream-consumer.scramPassword=secretpassword"
70 class KafkaStreamsConsumerServiceTest {
73 lateinit var bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
76 fun testProperties() {
77 val blueprintMessageConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
78 assertNotNull(blueprintMessageConsumerService, "failed to get blueprintMessageProducerService")
81 /** Integration Kafka Testing, Enable and use this test case only for local desktop testing with real kafka broker */
83 fun testKafkaStreamingMessageConsumer() {
85 val streamingConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
87 // Dynamic Consumer Function to create Topology
88 val consumerFunction = object : KafkaStreamConsumerFunction {
89 override suspend fun createTopology(
90 messageConsumerProperties: MessageConsumerProperties,
91 additionalConfig: Map<String, Any>?
93 val topology = Topology()
94 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
95 as KafkaStreamsBasicAuthConsumerProperties
97 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.split(",")
98 topology.addSource("Source", *topics.toTypedArray())
100 val firstProcessorSupplier = object : ProcessorSupplier<ByteArray, ByteArray> {
101 override fun get(): Processor<ByteArray, ByteArray> {
102 return FirstProcessor()
105 val changelogConfig: MutableMap<String, String> = hashMapOf()
106 changelogConfig.put("min.insync.replicas", "1")
109 val countStoreSupplier = Stores.keyValueStoreBuilder(
110 Stores.persistentKeyValueStore("PriorityMessageState"),
112 PriorityMessageSerde()
114 .withLoggingEnabled(changelogConfig)
116 topology.addProcessor("FirstProcessor", firstProcessorSupplier, "Source")
117 topology.addStateStore(countStoreSupplier, "FirstProcessor")
119 "SINK", "default-stream-topic-out", Serdes.String().serializer(),
120 PriorityMessageSerde().serializer(), "FirstProcessor"
126 /** Send message with every 1 sec */
127 val blueprintMessageProducerService = bluePrintMessageLibPropertyService
128 .blueprintMessageProducerService("sample") as KafkaMessageProducerService
132 val headers: MutableMap<String, String> = hashMapOf()
133 headers["id"] = it.toString()
134 blueprintMessageProducerService.sendMessageNB(
135 message = "this is my message($it)",
140 streamingConsumerService.consume(null, consumerFunction)
142 streamingConsumerService.shutDown()