44990ae7f189e1abe01cc17a942cce8458efc2ca
[ccsdk/cds.git] /
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 = [BluePrintMessageLibConfiguration::class,
45         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
46 )
47 @TestPropertySource(
48     properties =
49     [
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",
58
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"
67
68     ]
69 )
70 class KafkaStreamsConsumerServiceTest {
71
72     @Autowired
73     lateinit var bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
74
75     @Test
76     fun testProperties() {
77         val blueprintMessageConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
78         assertNotNull(blueprintMessageConsumerService, "failed to get blueprintMessageProducerService")
79     }
80
81     /** Integration Kafka Testing, Enable and use this test case only for local desktop testing with real kafka broker */
82     // @Test
83     fun testKafkaStreamingMessageConsumer() {
84         runBlocking {
85             val streamingConsumerService = bluePrintMessageLibPropertyService.blueprintMessageConsumerService("stream-consumer")
86
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>?
92                 ): Topology {
93                     val topology = Topology()
94                     val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
95                             as KafkaStreamsBasicAuthConsumerProperties
96
97                     val topics = kafkaStreamsBasicAuthConsumerProperties.topic.split(",")
98                     topology.addSource("Source", *topics.toTypedArray())
99                     // Processor Supplier
100                     val firstProcessorSupplier = object : ProcessorSupplier<ByteArray, ByteArray> {
101                         override fun get(): Processor<ByteArray, ByteArray> {
102                             return FirstProcessor()
103                         }
104                     }
105                     val changelogConfig: MutableMap<String, String> = hashMapOf()
106                     changelogConfig.put("min.insync.replicas", "1")
107
108                     // Store Buolder
109                     val countStoreSupplier = Stores.keyValueStoreBuilder(
110                         Stores.persistentKeyValueStore("PriorityMessageState"),
111                         Serdes.String(),
112                         PriorityMessageSerde()
113                     )
114                         .withLoggingEnabled(changelogConfig)
115
116                     topology.addProcessor("FirstProcessor", firstProcessorSupplier, "Source")
117                     topology.addStateStore(countStoreSupplier, "FirstProcessor")
118                     topology.addSink(
119                         "SINK", "default-stream-topic-out", Serdes.String().serializer(),
120                         PriorityMessageSerde().serializer(), "FirstProcessor"
121                     )
122                     return topology
123                 }
124             }
125
126             /** Send message with every 1 sec */
127             val blueprintMessageProducerService = bluePrintMessageLibPropertyService
128                 .blueprintMessageProducerService("sample") as KafkaMessageProducerService
129             launch {
130                 repeat(5) {
131                     delay(1000)
132                     val headers: MutableMap<String, String> = hashMapOf()
133                     headers["id"] = it.toString()
134                     blueprintMessageProducerService.sendMessageNB(
135                         key = "mykey",
136                         message = "this is my message($it)",
137                         headers = headers
138                     )
139                 }
140             }
141             streamingConsumerService.consume(null, consumerFunction)
142             delay(10000)
143             streamingConsumerService.shutDown()
144         }
145     }
146 }