Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / dmaap-lib / src / test / kotlin / org / ccsdk / cds / blueprintprocessor / dmaap / TestDmaapEventPublisher.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP - CDS
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.ccsdk.apps.blueprintprocessor.dmaap
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.junit.Test
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
45
46 /**
47  * Unit test cases for DMaap publisher code.
48  */
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 {
62
63     @Autowired
64     lateinit var dmaapService : BluePrintDmaapLibPropertyService
65
66     /**
67      * Tests the event properties being set properly and sent as request.
68      */
69     @Test
70     fun testEventProperties() {
71         val strList = mutableListOf<String>()
72         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
73
74         strList.add("{\n" +
75                 "    \"a\" : \"hello\"\n" +
76                 "}")
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)
82     }
83
84     /**
85      * Tests the event properties being set properly and sent as request with
86      * single message.
87      */
88     @Test
89     fun testEventPropertiesWithSingleMsg() {
90         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
91         val str : String = "{\n" +
92                 "    \"a\" : \"hello\"\n" +
93                 "}"
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)
99     }
100
101     /**
102      * Tests the event properties with multiple topics.
103      */
104     @Test
105     fun testMultiTopicProperties() {
106         val strList = mutableListOf<String>()
107         val dmaapClient = dmaapService.blueprintDmaapClientService("multi")
108
109         strList.add("{\n" +
110                 "    \"a\" : \"hello\"\n" +
111                 "}")
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)
119     }
120
121
122     /**
123      * Tests the event properties with multiple topics with JSON node as input.
124      */
125     @Test
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" +
131                 "}"
132         val mapper = ObjectMapper()
133         val node = mapper.readTree(jsonString)
134         val strList = mutableListOf<String>()
135         val dmaapClient = dmaapService.blueprintDmaapClientService(node)
136
137         strList.add("{\n" +
138                 "    \"a\" : \"hello\"\n" +
139                 "}")
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)
147     }
148
149
150     /**
151      * Tests the event properties with multiple messages.
152      */
153     @Test
154     fun testMultiMsgsProperties() {
155         val strList = mutableListOf<String>()
156         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
157
158         strList.add("{\n" +
159                 "    \"a\" : \"hello\"\n" +
160                 "}")
161         strList.add("{\n" +
162                 "    \"a\" : \"second\"\n" +
163                 "}")
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)
169     }
170
171     /**
172      * Tests the DMAAP client properties generated with the complete prefix.
173      */
174     @Test
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" +
180                 " in property bean")
181     }
182
183     /**
184      * Tests the blueprint DMAAP client service with only selector prefix.
185      */
186     @Test
187     fun testBlueprintDmaapClientService() {
188         val blueprintDmaapClientService =
189             dmaapService.blueprintDmaapClientService("aai")
190         assertNotNull(blueprintDmaapClientService,
191             "failed to create blueprintDmaapClientService")
192     }
193
194 }
195
196 /**
197  * Rest controller for testing the client request that is sent.
198  */
199 @RestController
200 @RequestMapping(path = ["/events"])
201 open class TestController {
202
203     /**
204      * Accepts request for a topic and sends a message as response.
205      */
206     @PostMapping(path = ["/{topic}"])
207     fun postTopic(@PathVariable(value = "topic") topic : String):
208             ResponseEntity<Any> {
209         var a = "{\n" +
210                 "    \"message\" : \"The message is published into $topic " +
211                 "topic\"\n" +
212                 "}"
213         return ResponseEntity(a, HttpStatus.OK)
214     }
215 }