2 * Copyright © 2019 Bell Canada
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.
16 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.messaginglib
18 import com.fasterxml.jackson.databind.node.ObjectNode
19 import kotlinx.coroutines.reactive.awaitSingle
20 import kotlinx.coroutines.runBlocking
21 import org.apache.commons.lang.builder.ToStringBuilder
22 import org.apache.kafka.clients.CommonClientConfigs
23 import org.apache.kafka.clients.consumer.ConsumerConfig
24 import org.apache.kafka.common.serialization.StringDeserializer
25 import org.junit.After
26 import org.junit.Before
28 import org.junit.runner.RunWith
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
33 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.MessagingController
34 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
36 import org.slf4j.LoggerFactory
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.beans.factory.annotation.Value
39 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
40 import org.springframework.boot.autoconfigure.security.SecurityProperties
41 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
42 import org.springframework.context.annotation.Bean
43 import org.springframework.context.annotation.ComponentScan
44 import org.springframework.context.annotation.Configuration
45 import org.springframework.core.io.ByteArrayResource
46 import org.springframework.http.client.MultipartBodyBuilder
47 import org.springframework.kafka.annotation.EnableKafka
48 import org.springframework.kafka.annotation.KafkaListener
49 import org.springframework.kafka.annotation.PartitionOffset
50 import org.springframework.kafka.annotation.TopicPartition
51 import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory
52 import org.springframework.kafka.core.ConsumerFactory
53 import org.springframework.kafka.core.DefaultKafkaConsumerFactory
54 import org.springframework.kafka.core.KafkaTemplate
55 import org.springframework.kafka.support.serializer.JsonDeserializer
56 import org.springframework.kafka.test.context.EmbeddedKafka
57 import org.springframework.test.annotation.DirtiesContext
58 import org.springframework.test.context.ContextConfiguration
59 import org.springframework.test.context.TestPropertySource
60 import org.springframework.test.context.junit4.SpringRunner
61 import org.springframework.test.web.reactive.server.WebTestClient
62 import org.springframework.test.web.reactive.server.returnResult
63 import org.springframework.web.reactive.function.BodyInserters
65 import java.nio.file.Files
66 import java.nio.file.Paths
67 import kotlin.test.assertNotNull
69 @RunWith(SpringRunner::class)
70 @EnableAutoConfiguration
71 @ContextConfiguration(classes = [MessagingControllerTest::class, SecurityProperties::class])
72 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
73 @TestPropertySource(locations = ["classpath:application-test.properties"])
75 @EmbeddedKafka(ports = [9092])
77 class MessagingControllerTest {
79 private val log = LoggerFactory.getLogger(MessagingControllerTest::class.java)!!
82 lateinit var controller: MessagingController
84 @Value("\${blueprintsprocessor.messageclient.self-service-api.consumerTopic}")
85 lateinit var topicUsedForConsumer: String
88 lateinit var kt: KafkaTemplate<String, ExecutionServiceInput>
91 lateinit var webTestClient: WebTestClient
93 var event: ExecutionServiceInput? = null
97 deleteDir("target", "blueprints")
103 deleteDir("target", "blueprints")
108 val samplePayload = "{\n" +
109 " \"resource-assignment-request\": {\n" +
110 " \"artifact-name\": [\"hostname\"],\n" +
111 " \"store-result\": true,\n" +
112 " \"resource-assignment-properties\" : {\n" +
113 " \"hostname\": \"demo123\"\n" +
118 kt.defaultTopic = topicUsedForConsumer
120 val input = ExecutionServiceInput().apply {
121 commonHeader = CommonHeader().apply {
124 subRequestId = "1234-1234"
127 actionIdentifiers = ActionIdentifiers().apply {
128 blueprintName = "golden"
129 blueprintVersion = "1.0.0"
130 actionName = "resource-assignment"
134 stepData = StepData().apply {
135 name = "resource-assignment"
138 payload = JacksonUtils.jsonNode(samplePayload) as ObjectNode
141 kt.sendDefault(input)
142 log.info("test-sender sent message='{}'", ToStringBuilder.reflectionToString(input))
149 @KafkaListener(topicPartitions = [TopicPartition(topic = "\${blueprintsprocessor.messageclient.self-service-api.topic}", partitionOffsets = [PartitionOffset(partition = "0", initialOffset = "0")])])
150 fun receivedEventFromBluePrintProducer(receivedEvent: ExecutionServiceInput) {
151 event = receivedEvent
154 private fun uploadBluePrint() {
156 val body = MultipartBodyBuilder().apply {
157 part("file", object : ByteArrayResource(Files.readAllBytes(loadCbaArchive().toPath())) {
158 override fun getFilename(): String {
159 return "test-cba.zip"
166 .uri("/api/v1/execution-service/upload")
167 .body(BodyInserters.fromMultipartData(body))
170 .returnResult<String>()
176 private fun loadCbaArchive():File {
177 return Paths.get("./src/test/resources/cba-for-kafka-integration_enriched.zip").toFile()
182 open class ConsumerConfiguration {
184 @Value("\${blueprintsprocessor.messageclient.self-service-api.bootstrapServers}")
185 lateinit var bootstrapServers: String
187 @Value("\${blueprintsprocessor.messageclient.self-service-api.groupId}")
188 lateinit var groupId:String
191 open fun consumerFactory2(): ConsumerFactory<String, ExecutionServiceInput>? {
192 val configProperties = hashMapOf<String, Any>()
193 configProperties[CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG] = bootstrapServers
194 configProperties[ConsumerConfig.GROUP_ID_CONFIG] = groupId
195 configProperties[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java.name
196 configProperties[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = JsonDeserializer::class.java.name
197 configProperties[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
198 configProperties[ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG] = 1000
200 return DefaultKafkaConsumerFactory(configProperties, StringDeserializer(),
201 JsonDeserializer(ExecutionServiceInput::class.java))
205 open fun listenerFactory(): ConcurrentKafkaListenerContainerFactory<String, ExecutionServiceInput> {
206 val factory = ConcurrentKafkaListenerContainerFactory<String, ExecutionServiceInput>()
207 factory.consumerFactory = consumerFactory2()