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