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.*
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
38 import org.springframework.context.annotation.ComponentScan
39 import org.springframework.test.annotation.DirtiesContext
40 import org.springframework.test.context.TestPropertySource
41 import org.springframework.test.context.junit4.SpringRunner
44 @RunWith(SpringRunner::class)
45 @EnableAutoConfiguration
47 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor",
48 "org.onap.ccsdk.cds.controllerblueprints"])
49 @TestPropertySource(locations = ["classpath:application-test.properties"])
50 class BluePrintManagementGRPCHandlerTest {
53 val grpcServerRule = GrpcServerRule().directExecutor()
56 lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
60 // Create a server, add service, start, and register for automatic graceful shutdown.
61 grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
62 deleteDir("target", "blueprints")
67 deleteDir("target", "blueprints")
71 fun `test upload blueprint`() {
72 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
74 val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
75 val output = blockingStub.uploadBlueprint(req)
77 assertEquals(200, output.status.code)
78 assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
79 "failed to get success status")
80 assertEquals(id, output.commonHeader.requestId)
84 fun `test delete blueprint`() {
85 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
87 val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())
89 var output = blockingStub.uploadBlueprint(req)
90 assertEquals(200, output.status.code)
91 assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
92 "failed to get success status")
93 assertEquals(id, output.commonHeader.requestId)
95 val removeReq = createRemoveInputRequest(id)
96 output = blockingStub.removeBlueprint(removeReq)
97 assertEquals(200, output.status.code)
100 /** This is Integration test sample, Do not enable this test case in server build, this is for local desktop testing*/
101 private fun integrationTestGrpcManagement() {
103 val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
106 type = GRPCLibConstants.TYPE_TOKEN_AUTH
107 token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
109 val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
110 val channel = basicAuthGrpcClientService.channel()
112 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(channel)
114 val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())
116 val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
117 assertNotNull(bluePrintManagementOutput, "failed to get response")
122 private fun createUploadInputRequest(id: String, action: String): BluePrintUploadInput {
123 val file = normalizedFile("./src/test/resources/test-cba.zip")
124 assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
126 val commonHeader = CommonHeader
128 .setTimestamp("2012-04-23T18:25:43.511Z")
129 .setOriginatorId("System")
131 .setSubRequestId("1234-56").build()
133 val actionIdentifier = ActionIdentifiers.newBuilder()
134 .setActionName(action)
135 .setBlueprintName("sample")
136 .setBlueprintVersion("1.0.0")
139 val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
142 return BluePrintUploadInput.newBuilder()
143 .setCommonHeader(commonHeader)
144 .setActionIdentifiers(actionIdentifier)
145 .setFileChunk(fileChunk)
149 private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
150 val commonHeader = CommonHeader
152 .setTimestamp("2012-04-23T18:25:43.511Z")
153 .setOriginatorId("System")
155 .setSubRequestId("1234-56").build()
157 return BluePrintRemoveInput.newBuilder()
158 .setCommonHeader(commonHeader)
159 .setBlueprintName("sample")
160 .setBlueprintVersion("1.0.0")