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
23 import org.junit.runner.RunWith
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
26 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.*
27 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.test.context.ContextConfiguration
30 import org.springframework.test.context.TestPropertySource
31 import org.springframework.test.context.junit4.SpringRunner
32 import kotlin.test.assertEquals
33 import kotlin.test.assertNotNull
34 import kotlin.test.assertTrue
36 @RunWith(SpringRunner::class)
37 @ContextConfiguration(classes = [BluePrintGrpcLibConfiguration::class,
38 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class])
39 @TestPropertySource(properties =
40 ["blueprintsprocessor.grpcclient.sample.type=basic-auth",
41 "blueprintsprocessor.grpcclient.sample.host=127.0.0.1",
42 "blueprintsprocessor.grpcclient.sample.port=50505",
43 "blueprintsprocessor.grpcclient.sample.username=sampleuser",
44 "blueprintsprocessor.grpcclient.sample.password=sampleuser",
46 "blueprintsprocessor.grpcclient.token.type=token-auth",
47 "blueprintsprocessor.grpcclient.token.host=127.0.0.1",
48 "blueprintsprocessor.grpcclient.token.port=50505",
49 "blueprintsprocessor.grpcclient.token.username=sampleuser",
50 "blueprintsprocessor.grpcclient.token.password=sampleuser",
52 "blueprintsprocessor.grpcserver.tls-sample.type=tls-auth",
53 "blueprintsprocessor.grpcserver.tls-sample.port=50505",
54 "blueprintsprocessor.grpcserver.tls-sample.certChain=server1.pem",
55 "blueprintsprocessor.grpcserver.tls-sample.privateKey=server1.key",
56 "blueprintsprocessor.grpcserver.tls-sample.trustCertCollection=ca.pem",
58 "blueprintsprocessor.grpcclient.tls-sample.type=tls-auth",
59 "blueprintsprocessor.grpcclient.tls-sample.host=127.0.0.1",
60 "blueprintsprocessor.grpcclient.tls-sample.port=50505",
61 "blueprintsprocessor.grpcclient.tls-sample.trustCertCollection=ca.pem",
62 "blueprintsprocessor.grpcclient.tls-sample.clientCertChain=client.pem",
63 "blueprintsprocessor.grpcclient.tls-sample.clientPrivateKey=client.key"
65 class BluePrintGrpcLibPropertyServiceTest {
68 lateinit var bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService
71 * Tests the GRPC client properties with selector for basic auth.
74 fun testGrpcClientProperties() {
75 val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
76 "blueprintsprocessor.grpcclient.sample")
77 as BasicAuthGrpcClientProperties
78 assertNotNull(properties, "failed to create property bean")
79 assertNotNull(properties.host, "failed to get host property" +
81 assertNotNull(properties.port, "failed to get host property" +
83 assertNotNull(properties.username, "failed to get host pro" +
84 "perty in property bean")
85 assertNotNull(properties.password, "failed to get host pr" +
86 "operty in property bean")
90 * Tests the GRPC client properties with JSON node for token auth.
93 fun testGrpcClientPropertiesWithJson() {
94 val json: String = "{\n" +
95 " \"type\" : \"token-auth\",\n" +
96 " \"host\" : \"127.0.0.1\",\n" +
97 " \"port\" : \"50505\"\n" +
99 val mapper = ObjectMapper()
100 val actualObj: JsonNode = mapper.readTree(json)
101 val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
102 actualObj) as TokenAuthGrpcClientProperties
103 assertNotNull(properties, "failed to create property bean")
104 assertEquals(properties.host, "127.0.0.1")
105 assertNotNull(properties.port, "50505")
109 * Tests the GRPC client service with selector for basic auth.
112 fun testGrpcClientServiceBasic() {
113 val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
115 assertTrue(svc is BasicAuthGrpcClientService)
119 * Tests the GRPC client service with selector for token auth.
122 fun testGrpcClientServiceToken() {
123 val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
125 assertTrue(svc is TokenAuthGrpcClientService)
129 * Tests the GRPC client service with JSON node for basic auth.
132 fun testGrpcClientServiceWithJson() {
133 val json: String = "{\n" +
134 " \"type\" : \"basic-auth\",\n" +
135 " \"host\" : \"127.0.0.1\",\n" +
136 " \"port\" : \"50505\",\n" +
137 " \"username\" : \"sampleuser\",\n" +
138 " \"password\" : \"samplepwd\"\n" +
140 val mapper = ObjectMapper()
141 val actualObj: JsonNode = mapper.readTree(json)
142 val svc = bluePrintGrpcLibPropertyService
143 .blueprintGrpcClientService(actualObj)
144 assertTrue(svc is BasicAuthGrpcClientService)
148 fun testGrpcClientTLSProperties() {
149 val properties = bluePrintGrpcLibPropertyService
150 .grpcClientProperties("blueprintsprocessor.grpcclient.tls-sample") as TLSAuthGrpcClientProperties
151 assertNotNull(properties, "failed to create property bean")
152 assertNotNull(properties.host, "failed to get host property in property bean")
153 assertNotNull(properties.port, "failed to get host property in property bean")
154 assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
155 assertNotNull(properties.clientCertChain, "failed to get clientCertChain property in property bean")
156 assertNotNull(properties.clientPrivateKey, "failed to get clientPrivateKey property in property bean")
160 "host" : "localhost",
162 "trustCertCollection" : "server1.pem",
163 "clientCertChain" : "server1.key",
164 "clientPrivateKey" : "ca.pem"
167 val jsonProperties = bluePrintGrpcLibPropertyService
168 .grpcClientProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcClientProperties
169 assertNotNull(jsonProperties, "failed to create property bean from json")
173 fun testGrpcServerTLSProperties() {
174 val properties = bluePrintGrpcLibPropertyService
175 .grpcServerProperties("blueprintsprocessor.grpcserver.tls-sample") as TLSAuthGrpcServerProperties
176 assertNotNull(properties, "failed to create property bean")
177 assertNotNull(properties.port, "failed to get host property in property bean")
178 assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
179 assertNotNull(properties.certChain, "failed to get certChain property in property bean")
180 assertNotNull(properties.privateKey, "failed to get privateKey property in property bean")
185 "certChain" : "server1.pem",
186 "privateKey" : "server1.key",
187 "trustCertCollection" : "ca.pem"
190 val jsonProperties = bluePrintGrpcLibPropertyService
191 .grpcServerProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcServerProperties
192 assertNotNull(jsonProperties, "failed to create property bean from json")