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(
45 BlueprintMessageLibConfiguration::class,
46 BlueprintPropertyConfiguration::class, BlueprintPropertiesService::class
52 "blueprintsprocessor.messageproducer.sample.type=kafka-scram-ssl-auth",
53 "blueprintsprocessor.messageproducer.sample.bootstrapServers=127.0.0.1:9092",
54 "blueprintsprocessor.messageproducer.sample.topic=default-stream-topic",
55 "blueprintsprocessor.messageproducer.sample.clientId=default-client-id",
56 "blueprintsprocessor.messageproducer.sample.truststore=/path/to/truststore.jks",
57 "blueprintsprocessor.messageproducer.sample.truststorePassword=secretpassword",
58 "blueprintsprocessor.messageproducer.sample.scramUsername=sample-user",
59 "blueprintsprocessor.messageproducer.sample.scramPassword=secretpassword",
61 "blueprintsprocessor.messageconsumer.stream-consumer.type=kafka-streams-scram-ssl-auth",
62 "blueprintsprocessor.messageconsumer.stream-consumer.bootstrapServers=127.0.0.1:9092",
63 "blueprintsprocessor.messageconsumer.stream-consumer.applicationId=test-streams-application",
64 "blueprintsprocessor.messageconsumer.stream-consumer.topic=default-stream-topic",
65 "blueprintsprocessor.messageproducer.stream-consumer.truststore=/path/to/truststore.jks",
66 "blueprintsprocessor.messageproducer.stream-consumer.truststorePassword=secretpassword",
67 "blueprintsprocessor.messageproducer.stream-consumer.scramUsername=sample-user",
68 "blueprintsprocessor.messageproducer.stream-consumer.scramPassword=secretpassword"
72 class KafkaStreamsConsumerServiceTest {
75 lateinit var bluePrintMessageLibPropertyService: BlueprintMessageLibPropertyService
78 fun testProperties() {
79 val blueprintMessageConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
80 assertNotNull(blueprintMessageConsumerService, "failed to get blueprintMessageProducerService")
83 /** Integration Kafka Testing, Enable and use this test case only for local desktop testing with real kafka broker */
85 fun testKafkaStreamingMessageConsumer() {
87 val streamingConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
89 // Dynamic Consumer Function to create Topology
90 val consumerFunction = object : KafkaStreamConsumerFunction {
91 override suspend fun createTopology(
92 messageConsumerProperties: MessageConsumerProperties,
93 additionalConfig: Map<String, Any>?
95 val topology = Topology()
96 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
97 as KafkaStreamsBasicAuthConsumerProperties
99 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.split(",")
100 topology.addSource("Source", *topics.toTypedArray())
101 // Processor Supplier
102 val firstProcessorSupplier = object : ProcessorSupplier<ByteArray, ByteArray> {
103 override fun get(): Processor<ByteArray, ByteArray> {
104 return FirstProcessor()
107 val changelogConfig: MutableMap<String, String> = hashMapOf()
108 changelogConfig.put("min.insync.replicas", "1")
111 val countStoreSupplier = Stores.keyValueStoreBuilder(
112 Stores.persistentKeyValueStore("PriorityMessageState"),
114 PriorityMessageSerde()
116 .withLoggingEnabled(changelogConfig)
118 topology.addProcessor("FirstProcessor", firstProcessorSupplier, "Source")
119 topology.addStateStore(countStoreSupplier, "FirstProcessor")
121 "SINK", "default-stream-topic-out", Serdes.String().serializer(),
122 PriorityMessageSerde().serializer(), "FirstProcessor"
128 /** Send message with every 1 sec */
129 val blueprintMessageProducerService = bluePrintMessageLibPropertyService
130 .blueprintMessageProducerService("sample") as KafkaMessageProducerService
134 val headers: MutableMap<String, String> = hashMapOf()
135 headers["id"] = it.toString()
136 blueprintMessageProducerService.sendMessageNB(
138 message = "this is my message($it)",
143 streamingConsumerService.consume(null, consumerFunction)
145 streamingConsumerService.shutDown()