3fb7f6802354e0d1a0236e6c7ec8fb727af295da
[ccsdk/cds.git] /
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 = [BluePrintDmaapLibConfiguration::class, TestController::class,
54         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
55 )
56 @TestPropertySource(
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"]
64 )
65 class TestDmaapEventPublisher {
66
67     @Autowired
68     lateinit var dmaapService: BluePrintDmaapLibPropertyService
69
70     /**
71      * Tests the event properties being set properly and sent as request.
72      */
73     @Test
74     fun testEventProperties() {
75         val strList = mutableListOf<String>()
76         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
77
78         strList.add(
79             "{\n" +
80                     "    \"a\" : \"hello\"\n" +
81                     "}"
82         )
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)
88     }
89
90     /**
91      * Tests the event properties being set properly and sent as request with
92      * single message.
93      */
94     @Test
95     fun testEventPropertiesWithSingleMsg() {
96         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
97         val str: String = "{\n" +
98                 "    \"a\" : \"hello\"\n" +
99                 "}"
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)
105     }
106
107     /**
108      * Tests the event properties with multiple topics.
109      */
110     @Test
111     fun testMultiTopicProperties() {
112         val strList = mutableListOf<String>()
113         val dmaapClient = dmaapService.blueprintDmaapClientService("multi")
114
115         strList.add(
116             "{\n" +
117                     "    \"a\" : \"hello\"\n" +
118                     "}"
119         )
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)
127     }
128
129     /**
130      * Tests the event properties with multiple topics with JSON node as input.
131      */
132     @Test
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" +
138                 "}"
139         val mapper = ObjectMapper()
140         val node = mapper.readTree(jsonString)
141         val strList = mutableListOf<String>()
142         val dmaapClient = dmaapService.blueprintDmaapClientService(node)
143
144         strList.add(
145             "{\n" +
146                     "    \"a\" : \"hello\"\n" +
147                     "}"
148         )
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)
156     }
157
158     /**
159      * Tests the event properties with multiple messages.
160      */
161     @Test
162     fun testMultiMsgsProperties() {
163         val strList = mutableListOf<String>()
164         val dmaapClient = dmaapService.blueprintDmaapClientService("aai")
165
166         strList.add(
167             "{\n" +
168                     "    \"a\" : \"hello\"\n" +
169                     "}"
170         )
171         strList.add(
172             "{\n" +
173                     "    \"a\" : \"second\"\n" +
174                     "}"
175         )
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)
181     }
182
183     /**
184      * Tests the DMAAP client properties generated with the complete prefix.
185      */
186     @Test
187     fun testDmaapClientProperties() {
188         val properties = dmaapService.dmaapClientProperties(
189             "blueprintsprocessor.dmaapclient.aai"
190         )
191         assertNotNull(properties, "failed to create property bean")
192         assertNotNull(
193             properties.host, "failed to get url property" +
194                     " in property bean"
195         )
196     }
197
198     /**
199      * Tests the blueprint DMAAP client service with only selector prefix.
200      */
201     @Test
202     fun testBlueprintDmaapClientService() {
203         val blueprintDmaapClientService =
204             dmaapService.blueprintDmaapClientService("aai")
205         assertNotNull(
206             blueprintDmaapClientService,
207             "failed to create blueprintDmaapClientService"
208         )
209     }
210 }
211
212 /**
213  * Rest controller for testing the client request that is sent.
214  */
215 @RestController
216 @RequestMapping(path = ["/events"])
217 open class TestController {
218
219     /**
220      * Accepts request for a topic and sends a message as response.
221      */
222     @PostMapping(path = ["/{topic}"])
223     fun postTopic(@PathVariable(value = "topic") topic: String):
224             ResponseEntity<Any> {
225         var a = "{\n" +
226                 "    \"message\" : \"The message is published into $topic " +
227                 "topic\"\n" +
228                 "}"
229         return ResponseEntity(a, HttpStatus.OK)
230     }
231 }