Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / service / KafkaStreamsConsumerServiceTest.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.onap.ccsdk.cds.blueprintsprocessor.message.service
18
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
27 import org.junit.Test
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
40
41 @RunWith(SpringRunner::class)
42 @DirtiesContext
43 @ContextConfiguration(
44     classes = [
45         BlueprintMessageLibConfiguration::class,
46         BlueprintPropertyConfiguration::class, BlueprintPropertiesService::class
47     ]
48 )
49 @TestPropertySource(
50     properties =
51         [
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",
60
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"
69
70         ]
71 )
72 class KafkaStreamsConsumerServiceTest {
73
74     @Autowired
75     lateinit var bluePrintMessageLibPropertyService: BlueprintMessageLibPropertyService
76
77     @Test
78     fun testProperties() {
79         val blueprintMessageConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
80         assertNotNull(blueprintMessageConsumerService, "failed to get blueprintMessageProducerService")
81     }
82
83     /** Integration Kafka Testing, Enable and use this test case only for local desktop testing with real kafka broker */
84     // @Test
85     fun testKafkaStreamingMessageConsumer() {
86         runBlocking {
87             val streamingConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
88
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>?
94                 ): Topology {
95                     val topology = Topology()
96                     val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
97                         as KafkaStreamsBasicAuthConsumerProperties
98
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()
105                         }
106                     }
107                     val changelogConfig: MutableMap<String, String> = hashMapOf()
108                     changelogConfig.put("min.insync.replicas", "1")
109
110                     // Store Buolder
111                     val countStoreSupplier = Stores.keyValueStoreBuilder(
112                         Stores.persistentKeyValueStore("PriorityMessageState"),
113                         Serdes.String(),
114                         PriorityMessageSerde()
115                     )
116                         .withLoggingEnabled(changelogConfig)
117
118                     topology.addProcessor("FirstProcessor", firstProcessorSupplier, "Source")
119                     topology.addStateStore(countStoreSupplier, "FirstProcessor")
120                     topology.addSink(
121                         "SINK", "default-stream-topic-out", Serdes.String().serializer(),
122                         PriorityMessageSerde().serializer(), "FirstProcessor"
123                     )
124                     return topology
125                 }
126             }
127
128             /** Send message with every 1 sec */
129             val blueprintMessageProducerService = bluePrintMessageLibPropertyService
130                 .blueprintMessageProducerService("sample") as KafkaMessageProducerService
131             launch {
132                 repeat(5) {
133                     delay(1000)
134                     val headers: MutableMap<String, String> = hashMapOf()
135                     headers["id"] = it.toString()
136                     blueprintMessageProducerService.sendMessageNB(
137                         key = "mykey",
138                         message = "this is my message($it)",
139                         headers = headers
140                     )
141                 }
142             }
143             streamingConsumerService.consume(null, consumerFunction)
144             delay(10000)
145             streamingConsumerService.shutDown()
146         }
147     }
148 }