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