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