f7459f522830edc12abf2e117620fb07913b5ac0
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2019 Bell Canada
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 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.messaginglib
17
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
27 import org.junit.Test
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
64 import java.io.File
65 import java.nio.file.Files
66 import java.nio.file.Paths
67 import kotlin.test.assertNotNull
68
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"])
74 @DirtiesContext
75 @EmbeddedKafka(ports = [9092])
76 @WebFluxTest
77 class MessagingControllerTest {
78
79     private val log = LoggerFactory.getLogger(MessagingControllerTest::class.java)!!
80
81     @Autowired
82     lateinit var controller: MessagingController
83
84     @Value("\${blueprintsprocessor.messageclient.self-service-api.consumerTopic}")
85     lateinit var topicUsedForConsumer: String
86
87     @Autowired
88     lateinit var kt: KafkaTemplate<String, ExecutionServiceInput>
89
90     @Autowired
91     lateinit var webTestClient: WebTestClient
92
93     var receivedEvent: String? = null
94
95     @Before
96     fun setup() {
97         deleteDir("target", "blueprints")
98         uploadBluePrint()
99     }
100
101     @After
102     fun clean() {
103         deleteDir("target", "blueprints")
104     }
105
106     @Test
107     fun testReceive() {
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" +
114                 "      }\n" +
115                 "    }\n" +
116                 "  }"
117
118         kt.defaultTopic = topicUsedForConsumer
119
120         val input = ExecutionServiceInput().apply {
121             commonHeader = CommonHeader().apply {
122                 originatorId = "1"
123                 requestId = "1234"
124                 subRequestId = "1234-1234"
125             }
126
127             actionIdentifiers = ActionIdentifiers().apply {
128                 blueprintName = "golden"
129                 blueprintVersion = "1.0.0"
130                 actionName = "resource-assignment"
131                 mode = "sync"
132             }
133
134             stepData = StepData().apply {
135                 name = "resource-assignment"
136             }
137
138             payload = JacksonUtils.jsonNode(samplePayload) as ObjectNode
139         }
140
141         kt.sendDefault(input)
142         log.info("test-sender sent message='{}'", ToStringBuilder.reflectionToString(input))
143
144         Thread.sleep(1000)
145     }
146
147     @KafkaListener(topicPartitions = [TopicPartition(topic = "\${blueprintsprocessor.messageclient.self-service-api.topic}", partitionOffsets = [PartitionOffset(partition = "0", initialOffset = "0")])])
148     fun receivedEventFromBluePrintProducer(event: ExecutionServiceInput) {
149         assertNotNull(event)
150     }
151
152     private fun uploadBluePrint() {
153         runBlocking {
154             val body = MultipartBodyBuilder().apply {
155                 part("file", object : ByteArrayResource(Files.readAllBytes(loadCbaArchive().toPath())) {
156                     override fun getFilename(): String {
157                         return "test-cba.zip"
158                     }
159                 })
160             }.build()
161
162             webTestClient
163                     .post()
164                     .uri("/api/v1/execution-service/upload")
165                     .body(BodyInserters.fromMultipartData(body))
166                     .exchange()
167                     .expectStatus().isOk
168                     .returnResult<String>()
169                     .responseBody
170                     .awaitSingle()
171         }
172     }
173
174     private fun loadCbaArchive():File {
175         return Paths.get("./src/test/resources/cba-for-kafka-integration.zip").toFile()
176     }
177
178     @Configuration
179     @EnableKafka
180     open class ConsumerConfiguration {
181
182         @Value("\${blueprintsprocessor.messageclient.self-service-api.bootstrapServers}")
183         lateinit var bootstrapServers: String
184
185         @Value("\${blueprintsprocessor.messageclient.self-service-api.groupId}")
186         lateinit var groupId:String
187
188         @Bean
189         open fun consumerFactory2(): ConsumerFactory<String, ExecutionServiceInput>? {
190             val configProperties = hashMapOf<String, Any>()
191             configProperties[CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG] = bootstrapServers
192             configProperties[ConsumerConfig.GROUP_ID_CONFIG] = groupId
193             configProperties[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java.name
194             configProperties[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = JsonDeserializer::class.java.name
195             configProperties[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
196             configProperties[ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG] = 1000
197
198             return DefaultKafkaConsumerFactory(configProperties, StringDeserializer(),
199                     JsonDeserializer(ExecutionServiceInput::class.java))
200         }
201
202         @Bean
203         open fun listenerFactory(): ConcurrentKafkaListenerContainerFactory<String, ExecutionServiceInput> {
204             val factory = ConcurrentKafkaListenerContainerFactory<String, ExecutionServiceInput>()
205             factory.consumerFactory = consumerFactory2()
206             return factory
207         }
208     }
209 }
210
211