c870bf7f8b675af1af19d8ec13fe4a76145b8807
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
18
19 import com.google.protobuf.ByteString
20 import io.grpc.testing.GrpcServerRule
21 import org.apache.commons.io.FileUtils
22 import org.junit.Rule
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintCoreConfiguration
26 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementServiceGrpc
27 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintUploadInput
28 import org.onap.ccsdk.apps.controllerblueprints.management.api.CommonHeader
29 import org.onap.ccsdk.apps.controllerblueprints.management.api.FileChunk
30 import org.slf4j.LoggerFactory
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.test.context.ContextConfiguration
33 import org.springframework.test.context.TestPropertySource
34 import org.springframework.test.context.junit4.SpringRunner
35 import java.io.File
36 import java.nio.file.Paths
37 import kotlin.test.AfterTest
38 import kotlin.test.BeforeTest
39 import kotlin.test.assertNotNull
40 import kotlin.test.assertTrue
41
42 @RunWith(SpringRunner::class)
43 @ContextConfiguration(classes = [BluePrintManagementGRPCHandler::class, BluePrintCoreConfiguration::class])
44 @TestPropertySource(locations = ["classpath:application-test.properties"])
45 class BluePrintManagementGRPCHandlerTest {
46
47     private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandlerTest::class.java)!!
48
49     @get:Rule
50     val grpcServerRule = GrpcServerRule().directExecutor()
51
52     @Autowired
53     lateinit var bluePrintManagementGRPCHandler: BluePrintManagementGRPCHandler
54
55     @BeforeTest
56     fun init() {
57         // Create a server, add service, start, and register for automatic graceful shutdown.
58         grpcServerRule.serviceRegistry.addService(bluePrintManagementGRPCHandler)
59     }
60
61     @AfterTest
62     fun cleanDir() {
63         FileUtils.deleteDirectory(File("./target/blueprints"))
64     }
65
66     @Test
67     fun testFileUpload() {
68         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
69
70         val file = Paths.get("./src/test/resources/test-cba.zip").toFile()
71         assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
72
73         val commonHeader = CommonHeader.newBuilder()
74                 .setTimestamp("2012-04-23T18:25:43.511Z")
75                 .setOriginatorId("System")
76                 .setRequestId("1234")
77                 .setSubRequestId("1234-56").build()
78
79         val fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(file.inputStream().readBytes()))
80                 .build()
81
82         val input = BluePrintUploadInput.newBuilder()
83                 .setCommonHeader(commonHeader)
84                 .setBlueprintName("sample")
85                 .setBlueprintVersion("1.0.0")
86                 .setFileChunk(fileChunk)
87                 .build()
88
89         val output = blockingStub.uploadBlueprint(input)
90         assertNotNull(output, "failed to get upload response")
91     }
92 }