Merge "Implement GRPC download cab."
[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 kotlinx.coroutines.runBlocking
24 import org.junit.Rule
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GRPCLibConstants
28 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TokenAuthGrpcClientProperties
29 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.TokenAuthGrpcClientService
30 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers
31 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
33 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
34 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
35 import org.onap.ccsdk.cds.controllerblueprints.management.api.*
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
38 import org.springframework.context.annotation.ComponentScan
39 import org.springframework.test.annotation.DirtiesContext
40 import org.springframework.test.context.TestPropertySource
41 import org.springframework.test.context.junit4.SpringRunner
42 import kotlin.test.*
43
44 @RunWith(SpringRunner::class)
45 @EnableAutoConfiguration
46 @DirtiesContext
47 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor",
48     "org.onap.ccsdk.cds.controllerblueprints"])
49 @TestPropertySource(locations = ["classpath:application-test.properties"])
50 class BluePrintManagementGRPCHandlerTest {
51
52     @get:Rule
53     val grpcServerRule = GrpcServerRule().directExecutor()
54
55     @Autowired
56     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
57
58     @BeforeTest
59     fun init() {
60         // Create a server, add service, start, and register for automatic graceful shutdown.
61         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
62         deleteDir("target", "blueprints")
63     }
64
65     @AfterTest
66     fun cleanDir() {
67         deleteDir("target", "blueprints")
68     }
69
70     @Test
71     fun `test upload and download blueprint`() {
72         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
73         val id = "123_upload"
74         val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
75         val output = blockingStub.uploadBlueprint(req)
76
77         assertEquals(200, output.status.code)
78         assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
79                 "failed to get success status")
80         assertEquals(id, output.commonHeader.requestId)
81
82         val downloadId = "123_download"
83         val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())
84
85         val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
86         assertEquals(200, downloadOutput.status.code)
87         assertTrue(downloadOutput.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
88                 "failed to get success status")
89         assertNotNull(downloadOutput.fileChunk?.chunk, "failed to get cba file chunks")
90         assertEquals(downloadId, downloadOutput.commonHeader.requestId)
91     }
92
93     @Test
94     fun `test delete blueprint`() {
95         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
96         val id = "123_delete"
97         val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())
98
99         var output = blockingStub.uploadBlueprint(req)
100         assertEquals(200, output.status.code)
101         assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
102                 "failed to get success status")
103         assertEquals(id, output.commonHeader.requestId)
104
105         val removeReq = createRemoveInputRequest(id)
106         output = blockingStub.removeBlueprint(removeReq)
107         assertEquals(200, output.status.code)
108     }
109
110     /** This is Integration test sample, Do not enable this test case in server build, this is for local desktop testing*/
111     private fun integrationTestGrpcManagement() {
112         runBlocking {
113             val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
114                 host = "127.0.0.1"
115                 port = 9111
116                 type = GRPCLibConstants.TYPE_TOKEN_AUTH
117                 token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
118             }
119             val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
120             val channel = basicAuthGrpcClientService.channel()
121
122             val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(channel)
123
124             val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())
125
126             val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
127             assertNotNull(bluePrintManagementOutput, "failed to get response")
128         }
129     }
130
131
132     private fun createUploadInputRequest(id: String, action: String): BluePrintUploadInput {
133         val file = normalizedFile("./src/test/resources/test-cba.zip")
134         assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
135
136         val commonHeader = CommonHeader
137                 .newBuilder()
138                 .setTimestamp("2012-04-23T18:25:43.511Z")
139                 .setOriginatorId("System")
140                 .setRequestId(id)
141                 .setSubRequestId("1234-56").build()
142
143         val actionIdentifier = ActionIdentifiers.newBuilder()
144                 .setActionName(action)
145                 .setBlueprintName("sample")
146                 .setBlueprintVersion("1.0.0")
147                 .build()
148
149         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
150                 .build()
151
152         return BluePrintUploadInput.newBuilder()
153                 .setCommonHeader(commonHeader)
154                 .setActionIdentifiers(actionIdentifier)
155                 .setFileChunk(fileChunk)
156                 .build()
157     }
158
159     private fun createDownloadInputRequest(id: String, action: String): BluePrintDownloadInput {
160         val commonHeader = CommonHeader
161                 .newBuilder()
162                 .setTimestamp("2012-04-23T18:25:43.511Z")
163                 .setOriginatorId("System")
164                 .setRequestId(id)
165                 .setSubRequestId("1234-56").build()
166
167         return BluePrintDownloadInput.newBuilder()
168                 .setCommonHeader(commonHeader)
169                 .setActionIdentifiers(ActionIdentifiers.newBuilder()
170                         .setBlueprintName("baseconfiguration")
171                         .setBlueprintVersion("1.0.0")
172                         .setActionName(action).build())
173                 .build()
174     }
175
176     private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
177         val commonHeader = CommonHeader
178                 .newBuilder()
179                 .setTimestamp("2012-04-23T18:25:43.511Z")
180                 .setOriginatorId("System")
181                 .setRequestId(id)
182                 .setSubRequestId("1234-56").build()
183
184         return BluePrintRemoveInput.newBuilder()
185                 .setCommonHeader(commonHeader)
186                 .setBlueprintName("sample")
187                 .setBlueprintVersion("1.0.0")
188                 .build()
189     }
190 }