aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/inbounds/designer-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/BlueprintManagementGRPCHandlerTest.kt
blob: ed60f721aea82296f82b54128ad632f5bcd7e227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2019 Bell Canada.
 * Modifications Copyright © 2019 IBM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.designer.api

import com.google.protobuf.ByteString
import io.grpc.testing.GrpcServerRule
import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GRPCLibConstants
import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TokenAuthGrpcClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.TokenAuthGrpcClientService
import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers
import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.compress
import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
import org.onap.ccsdk.cds.controllerblueprints.management.api.BlueprintBootstrapInput
import org.onap.ccsdk.cds.controllerblueprints.management.api.BlueprintDownloadInput
import org.onap.ccsdk.cds.controllerblueprints.management.api.BlueprintManagementServiceGrpc
import org.onap.ccsdk.cds.controllerblueprints.management.api.BlueprintRemoveInput
import org.onap.ccsdk.cds.controllerblueprints.management.api.BlueprintUploadInput
import org.onap.ccsdk.cds.controllerblueprints.management.api.DownloadAction
import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk
import org.onap.ccsdk.cds.controllerblueprints.management.api.RemoveAction
import org.onap.ccsdk.cds.controllerblueprints.management.api.UploadAction
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit4.SpringRunner
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

@RunWith(SpringRunner::class)
@ContextConfiguration(
    classes = [DesignerApiTestConfiguration::class, ErrorCatalogTestConfiguration::class]
)
@TestPropertySource(locations = ["classpath:application-test.properties"])
class BlueprintManagementGRPCHandlerTest {

    @get:Rule
    val grpcServerRule = GrpcServerRule().directExecutor()

    @Autowired
    lateinit var bluePrintManagementGRPCHandler: BlueprintManagementGRPCHandler

    @BeforeTest
    fun init() {

        deleteDir("target", "blueprints")

        // Create sample CBA zip
        normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
            .compress(normalizedFile("./target/blueprints/generated-cba.zip"))

        // Create a server, add service, start, and register for automatic graceful shutdown.
        grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
    }

    @AfterTest
    fun cleanDir() {
        deleteDir("target", "blueprints")
    }

    @Test
    fun testBootstrap() {
        val blockingStub = BlueprintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
        val id = "123_Bootstrap"
        val req = createBootstrapInputRequest(id)
        val bootstrapOutput = blockingStub.bootstrapBlueprint(req)
        assertEquals(200, bootstrapOutput.status.code)
        assertTrue(
            bootstrapOutput.status.message!!.contentEquals(BlueprintConstants.STATUS_SUCCESS),
            "failed to get success status"
        )
        assertEquals(EventType.EVENT_COMPONENT_EXECUTED, bootstrapOutput.status.eventType)
        assertEquals(id, bootstrapOutput.commonHeader.requestId)
    }

    @Test
    fun `test upload and download blueprint`() {
        val blockingStub = BlueprintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
        val id = "123_upload"
        val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
        val output = blockingStub.uploadBlueprint(req)

        assertEquals(200, output.status.code)
        assertTrue(
            output.status.message!!.contentEquals(BlueprintConstants.STATUS_SUCCESS),
            "failed to get success status"
        )
        assertEquals(EventType.EVENT_COMPONENT_EXECUTED, output.status.eventType)
        assertEquals(id, output.commonHeader.requestId)

        val downloadId = "123_download"
        val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())

