8154d3747a371becd83745ed32dbc9b6dc0e0b08
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / grpc-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / grpc / service / BluePrintGrpcServerTest.kt
1 /*
2  * Copyright © 2018-2019 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.cds.blueprintsprocessor.grpc.service
18
19 import com.github.marcoferrer.krotoplus.coroutines.client.clientCallBidiStreaming
20 import com.google.protobuf.util.JsonFormat
21 import kotlinx.coroutines.channels.consumeEach
22 import kotlinx.coroutines.launch
23 import kotlinx.coroutines.runBlocking
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ACTION_MODE_SYNC
25 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GRPCLibConstants
26 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TLSAuthGrpcClientProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TLSAuthGrpcServerProperties
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.common.api.EventType
31 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc
32 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
33 import java.util.*
34 import kotlin.test.Test
35 import kotlin.test.assertNotNull
36
37 class BluePrintGrpcServerTest {
38
39     private val tlsAuthGrpcServerProperties = TLSAuthGrpcServerProperties().apply {
40         port = 50052
41         type = GRPCLibConstants.TYPE_TLS_AUTH
42         certChain = "src/test/resources/tls-manual/py-executor-chain.pem"
43         privateKey = "src/test/resources/tls-manual/py-executor-key.pem"
44     }
45
46     private val tlsAuthGrpcClientProperties = TLSAuthGrpcClientProperties().apply {
47         host = "localhost"
48         port = 50052
49         type = GRPCLibConstants.TYPE_TLS_AUTH
50         trustCertCollection = "src/test/resources/tls-manual/py-executor-chain.pem"
51     }
52
53     @Test
54     fun testGrpcTLSContext() {
55         val tlsAuthGrpcServerService = TLSAuthGrpcServerService(tlsAuthGrpcServerProperties)
56         val sslContext = tlsAuthGrpcServerService.sslContext()
57         assertNotNull(sslContext, "failed to create grpc server ssl context")
58
59         val tlsAuthGrpcClientService = TLSAuthGrpcClientService(tlsAuthGrpcClientProperties)
60         val clientSslContext = tlsAuthGrpcClientService.sslContext()
61         assertNotNull(clientSslContext, "failed to create grpc client ssl context")
62     }
63
64     /** TLS Client Integration testing, GRPC TLS Junit testing is not supported. */
65     //@Test
66     fun testGrpcTLSServerIntegration() {
67         runBlocking {
68             val tlsAuthGrpcClientService = TLSAuthGrpcClientService(tlsAuthGrpcClientProperties)
69             val grpcChannel = tlsAuthGrpcClientService.channel()
70             /** Get Send and Receive Channel for bidirectional process method*/
71             val (reqChannel, resChannel) = clientCallBidiStreaming(BluePrintProcessingServiceGrpc.getProcessMethod(),
72                     grpcChannel)
73             launch {
74                 resChannel.consumeEach {
75                     log.info("Received Response")
76                     if (it.status.eventType == EventType.EVENT_COMPONENT_EXECUTED) {
77                         resChannel.cancel()
78                     }
79                 }
80             }
81             val request = getRequest("12345")
82             reqChannel.send(request)
83         }
84     }
85
86     private fun getRequest(requestId: String): ExecutionServiceInput {
87         val commonHeader = CommonHeader.newBuilder()
88                 .setTimestamp("2012-04-23T18:25:43.511Z")
89                 .setOriginatorId("System")
90                 .setRequestId(requestId)
91                 .setSubRequestId("$requestId-" + UUID.randomUUID().toString()).build()
92         val actionIdentifier = ActionIdentifiers.newBuilder()
93                 .setActionName("SampleScript")
94                 .setBlueprintName("sample-cba")
95                 .setBlueprintVersion("1.0.0")
96                 .setMode(ACTION_MODE_SYNC)
97                 .build()
98         val jsonContent = """{ "key1" : "value1" }"""
99         val payloadBuilder = ExecutionServiceInput.newBuilder().payloadBuilder
100         JsonFormat.parser().merge(jsonContent, payloadBuilder)
101
102         return ExecutionServiceInput.newBuilder()
103                 .setCommonHeader(commonHeader)
104                 .setActionIdentifiers(actionIdentifier)
105                 .setPayload(payloadBuilder.build())
106                 .build()
107     }
108
109 }