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
 
  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
 
  43  * Unit test cases for DMaap publisher code.
 
  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 {
 
  57      * Tests the event properties being set properly and sent as request.
 
  60     fun testEventProperties() {
 
  61         val strList = mutableListOf<String>()
 
  62         val pub = DmaapEventPublisher(compName = "aai")
 
  64                 "    \"a\" : \"hello\"\n" +
 
  66         pub.sendMessage("1", strList)
 
  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")
 
  77      * Tests the event properties with multiple topics.
 
  80     fun testMultiTopicProperties() {
 
  81         val strList = mutableListOf<String>()
 
  82         val pub = DmaapEventPublisher(compName = "mul")
 
  84                 "    \"a\" : \"hello\"\n" +
 
  86         pub.sendMessage("1", strList)
 
  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")
 
 100  * Rest controller for testing the client request that is sent.
 
 103 @RequestMapping(path = ["/events"])
 
 104 open class TestController {
 
 107      * Accepts request for a topic and sends a message as response.
 
 109     @PostMapping(path = ["/{topic}"])
 
 110     fun postTopic(@PathVariable(value = "topic") topic : String):
 
 111             ResponseEntity<Any> {
 
 113                 "    \"message\" : \"The message is published into $topic " +
 
 116         return ResponseEntity(a, HttpStatus.OK)