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.selfservice.api
21 import com.google.protobuf.ByteString
22 import io.grpc.testing.GrpcServerRule
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
27 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
28 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
29 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc
30 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintRemoveInput
31 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput
32 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
35 import org.springframework.context.annotation.ComponentScan
36 import org.springframework.test.annotation.DirtiesContext
37 import org.springframework.test.context.TestPropertySource
38 import org.springframework.test.context.junit4.SpringRunner
39 import kotlin.test.AfterTest
40 import kotlin.test.BeforeTest
41 import kotlin.test.assertEquals
42 import kotlin.test.assertTrue
44 @RunWith(SpringRunner::class)
45 @EnableAutoConfiguration
47 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
48 @TestPropertySource(locations = ["classpath:application-test.properties"])
49 class BluePrintManagementGRPCHandlerTest {
52 val grpcServerRule = GrpcServerRule().directExecutor()
55 lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
59 // Create a server, add service, start, and register for automatic graceful shutdown.
60 grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
61 deleteDir("target", "blueprints")
66 deleteDir("target", "blueprints")
70 fun `test upload blueprint`() {
71 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
73 val req = createUploadInputRequest(id)
74 val output = blockingStub.uploadBlueprint(req)
76 assertEquals(200, output.status.code)
77 assertTrue(output.status.message.contains("Successfully uploaded CBA"))
78 assertEquals(id, output.commonHeader.requestId)
82 fun `test delete blueprint`() {
83 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
85 val req = createUploadInputRequest(id)
87 var output = blockingStub.uploadBlueprint(req)
88 assertEquals(200, output.status.code)
89 assertTrue(output.status.message.contains("Successfully uploaded CBA"))
90 assertEquals(id, output.commonHeader.requestId)
92 val removeReq = createRemoveInputRequest(id)
93 output = blockingStub.removeBlueprint(removeReq)
94 assertEquals(200, output.status.code)
97 private fun createUploadInputRequest(id: String): BluePrintUploadInput {
98 val file = normalizedFile("./src/test/resources/test-cba.zip")
99 assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
101 val commonHeader = CommonHeader
103 .setTimestamp("2012-04-23T18:25:43.511Z")
104 .setOriginatorId("System")
106 .setSubRequestId("1234-56").build()
108 val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
111 return BluePrintUploadInput.newBuilder()
112 .setCommonHeader(commonHeader)
113 .setFileChunk(fileChunk)
117 private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
118 val commonHeader = CommonHeader
120 .setTimestamp("2012-04-23T18:25:43.511Z")
121 .setOriginatorId("System")
123 .setSubRequestId("1234-56").build()
125 return BluePrintRemoveInput.newBuilder()
126 .setCommonHeader(commonHeader)
127 .setBlueprintName("sample")
128 .setBlueprintVersion("1.0.0")