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.processing.api.BluePrintProcessingServiceGrpc
31 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
33 import kotlin.test.Test
34 import kotlin.test.assertNotNull
36 class BluePrintGrpcServerTest {
38 private val tlsAuthGrpcServerProperties = TLSAuthGrpcServerProperties().apply {
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"
45 private val tlsAuthGrpcClientProperties = TLSAuthGrpcClientProperties().apply {
48 type = GRPCLibConstants.TYPE_TLS_AUTH
49 trustCertCollection = "src/test/resources/tls-manual/my-public-key-cert.pem"
53 fun testGrpcTLSContext() {
54 val tlsAuthGrpcServerService = TLSAuthGrpcServerService(tlsAuthGrpcServerProperties)
55 val sslContext = tlsAuthGrpcServerService.sslContext()
56 assertNotNull(sslContext, "failed to create grpc server ssl context")
58 val tlsAuthGrpcClientService = TLSAuthGrpcClientService(tlsAuthGrpcClientProperties)
59 val clientSslContext = tlsAuthGrpcClientService.sslContext()
60 assertNotNull(clientSslContext, "failed to create grpc client ssl context")
63 /** TLS Client Integration testing, GRPC TLS Junit testing is not supported. */
65 fun testGrpcTLSServerIntegration() {
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(),
73 resChannel.consumeEach {
74 log.info("Received Response")
77 val request = getRequest("12345")
78 reqChannel.send(request)
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)
94 val jsonContent = """{ "key1" : "value1" }"""
95 val payloadBuilder = ExecutionServiceInput.newBuilder().payloadBuilder
96 JsonFormat.parser().merge(jsonContent, payloadBuilder)
98 return ExecutionServiceInput.newBuilder()
99 .setCommonHeader(commonHeader)
100 .setActionIdentifiers(actionIdentifier)
101 .setPayload(payloadBuilder.build())