Upgrade CDS GRPC implementations.
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / grpc-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / grpc / service / BluePrintGrpcLibPropertyServiceTest.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *  Modifications Copyright © 2018-2019 AT&T Intellectual Property.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.ObjectMapper
22 import org.junit.Assert
23 import org.junit.Test
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
40
41 @RunWith(SpringRunner::class)
42 @ContextConfiguration(
43     classes = [BluePrintGrpcLibConfiguration::class,
44         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
45 )
46 @TestPropertySource(
47     properties =
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",
53
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",
59
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",
65
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"
72     ]
73 )
74 class BluePrintGrpcLibPropertyServiceTest {
75
76     @Autowired
77     lateinit var bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService
78
79     /**
80      * Tests the GRPC client properties with selector for basic auth.
81      */
82     @Test
83     fun testGrpcClientProperties() {
84         val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
85             "blueprintsprocessor.grpcclient.sample"
86         )
87             as BasicAuthGrpcClientProperties
88         assertNotNull(properties, "failed to create property bean")
89         assertNotNull(
90             properties.host, "failed to get host property in property bean"
91         )
92         assertNotNull(
93             properties.port, "failed to get host property in property bean"
94         )
95         assertNotNull(
96             properties.username, "failed to get host property in property bean"
97         )
98         assertNotNull(
99             properties.password, "failed to get host property in property bean"
100         )
101     }
102
103     /**
104      * Tests the GRPC client properties with JSON node for token auth.
105      */
106     @Test
107     fun testGrpcClientPropertiesWithJson() {
108         val json: String = "{\n" +
109             "  \"type\" : \"token-auth\",\n" +
110             "  \"host\" : \"127.0.0.1\",\n" +
111             "  \"port\" : \"50505\"\n" +
112             "}"
113         val mapper = ObjectMapper()
114         val actualObj: JsonNode = mapper.readTree(json)
115         val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
116             actualObj
117         ) as TokenAuthGrpcClientProperties
118         assertNotNull(properties, "failed to create property bean")
119         assertEquals(properties.host, "127.0.0.1")
120         assertNotNull(properties.port, "50505")
121     }
122
123     /**
124      * Tests the GRPC client service with selector for basic auth.
125      */
126     @Test
127     fun testGrpcClientServiceBasic() {
128         val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
129             "sample"
130         )
131         assertTrue(svc is BasicAuthGrpcClientService)
132     }
133
134     /**
135      * Tests the GRPC client service with selector for token auth.
136      */
137     @Test
138     fun testGrpcClientServiceToken() {
139         val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
140             "token"
141         )
142         assertTrue(svc is TokenAuthGrpcClientService)
143     }
144
145     /**
146      * Tests the GRPC client service with JSON node for basic auth.
147      */
148     @Test
149     fun testGrpcClientServiceWithJson() {
150         val json: String = "{\n" +
151             "  \"type\" : \"basic-auth\",\n" +
152             "  \"host\" : \"127.0.0.1\",\n" +
153             "  \"port\" : \"50505\",\n" +
154             "  \"username\" : \"sampleuser\",\n" +
155             "  \"password\" : \"samplepwd\"\n" +
156             "}"
157         val mapper = ObjectMapper()
158         val actualObj: JsonNode = mapper.readTree(json)
159         val svc = bluePrintGrpcLibPropertyService
160             .blueprintGrpcClientService(actualObj)
161         assertTrue(svc is BasicAuthGrpcClientService)
162     }
163
164     @Test
165     fun testGrpcClientTLSProperties() {
166         val properties = bluePrintGrpcLibPropertyService
167             .grpcClientProperties("blueprintsprocessor.grpcclient.tls-sample") as TLSAuthGrpcClientProperties
168         assertNotNull(properties, "failed to create property bean")
169         assertNotNull(properties.host, "failed to get host property in property bean")
170         assertNotNull(properties.port, "failed to get host property in property bean")
171         assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
172         assertNotNull(properties.clientCertChain, "failed to get clientCertChain property in property bean")
173         assertNotNull(properties.clientPrivateKey, "failed to get clientPrivateKey property in property bean")
174
175         val configDsl = """{
176             "type" : "tls-auth",
177             "host" : "localhost",
178             "port" : "50505",
179             "trustCertCollection" : "server1.pem",
180             "clientCertChain" : "server1.key",
181             "clientPrivateKey" : "ca.pem"
182             }           
183         """.trimIndent()
184         val jsonProperties = bluePrintGrpcLibPropertyService
185             .grpcClientProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcClientProperties
186         assertNotNull(jsonProperties, "failed to create property bean from json")
187     }
188
189     @Test
190     fun testGrpcServerTLSProperties() {
191         val properties = bluePrintGrpcLibPropertyService
192             .grpcServerProperties("blueprintsprocessor.grpcserver.tls-sample") as TLSAuthGrpcServerProperties
193         assertNotNull(properties, "failed to create property bean")
194         assertNotNull(properties.port, "failed to get host property in property bean")
195         assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
196         assertNotNull(properties.certChain, "failed to get certChain property in property bean")
197         assertNotNull(properties.privateKey, "failed to get privateKey property in property bean")
198
199         val configDsl = """{
200             "type" : "tls-auth",
201             "port" : "50505",
202             "certChain" : "server1.pem",
203             "privateKey" : "server1.key",
204             "trustCertCollection" : "ca.pem"
205             }           
206         """.trimIndent()
207         val jsonProperties = bluePrintGrpcLibPropertyService
208             .grpcServerProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcServerProperties
209         assertNotNull(jsonProperties, "failed to create property bean from json")
210
211         val grpcServerService = bluePrintGrpcLibPropertyService.blueprintGrpcServerService("tls-sample")
212         assertNotNull(grpcServerService, "failed to get grpc server service")
213         Assert.assertEquals(TLSAuthGrpcServerService::class.java, grpcServerService.javaClass)
214     }
215 }