Metadata for name, version, tags and type
[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.compress
34 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
35 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
36 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintBootstrapInput
37 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintDownloadInput
38 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc
39 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintRemoveInput
40 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput
41 import org.onap.ccsdk.cds.controllerblueprints.management.api.DownloadAction
42 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk
43 import org.onap.ccsdk.cds.controllerblueprints.management.api.RemoveAction
44 import org.onap.ccsdk.cds.controllerblueprints.management.api.UploadAction
45 import org.springframework.beans.factory.annotation.Autowired
46 import org.springframework.test.context.ContextConfiguration
47 import org.springframework.test.context.TestPropertySource
48 import org.springframework.test.context.junit4.SpringRunner
49 import kotlin.test.AfterTest
50 import kotlin.test.BeforeTest
51 import kotlin.test.assertEquals
52 import kotlin.test.assertNotNull
53 import kotlin.test.assertTrue
54
55 @RunWith(SpringRunner::class)
56 @ContextConfiguration(
57     classes = [DesignerApiTestConfiguration::class]
58 )
59 @TestPropertySource(locations = ["classpath:application-test.properties"])
60 class BluePrintManagementGRPCHandlerTest {
61
62     @get:Rule
63     val grpcServerRule = GrpcServerRule().directExecutor()
64
65     @Autowired
66     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
67
68     @BeforeTest
69     fun init() {
70
71         deleteDir("target", "blueprints")
72
73         // Create sample CBA zip
74         normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
75             .compress(normalizedFile("./target/blueprints/generated-cba.zip"))
76
77         // Create a server, add service, start, and register for automatic graceful shutdown.
78         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
79     }
80
81     @AfterTest
82     fun cleanDir() {
83         deleteDir("target", "blueprints")
84     }
85
86     @Test
87     fun testBootstrap() {
88         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
89         val id = "123_Bootstrap"
90         val req = createBootstrapInputRequest(id)
91         val bootstrapOutput = blockingStub.bootstrapBlueprint(req)
92         assertEquals(200, bootstrapOutput.status.code)
93         assertTrue(
94             bootstrapOutput.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
95             "failed to get success status"
96         )
97         assertEquals(id, bootstrapOutput.commonHeader.requestId)
98     }
99
100     @Test
101     fun `test upload and download blueprint`() {
102         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
103         val id = "123_upload"
104         val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
105         val output = blockingStub.uploadBlueprint(req)
106
107         assertEquals(200, output.status.code)
108         assertTrue(
109             output.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
110             "failed to get success status"
111         )
112         assertEquals(id, output.commonHeader.requestId)
113
114         val downloadId = "123_download"
115         val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())
116
117         val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
118         assertEquals(200, downloadOutput.status.code)
119         assertTrue(
120             downloadOutput.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
121             "failed to get success status"
122         )
123         assertNotNull(downloadOutput.fileChunk?.chunk, "failed to get cba file chunks")
124         assertEquals(downloadId, downloadOutput.commonHeader.requestId)
125     }
126
127     @Test
128     fun `test delete blueprint`() {
129         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
130         val id = "123_delete"
131         val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())
132
133         var output = blockingStub.uploadBlueprint(req)
134         assertEquals(200, output.status.code)
135         assertTrue(
136             output.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
137             "failed to get success status"
138         )
139         assertEquals(id, output.commonHeader.requestId)
140
141         val removeReq = createRemoveInputRequest(id)
142         output = blockingStub.removeBlueprint(removeReq)
143         assertEquals(200, output.status.code)
144     }
145
146     /** This is Integration test sample, Do not enable this test case in server build, this is for local desktop testing*/
147     private fun integrationTestGrpcManagement() {
148         runBlocking {
149             val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
150                 host = "127.0.0.1"
151                 port = 9111
152                 type = GRPCLibConstants.TYPE_TOKEN_AUTH
153                 token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
154             }
155             val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
156             val channel = basicAuthGrpcClientService.channel()
157
158             val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(channel)
159
160             val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())
161
162             val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
163             assertNotNull(bluePrintManagementOutput, "failed to get response")
164         }
165     }
166
167     private fun createBootstrapInputRequest(id: String): BluePrintBootstrapInput {
168         val commonHeader = CommonHeader
169             .newBuilder()
170             .setTimestamp("2012-04-23T18:25:43.511Z")
171             .setOriginatorId("System")
172             .setRequestId(id)
173             .setSubRequestId("1234-56").build()
174
175         return BluePrintBootstrapInput.newBuilder()
176             .setCommonHeader(commonHeader)
177             .setLoadModelType(false)
178             .setLoadResourceDictionary(false)
179             .setLoadCBA(false)
180             .build()
181     }
182
183     private fun createUploadInputRequest(id: String, action: String): BluePrintUploadInput {
184         val file = normalizedFile("./target/blueprints/generated-cba.zip")
185         assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
186
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         val actionIdentifier = ActionIdentifiers.newBuilder()
195             .setActionName(action)
196             .setBlueprintName("sample")
197             .setBlueprintVersion("1.0.0")
198             .build()
199
200         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
201             .build()
202
203         return BluePrintUploadInput.newBuilder()
204             .setCommonHeader(commonHeader)
205             .setActionIdentifiers(actionIdentifier)
206             .setFileChunk(fileChunk)
207             .build()
208     }
209
210     private fun createDownloadInputRequest(id: String, action: String): BluePrintDownloadInput {
211         val commonHeader = CommonHeader
212             .newBuilder()
213             .setTimestamp("2012-04-23T18:25:43.511Z")
214             .setOriginatorId("System")
215             .setRequestId(id)
216             .setSubRequestId("1234-56").build()
217
218         return BluePrintDownloadInput.newBuilder()
219             .setCommonHeader(commonHeader)
220             .setActionIdentifiers(
221                 ActionIdentifiers.newBuilder()
222                     .setBlueprintName("baseconfiguration")
223                     .setBlueprintVersion("1.0.0")
224                     .setActionName(action).build()
225             )
226             .build()
227     }
228
229     private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
230         val commonHeader = CommonHeader
231             .newBuilder()
232             .setTimestamp("2012-04-23T18:25:43.511Z")
233             .setOriginatorId("System")
234             .setRequestId(id)
235             .setSubRequestId("1234-56").build()
236
237         return BluePrintRemoveInput.newBuilder()
238             .setCommonHeader(commonHeader)
239             .setActionIdentifiers(
240                 ActionIdentifiers.newBuilder()
241                     .setBlueprintName("sample")
242                     .setBlueprintVersion("1.0.0")
243                     .setActionName(RemoveAction.DEFAULT.toString()).build()
244             )
245             .build()
246     }
247 }