Add grpc TLS property lib services.
[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.Test
23 import org.junit.runner.RunWith
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
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
35
36 @RunWith(SpringRunner::class)
37 @ContextConfiguration(classes = [BluePrintGrpcLibConfiguration::class,
38     BlueprintPropertyConfiguration::class, BluePrintProperties::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",
45
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",
51
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",
57
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"
64 ])
65 class BluePrintGrpcLibPropertyServiceTest {
66
67     @Autowired
68     lateinit var bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService
69
70     /**
71      * Tests the GRPC client properties with selector for basic auth.
72      */
73     @Test
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" +
80                 " in property bean")
81         assertNotNull(properties.port, "failed to get host property" +
82                 " in property bean")
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")
87     }
88
89     /**
90      * Tests the GRPC client properties with JSON node for token auth.
91      */
92     @Test
93     fun testGrpcClientPropertiesWithJson() {
94         val json: String = "{\n" +
95                 "  \"type\" : \"token-auth\",\n" +
96                 "  \"host\" : \"127.0.0.1\",\n" +
97                 "  \"port\" : \"50505\"\n" +
98                 "}"
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")
106     }
107
108     /**
109      * Tests the GRPC client service with selector for basic auth.
110      */
111     @Test
112     fun testGrpcClientServiceBasic() {
113         val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
114                 "sample")
115         assertTrue(svc is BasicAuthGrpcClientService)
116     }
117
118     /**
119      * Tests the GRPC client service with selector for token auth.
120      */
121     @Test
122     fun testGrpcClientServiceToken() {
123         val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
124                 "token")
125         assertTrue(svc is TokenAuthGrpcClientService)
126     }
127
128     /**
129      * Tests the GRPC client service with JSON node for basic auth.
130      */
131     @Test
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" +
139                 "}"
140         val mapper = ObjectMapper()
141         val actualObj: JsonNode = mapper.readTree(json)
142         val svc = bluePrintGrpcLibPropertyService
143                 .blueprintGrpcClientService(actualObj)
144         assertTrue(svc is BasicAuthGrpcClientService)
145     }
146
147     @Test
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")
157
158         val configDsl = """{
159             "type" : "tls-auth",
160             "host" : "localhost",
161             "port" : "50505",
162             "trustCertCollection" : "server1.pem",
163             "clientCertChain" : "server1.key",
164             "clientPrivateKey" : "ca.pem"
165             }           
166         """.trimIndent()
167         val jsonProperties = bluePrintGrpcLibPropertyService
168                 .grpcClientProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcClientProperties
169         assertNotNull(jsonProperties, "failed to create property bean from json")
170     }
171
172     @Test
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")
181
182         val configDsl = """{
183             "type" : "tls-auth",
184             "port" : "50505",
185             "certChain" : "server1.pem",
186             "privateKey" : "server1.key",
187             "trustCertCollection" : "ca.pem"
188             }           
189         """.trimIndent()
190         val jsonProperties = bluePrintGrpcLibPropertyService
191                 .grpcServerProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcServerProperties
192         assertNotNull(jsonProperties, "failed to create property bean from json")
193     }
194 }