Revert "Renaming Files having BluePrint to have Blueprint"
[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.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
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(
53     classes = [
54         BluePrintDmaapLibConfiguration::class, TestController::class,
55         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class
56     ]
57 )
58 @TestPropertySource(
59     properties = [
60         "server.port=9111",
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"
67     ]
68 )
69 class TestDmaapEventPublisher {
70
71     @Autowired
72     lateinit var dmaapService: BluePrintDmaapLibPropertyService
73
74     /**
75      * Tests the event properties being set properly and sent as request.
76      */
77     @Test
78     fun testEventProperties() {
79         val strList = mutableListOf<String>()
80         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
81
82         strList.add(
83             "{\n" +
84                 "    \"a\" : \"hello\"\n" +
85                 "}"
86         )
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)
92     }
93
94     /**
95      * Tests the event properties being set properly and sent as request with
96      * single message.
97      */
98     @Test
99     fun testEventPropertiesWithSingleMsg() {
100         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
101         val str: String = "{\n" +
102             "    \"a\" : \"hello\"\n" +
103             "}"
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)
109     }
110
111     /**
112      * Tests the event properties with multiple topics.
113      */
114     @Test
115     fun testMultiTopicProperties() {
116         val strList = mutableListOf<String>()
117         val dmaapClient = dmaapService.blueprintDmaapClientService("multi")
118
119         strList.add(
120             "{\n" +
121                 "    \"a\" : \"hello\"\n" +
122                 "}"
123         )
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)
131     }
132
133     /**
134      * Tests the event properties with multiple topics with JSON node as input.
135      */
136     @Test
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" +
142             "}"
143         val mapper = ObjectMapper()
144         val node = mapper.readTree(jsonString)
145         val strList = mutableListOf<String>()
146         val dmaapClient = dmaapService.blueprintDmaapClientService(node)
147
148         strList.add(
149             "{\n" +
150                 "    \"a\" : \"hello\"\n" +
151                 "}"
152         )
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)
160     }
161
162     /**
163      * Tests the event properties with multiple messages.
164      */
165     @Test
166     fun testMultiMsgsProperties() {
167         val strList = mutableListOf<String>()
168         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
169
170         strList.add(
171             "{\n" +
172                 "    \"a\" : \"hello\"\n" +
173                 "}"
174         )
175         strList.add(
176             "{\n" +
177                 "    \"a\" : \"second\"\n" +
178                 "}"
179         )
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)
185     }
186
187     /**
188      * Tests the DMAAP client properties generated with the complete prefix.
189      */
190     @Test
191     fun testDmaapClientProperties() {
192         val properties = dmaapService.dmaapClientProperties(
193             "blueprintsprocessor.dmaapclient.aai"
194         )
195         assertNotNull(properties, "failed to create property bean")
196         assertNotNull(
197             properties.host,
198             "failed to get url property" +
199                 " in property bean"
200         )
201     }
202
203     /**
204      * Tests the blueprint DMAAP client service with only selector prefix.
205      */
206     @Test
207     fun testBlueprintDmaapClientService() {
208         val blueprintDmaapClientService =
209             dmaapService.blueprintDmaapClientService("aai")
210         assertNotNull(
211             blueprintDmaapClientService,
212             "failed to create blueprintDmaapClientService"
213         )
214     }
215 }
216
217 /**
218  * Rest controller for testing the client request that is sent.
219  */
220 @RestController
221 @RequestMapping(path = ["/events"])
222 open class TestController {
223
224     /**
225      * Accepts request for a topic and sends a message as response.
226      */
227     @PostMapping(path = ["/{topic}"])
228     fun postTopic(@PathVariable(value = "topic") topic: String):
229         ResponseEntity<Any> {
230             var a = "{\n" +
231                 "    \"message\" : \"The message is published into $topic " +
232                 "topic\"\n" +
233                 "}"
234             return ResponseEntity(a, HttpStatus.OK)
235         }
236 }