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.apps.blueprintsprocessor.selfservice.api
20 import com.google.protobuf.ByteString
21 import io.grpc.testing.GrpcServerRule
22 import org.apache.commons.io.FileUtils
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.apps.controllerblueprints.common.api.CommonHeader
27 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementInput
28 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementServiceGrpc
29 import org.onap.ccsdk.apps.controllerblueprints.management.api.FileChunk
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
32 import org.springframework.context.annotation.ComponentScan
33 import org.springframework.test.annotation.DirtiesContext
34 import org.springframework.test.context.TestPropertySource
35 import org.springframework.test.context.junit4.SpringRunner
37 import java.nio.file.Paths
38 import kotlin.test.AfterTest
39 import kotlin.test.BeforeTest
40 import kotlin.test.assertEquals
41 import kotlin.test.assertTrue
43 @RunWith(SpringRunner::class)
44 @EnableAutoConfiguration
46 @ComponentScan(basePackages = ["org.onap.ccsdk.apps.blueprintsprocessor", "org.onap.ccsdk.apps.controllerblueprints"])
47 @TestPropertySource(locations = ["classpath:application-test.properties"])
48 class BluePrintManagementGRPCHandlerTest {
51 val grpcServerRule = GrpcServerRule().directExecutor()
54 lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
58 // Create a server, add service, start, and register for automatic graceful shutdown.
59 grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
64 //TODO It's giving fluctuating results, need to look for another way to cleanup
65 // works sometimes otherwise results IO Exception
66 // Most probably bufferReader stream is not getting closed when cleanDir is getting invoked
67 FileUtils.deleteDirectory(File("./target/blueprints"))
71 fun `test upload blueprint`() {
72 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
74 val req = createInputRequest(id)
75 val output = blockingStub.uploadBlueprint(req)
77 assertEquals(200, output.status.code)
78 assertTrue(output.status.message.contains("Successfully uploaded blueprint sample:1.0.0 with id("))
79 assertEquals(id, output.commonHeader.requestId)
83 fun `test delete blueprint`() {
84 val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
86 val req = createInputRequest(id)
88 var output = blockingStub.uploadBlueprint(req)
89 assertEquals(200, output.status.code)
90 assertTrue(output.status.message.contains("Successfully uploaded blueprint sample:1.0.0 with id("))
91 assertEquals(id, output.commonHeader.requestId)
93 output = blockingStub.removeBlueprint(req)
94 assertEquals(200, output.status.code)
97 private fun createInputRequest(id: String): BluePrintManagementInput {
98 val file = Paths.get("./src/test/resources/test-cba.zip").toFile()
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 BluePrintManagementInput.newBuilder()
112 .setCommonHeader(commonHeader)
113 .setBlueprintName("sample")
114 .setBlueprintVersion("1.0.0")
115 .setFileChunk(fileChunk)