Merge "File manager changes"
[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
18 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
19
20 import org.junit.runner.RunWith
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration
24 import org.springframework.beans.factory.annotation.Autowired
25 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
26 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
27 import org.springframework.boot.test.context.SpringBootTest
28 import org.springframework.http.HttpMethod
29 import org.springframework.test.context.ContextConfiguration
30 import org.springframework.test.context.TestPropertySource
31 import org.springframework.test.context.junit4.SpringRunner
32 import org.springframework.web.bind.annotation.GetMapping
33 import org.springframework.web.bind.annotation.PatchMapping
34 import org.springframework.web.bind.annotation.RequestMapping
35 import org.springframework.web.bind.annotation.RestController
36 import kotlin.test.Test
37 import kotlin.test.assertEquals
38 import kotlin.test.assertNotNull
39
40 @RunWith(SpringRunner::class)
41 @EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
42 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
43 @ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, BlueprintPropertyConfiguration::class,
44     SampleController::class, BluePrintProperties::class, BluePrintProperties::class])
45 @TestPropertySource(properties =
46 ["blueprintsprocessor.restclient.sample.type=basic-auth",
47     "blueprintsprocessor.restclient.sample.url=http://127.0.0.1:8080",
48     "blueprintsprocessor.restclient.sample.username=sampleuser",
49     "blueprintsprocessor.restclient.sample.password=sampletoken"])
50 class RestClientServiceTest {
51
52     @Autowired
53     lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
54
55     @Test
56     fun testBaseAuth() {
57         val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("sample")
58         val headers = mutableMapOf<String, String>()
59         headers["X-Transaction-Id"] = "1234"
60         val response = restClientService.exchangeResource(HttpMethod.GET.name, "/sample/name", "")
61         assertNotNull(response, "failed to get response")
62     }
63
64     @Test
65     fun testPatch() {
66         val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("sample")
67         val response = restClientService.exchangeResource(HttpMethod.PATCH.name, "/sample/name", "")
68         assertEquals("Patch request successful", response, "failed to get patch response")
69     }
70
71 }
72
73 @RestController
74 @RequestMapping("/sample")
75 open class SampleController {
76     @GetMapping("/name")
77     fun getName(): String = "Sample Controller"
78     @PatchMapping("/name")
79     fun patchName(): String = "Patch request successful"
80 }
81