e7f04a5d4602481e20370a19c0dffafe1bb2ea2f
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 Huawei.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restful.executor.nrmfunction
18
19 import com.fasterxml.jackson.databind.node.ObjectNode
20 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
21 import org.junit.Test
22 import org.junit.Ignore
23 import org.springframework.beans.factory.annotation.Autowired
24 import org.junit.runner.RunWith
25 import org.springframework.test.context.junit4.SpringRunner
26 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
27 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
28 import org.springframework.boot.test.context.SpringBootTest
29 import org.springframework.test.context.ContextConfiguration
30 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration
31 import org.springframework.test.context.TestPropertySource
32 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
33 import kotlin.test.assertNotNull
34 import kotlin.test.assertEquals
35 import org.springframework.web.bind.annotation.RestController
36 import org.springframework.web.bind.annotation.RequestMapping
37 import org.springframework.web.bind.annotation.PutMapping
38 import org.springframework.web.bind.annotation.GetMapping
39 import org.springframework.web.bind.annotation.PatchMapping
40 import org.springframework.web.bind.annotation.DeleteMapping
41 import org.springframework.http.ResponseEntity
42 import org.springframework.http.HttpStatus
43
44 @RunWith(SpringRunner::class)
45 @EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
46 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
47 @ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, NrmTestController::class])
48 @TestPropertySource(properties = [
49     "blueprintsprocessor.restclient.nrm.type=basic-auth",
50     "blueprintsprocessor.restclient.nrm.url=http://127.0.0.1:8080",
51     "blueprintsprocessor.restclient.nrm.username=admin",
52     "blueprintsprocessor.restclient.nrm.password=admin"
53 ])
54
55 @Ignore
56 class RestfulNRMServiceClientTest {
57
58     @Autowired
59     lateinit var restfulNRMServiceClient: RestfulNRMServiceClient
60     lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
61
62     @Test
63     fun testCreateMOI() {
64         val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("nrm")
65         val idStr = restfulNRMServiceClient.generateMOIid()
66         var test_moi_data = JacksonUtils.jsonNode("{}") as ObjectNode
67         test_moi_data.put("className", "TestMangedObjectInstance")
68         var test_attributes_data = JacksonUtils.jsonNode("{}") as ObjectNode
69         test_attributes_data.put("test_attribute_key", "test_attribute_value")
70         test_moi_data.put("data", test_attributes_data)
71         val response = restfulNRMServiceClient.createMOI(restClientService, idStr, test_moi_data)
72         assertNotNull(response, "failed to get createMOI response")
73         assertEquals("Create MOI object successfully", response.get("body").get("data").toString(), "failed to get createMOI response")
74     }
75
76     @Test
77     fun testGetMOIAttributes() {
78         val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("nrm")
79         val idStr = restfulNRMServiceClient.generateMOIid()
80         var test_moi_data = JacksonUtils.jsonNode("{}") as ObjectNode
81         test_moi_data.put("className", "TestMangedObjectInstance")
82         test_moi_data.put("scope", "BASE_ONLY")
83         test_moi_data.put("filter", "TestMangedObjectInstance")
84         test_moi_data.put("fields", "test_attribute_key")
85         val response = restfulNRMServiceClient.getMOIAttributes(restClientService, idStr, test_moi_data)
86         assertNotNull(response, "failed to get getMOIAttributes response")
87         assertEquals("Get MOI object attributes successfully", response.get("body").get("data").toString(), "failed to get getMOIAttributes response")
88     }
89
90     @Test
91     fun testModifyMOIAttributes() {
92         val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("nrm")
93         val idStr = restfulNRMServiceClient.generateMOIid()
94         var test_moi_data = JacksonUtils.jsonNode("{}") as ObjectNode
95         test_moi_data.put("className", "TestMangedObjectInstance")
96         test_moi_data.put("scope", "BASE_ONLY")
97         test_moi_data.put("filter", "TestMangedObjectInstance")
98         var test_attributes_data = JacksonUtils.jsonNode("{}") as ObjectNode
99         test_attributes_data.put("test_attribute_key", "modified_attribute_value")
100         test_moi_data.put("data", test_attributes_data)
101         val response = restfulNRMServiceClient.modifyMOIAttributes(restClientService, idStr, test_moi_data)
102         assertNotNull(response, "failed to get modifyMOIAttributes response")
103         assertEquals("Modify MOI object attributes successfully", response.get("body").get("data").toString(), "failed to get modifyMOIAttributes response")
104     }
105
106     @Test
107     fun testDeleteMOI() {
108         val restClientService = bluePrintRestLibPropertyService.blueprintWebClientService("nrm")
109         val idStr = restfulNRMServiceClient.generateMOIid()
110         var test_moi_data = JacksonUtils.jsonNode("{}") as ObjectNode
111         test_moi_data.put("className", "TestMangedObjectInstance")
112         test_moi_data.put("scope", "BASE_ONLY")
113         test_moi_data.put("filter", "TestMangedObjectInstance")
114         val response = restfulNRMServiceClient.deleteMOI(restClientService, idStr, test_moi_data)
115         assertNotNull(response, "failed to get delete response")
116         assertEquals("Delete MOI object attributes successfully", response.get("body").get("data").toString(), "failed to get delete response")
117     }
118 }
119
120 /**
121  * Sample controller code for testing the above four functions.
122  */
123 @RestController
124 @RequestMapping("/ProvisioningMnS/v1500")
125 open class NrmTestController {
126
127     @PutMapping("/TestMangedObjectInstance")
128     fun putMOI(): ResponseEntity<Any> {
129         var a = "{\n" + "\"data\" : \"Create MOI object successfully" + "}"
130         return ResponseEntity(a, HttpStatus.OK)
131     }
132
133     @GetMapping("/TestMangedObjectInstance")
134     fun getMOI(): ResponseEntity<Any> {
135         var a = "{\n" + "\"data\" : \"Get MOI object attributes successfully" + "}"
136         return ResponseEntity(a, HttpStatus.OK)
137     }
138
139     @PatchMapping("/TestMangedObjectInstance")
140     fun patchMOI(): ResponseEntity<Any> {
141         var a = "{\n" + "\"data\" : \"Modify MOI object attributes successfully" + "}"
142         return ResponseEntity(a, HttpStatus.OK)
143     }
144
145     @DeleteMapping("/TestMangedObjectInstance")
146     fun deleteMOI(): ResponseEntity<Any> {
147         var a = "{\n" + "\"data\" : \"Delete MOI object attributes successfully" + "}"
148         return ResponseEntity(a, HttpStatus.OK)
149     }
150 }