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.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
55 @RunWith(SpringRunner::class)
56 @ContextConfiguration(
57 classes = [DesignerApiTestConfiguration::class]
59 @TestPropertySource(locations = ["classpath:application-test.properties"])
60 class BluePrintManagementGRPCHandlerTest {
63 val grpcServerRule = GrpcServerRule().directExecutor()
66 lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
71 deleteDir("target", "blueprints")
73 // Create sample CBA zip
74 normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
75 .compress(normalizedFile("./target/blueprints/generated-cba.zip"))
77 // Create a server, add service, start, and register for automatic graceful shutdown.
78 grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
83 deleteDir("target", "blueprints")
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)
94 bootstrapOutput.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
95 "failed to get success status"
97 assertEquals(id, bootstrapOutput.commonHeader.requestId)
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)
107 assertEquals(200, output.status.code)
109 output.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
110 "failed to get success status"
112 assertEquals(id, output.commonHeader.requestId)
114 val downloadId = "123_download"
115 val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())
117 val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
118 assertEquals(200, downloadOutput.status.code)
120 downloadOutput.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
121 "failed to get success status"
123 assertNotNull(downloadOutput.fileChunk?.chunk, "failed to get cba file chunks")
124 assertEquals(downloadId, downloadOutput.commonHeader.requestId)
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())
133 var output = blockingStub.uploadBlueprint(req)
134 assertEquals(200, output.status.code)
136 output.status.message!!.contentEquals(BluePrintConstants.STATUS_SUCCESS),
137 "failed to get success status"
139 assertEquals(id, output.commonHeader.requestId)
141 val removeReq = createRemoveInputRequest(id)
142 output = blockingStub.removeBlueprint(removeReq)
143 assertEquals(200, output.status.code)
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() {
149 val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
152 type = GRPCLibConstants.TYPE_TOKEN_AUTH
153 token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
155 val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
156 val channel = basicAuthGrpcClientService.channel()
158 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(channel)
160 val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())
162 val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
163 assertNotNull(bluePrintManagementOutput, "failed to get response")
167 private fun createBootstrapInputRequest(id: String): BluePrintBootstrapInput {
168 val commonHeader = CommonHeader
170 .setTimestamp("2012-04-23T18:25:43.511Z")
171 .setOriginatorId("System")
173 .setSubRequestId("1234-56").build()
175 return BluePrintBootstrapInput.newBuilder()
176 .setCommonHeader(commonHeader)
177 .setLoadModelType(false)
178 .setLoadResourceDictionary(false)
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}")
187 val commonHeader = CommonHeader
189 .setTimestamp("2012-04-23T18:25:43.511Z")
190 .setOriginatorId("System")
192 .setSubRequestId("1234-56").build()
194 val actionIdentifier = ActionIdentifiers.newBuilder()
195 .setActionName(action)
196 .setBlueprintName("sample")
197 .setBlueprintVersion("1.0.0")
200 val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
203 return BluePrintUploadInput.newBuilder()
204 .setCommonHeader(commonHeader)
205 .setActionIdentifiers(actionIdentifier)
206 .setFileChunk(fileChunk)
210 private fun createDownloadInputRequest(id: String, action: String): BluePrintDownloadInput {
211 val commonHeader = CommonHeader
213 .setTimestamp("2012-04-23T18:25:43.511Z")
214 .setOriginatorId("System")
216 .setSubRequestId("1234-56").build()
218 return BluePrintDownloadInput.newBuilder()
219 .setCommonHeader(commonHeader)
220 .setActionIdentifiers(
221 ActionIdentifiers.newBuilder()
222 .setBlueprintName("baseconfiguration")
223 .setBlueprintVersion("1.0.0")
224 .setActionName(action).build()
229 private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
230 val commonHeader = CommonHeader
232 .setTimestamp("2012-04-23T18:25:43.511Z")
233 .setOriginatorId("System")
235 .setSubRequestId("1234-56").build()
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()