a48e699cbf48ce405e46b09dfda99262d79253f5
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
19
20 import com.google.protobuf.ByteString
21 import io.grpc.testing.GrpcServerRule
22 import org.apache.commons.io.FileUtils
23 import org.junit.Rule
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementInput
27 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementServiceGrpc
28 import org.onap.ccsdk.apps.controllerblueprints.management.api.CommonHeader
29 import org.onap.ccsdk.apps.controllerblueprints.management.api.FileChunk
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
32 import org.springframework.context.annotation.ComponentScan
33 import org.springframework.test.annotation.DirtiesContext
34 import org.springframework.test.context.TestPropertySource
35 import org.springframework.test.context.junit4.SpringRunner
36 import java.io.File
37 import java.nio.file.Paths
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.apps.blueprintsprocessor", "org.onap.ccsdk.apps.controllerblueprints"])
47 @TestPropertySource(locations = ["classpath:application-test.properties"])
48 class BluePrintManagementGRPCHandlerTest {
49
50     @get:Rule
51     val grpcServerRule = GrpcServerRule().directExecutor()
52
53     @Autowired
54     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
55
56     @BeforeTest
57     fun init() {
58         // Create a server, add service, start, and register for automatic graceful shutdown.
59         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
60     }
61
62     @AfterTest
63     fun cleanDir() {
64         FileUtils.deleteDirectory(File("./target/blueprints"))
65     }
66
67     @Test
68     fun `test upload blueprint`() {
69         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
70         val id = "123"
71         val output = blockingStub.uploadBlueprint(createInputRequest(id))
72         assertEquals(200, output.status.code)
73         assertTrue(output.status.message.contains("Successfully uploaded blueprint sample:1.0.0 with id("))
74         assertEquals(id, output.commonHeader.requestId)
75     }
76
77     @Test
78     fun `test delete blueprint`() {
79         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
80         val id = "123"
81         val req = createInputRequest(id)
82         blockingStub.uploadBlueprint(req)
83         blockingStub.removeBlueprint(req)
84     }
85
86     private fun createInputRequest(id: String): BluePrintManagementInput {
87         val file = Paths.get("./src/test/resources/test-cba.zip").toFile()
88         assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
89
90         val commonHeader = CommonHeader.newBuilder()
91                 .setTimestamp("2012-04-23T18:25:43.511Z")
92                 .setOriginatorId("System")
93                 .setRequestId(id)
94                 .setSubRequestId("1234-56").build()
95
96         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
97                 .build()
98
99         return BluePrintManagementInput.newBuilder()
100                 .setCommonHeader(commonHeader)
101                 .setBlueprintName("sample")
102                 .setBlueprintVersion("1.0.0")
103                 .setFileChunk(fileChunk)
104                 .build()
105     }
106 }