2 * Copyright © 2019 Huawei.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restful.executor.function
19 import com.fasterxml.jackson.databind.node.ObjectNode
20 import org.junit.Ignore
22 import org.junit.runner.RunWith
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
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.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.DeleteMapping
36 import org.springframework.web.bind.annotation.GetMapping
37 import org.springframework.web.bind.annotation.PatchMapping
38 import org.springframework.web.bind.annotation.PutMapping
39 import org.springframework.web.bind.annotation.RequestMapping
40 import org.springframework.web.bind.annotation.RestController
41 import kotlin.test.assertEquals
42 import kotlin.test.assertNotNull
44 @RunWith(SpringRunner::class)
45 @EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
46 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
47 @ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, NrmTestController::class])
50 "blueprintsprocessor.restclient.nrm.type=basic-auth",
51 "blueprintsprocessor.restclient.nrm.url=http://127.0.0.1:8080",
52 "blueprintsprocessor.restclient.nrm.username=admin",
53 "blueprintsprocessor.restclient.nrm.password=admin"
58 class RestfulServiceClientTest {
61 lateinit var restfulServiceClient: RestfulServiceClient
62 lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
66 val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("nrm")
67 val idStr = restfulServiceClient.generateMOIid()
68 var test_moi_data = JacksonUtils.jsonNode("{}") as ObjectNode
69 test_moi_data.put("className", "TestMangedObjectInstance")
70 var test_attributes_data = JacksonUtils.jsonNode("{}") as ObjectNode
71 test_attributes_data.put("test_attribute_key", "test_attribute_value")
72 test_moi_data.put("data", test_attributes_data)
73 val response = restfulServiceClient.createMOI(restClientService, idStr, test_moi_data)
74 assertNotNull(response, "failed to get createMOI response")
75 assertEquals("Create MOI object successfully", response.get("body").get("data").toString(), "failed to get createMOI response")
79 fun testGetMOIAttributes() {
80 val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("nrm")
81 val idStr = restfulServiceClient.generateMOIid()
82 var test_moi_data = JacksonUtils.jsonNode("{}") as ObjectNode
83 test_moi_data.put("className", "TestMangedObjectInstance")
84 test_moi_data.put("scope", "BASE_ONLY")
85 test_moi_data.put("filter", "TestMangedObjectInstance")
86 test_moi_data.put("fields", "test_attribute_key")
87 val response = restfulServiceClient.getMOIAttributes(restClientService, idStr, test_moi_data)
88 assertNotNull(response, "failed to get getMOIAttributes response")
89 assertEquals("Get MOI object attributes successfully", response.get("body").get("data").toString(), "failed to get getMOIAttributes response")
93 fun testModifyMOIAttributes() {
94 val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("nrm")
95 val idStr = restfulServiceClient.generateMOIid()
96 var test_moi_data = JacksonUtils.jsonNode("{}") as ObjectNode
97 test_moi_data.put("className", "TestMangedObjectInstance")
98 test_moi_data.put("scope", "BASE_ONLY")
99 test_moi_data.put("filter", "TestMangedObjectInstance")
100 var test_attributes_data = JacksonUtils.jsonNode("{}") as ObjectNode
101 test_attributes_data.put("test_attribute_key", "modified_attribute_value")
102 test_moi_data.put("data", test_attributes_data)
103 val response = restfulServiceClient.modifyMOIAttributes(restClientService, idStr, test_moi_data)
104 assertNotNull(response, "failed to get modifyMOIAttributes response")
106 "Modify MOI object attributes successfully",
107 response.get("body").get("data").toString(),
108 "failed to get modifyMOIAttributes response"
113 fun testDeleteMOI() {
114 val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("nrm")
115 val idStr = restfulServiceClient.generateMOIid()
116 var test_moi_data = JacksonUtils.jsonNode("{}") as ObjectNode
117 test_moi_data.put("className", "TestMangedObjectInstance")
118 test_moi_data.put("scope", "BASE_ONLY")
119 test_moi_data.put("filter", "TestMangedObjectInstance")
120 val response = restfulServiceClient.deleteMOI(restClientService, idStr, test_moi_data)
121 assertNotNull(response, "failed to get delete response")
122 assertEquals("Delete MOI object attributes successfully", response.get("body").get("data").toString(), "failed to get delete response")
127 * Sample controller code for testing the above four functions.
130 @RequestMapping("/ProvisioningMnS/v1500")
131 open class NrmTestController {
133 @PutMapping("/TestMangedObjectInstance")
134 fun putMOI(): ResponseEntity<Any> {
135 var a = "{\n" + "\"data\" : \"Create MOI object successfully" + "}"
136 return ResponseEntity(a, HttpStatus.OK)
139 @GetMapping("/TestMangedObjectInstance")
140 fun getMOI(): ResponseEntity<Any> {
141 var a = "{\n" + "\"data\" : \"Get MOI object attributes successfully" + "}"
142 return ResponseEntity(a, HttpStatus.OK)
145 @PatchMapping("/TestMangedObjectInstance")
146 fun patchMOI(): ResponseEntity<Any> {
147 var a = "{\n" + "\"data\" : \"Modify MOI object attributes successfully" + "}"
148 return ResponseEntity(a, HttpStatus.OK)
151 @DeleteMapping("/TestMangedObjectInstance")
152 fun deleteMOI(): ResponseEntity<Any> {
153 var a = "{\n" + "\"data\" : \"Delete MOI object attributes successfully" + "}"
154 return ResponseEntity(a, HttpStatus.OK)