Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / rest-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / rest / service / RestClientServiceTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Copyright (C) 2019 Nordix Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  */
19
20 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
21
22 import org.junit.runner.RunWith
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration
26 import org.springframework.beans.factory.annotation.Autowired
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.HttpMethod
31 import org.springframework.test.context.ContextConfiguration
32 import org.springframework.test.context.TestPropertySource
33 import org.springframework.test.context.junit4.SpringRunner
34 import org.springframework.web.bind.annotation.GetMapping
35 import org.springframework.web.bind.annotation.PatchMapping
36 import org.springframework.web.bind.annotation.RequestMapping
37 import org.springframework.web.bind.annotation.RestController
38 import kotlin.test.Test
39 import kotlin.test.assertEquals
40 import kotlin.test.assertNotNull
41
42 @RunWith(SpringRunner::class)
43 @EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
44 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
45 @ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, BlueprintPropertyConfiguration::class,
46     SampleController::class, BluePrintProperties::class, BluePrintProperties::class])
47 @TestPropertySource(properties =
48 ["blueprintsprocessor.restclient.sample.type=basic-auth",
49     "blueprintsprocessor.restclient.sample.url=http://127.0.0.1:8080",
50     "blueprintsprocessor.restclient.sample.username=sampleuser",
51     "blueprintsprocessor.restclient.sample.password=sampletoken"])
52 class RestClientServiceTest {
53
54     @Autowired
55     lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
56
57     @Test
58     fun testBaseAuth() {
59         val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("sample")
60         val headers = mutableMapOf<String, String>()
61         headers["X-Transaction-Id"] = "1234"
62         val response = restClientService.exchangeResource(HttpMethod.GET.name, "/sample/name", "")
63         assertNotNull(response, "failed to get response")
64     }
65
66     @Test
67     fun testPatch() {
68         val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("sample")
69         val response = restClientService.exchangeResource(HttpMethod.PATCH.name, "/sample/name", "")
70         assertEquals("Patch request successful", response, "failed to get patch response")
71     }
72
73 }
74
75 @RestController
76 @RequestMapping("/sample")
77 open class SampleController {
78     @GetMapping("/name")
79     fun getName(): String = "Sample Controller"
80     @PatchMapping("/name")
81     fun patchName(): String = "Patch request successful"
82 }
83