2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
20 import com.google.protobuf.ByteString
21 import io.grpc.testing.GrpcServerRule
24 import org.junit.runner.RunWith
25 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
26 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementInput
27 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc
28 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
31 import org.springframework.context.annotation.ComponentScan
32 import org.springframework.test.annotation.DirtiesContext
33 import org.springframework.test.context.TestPropertySource
34 import org.springframework.test.context.junit4.SpringRunner
36 import kotlin.test.AfterTest
37 import kotlin.test.BeforeTest
38 import kotlin.test.assertEquals
39 import kotlin.test.assertTrue
41 @RunWith(SpringRunner::class)
42 @EnableAutoConfiguration
44 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
45 @TestPropertySource(locations = ["classpath:application-test.properties"])
46 class BluePrintManagementGRPCHandlerTest {
49 val grpcServerRule = GrpcServerRule().directExecutor()
52 lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
56 // Create a server, add service, start, and register for automatic graceful shutdown.
57 grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
62 //TODO It's giving fluctuating results, need to look for another way to cleanup
63 // works sometimes otherwise results IO Exception
64 // Most probably bufferReader stream is not getting closed when cleanDir is getting invoked
65 File("./target/blueprints").deleteRecursively()
69 fun `test upload blueprint`() {
70 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
72 val req = createInputRequest(id)
73 val output = blockingStub.uploadBlueprint(req)
75 assertEquals(200, output.status.code)
76 assertTrue(output.status.message.contains("Successfully uploaded blueprint sample:1.0.0 with id("))
77 assertEquals(id, output.commonHeader.requestId)
81 fun `test delete blueprint`() {
82 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
84 val req = createInputRequest(id)
86 var output = blockingStub.uploadBlueprint(req)
87 assertEquals(200, output.status.code)
88 assertTrue(output.status.message.contains("Successfully uploaded blueprint sample:1.0.0 with id("))
89 assertEquals(id, output.commonHeader.requestId)
91 output = blockingStub.removeBlueprint(req)
92 assertEquals(200, output.status.code)
95 private fun createInputRequest(id: String): BluePrintManagementInput {
96 val file = File("./src/test/resources/test-cba.zip")
97 assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
99 val commonHeader = CommonHeader
101 .setTimestamp("2012-04-23T18:25:43.511Z")
102 .setOriginatorId("System")
104 .setSubRequestId("1234-56").build()
106 val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
109 return BluePrintManagementInput.newBuilder()
110 .setCommonHeader(commonHeader)
111 .setBlueprintName("sample")
112 .setBlueprintVersion("1.0.0")
113 .setFileChunk(fileChunk)