Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / service / MockKafkaTopologyComponents.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 org.apache.kafka.common.serialization.Deserializer
20 import org.apache.kafka.common.serialization.Serde
21 import org.apache.kafka.common.serialization.Serializer
22 import org.apache.kafka.streams.processor.Processor
23 import org.apache.kafka.streams.processor.ProcessorContext
24 import org.apache.kafka.streams.state.KeyValueStore
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import java.io.Serializable
30 import java.nio.charset.Charset
31 import java.util.UUID
32
33 class PriorityMessage : Serializable {
34     lateinit var id: String
35     lateinit var requestMessage: String
36 }
37
38 open class PriorityMessageSerde : Serde<PriorityMessage> {
39
40     override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
41     }
42
43     override fun close() {
44     }
45
46     override fun deserializer(): Deserializer<PriorityMessage> {
47         return object : Deserializer<PriorityMessage> {
48             override fun deserialize(topic: String, data: ByteArray): PriorityMessage {
49                 return JacksonUtils.readValue(String(data), PriorityMessage::class.java)
50                     ?: throw BluePrintProcessorException("failed to convert")
51             }
52
53             override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
54             }
55
56             override fun close() {
57             }
58         }
59     }
60
61     override fun serializer(): Serializer<PriorityMessage> {
62         return object : Serializer<PriorityMessage> {
63             override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
64             }
65
66             override fun serialize(topic: String?, data: PriorityMessage): ByteArray {
67                 return data.asJsonString().toByteArray(Charset.defaultCharset())
68             }
69
70             override fun close() {
71             }
72         }
73     }
74 }
75
76 class FirstProcessor : Processor<ByteArray, ByteArray> {
77
78     private val log = logger(FirstProcessor::class)
79
80     private lateinit var context: ProcessorContext
81     private lateinit var kvStore: KeyValueStore<String, PriorityMessage>
82
83     override fun process(key: ByteArray, value: ByteArray) {
84         log.info("First Processor key(${String(key)} : value(${String(value)})")
85         val newMessage = PriorityMessage().apply {
86             id = UUID.randomUUID().toString()
87             requestMessage = String(value)
88         }
89         kvStore.put(newMessage.id, newMessage)
90         this.context.forward(newMessage.id, newMessage)
91     }
92
93     override fun init(context: ProcessorContext) {
94         log.info("init... ${context.keySerde()}, ${context.valueSerde()}")
95         this.context = context
96         this.kvStore = context.getStateStore("PriorityMessageState") as KeyValueStore<String, PriorityMessage>
97     }
98
99     override fun close() {
100         log.info("Close...")
101     }
102 }