Merge "Implement GRPC enrich rpc."
[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.BluePrintConstants
29 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
30 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
31 import org.onap.ccsdk.cds.controllerblueprints.management.api.*
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
34 import org.springframework.context.annotation.ComponentScan
35 import org.springframework.test.annotation.DirtiesContext
36 import org.springframework.test.context.TestPropertySource
37 import org.springframework.test.context.junit4.SpringRunner
38 import kotlin.test.AfterTest
39 import kotlin.test.BeforeTest
40 import kotlin.test.assertEquals
41 import kotlin.test.assertTrue
42
43 @RunWith(SpringRunner::class)
44 @EnableAutoConfiguration
45 @DirtiesContext
46 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor",
47     "org.onap.ccsdk.cds.controllerblueprints"])
48 @TestPropertySource(locations = ["classpath:application-test.properties"])
49 class BluePrintManagementGRPCHandlerTest {
50
51     @get:Rule
52     val grpcServerRule = GrpcServerRule().directExecutor()
53
54     @Autowired
55     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
56
57     @BeforeTest
58     fun init() {
59         // Create a server, add service, start, and register for automatic graceful shutdown.
60         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
61         deleteDir("target", "blueprints")
62     }
63
64     @AfterTest
65     fun cleanDir() {
66         deleteDir("target", "blueprints")
67     }
68
69     @Test
70     fun `test upload blueprint`() {
71         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
72         val id = "123_upload"
73         val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
74         val output = blockingStub.uploadBlueprint(req)
75
76         assertEquals(200, output.status.code)
77         assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
78                 "failed to get success status")
79         assertEquals(id, output.commonHeader.requestId)
80     }
81
82     @Test
83     fun `test delete blueprint`() {
84         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
85         val id = "123_delete"
86         val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())
87
88         var output = blockingStub.uploadBlueprint(req)
89         assertEquals(200, output.status.code)
90         assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
91                 "failed to get success status")
92         assertEquals(id, output.commonHeader.requestId)
93
94         val removeReq = createRemoveInputRequest(id)
95         output = blockingStub.removeBlueprint(removeReq)
96         assertEquals(200, output.status.code)
97     }
98
99     private fun createUploadInputRequest(id: String, action: String): BluePrintUploadInput {
100         val file = normalizedFile("./src/test/resources/test-cba.zip")
101         assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
102
103         val commonHeader = CommonHeader
104                 .newBuilder()
105                 .setTimestamp("2012-04-23T18:25:43.511Z")
106                 .setOriginatorId("System")
107                 .setRequestId(id)
108                 .setSubRequestId("1234-56").build()
109
110         val actionIdentifier = ActionIdentifiers.newBuilder()
111                 .setActionName(action)
112                 .setBlueprintName("sample")
113                 .setBlueprintVersion("1.0.0")
114                 .build()
115
116         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
117                 .build()
118
119         return BluePrintUploadInput.newBuilder()
120                 .setCommonHeader(commonHeader)
121                 .setActionIdentifiers(actionIdentifier)
122                 .setFileChunk(fileChunk)
123                 .build()
124     }
125
126     private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
127         val commonHeader = CommonHeader
128                 .newBuilder()
129                 .setTimestamp("2012-04-23T18:25:43.511Z")
130                 .setOriginatorId("System")
131                 .setRequestId(id)
132                 .setSubRequestId("1234-56").build()
133
134         return BluePrintRemoveInput.newBuilder()
135                 .setCommonHeader(commonHeader)
136                 .setBlueprintName("sample")
137                 .setBlueprintVersion("1.0.0")
138                 .build()
139     }
140 }