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.deleteDir
34 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
35 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintBootstrapInput
36 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintDownloadInput
37 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc
38 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintRemoveInput
39 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput
40 import org.onap.ccsdk.cds.controllerblueprints.management.api.DownloadAction
41 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk
42 import org.onap.ccsdk.cds.controllerblueprints.management.api.RemoveAction
43 import org.onap.ccsdk.cds.controllerblueprints.management.api.UploadAction
44 import org.springframework.beans.factory.annotation.Autowired
45 import org.springframework.test.context.ContextConfiguration
46 import org.springframework.test.context.TestPropertySource
47 import org.springframework.test.context.junit4.SpringRunner
48 import kotlin.test.AfterTest
49 import kotlin.test.BeforeTest
50 import kotlin.test.assertEquals
51 import kotlin.test.assertNotNull
52 import kotlin.test.assertTrue
54 @RunWith(SpringRunner::class)
55 @ContextConfiguration(
56 classes = [DesignerApiTestConfiguration::class]
58 @TestPropertySource(locations = ["classpath:application-test.properties"])
59 class BluePrintManagementGRPCHandlerTest {
62 val grpcServerRule = GrpcServerRule().directExecutor()
65 lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
69 // Create a server, add service, start, and register for automatic graceful shutdown.
70 grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
71 deleteDir("target", "blueprints")
76 deleteDir("target", "blueprints")
81 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
82 val id = "123_Bootstrap"
83 val req = createBootstrapInputRequest(id)
84 val bootstrapOutput = blockingStub.bootstrapBlueprint(req)
85 assertEquals(200, bootstrapOutput.status.code)
87 bootstrapOutput.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
88 "failed to get success status"
90 assertEquals(id, bootstrapOutput.commonHeader.requestId)
94 fun `test upload and download blueprint`() {
95 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
97 val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
98 val output = blockingStub.uploadBlueprint(req)
100 assertEquals(200, output.status.code)
102 output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
103 "failed to get success status"
105 assertEquals(id, output.commonHeader.requestId)
107 val downloadId = "123_download"
108 val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())
110 val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
111 assertEquals(200, downloadOutput.status.code)
113 downloadOutput.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
114 "failed to get success status"
116 assertNotNull(downloadOutput.fileChunk?.chunk, "failed to get cba file chunks")
117 assertEquals(downloadId, downloadOutput.commonHeader.requestId)
121 fun `test delete blueprint`() {
122 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
123 val id = "123_delete"
124 val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())
126 var output = blockingStub.uploadBlueprint(req)
127 assertEquals(200, output.status.code)
129 output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
130 "failed to get success status"
132 assertEquals(id, output.commonHeader.requestId)
134 val removeReq = createRemoveInputRequest(id)
135 output = blockingStub.removeBlueprint(removeReq)
136 assertEquals(200, output.status.code)
139 /** This is Integration test sample, Do not enable this test case in server build, this is for local desktop testing*/
140 private fun integrationTestGrpcManagement() {
142 val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
145 type = GRPCLibConstants.TYPE_TOKEN_AUTH
146 token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
148 val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
149 val channel = basicAuthGrpcClientService.channel()
151 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(channel)
153 val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())
155 val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
156 assertNotNull(bluePrintManagementOutput, "failed to get response")
160 private fun createBootstrapInputRequest(id: String): BluePrintBootstrapInput {
161 val commonHeader = CommonHeader
163 .setTimestamp("2012-04-23T18:25:43.511Z")
164 .setOriginatorId("System")
166 .setSubRequestId("1234-56").build()
168 return BluePrintBootstrapInput.newBuilder()
169 .setCommonHeader(commonHeader)
170 .setLoadModelType(false)
171 .setLoadResourceDictionary(false)
176 private fun createUploadInputRequest(id: String, action: String): BluePrintUploadInput {
177 val file = normalizedFile("./src/test/resources/test-cba.zip")
178 assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
180 val commonHeader = CommonHeader
182 .setTimestamp("2012-04-23T18:25:43.511Z")
183 .setOriginatorId("System")
185 .setSubRequestId("1234-56").build()
187 val actionIdentifier = ActionIdentifiers.newBuilder()
188 .setActionName(action)
189 .setBlueprintName("sample")
190 .setBlueprintVersion("1.0.0")
193 val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
196 return BluePrintUploadInput.newBuilder()
197 .setCommonHeader(commonHeader)
198 .setActionIdentifiers(actionIdentifier)
199 .setFileChunk(fileChunk)
203 private fun createDownloadInputRequest(id: String, action: String): BluePrintDownloadInput {
204 val commonHeader = CommonHeader
206 .setTimestamp("2012-04-23T18:25:43.511Z")
207 .setOriginatorId("System")
209 .setSubRequestId("1234-56").build()
211 return BluePrintDownloadInput.newBuilder()
212 .setCommonHeader(commonHeader)
213 .setActionIdentifiers(
214 ActionIdentifiers.newBuilder()
215 .setBlueprintName("baseconfiguration")
216 .setBlueprintVersion("1.0.0")
217 .setActionName(action).build()
222 private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
223 val commonHeader = CommonHeader
225 .setTimestamp("2012-04-23T18:25:43.511Z")
226 .setOriginatorId("System")
228 .setSubRequestId("1234-56").build()
230 return BluePrintRemoveInput.newBuilder()
231 .setCommonHeader(commonHeader)
232 .setActionIdentifiers(
233 ActionIdentifiers.newBuilder()
234 .setBlueprintName("sample")
235 .setBlueprintVersion("1.0.0")
236 .setActionName(RemoveAction.DEFAULT.toString()).build()