2 * Copyright © 2018-2019 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
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
34 import kotlin.test.Test
35 import kotlin.test.assertNotNull
37 class BluePrintGrpcServerTest {
39 private val tlsAuthGrpcServerProperties = TLSAuthGrpcServerProperties().apply {
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"
46 private val tlsAuthGrpcClientProperties = TLSAuthGrpcClientProperties().apply {
49 type = GRPCLibConstants.TYPE_TLS_AUTH
50 trustCertCollection = "src/test/resources/tls-manual/py-executor-chain.pem"
54 fun testGrpcTLSContext() {
55 val tlsAuthGrpcServerService = TLSAuthGrpcServerService(tlsAuthGrpcServerProperties)
56 val sslContext = tlsAuthGrpcServerService.sslContext()
57 assertNotNull(sslContext, "failed to create grpc server ssl context")
59 val tlsAuthGrpcClientService = TLSAuthGrpcClientService(tlsAuthGrpcClientProperties)
60 val clientSslContext = tlsAuthGrpcClientService.sslContext()
61 assertNotNull(clientSslContext, "failed to create grpc client ssl context")
64 /** TLS Client Integration testing, GRPC TLS Junit testing is not supported. */
66 fun testGrpcTLSServerIntegration() {
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(),
76 resChannel.consumeEach {
77 log.info("Received Response")
78 if (it.status.eventType == EventType.EVENT_COMPONENT_EXECUTED) {
83 val request = getRequest("12345")
84 reqChannel.send(request)
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)
100 val jsonContent = """{ "key1" : "value1" }"""
101 val payloadBuilder = ExecutionServiceInput.newBuilder().payloadBuilder
102 JsonFormat.parser().merge(jsonContent, payloadBuilder)
104 return ExecutionServiceInput.newBuilder()
105 .setCommonHeader(commonHeader)
106 .setActionIdentifiers(actionIdentifier)
107 .setPayload(payloadBuilder.build())