2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
4 * Modifications Copyright © 2019 IBM.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
21 import com.google.protobuf.ByteString
22 import io.grpc.testing.GrpcServerRule
23 import kotlinx.coroutines.runBlocking
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
56 @RunWith(SpringRunner::class)
57 @ContextConfiguration(
58 classes = [DesignerApiTestConfiguration::class, ErrorCatalogTestConfiguration::class]
60 @TestPropertySource(locations = ["classpath:application-test.properties"])
61 class BluePrintManagementGRPCHandlerTest {
64 val grpcServerRule = GrpcServerRule().directExecutor()
67 lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
72 deleteDir("target", "blueprints")
74 // Create sample CBA zip
75 normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
76 .compress(normalizedFile("./target/blueprints/generated-cba.zip"))
78 // Create a server, add service, start, and register for automatic graceful shutdown.
79 grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
84 deleteDir("target", "blueprints")
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)
95 bootstrapOutput.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
96 "failed to get success status"
98 assertEquals(EventType.EVENT_COMPONENT_EXECUTED, bootstrapOutput.status.eventType)
99 assertEquals(id, bootstrapOutput.commonHeader.requestId)
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)
109 assertEquals(200, output.status.code)
111 output.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
112 "failed to get success status"
114 assertEquals(EventType.EVENT_COMPONENT_EXECUTED, output.status.eventType)
115 assertEquals(id, output.commonHeader.requestId)
117 val downloadId = "123_download"
118 val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())
120 val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
121 assertEquals(200, downloadOutput.status.code)
123 downloadOutput.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
124 "failed to get success status"
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)
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())
137 var output = blockingStub.uploadBlueprint(req)
138 assertEquals(200, output.status.code)
140 output.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
141 "failed to get success status"
143 assertEquals(id, output.commonHeader.requestId)
144 assertEquals(EventType.EVENT_COMPONENT_EXECUTED, output.status.eventType)
146 val removeReq = createRemoveInputRequest(id)
147 output = blockingStub.removeBlueprint(removeReq)
148 assertEquals(200, output.status.code)
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() {
154 val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
157 type = GRPCLibConstants.TYPE_TOKEN_AUTH
158 token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
160 val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
161 val channel = basicAuthGrpcClientService.channel()
163 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(channel)
165 val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())
167 val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
168 assertNotNull(bluePrintManagementOutput, "failed to get response")
172 private fun createBootstrapInputRequest(id: String): BluePrintBootstrapInput {
173 val commonHeader = CommonHeader
175 .setTimestamp("2012-04-23T18:25:43.511Z")
176 .setOriginatorId("System")
178 .setSubRequestId("1234-56").build()
180 return BluePrintBootstrapInput.newBuilder()
181 .setCommonHeader(commonHeader)
182 .setLoadModelType(false)
183 .setLoadResourceDictionary(false)
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}")
192 val commonHeader = CommonHeader
194 .setTimestamp("2012-04-23T18:25:43.511Z")
195 .setOriginatorId("System")
197 .setSubRequestId("1234-56").build()
199 val actionIdentifier = ActionIdentifiers.newBuilder()
200 .setActionName(action)
201 .setBlueprintName("sample")
202 .setBlueprintVersion("1.0.0")
205 val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
208 return BluePrintUploadInput.newBuilder()
209 .setCommonHeader(commonHeader)
210 .setActionIdentifiers(actionIdentifier)
211 .setFileChunk(fileChunk)
215 private fun createDownloadInputRequest(id: String, action: String): BluePrintDownloadInput {
216 val commonHeader = CommonHeader
218 .setTimestamp("2012-04-23T18:25:43.511Z")
219 .setOriginatorId("System")
221 .setSubRequestId("1234-56").build()
223 return BluePrintDownloadInput.newBuilder()
224 .setCommonHeader(commonHeader)
225 .setActionIdentifiers(
226 ActionIdentifiers.newBuilder()
227 .setBlueprintName("baseconfiguration")
228 .setBlueprintVersion("1.0.0")
229 .setActionName(action).build()
234 private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
235 val commonHeader = CommonHeader
237 .setTimestamp("2012-04-23T18:25:43.511Z")
238 .setOriginatorId("System")
240 .setSubRequestId("1234-56").build()
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()