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.BluePrintProperties
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(classes = [BluePrintDmaapLibConfiguration::class, TestController::class,
53 BlueprintPropertyConfiguration::class, BluePrintProperties::class])
54 @TestPropertySource(properties = ["server.port=9111",
55 "blueprintsprocessor.dmaapclient.aai.topic=cds_aai",
56 "blueprintsprocessor.dmaapclient.aai.type=HTTPNOAUTH",
57 "blueprintsprocessor.dmaapclient.aai.host=127.0.0.1:9111",
58 "blueprintsprocessor.dmaapclient.multi.topic=cds_multi1,cds_multi2",
59 "blueprintsprocessor.dmaapclient.multi.type=HTTPNOAUTH",
60 "blueprintsprocessor.dmaapclient.multi.host=127.0.0.1:9111"])
61 class TestDmaapEventPublisher {
64 lateinit var dmaapService : BluePrintDmaapLibPropertyService
67 * Tests the event properties being set properly and sent as request.
70 fun testEventProperties() {
71 val strList = mutableListOf<String>()
72 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
75 " \"a\" : \"hello\"\n" +
77 dmaapClient.sendMessage(strList)
78 val msgs = dmaapClient.close(2)
79 assertEquals(msgs!!.size, 1)
80 val topic1 = msgs.get(0)
81 assertEquals(topic1!!.size, 0)
85 * Tests the event properties being set properly and sent as request with
89 fun testEventPropertiesWithSingleMsg() {
90 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
91 val str : String = "{\n" +
92 " \"a\" : \"hello\"\n" +
94 dmaapClient.sendMessage(str)
95 val msgs = dmaapClient.close(2)
96 assertEquals(msgs!!.size, 1)
97 val topic1 = msgs.get(0)
98 assertEquals(topic1!!.size, 0)
102 * Tests the event properties with multiple topics.
105 fun testMultiTopicProperties() {
106 val strList = mutableListOf<String>()
107 val dmaapClient = dmaapService.blueprintDmaapClientService("multi")
110 " \"a\" : \"hello\"\n" +
112 dmaapClient.sendMessage(strList)
113 val msgs = dmaapClient.close(2)
114 assertEquals(msgs!!.size, 2)
115 val topic1 = msgs.get(0)
116 assertEquals(topic1!!.size, 0)
117 val topic2 = msgs.get(1)
118 assertEquals(topic2!!.size, 0)
123 * Tests the event properties with multiple topics with JSON node as input.
126 fun testMultiTopicPropertiesWithJsonInput() {
127 val jsonString = "{\n" +
128 " \"topic\" : \"cds_json1,cds_json2\",\n" +
129 " \"type\" : \"HTTPNOAUTH\",\n" +
130 " \"host\" : \"127.0.0.1:9111\"\n" +
132 val mapper = ObjectMapper()
133 val node = mapper.readTree(jsonString)
134 val strList = mutableListOf<String>()
135 val dmaapClient = dmaapService.blueprintDmaapClientService(node)
138 " \"a\" : \"hello\"\n" +
140 dmaapClient.sendMessage(strList)
141 val msgs = dmaapClient.close(2)
142 assertEquals(msgs!!.size, 2)
143 val topic1 = msgs.get(0)
144 assertEquals(topic1!!.size, 0)
145 val topic2 = msgs.get(1)
146 assertEquals(topic2!!.size, 0)
151 * Tests the event properties with multiple messages.
154 fun testMultiMsgsProperties() {
155 val strList = mutableListOf<String>()
156 val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
159 " \"a\" : \"hello\"\n" +
162 " \"a\" : \"second\"\n" +
164 dmaapClient.sendMessage(strList)
165 val msgs = dmaapClient.close(2)
166 assertEquals(msgs!!.size, 1)
167 val topic1 = msgs.get(0)
168 assertEquals(topic1!!.size, 0)
172 * Tests the DMAAP client properties generated with the complete prefix.
175 fun testDmaapClientProperties() {
176 val properties = dmaapService.dmaapClientProperties(
177 "blueprintsprocessor.dmaapclient.aai")
178 assertNotNull(properties, "failed to create property bean")
179 assertNotNull(properties.host, "failed to get url property" +
184 * Tests the blueprint DMAAP client service with only selector prefix.
187 fun testBlueprintDmaapClientService() {
188 val blueprintDmaapClientService =
189 dmaapService.blueprintDmaapClientService("aai")
190 assertNotNull(blueprintDmaapClientService,
191 "failed to create blueprintDmaapClientService")
197 * Rest controller for testing the client request that is sent.
200 @RequestMapping(path = ["/events"])
201 open class TestController {
204 * Accepts request for a topic and sends a message as response.
206 @PostMapping(path = ["/{topic}"])
207 fun postTopic(@PathVariable(value = "topic") topic : String):
208 ResponseEntity<Any> {
210 " \"message\" : \"The message is published into $topic " +
213 return ResponseEntity(a, HttpStatus.OK)