2 * Copyright © 2019 IBM.
3 * Modifications Copyright © 2018-2019 AT&T Intellectual Property.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.ObjectMapper
22 import org.junit.Assert
24 import org.junit.runner.RunWith
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
27 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.*
28 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.test.context.ContextConfiguration
31 import org.springframework.test.context.TestPropertySource
32 import org.springframework.test.context.junit4.SpringRunner
33 import kotlin.test.assertEquals
34 import kotlin.test.assertNotNull
35 import kotlin.test.assertTrue
37 @RunWith(SpringRunner::class)
38 @ContextConfiguration(classes = [BluePrintGrpcLibConfiguration::class,
39 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class])
40 @TestPropertySource(properties =
41 ["blueprintsprocessor.grpcclient.sample.type=basic-auth",
42 "blueprintsprocessor.grpcclient.sample.host=127.0.0.1",
43 "blueprintsprocessor.grpcclient.sample.port=50505",
44 "blueprintsprocessor.grpcclient.sample.username=sampleuser",
45 "blueprintsprocessor.grpcclient.sample.password=sampleuser",
47 "blueprintsprocessor.grpcclient.token.type=token-auth",
48 "blueprintsprocessor.grpcclient.token.host=127.0.0.1",
49 "blueprintsprocessor.grpcclient.token.port=50505",
50 "blueprintsprocessor.grpcclient.token.username=sampleuser",
51 "blueprintsprocessor.grpcclient.token.password=sampleuser",
53 "blueprintsprocessor.grpcserver.tls-sample.type=tls-auth",
54 "blueprintsprocessor.grpcserver.tls-sample.port=50505",
55 "blueprintsprocessor.grpcserver.tls-sample.certChain=server1.pem",
56 "blueprintsprocessor.grpcserver.tls-sample.privateKey=server1.key",
57 "blueprintsprocessor.grpcserver.tls-sample.trustCertCollection=ca.pem",
59 "blueprintsprocessor.grpcclient.tls-sample.type=tls-auth",
60 "blueprintsprocessor.grpcclient.tls-sample.host=127.0.0.1",
61 "blueprintsprocessor.grpcclient.tls-sample.port=50505",
62 "blueprintsprocessor.grpcclient.tls-sample.trustCertCollection=ca.pem",
63 "blueprintsprocessor.grpcclient.tls-sample.clientCertChain=client.pem",
64 "blueprintsprocessor.grpcclient.tls-sample.clientPrivateKey=client.key"
66 class BluePrintGrpcLibPropertyServiceTest {
69 lateinit var bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService
72 * Tests the GRPC client properties with selector for basic auth.
75 fun testGrpcClientProperties() {
76 val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
77 "blueprintsprocessor.grpcclient.sample")
78 as BasicAuthGrpcClientProperties
79 assertNotNull(properties, "failed to create property bean")
80 assertNotNull(properties.host, "failed to get host property" +
82 assertNotNull(properties.port, "failed to get host property" +
84 assertNotNull(properties.username, "failed to get host pro" +
85 "perty in property bean")
86 assertNotNull(properties.password, "failed to get host pr" +
87 "operty in property bean")
91 * Tests the GRPC client properties with JSON node for token auth.
94 fun testGrpcClientPropertiesWithJson() {
95 val json: String = "{\n" +
96 " \"type\" : \"token-auth\",\n" +
97 " \"host\" : \"127.0.0.1\",\n" +
98 " \"port\" : \"50505\"\n" +
100 val mapper = ObjectMapper()
101 val actualObj: JsonNode = mapper.readTree(json)
102 val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
103 actualObj) as TokenAuthGrpcClientProperties
104 assertNotNull(properties, "failed to create property bean")
105 assertEquals(properties.host, "127.0.0.1")
106 assertNotNull(properties.port, "50505")
110 * Tests the GRPC client service with selector for basic auth.
113 fun testGrpcClientServiceBasic() {
114 val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
116 assertTrue(svc is BasicAuthGrpcClientService)
120 * Tests the GRPC client service with selector for token auth.
123 fun testGrpcClientServiceToken() {
124 val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
126 assertTrue(svc is TokenAuthGrpcClientService)
130 * Tests the GRPC client service with JSON node for basic auth.
133 fun testGrpcClientServiceWithJson() {
134 val json: String = "{\n" +
135 " \"type\" : \"basic-auth\",\n" +
136 " \"host\" : \"127.0.0.1\",\n" +
137 " \"port\" : \"50505\",\n" +
138 " \"username\" : \"sampleuser\",\n" +
139 " \"password\" : \"samplepwd\"\n" +
141 val mapper = ObjectMapper()
142 val actualObj: JsonNode = mapper.readTree(json)
143 val svc = bluePrintGrpcLibPropertyService
144 .blueprintGrpcClientService(actualObj)
145 assertTrue(svc is BasicAuthGrpcClientService)
149 fun testGrpcClientTLSProperties() {
150 val properties = bluePrintGrpcLibPropertyService
151 .grpcClientProperties("blueprintsprocessor.grpcclient.tls-sample") as TLSAuthGrpcClientProperties
152 assertNotNull(properties, "failed to create property bean")
153 assertNotNull(properties.host, "failed to get host property in property bean")
154 assertNotNull(properties.port, "failed to get host property in property bean")
155 assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
156 assertNotNull(properties.clientCertChain, "failed to get clientCertChain property in property bean")
157 assertNotNull(properties.clientPrivateKey, "failed to get clientPrivateKey property in property bean")
161 "host" : "localhost",
163 "trustCertCollection" : "server1.pem",
164 "clientCertChain" : "server1.key",
165 "clientPrivateKey" : "ca.pem"
168 val jsonProperties = bluePrintGrpcLibPropertyService
169 .grpcClientProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcClientProperties
170 assertNotNull(jsonProperties, "failed to create property bean from json")
174 fun testGrpcServerTLSProperties() {
175 val properties = bluePrintGrpcLibPropertyService
176 .grpcServerProperties("blueprintsprocessor.grpcserver.tls-sample") as TLSAuthGrpcServerProperties
177 assertNotNull(properties, "failed to create property bean")
178 assertNotNull(properties.port, "failed to get host property in property bean")
179 assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
180 assertNotNull(properties.certChain, "failed to get certChain property in property bean")
181 assertNotNull(properties.privateKey, "failed to get privateKey property in property bean")
186 "certChain" : "server1.pem",
187 "privateKey" : "server1.key",
188 "trustCertCollection" : "ca.pem"
191 val jsonProperties = bluePrintGrpcLibPropertyService
192 .grpcServerProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcServerProperties
193 assertNotNull(jsonProperties, "failed to create property bean from json")
195 val grpcServerService = bluePrintGrpcLibPropertyService.blueprintGrpcServerService("tls-sample")
196 assertNotNull(grpcServerService, "failed to get grpc server service")
197 Assert.assertEquals(TLSAuthGrpcServerService::class.java, grpcServerService.javaClass)