Merge "Move GRPC management api to designer api."
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / BluePrintManagementGRPCHandlerTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2019 IBM.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
20
21 import com.google.protobuf.ByteString
22 import io.grpc.testing.GrpcServerRule
23 import org.junit.Rule
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers
27 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
28 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
29 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
30 import org.onap.ccsdk.cds.controllerblueprints.management.api.*
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
33 import org.springframework.context.annotation.ComponentScan
34 import org.springframework.test.annotation.DirtiesContext
35 import org.springframework.test.context.TestPropertySource
36 import org.springframework.test.context.junit4.SpringRunner
37 import kotlin.test.AfterTest
38 import kotlin.test.BeforeTest
39 import kotlin.test.assertEquals
40 import kotlin.test.assertTrue
41
42 @RunWith(SpringRunner::class)
43 @EnableAutoConfiguration
44 @DirtiesContext
45 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
46 @TestPropertySource(locations = ["classpath:application-test.properties"])
47 class BluePrintManagementGRPCHandlerTest {
48
49     @get:Rule
50     val grpcServerRule = GrpcServerRule().directExecutor()
51
52     @Autowired
53     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
54
55     @BeforeTest
56     fun init() {
57         // Create a server, add service, start, and register for automatic graceful shutdown.
58         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
59         deleteDir("target", "blueprints")
60     }
61
62     @AfterTest
63     fun cleanDir() {
64         deleteDir("target", "blueprints")
65     }
66
67     @Test
68     fun `test upload blueprint`() {
69         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
70         val id = "123_upload"
71         val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
72         val output = blockingStub.uploadBlueprint(req)
73
74         assertEquals(200, output.status.code)
75         assertTrue(output.status.message.contains("Successfully uploaded CBA"))
76         assertEquals(id, output.commonHeader.requestId)
77     }
78
79     @Test
80     fun `test delete blueprint`() {
81         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
82         val id = "123_delete"
83         val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())
84
85         var output = blockingStub.uploadBlueprint(req)
86         assertEquals(200, output.status.code)
87         assertTrue(output.status.message.contains("Successfully uploaded CBA"))
88         assertEquals(id, output.commonHeader.requestId)
89
90         val removeReq = createRemoveInputRequest(id)
91         output = blockingStub.removeBlueprint(removeReq)
92         assertEquals(200, output.status.code)
93     }
94
95     private fun createUploadInputRequest(id: String, action: String): BluePrintUploadInput {
96         val file = normalizedFile("./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 actionIdentifier = ActionIdentifiers.newBuilder()
107                 .setActionName(action)
108                 .setBlueprintName("sample")
109                 .setBlueprintVersion("1.0.0")
110                 .build()
111
112         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
113                 .build()
114
115         return BluePrintUploadInput.newBuilder()
116                 .setCommonHeader(commonHeader)
117                 .setActionIdentifiers(actionIdentifier)
118                 .setFileChunk(fileChunk)
119                 .build()
120     }
121
122     private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
123         val commonHeader = CommonHeader
124                 .newBuilder()
125                 .setTimestamp("2012-04-23T18:25:43.511Z")
126                 .setOriginatorId("System")
127                 .setRequestId(id)
128                 .setSubRequestId("1234-56").build()
129
130         return BluePrintRemoveInput.newBuilder()
131                 .setCommonHeader(commonHeader)
132                 .setBlueprintName("sample")
133                 .setBlueprintVersion("1.0.0")
134                 .build()
135     }
136 }