ac8882187c7b68bbfe72818cc602afbd591b21c9
[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 org.junit.Test
24 import org.junit.runner.RunWith
25 import org.onap.ccsdk.apps.blueprintsprocessor.dmaap.DmaapEventPublisher
26 import org.onap.ccsdk.apps.blueprintsprocessor.dmaap.EnvironmentContext
27 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
28 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
29 import org.springframework.boot.test.context.SpringBootTest
30 import org.springframework.http.HttpStatus
31 import org.springframework.http.ResponseEntity
32 import org.springframework.test.context.ContextConfiguration
33 import org.springframework.test.context.TestPropertySource
34 import org.springframework.test.context.junit4.SpringRunner
35 import org.springframework.web.bind.annotation.PathVariable
36 import org.springframework.web.bind.annotation.PostMapping
37 import org.springframework.web.bind.annotation.RequestMapping
38 import org.springframework.web.bind.annotation.RestController
39 import kotlin.test.assertEquals
40 import kotlin.test.assertNotNull
41
42 /**
43  * Unit test cases for DMaap publisher code.
44  */
45 @RunWith(SpringRunner::class)
46 @EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
47 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
48 @ContextConfiguration(classes = [EnvironmentContext::class, TestController::class,
49     DmaapEventPublisher::class])
50 @TestPropertySource(properties = ["server.port=9111","aai.topic=cds_aai",
51     "aai.username=admin","aai.password=admin","aai.host=127.0.0.1:9111",
52     "mul.topic=cds_mul_1,cds_mul_2", "mul.username=admin","mul.password=admin",
53     "mul.host=127.0.0.1:9111"])
54 class TestDmaapEventPublisher {
55
56     /**
57      * Tests the event properties being set properly and sent as request.
58      */
59     @Test
60     fun testEventProperties() {
61         val strList = mutableListOf<String>()
62         val pub = DmaapEventPublisher(compName = "aai")
63         strList.add("{\n" +
64                 "    \"a\" : \"hello\"\n" +
65                 "}")
66         pub.sendMessage("1", strList)
67         pub.close(2)
68         pub.prodProps
69         assertNotNull(pub.prodProps, "The property file updation failed")
70         assertEquals(pub.prodProps.get("topic"), "cds_aai")
71         assertEquals(pub.prodProps.get("username"), "admin")
72         assertEquals(pub.prodProps.get("password"), "admin")
73         assertEquals(pub.prodProps.get("host"), "127.0.0.1:9111")
74     }
75
76     /**
77      * Tests the event properties with multiple topics.
78      */
79     @Test
80     fun testMultiTopicProperties() {
81         val strList = mutableListOf<String>()
82         val pub = DmaapEventPublisher(compName = "mul")
83         strList.add("{\n" +
84                 "    \"a\" : \"hello\"\n" +
85                 "}")
86         pub.sendMessage("1", strList)
87         pub.close(2)
88         var tops = pub.topics
89         assertNotNull(pub.prodProps, "The property file updation failed")
90         assertEquals(tops[0], "cds_mul_1")
91         assertEquals(tops[1], "cds_mul_2")
92         //assertEquals(pub.topics.contains("cds_mul_2`"), true)
93         assertEquals(pub.prodProps.get("username"), "admin")
94         assertEquals(pub.prodProps.get("password"), "admin")
95         assertEquals(pub.prodProps.get("host"), "127.0.0.1:9111")
96     }
97 }
98
99 /**
100  * Rest controller for testing the client request that is sent.
101  */
102 @RestController
103 @RequestMapping(path = ["/events"])
104 open class TestController {
105
106     /**
107      * Accepts request for a topic and sends a message as response.
108      */
109     @PostMapping(path = ["/{topic}"])
110     fun postTopic(@PathVariable(value = "topic") topic : String):
111             ResponseEntity<Any> {
112         var a = "{\n" +
113                 "    \"message\" : \"The message is published into $topic " +
114                 "topic\"\n" +
115                 "}"
116         return ResponseEntity(a, HttpStatus.OK)
117     }
118 }