        val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
        assertEquals(200, downloadOutput.status.code)
        assertTrue(
            downloadOutput.status.message!!.contentEquals(BlueprintConstants.STATUS_SUCCESS),
            "failed to get success status"
        )
        assertEquals(EventType.EVENT_COMPONENT_EXECUTED, downloadOutput.status.eventType)
        assertNotNull(downloadOutput.fileChunk?.chunk, "failed to get cba file chunks")
        assertEquals(downloadId, downloadOutput.commonHeader.requestId)
    }

    @Test
    fun `test delete blueprint`() {
        val blockingStub = BlueprintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
        val id = "123_delete"
        val req = createUploadInputRequest(id, UploadAction.DRAFT.toString())

        var output = blockingStub.uploadBlueprint(req)
        assertEquals(200, output.status.code)
        assertTrue(
            output.status.message!!.contentEquals(BlueprintConstants.STATUS_SUCCESS),
            "failed to get success status"
        )
        assertEquals(id, output.commonHeader.requestId)
        assertEquals(EventType.EVENT_COMPONENT_EXECUTED, output.status.eventType)

        val removeReq = createRemoveInputRequest(id)
        output = blockingStub.removeBlueprint(removeReq)
        assertEquals(200, output.status.code)
    }

    /** This is Integration test sample, Do not enable this test case in server build, this is for local desktop testing*/
    private fun integrationTestGrpcManagement() {
        runBlocking {
            val tokenAuthGrpcClientProperties = TokenAuthGrpcClientProperties().apply {
                host = "127.0.0.1"
                port = 9111
                type = GRPCLibConstants.TYPE_TOKEN_AUTH
                token = "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="
            }
            val basicAuthGrpcClientService = TokenAuthGrpcClientService(tokenAuthGrpcClientProperties)
            val channel = basicAuthGrpcClientService.channel()

            val blockingStub = BlueprintManagementServiceGrpc.newBlockingStub(channel)

            val bluePrintUploadInput = createUploadInputRequest("12345", UploadAction.DRAFT.toString())

            val bluePrintManagementOutput = blockingStub.uploadBlueprint(bluePrintUploadInput)
            assertNotNull(bluePrintManagementOutput, "failed to get response")
        }
    }

    private fun createBootstrapInputRequest(id: String): BlueprintBootstrapInput {
        val commonHeader = CommonHeader
            .newBuilder()
            .setTimestamp("2012-04-23T18:25:43.511Z")
            .setOriginatorId("System")
            .setRequestId(id)
            .setSubRequestId("1234-56").build()

        return BlueprintBootstrapInput.newBuilder()
            .setCommonHeader(commonHeader)
            .setLoadModelType(false)
            .setLoadResourceDictionary(false)
            .setLoadCBA(false)
            .build()
    }

    private fun createUploadInputRequest(id: String, action: String): BlueprintUploadInput {
        val file = normalizedFile("./target/blueprints/generated-cba.zip")
        assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")

        val commonHeader = CommonHeader
            .newBuilder()
            .setTimestamp("2012-04-23T18:25:43.511Z")
            .setOriginatorId("System")
            .setRequestId(id)
            .setSubRequestId("1234-56").build()

        val actionIdentifier = ActionIdentifiers.newBuilder()
            .setActionName(action)
            .setBlueprintName("sample")
            .setBlueprintVersion("1.0.0")
            .build()

        val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
            .build()

        return BlueprintUploadInput.newBuilder()
            .setCommonHeader(commonHeader)
            .setActionIdentifiers(actionIdentifier)
            .setFileChunk(fileChunk)
            .build()
    }

    private fun createDownloadInputRequest(id: String, action: String): BlueprintDownloadInput {
        val commonHeader = CommonHeader
            .newBuilder()
            .setTimestamp("2012-04-23T18:25:43.511Z")
            .setOriginatorId("System")
            .setRequestId(id)
            .setSubRequestId("1234-56").build()

        return BlueprintDownloadInput.newBuilder()
            .setCommonHeader(commonHeader)
            .setActionIdentifiers(
                ActionIdentifiers.newBuilder()
                    .setBlueprintName("baseconfiguration")
                    .setBlueprintVersion("1.0.0")
                    .setActionName(action).build()
            )
            .build()
    }

    private fun createRemoveInputRequest(id: String): BlueprintRemoveInput {
        val commonHeader = CommonHeader
            .newBuilder()
            .setTimestamp("2012-04-23T18:25:43.511Z")
            .setOriginatorId("System")
            .setRequestId(id)
            .setSubRequestId("1234-56").build()

        return BlueprintRemoveInput.newBuilder()
            .setCommonHeader(commonHeader)
            .setActionIdentifiers(
                ActionIdentifiers.newBuilder()
                    .setBlueprintName("sample")
                    .setBlueprintVersion("1.0.0")
                    .setActionName(RemoveAction.DEFAULT.toString()).build()
            )
            .build()
    }
}