Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / service / BlueprintMessageProducerServiceTest.kt
1 /*
2  *  Copyright © 2019 IBM.
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 io.mockk.every
20 import io.mockk.mockk
21 import io.mockk.spyk
22 import kotlinx.coroutines.runBlocking
23 import org.apache.kafka.clients.producer.KafkaProducer
24 import org.apache.kafka.clients.producer.RecordMetadata
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
28 import org.onap.ccsdk.cds.blueprintsprocessor.message.BluePrintMessageLibConfiguration
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.test.annotation.DirtiesContext
31 import org.springframework.test.context.ContextConfiguration
32 import org.springframework.test.context.TestPropertySource
33 import org.springframework.test.context.junit4.SpringRunner
34 import java.util.concurrent.Future
35 import kotlin.test.Test
36 import kotlin.test.assertTrue
37
38 @RunWith(SpringRunner::class)
39 @DirtiesContext
40 @ContextConfiguration(
41     classes = [BluePrintMessageLibConfiguration::class,
42         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
43 )
44 @TestPropertySource(
45     properties =
46     ["blueprintsprocessor.messageproducer.sample.type=kafka-basic-auth",
47         "blueprintsprocessor.messageproducer.sample.bootstrapServers=127.0.0.1:9092",
48         "blueprintsprocessor.messageproducer.sample.topic=default-topic",
49         "blueprintsprocessor.messageproducer.sample.clientId=default-client-id"
50     ]
51 )
52 open class BlueprintMessageProducerServiceTest {
53
54     @Autowired
55     lateinit var bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
56
57     @Test
58     fun testKafkaBasicAuthProducertService() {
59         runBlocking {
60             val blueprintMessageProducerService = bluePrintMessageLibPropertyService
61                 .blueprintMessageProducerService("sample") as KafkaBasicAuthMessageProducerService
62
63             val mockKafkaTemplate = mockk<KafkaProducer<String, ByteArray>>()
64
65             val responseMock = mockk<Future<RecordMetadata>>()
66             every { responseMock.get() } returns mockk()
67
68             every { mockKafkaTemplate.send(any(), any()) } returns responseMock
69
70             val spyBluePrintMessageProducerService = spyk(blueprintMessageProducerService, recordPrivateCalls = true)
71
72             every { spyBluePrintMessageProducerService.messageTemplate(any()) } returns mockKafkaTemplate
73
74             val response = spyBluePrintMessageProducerService.sendMessage("Testing message")
75             assertTrue(response, "failed to get command response")
76         }
77     }
78 }