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.BasicAuthGrpcClientProperties
28 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.BluePrintGrpcLibConfiguration
29 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TLSAuthGrpcClientProperties
30 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TLSAuthGrpcServerProperties
31 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TokenAuthGrpcClientProperties
32 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.test.context.ContextConfiguration
35 import org.springframework.test.context.TestPropertySource
36 import org.springframework.test.context.junit4.SpringRunner
37 import kotlin.test.assertEquals
38 import kotlin.test.assertNotNull
39 import kotlin.test.assertTrue
41 @RunWith(SpringRunner::class)
42 @ContextConfiguration(
43 classes = [BluePrintGrpcLibConfiguration::class,
44 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
48 ["blueprintsprocessor.grpcclient.sample.type=basic-auth",
49 "blueprintsprocessor.grpcclient.sample.host=127.0.0.1",
50 "blueprintsprocessor.grpcclient.sample.port=50505",
51 "blueprintsprocessor.grpcclient.sample.username=sampleuser",
52 "blueprintsprocessor.grpcclient.sample.password=sampleuser",
54 "blueprintsprocessor.grpcclient.token.type=token-auth",
55 "blueprintsprocessor.grpcclient.token.host=127.0.0.1",
56 "blueprintsprocessor.grpcclient.token.port=50505",
57 "blueprintsprocessor.grpcclient.token.username=sampleuser",
58 "blueprintsprocessor.grpcclient.token.password=sampleuser",
60 "blueprintsprocessor.grpcserver.tls-sample.type=tls-auth",
61 "blueprintsprocessor.grpcserver.tls-sample.port=50505",
62 "blueprintsprocessor.grpcserver.tls-sample.certChain=server1.pem",
63 "blueprintsprocessor.grpcserver.tls-sample.privateKey=server1.key",
64 "blueprintsprocessor.grpcserver.tls-sample.trustCertCollection=ca.pem",
66 "blueprintsprocessor.grpcclient.tls-sample.type=tls-auth",
67 "blueprintsprocessor.grpcclient.tls-sample.host=127.0.0.1",
68 "blueprintsprocessor.grpcclient.tls-sample.port=50505",
69 "blueprintsprocessor.grpcclient.tls-sample.trustCertCollection=ca.pem",
70 "blueprintsprocessor.grpcclient.tls-sample.clientCertChain=client.pem",
71 "blueprintsprocessor.grpcclient.tls-sample.clientPrivateKey=client.key"
74 class BluePrintGrpcLibPropertyServiceTest {
77 lateinit var bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService
80 * Tests the GRPC client properties with selector for basic auth.
83 fun testGrpcClientProperties() {
84 val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
85 "blueprintsprocessor.grpcclient.sample"
87 as BasicAuthGrpcClientProperties
88 assertNotNull(properties, "failed to create property bean")
90 properties.host, "failed to get host property" +
94 properties.port, "failed to get host property" +
98 properties.username, "failed to get host pro" +
99 "perty in property bean"
102 properties.password, "failed to get host pr" +
103 "operty in property bean"
108 * Tests the GRPC client properties with JSON node for token auth.
111 fun testGrpcClientPropertiesWithJson() {
112 val json: String = "{\n" +
113 " \"type\" : \"token-auth\",\n" +
114 " \"host\" : \"127.0.0.1\",\n" +
115 " \"port\" : \"50505\"\n" +
117 val mapper = ObjectMapper()
118 val actualObj: JsonNode = mapper.readTree(json)
119 val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
121 ) as TokenAuthGrpcClientProperties
122 assertNotNull(properties, "failed to create property bean")
123 assertEquals(properties.host, "127.0.0.1")
124 assertNotNull(properties.port, "50505")
128 * Tests the GRPC client service with selector for basic auth.
131 fun testGrpcClientServiceBasic() {
132 val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
135 assertTrue(svc is BasicAuthGrpcClientService)
139 * Tests the GRPC client service with selector for token auth.
142 fun testGrpcClientServiceToken() {
143 val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
146 assertTrue(svc is TokenAuthGrpcClientService)
150 * Tests the GRPC client service with JSON node for basic auth.
153 fun testGrpcClientServiceWithJson() {
154 val json: String = "{\n" +
155 " \"type\" : \"basic-auth\",\n" +
156 " \"host\" : \"127.0.0.1\",\n" +
157 " \"port\" : \"50505\",\n" +
158 " \"username\" : \"sampleuser\",\n" +
159 " \"password\" : \"samplepwd\"\n" +
161 val mapper = ObjectMapper()
162 val actualObj: JsonNode = mapper.readTree(json)
163 val svc = bluePrintGrpcLibPropertyService
164 .blueprintGrpcClientService(actualObj)
165 assertTrue(svc is BasicAuthGrpcClientService)
169 fun testGrpcClientTLSProperties() {
170 val properties = bluePrintGrpcLibPropertyService
171 .grpcClientProperties("blueprintsprocessor.grpcclient.tls-sample") as TLSAuthGrpcClientProperties
172 assertNotNull(properties, "failed to create property bean")
173 assertNotNull(properties.host, "failed to get host property in property bean")
174 assertNotNull(properties.port, "failed to get host property in property bean")
175 assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
176 assertNotNull(properties.clientCertChain, "failed to get clientCertChain property in property bean")
177 assertNotNull(properties.clientPrivateKey, "failed to get clientPrivateKey property in property bean")
181 "host" : "localhost",
183 "trustCertCollection" : "server1.pem",
184 "clientCertChain" : "server1.key",
185 "clientPrivateKey" : "ca.pem"
188 val jsonProperties = bluePrintGrpcLibPropertyService
189 .grpcClientProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcClientProperties
190 assertNotNull(jsonProperties, "failed to create property bean from json")
194 fun testGrpcServerTLSProperties() {
195 val properties = bluePrintGrpcLibPropertyService
196 .grpcServerProperties("blueprintsprocessor.grpcserver.tls-sample") as TLSAuthGrpcServerProperties
197 assertNotNull(properties, "failed to create property bean")
198 assertNotNull(properties.port, "failed to get host property in property bean")
199 assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
200 assertNotNull(properties.certChain, "failed to get certChain property in property bean")
201 assertNotNull(properties.privateKey, "failed to get privateKey property in property bean")
206 "certChain" : "server1.pem",
207 "privateKey" : "server1.key",
208 "trustCertCollection" : "ca.pem"
211 val jsonProperties = bluePrintGrpcLibPropertyService
212 .grpcServerProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcServerProperties
213 assertNotNull(jsonProperties, "failed to create property bean from json")
215 val grpcServerService = bluePrintGrpcLibPropertyService.blueprintGrpcServerService("tls-sample")
216 assertNotNull(grpcServerService, "failed to get grpc server service")
217 Assert.assertEquals(TLSAuthGrpcServerService::class.java, grpcServerService.javaClass)