Fixing Blueprint Typo's and docs
[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.UUID
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(
72                 BlueprintProcessingServiceGrpc.getProcessMethod(),
73                 grpcChannel
74             )
75             launch {
76                 resChannel.consumeEach {
77                     log.info("Received Response")
78                     if (it.status.eventType == EventType.EVENT_COMPONENT_EXECUTED) {
79                         resChannel.cancel()
80                     }
81                 }
82             }
83             val request = getRequest("12345")
84             reqChannel.send(request)
85         }
86     }
87
88     private fun getRequest(requestId: String): ExecutionServiceInput {
89         val commonHeader = CommonHeader.newBuilder()
90             .setTimestamp("2012-04-23T18:25:43.511Z")
91             .setOriginatorId("System")
92             .setRequestId(requestId)
93             .setSubRequestId("$requestId-" + UUID.randomUUID().toString()).build()
94         val actionIdentifier = ActionIdentifiers.newBuilder()
95             .setActionName("SampleScript")
96             .setBlueprintName("sample-cba")
97             .setBlueprintVersion("1.0.0")
98             .setMode(ACTION_MODE_SYNC)
99             .build()
100         val jsonContent = """{ "key1" : "value1" }"""
101         val payloadBuilder = ExecutionServiceInput.newBuilder().payloadBuilder
102         JsonFormat.parser().merge(jsonContent, payloadBuilder)
103
104         return ExecutionServiceInput.newBuilder()
105             .setCommonHeader(commonHeader)
106             .setActionIdentifiers(actionIdentifier)
107             .setPayload(payloadBuilder.build())
108             .build()
109     }
110 }