2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.ccsdk.apps.blueprintprocessor.dmaap
23 import com.fasterxml.jackson.databind.ObjectMapper
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.dmaap.BluePrintDmaapLibConfiguration
29 import org.onap.ccsdk.cds.blueprintsprocessor.dmaap.BluePrintDmaapLibPropertyService
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
32 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
33 import org.springframework.boot.test.context.SpringBootTest
34 import org.springframework.http.HttpStatus
35 import org.springframework.http.ResponseEntity
36 import org.springframework.test.context.ContextConfiguration
37 import org.springframework.test.context.TestPropertySource
38 import org.springframework.test.context.junit4.SpringRunner
39 import org.springframework.web.bind.annotation.PathVariable
40 import org.springframework.web.bind.annotation.PostMapping
41 import org.springframework.web.bind.annotation.RequestMapping
42 import org.springframework.web.bind.annotation.RestController
43 import kotlin.test.assertEquals
44 import kotlin.test.assertNotNull
47 * Unit test cases for DMaap publisher code.
49 @RunWith(SpringRunner::class)
50 @EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
51 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
52 @ContextConfiguration(
53 classes = [BluePrintDmaapLibConfiguration::class, TestController::class,
54 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
57 properties = ["server.port=9111",
58 "blueprintsprocessor.dmaapclient.aai.topic=cds_aai",
59 "blueprintsprocessor.dmaapclient.aai.type=HTTPNOAUTH",
60 "blueprintsprocessor.dmaapclient.aai.host=127.0.0.1:9111",
61 "blueprintsprocessor.dmaapclient.multi.topic=cds_multi1,cds_multi2",
62 "blueprintsprocessor.dmaapclient.multi.type=HTTPNOAUTH",
63 "blueprintsprocessor.dmaapclient.multi.host=127.0.0.1:9111"]
65 class TestDmaapEventPublisher {
68 lateinit var dmaapService: BluePrintDmaapLibPropertyService
71 * Tests the event properties being set properly and sent as request.
74 fun testEventProperties() {
75 val strList = mutableListOf<String>()
76 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
80 " \"a\" : \"hello\"\n" +
83 dmaapClient.sendMessage(strList)
84 val msgs = dmaapClient.close(2)
85 assertEquals(msgs!!.size, 1)
86 val topic1 = msgs.get(0)
87 assertEquals(topic1!!.size, 0)
91 * Tests the event properties being set properly and sent as request with
95 fun testEventPropertiesWithSingleMsg() {
96 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
97 val str: String = "{\n" +
98 " \"a\" : \"hello\"\n" +
100 dmaapClient.sendMessage(str)
101 val msgs = dmaapClient.close(2)
102 assertEquals(msgs!!.size, 1)
103 val topic1 = msgs.get(0)
104 assertEquals(topic1!!.size, 0)
108 * Tests the event properties with multiple topics.
111 fun testMultiTopicProperties() {
112 val strList = mutableListOf<String>()
113 val dmaapClient = dmaapService.blueprintDmaapClientService("multi")
117 " \"a\" : \"hello\"\n" +
120 dmaapClient.sendMessage(strList)
121 val msgs = dmaapClient.close(2)
122 assertEquals(msgs!!.size, 2)
123 val topic1 = msgs.get(0)
124 assertEquals(topic1!!.size, 0)
125 val topic2 = msgs.get(1)
126 assertEquals(topic2!!.size, 0)
130 * Tests the event properties with multiple topics with JSON node as input.
133 fun testMultiTopicPropertiesWithJsonInput() {
134 val jsonString = "{\n" +
135 " \"topic\" : \"cds_json1,cds_json2\",\n" +
136 " \"type\" : \"HTTPNOAUTH\",\n" +
137 " \"host\" : \"127.0.0.1:9111\"\n" +
139 val mapper = ObjectMapper()
140 val node = mapper.readTree(jsonString)
141 val strList = mutableListOf<String>()
142 val dmaapClient = dmaapService.blueprintDmaapClientService(node)
146 " \"a\" : \"hello\"\n" +
149 dmaapClient.sendMessage(strList)
150 val msgs = dmaapClient.close(2)
151 assertEquals(msgs!!.size, 2)
152 val topic1 = msgs.get(0)
153 assertEquals(topic1!!.size, 0)
154 val topic2 = msgs.get(1)
155 assertEquals(topic2!!.size, 0)
159 * Tests the event properties with multiple messages.
162 fun testMultiMsgsProperties() {
163 val strList = mutableListOf<String>()
164 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
168 " \"a\" : \"hello\"\n" +
173 " \"a\" : \"second\"\n" +
176 dmaapClient.sendMessage(strList)
177 val msgs = dmaapClient.close(2)
178 assertEquals(msgs!!.size, 1)
179 val topic1 = msgs.get(0)
180 assertEquals(topic1!!.size, 0)
184 * Tests the DMAAP client properties generated with the complete prefix.
187 fun testDmaapClientProperties() {
188 val properties = dmaapService.dmaapClientProperties(
189 "blueprintsprocessor.dmaapclient.aai"
191 assertNotNull(properties, "failed to create property bean")
193 properties.host, "failed to get url property" +
199 * Tests the blueprint DMAAP client service with only selector prefix.
202 fun testBlueprintDmaapClientService() {
203 val blueprintDmaapClientService =
204 dmaapService.blueprintDmaapClientService("aai")
206 blueprintDmaapClientService,
207 "failed to create blueprintDmaapClientService"
213 * Rest controller for testing the client request that is sent.
216 @RequestMapping(path = ["/events"])
217 open class TestController {
220 * Accepts request for a topic and sends a message as response.
222 @PostMapping(path = ["/{topic}"])
223 fun postTopic(@PathVariable(value = "topic") topic: String):
224 ResponseEntity<Any> {
226 " \"message\" : \"The message is published into $topic " +
229 return ResponseEntity(a, HttpStatus.OK)