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.boot.autoconfigure.EnableAutoConfiguration
46 import org.springframework.context.annotation.ComponentScan
47 import org.springframework.test.annotation.DirtiesContext
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 @EnableAutoConfiguration
60 basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor",
61 "org.onap.ccsdk.cds.controllerblueprints"]
63 @TestPropertySource(locations = ["classpath:application-test.properties"])
64 class BluePrintManagementGRPCHandlerTest {
67 val grpcServerRule = GrpcServerRule().directExecutor()
70 lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
74 // Create a server, add service, start, and register for automatic graceful shutdown.
75 grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
76 deleteDir("target", "blueprints")
81 deleteDir("target", "blueprints")
86 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
87 val id = "123_Bootstrap"
88 val req = createBootstrapInputRequest(id)
89 val bootstrapOutput = blockingStub.bootstrapBlueprint(req)
90 assertEquals(200, bootstrapOutput.status.code)
92 bootstrapOutput.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
93 "failed to get success status"
95 assertEquals(id, bootstrapOutput.commonHeader.requestId)
99 fun `test upload and download blueprint`() {
100 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
101 val id = "123_upload"
102 val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
103 val output = blockingStub.uploadBlueprint(req)
105 assertEquals(200, output.status.code)
107 output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
108 "failed to get success status"
110 assertEquals(id, output.commonHeader.requestId)
112 val downloadId = "123_download"
113 val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())
115 val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
116 assertEquals(200, downloadOutput.status.code)
118 downloadOutput.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
119 "failed to get success status"
121 assertNotNull(downloadOutput.fileChunk?.chunk, "failed to get cba file chunks")
122 assertEquals(downloadId, downloadOutput.commonHeader.requestId)
126 fun `test delete blueprint`() {
127 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
128 val id = "123_delete"
129 val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())
131 var output = blockingStub.uploadBlueprint(req)
132 assertEquals(200, output.status.code)
134 output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
135 "failed to get success status"
137 assertEquals(id, output.commonHeader.requestId)
139 val removeReq = createRemoveInputRequest(id)
140 output = blockingStub.removeBlueprint(removeReq)
141 assertEquals(200, output.status.code)
144 /** This is Integration test sample, Do not enable this test case in server build, this is for local desktop testing*/
145 private fun integrationTestGrpcManagement() {
147 val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
150 type = GRPCLibConstants.TYPE_TOKEN_AUTH
151 token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
153 val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
154 val channel = basicAuthGrpcClientService.channel()
156 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(channel)
158 val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())
160 val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
161 assertNotNull(bluePrintManagementOutput, "failed to get response")
165 private fun createBootstrapInputRequest(id: String): BluePrintBootstrapInput {
166 val commonHeader = CommonHeader
168 .setTimestamp("2012-04-23T18:25:43.511Z")
169 .setOriginatorId("System")
171 .setSubRequestId("1234-56").build()
173 return BluePrintBootstrapInput.newBuilder()
174 .setCommonHeader(commonHeader)
175 .setLoadModelType(false)
176 .setLoadResourceDictionary(false)
181 private fun createUploadInputRequest(id: String, action: String): BluePrintUploadInput {
182 val file = normalizedFile("./src/test/resources/test-cba.zip")
183 assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
185 val commonHeader = CommonHeader
187 .setTimestamp("2012-04-23T18:25:43.511Z")
188 .setOriginatorId("System")
190 .setSubRequestId("1234-56").build()
192 val actionIdentifier = ActionIdentifiers.newBuilder()
193 .setActionName(action)
194 .setBlueprintName("sample")
195 .setBlueprintVersion("1.0.0")
198 val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
201 return BluePrintUploadInput.newBuilder()
202 .setCommonHeader(commonHeader)
203 .setActionIdentifiers(actionIdentifier)
204 .setFileChunk(fileChunk)
208 private fun createDownloadInputRequest(id: String, action: String): BluePrintDownloadInput {
209 val commonHeader = CommonHeader
211 .setTimestamp("2012-04-23T18:25:43.511Z")
212 .setOriginatorId("System")
214 .setSubRequestId("1234-56").build()
216 return BluePrintDownloadInput.newBuilder()
217 .setCommonHeader(commonHeader)
218 .setActionIdentifiers(
219 ActionIdentifiers.newBuilder()
220 .setBlueprintName("baseconfiguration")
221 .setBlueprintVersion("1.0.0")
222 .setActionName(action).build()
227 private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
228 val commonHeader = CommonHeader
230 .setTimestamp("2012-04-23T18:25:43.511Z")
231 .setOriginatorId("System")
233 .setSubRequestId("1234-56").build()
235 return BluePrintRemoveInput.newBuilder()
236 .setCommonHeader(commonHeader)
237 .setActionIdentifiers(
238 ActionIdentifiers.newBuilder()
239 .setBlueprintName("sample")
240 .setBlueprintVersion("1.0.0")
241 .setActionName(RemoveAction.DEFAULT.toString()).build()