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