Bootstrap Rest and GRPC API
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / BluePrintManagementGRPCHandlerTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2019 IBM.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
20
21 import com.google.protobuf.ByteString
22 import io.grpc.testing.GrpcServerRule
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Rule
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GRPCLibConstants
28 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TokenAuthGrpcClientProperties
29 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.TokenAuthGrpcClientService
30 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers
31 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
33 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
34 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
35 import org.onap.ccsdk.cds.controllerblueprints.management.api.*
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
38 import org.springframework.context.annotation.ComponentScan
39 import org.springframework.test.annotation.DirtiesContext
40 import org.springframework.test.context.TestPropertySource
41 import org.springframework.test.context.junit4.SpringRunner
42 import kotlin.test.*
43
44 @RunWith(SpringRunner::class)
45 @EnableAutoConfiguration
46 @DirtiesContext
47 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor",
48     "org.onap.ccsdk.cds.controllerblueprints"])
49 @TestPropertySource(locations = ["classpath:application-test.properties"])
50 class BluePrintManagementGRPCHandlerTest {
51
52     @get:Rule
53     val grpcServerRule = GrpcServerRule().directExecutor()
54
55     @Autowired
56     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
57
58     @BeforeTest
59     fun init() {
60         // Create a server, add service, start, and register for automatic graceful shutdown.
61         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
62         deleteDir("target", "blueprints")
63     }
64
65     @AfterTest
66     fun cleanDir() {
67         deleteDir("target", "blueprints")
68     }
69
70     @Test
71     fun testBootstrap() {
72         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
73         val id = "123_Bootstrap"
74         val req = createBootstrapInputRequest(id)
75         val bootstrapOutput = blockingStub.bootstrapBlueprint(req)
76         assertEquals(200, bootstrapOutput.status.code)
77         assertTrue(bootstrapOutput.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
78                 "failed to get success status")
79         assertEquals(id, bootstrapOutput.commonHeader.requestId)
80     }
81
82     @Test
83     fun `test upload and download blueprint`() {
84         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
85         val id = "123_upload"
86         val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
87         val output = blockingStub.uploadBlueprint(req)
88
89         assertEquals(200, output.status.code)
90         assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
91                 "failed to get success status")
92         assertEquals(id, output.commonHeader.requestId)
93
94         val downloadId = "123_download"
95         val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())
96
97         val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
98         assertEquals(200, downloadOutput.status.code)
99         assertTrue(downloadOutput.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
100                 "failed to get success status")
101         assertNotNull(downloadOutput.fileChunk?.chunk, "failed to get cba file chunks")
102         assertEquals(downloadId, downloadOutput.commonHeader.requestId)
103     }
104
105     @Test
106     fun `test delete blueprint`() {
107         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
108         val id = "123_delete"
109         val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())
110
111         var output = blockingStub.uploadBlueprint(req)
112         assertEquals(200, output.status.code)
113         assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
114                 "failed to get success status")
115         assertEquals(id, output.commonHeader.requestId)
116
117         val removeReq = createRemoveInputRequest(id)
118         output = blockingStub.removeBlueprint(removeReq)
119         assertEquals(200, output.status.code)
120     }
121
122     /** This is Integration test sample, Do not enable this test case in server build, this is for local desktop testing*/
123     private fun integrationTestGrpcManagement() {
124         runBlocking {
125             val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
126                 host = "127.0.0.1"
127                 port = 9111
128                 type = GRPCLibConstants.TYPE_TOKEN_AUTH
129                 token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
130             }
131             val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
132             val channel = basicAuthGrpcClientService.channel()
133
134             val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(channel)
135
136             val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())
137
138             val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
139             assertNotNull(bluePrintManagementOutput, "failed to get response")
140         }
141     }
142
143     private fun createBootstrapInputRequest(id: String): BluePrintBootstrapInput {
144         val commonHeader = CommonHeader
145                 .newBuilder()
146                 .setTimestamp("2012-04-23T18:25:43.511Z")
147                 .setOriginatorId("System")
148                 .setRequestId(id)
149                 .setSubRequestId("1234-56").build()
150
151         return BluePrintBootstrapInput.newBuilder()
152                 .setCommonHeader(commonHeader)
153                 .setLoadModelType(false)
154                 .setLoadResourceDictionary(false)
155                 .setLoadCBA(false)
156                 .build()
157     }
158
159     private fun createUploadInputRequest(id: String, action: String): BluePrintUploadInput {
160         val file = normalizedFile("./src/test/resources/test-cba.zip")
161         assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
162
163         val commonHeader = CommonHeader
164                 .newBuilder()
165                 .setTimestamp("2012-04-23T18:25:43.511Z")
166                 .setOriginatorId("System")
167                 .setRequestId(id)
168                 .setSubRequestId("1234-56").build()
169
170         val actionIdentifier = ActionIdentifiers.newBuilder()
171                 .setActionName(action)
172                 .setBlueprintName("sample")
173                 .setBlueprintVersion("1.0.0")
174                 .build()
175
176         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
177                 .build()
178
179         return BluePrintUploadInput.newBuilder()
180                 .setCommonHeader(commonHeader)
181                 .setActionIdentifiers(actionIdentifier)
182                 .setFileChunk(fileChunk)
183                 .build()
184     }
185
186     private fun createDownloadInputRequest(id: String, action: String): BluePrintDownloadInput {
187         val commonHeader = CommonHeader
188                 .newBuilder()
189                 .setTimestamp("2012-04-23T18:25:43.511Z")
190                 .setOriginatorId("System")
191                 .setRequestId(id)
192                 .setSubRequestId("1234-56").build()
193
194         return BluePrintDownloadInput.newBuilder()
195                 .setCommonHeader(commonHeader)
196                 .setActionIdentifiers(ActionIdentifiers.newBuilder()
197                         .setBlueprintName("baseconfiguration")
198                         .setBlueprintVersion("1.0.0")
199                         .setActionName(action).build())
200                 .build()
201     }
202
203     private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
204         val commonHeader = CommonHeader
205                 .newBuilder()
206                 .setTimestamp("2012-04-23T18:25:43.511Z")
207                 .setOriginatorId("System")
208                 .setRequestId(id)
209                 .setSubRequestId("1234-56").build()
210
211         return BluePrintRemoveInput.newBuilder()
212                 .setCommonHeader(commonHeader)
213                 .setActionIdentifiers(ActionIdentifiers.newBuilder()
214                         .setBlueprintName("sample")
215                         .setBlueprintVersion("1.0.0")
216                         .setActionName(RemoveAction.DEFAULT.toString()).build())
217                 .build()
218     }
219 }