Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / test / kotlin / org / onap / ccsdk / cds / 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.cds.blueprintsprocessor.selfservice.api
19
20 import com.google.protobuf.ByteString
21 import io.grpc.testing.GrpcServerRule
22 import org.junit.Rule
23 import org.junit.Test
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
35 import java.io.File
36 import kotlin.test.AfterTest
37 import kotlin.test.BeforeTest
38 import kotlin.test.assertEquals
39 import kotlin.test.assertTrue
40
41 @RunWith(SpringRunner::class)
42 @EnableAutoConfiguration
43 @DirtiesContext
44 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
45 @TestPropertySource(locations = ["classpath:application-test.properties"])
46 class BluePrintManagementGRPCHandlerTest {
47
48     @get:Rule
49     val grpcServerRule = GrpcServerRule().directExecutor()
50
51     @Autowired
52     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
53
54     @BeforeTest
55     fun init() {
56         // Create a server, add service, start, and register for automatic graceful shutdown.
57         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
58     }
59
60     @AfterTest
61     fun cleanDir() {
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()
66     }
67
68     @Test
69     fun `test upload blueprint`() {
70         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
71         val id = "123_upload"
72         val req = createInputRequest(id)
73         val output = blockingStub.uploadBlueprint(req)
74
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)
78     }
79
80     @Test
81     fun `test delete blueprint`() {
82         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
83         val id = "123_delete"
84         val req = createInputRequest(id)
85
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)
90
91         output = blockingStub.removeBlueprint(req)
92         assertEquals(200, output.status.code)
93     }
94
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}")
98
99         val commonHeader = CommonHeader
100                 .newBuilder()
101                 .setTimestamp("2012-04-23T18:25:43.511Z")
102                 .setOriginatorId("System")
103                 .setRequestId(id)
104                 .setSubRequestId("1234-56").build()
105
106         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
107                 .build()
108
109         return BluePrintManagementInput.newBuilder()
110                 .setCommonHeader(commonHeader)
111                 .setBlueprintName("sample")
112                 .setBlueprintVersion("1.0.0")
113                 .setFileChunk(fileChunk)
114                 .build()
115     }
116 }