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(
54 BluePrintDmaapLibConfiguration::class, TestController::class,
55 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class
61 "blueprintsprocessor.dmaapclient.aai.topic=cds_aai",
62 "blueprintsprocessor.dmaapclient.aai.type=HTTPNOAUTH",
63 "blueprintsprocessor.dmaapclient.aai.host=127.0.0.1:9111",
64 "blueprintsprocessor.dmaapclient.multi.topic=cds_multi1,cds_multi2",
65 "blueprintsprocessor.dmaapclient.multi.type=HTTPNOAUTH",
66 "blueprintsprocessor.dmaapclient.multi.host=127.0.0.1:9111"
69 class TestDmaapEventPublisher {
72 lateinit var dmaapService: BluePrintDmaapLibPropertyService
75 * Tests the event properties being set properly and sent as request.
78 fun testEventProperties() {
79 val strList = mutableListOf<String>()
80 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
84 " \"a\" : \"hello\"\n" +
87 dmaapClient.sendMessage(strList)
88 val msgs = dmaapClient.close(2)
89 assertEquals(msgs!!.size, 1)
90 val topic1 = msgs.get(0)
91 assertEquals(topic1!!.size, 0)
95 * Tests the event properties being set properly and sent as request with
99 fun testEventPropertiesWithSingleMsg() {
100 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
101 val str: String = "{\n" +
102 " \"a\" : \"hello\"\n" +
104 dmaapClient.sendMessage(str)
105 val msgs = dmaapClient.close(2)
106 assertEquals(msgs!!.size, 1)
107 val topic1 = msgs.get(0)
108 assertEquals(topic1!!.size, 0)
112 * Tests the event properties with multiple topics.
115 fun testMultiTopicProperties() {
116 val strList = mutableListOf<String>()
117 val dmaapClient = dmaapService.blueprintDmaapClientService("multi")
121 " \"a\" : \"hello\"\n" +
124 dmaapClient.sendMessage(strList)
125 val msgs = dmaapClient.close(2)
126 assertEquals(msgs!!.size, 2)
127 val topic1 = msgs.get(0)
128 assertEquals(topic1!!.size, 0)
129 val topic2 = msgs.get(1)
130 assertEquals(topic2!!.size, 0)
134 * Tests the event properties with multiple topics with JSON node as input.
137 fun testMultiTopicPropertiesWithJsonInput() {
138 val jsonString = "{\n" +
139 " \"topic\" : \"cds_json1,cds_json2\",\n" +
140 " \"type\" : \"HTTPNOAUTH\",\n" +
141 " \"host\" : \"127.0.0.1:9111\"\n" +
143 val mapper = ObjectMapper()
144 val node = mapper.readTree(jsonString)
145 val strList = mutableListOf<String>()
146 val dmaapClient = dmaapService.blueprintDmaapClientService(node)
150 " \"a\" : \"hello\"\n" +
153 dmaapClient.sendMessage(strList)
154 val msgs = dmaapClient.close(2)
155 assertEquals(msgs!!.size, 2)
156 val topic1 = msgs.get(0)
157 assertEquals(topic1!!.size, 0)
158 val topic2 = msgs.get(1)
159 assertEquals(topic2!!.size, 0)
163 * Tests the event properties with multiple messages.
166 fun testMultiMsgsProperties() {
167 val strList = mutableListOf<String>()
168 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
172 " \"a\" : \"hello\"\n" +
177 " \"a\" : \"second\"\n" +
180 dmaapClient.sendMessage(strList)
181 val msgs = dmaapClient.close(2)
182 assertEquals(msgs!!.size, 1)
183 val topic1 = msgs.get(0)
184 assertEquals(topic1!!.size, 0)
188 * Tests the DMAAP client properties generated with the complete prefix.
191 fun testDmaapClientProperties() {
192 val properties = dmaapService.dmaapClientProperties(
193 "blueprintsprocessor.dmaapclient.aai"
195 assertNotNull(properties, "failed to create property bean")
198 "failed to get url property" +
204 * Tests the blueprint DMAAP client service with only selector prefix.
207 fun testBlueprintDmaapClientService() {
208 val blueprintDmaapClientService =
209 dmaapService.blueprintDmaapClientService("aai")
211 blueprintDmaapClientService,
212 "failed to create blueprintDmaapClientService"
218 * Rest controller for testing the client request that is sent.
221 @RequestMapping(path = ["/events"])
222 open class TestController {
225 * Accepts request for a topic and sends a message as response.
227 @PostMapping(path = ["/{topic}"])
228 fun postTopic(@PathVariable(value = "topic") topic: String):
229 ResponseEntity<Any> {
231 " \"message\" : \"The message is published into $topic " +
234 return ResponseEntity(a, HttpStatus.OK)