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