4da3b74591cf7bd7eb601e874b32b12e4d0788c5
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / test / kotlin / org / onap / ccsdk / apps / blueprintsprocessor / selfservice / api / BluePrintManagementGRPCHandlerTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
19
20 import com.google.protobuf.ByteString
21 import io.grpc.testing.GrpcServerRule
22 import org.apache.commons.io.FileUtils
23 import org.junit.Rule
24 import org.junit.Test
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
36 import java.io.File
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
42
43 @RunWith(SpringRunner::class)
44 @EnableAutoConfiguration
45 @DirtiesContext
46 @ComponentScan(basePackages = ["org.onap.ccsdk.apps.blueprintsprocessor", "org.onap.ccsdk.apps.controllerblueprints"])
47 @TestPropertySource(locations = ["classpath:application-test.properties"])
48 class BluePrintManagementGRPCHandlerTest {
49
50     @get:Rule
51     val grpcServerRule = GrpcServerRule().directExecutor()
52
53     @Autowired
54     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
55
56     @BeforeTest
57     fun init() {
58         // Create a server, add service, start, and register for automatic graceful shutdown.
59         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
60     }
61
62     //@AfterTest
63     fun cleanDir() {
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"))
68     }
69
70     @Test
71     fun `test upload blueprint`() {
72         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
73         val id = "123_upload"
74         val req = createInputRequest(id)
75         val output = blockingStub.uploadBlueprint(req)
76
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)
80     }
81
82     @Test
83     fun `test delete blueprint`() {
84         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
85         val id = "123_delete"
86         val req = createInputRequest(id)
87
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)
92
93         output = blockingStub.removeBlueprint(req)
94         assertEquals(200, output.status.code)
95     }
96
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}")
100
101         val commonHeader = CommonHeader
102                 .newBuilder()
103                 .setTimestamp("2012-04-23T18:25:43.511Z")
104                 .setOriginatorId("System")
105                 .setRequestId(id)
106                 .setSubRequestId("1234-56").build()
107
108         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
109                 .build()
110
111         return BluePrintManagementInput.newBuilder()
112                 .setCommonHeader(commonHeader)
113                 .setBlueprintName("sample")
114                 .setBlueprintVersion("1.0.0")
115                 .setFileChunk(fileChunk)
116                 .build()
117     }
118 }