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