Add missing k8s-rb-instance-release-name.json
[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 = [
44         BluePrintGrpcLibConfiguration::class,
45         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class
46     ]
47 )
48 @TestPropertySource(
49     properties =
50         [
51             "blueprintsprocessor.grpcclient.sample.type=basic-auth",
52             "blueprintsprocessor.grpcclient.sample.host=127.0.0.1",
53             "blueprintsprocessor.grpcclient.sample.port=50505",
54             "blueprintsprocessor.grpcclient.sample.username=sampleuser",
55             "blueprintsprocessor.grpcclient.sample.password=sampleuser",
56
57             "blueprintsprocessor.grpcclient.token.type=token-auth",
58             "blueprintsprocessor.grpcclient.token.host=127.0.0.1",
59             "blueprintsprocessor.grpcclient.token.port=50505",
60             "blueprintsprocessor.grpcclient.token.username=sampleuser",
61             "blueprintsprocessor.grpcclient.token.password=sampleuser",
62
63             "blueprintsprocessor.grpcserver.tls-sample.type=tls-auth",
64             "blueprintsprocessor.grpcserver.tls-sample.port=50505",
65             "blueprintsprocessor.grpcserver.tls-sample.certChain=server1.pem",
66             "blueprintsprocessor.grpcserver.tls-sample.privateKey=server1.key",
67             "blueprintsprocessor.grpcserver.tls-sample.trustCertCollection=ca.pem",
68
69             "blueprintsprocessor.grpcclient.tls-sample.type=tls-auth",
70             "blueprintsprocessor.grpcclient.tls-sample.host=127.0.0.1",
71             "blueprintsprocessor.grpcclient.tls-sample.port=50505",
72             "blueprintsprocessor.grpcclient.tls-sample.trustCertCollection=ca.pem",
73             "blueprintsprocessor.grpcclient.tls-sample.clientCertChain=client.pem",
74             "blueprintsprocessor.grpcclient.tls-sample.clientPrivateKey=client.key"
75         ]
76 )
77 class BluePrintGrpcLibPropertyServiceTest {
78
79     @Autowired
80     lateinit var bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService
81
82     /**
83      * Tests the GRPC client properties with selector for basic auth.
84      */
85     @Test
86     fun testGrpcClientProperties() {
87         val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
88             "blueprintsprocessor.grpcclient.sample"
89         )
90             as BasicAuthGrpcClientProperties
91         assertNotNull(properties, "failed to create property bean")
92         assertNotNull(
93             properties.host, "failed to get host property in property bean"
94         )
95         assertNotNull(
96             properties.port, "failed to get host property in property bean"
97         )
98         assertNotNull(
99             properties.username, "failed to get host property in property bean"
100         )
101         assertNotNull(
102             properties.password, "failed to get host property in property bean"
103         )
104     }
105
106     /**
107      * Tests the GRPC client properties with JSON node for token auth.
108      */
109     @Test
110     fun testGrpcClientPropertiesWithJson() {
111         val json: String = "{\n" +
112             "  \"type\" : \"token-auth\",\n" +
113             "  \"host\" : \"127.0.0.1\",\n" +
114             "  \"port\" : \"50505\"\n" +
115             "}"
116         val mapper = ObjectMapper()
117         val actualObj: JsonNode = mapper.readTree(json)
118         val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
119             actualObj
120         ) as TokenAuthGrpcClientProperties
121         assertNotNull(properties, "failed to create property bean")
122         assertEquals(properties.host, "127.0.0.1")
123         assertNotNull(properties.port, "50505")
124     }
125
126     /**
127      * Tests the GRPC client service with selector for basic auth.
128      */
129     @Test
130     fun testGrpcClientServiceBasic() {
131         val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
132             "sample"
133         )
134         assertTrue(svc is BasicAuthGrpcClientService)
135     }
136
137     /**
138      * Tests the GRPC client service with selector for token auth.
139      */
140     @Test
141     fun testGrpcClientServiceToken() {
142         val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
143             "token"
144         )
145         assertTrue(svc is TokenAuthGrpcClientService)
146     }
147
148     /**
149      * Tests the GRPC client service with JSON node for basic auth.
150      */
151     @Test
152     fun testGrpcClientServiceWithJson() {
153         val json: String = "{\n" +
154             "  \"type\" : \"basic-auth\",\n" +
155             "  \"host\" : \"127.0.0.1\",\n" +
156             "  \"port\" : \"50505\",\n" +
157             "  \"username\" : \"sampleuser\",\n" +
158             "  \"password\" : \"samplepwd\"\n" +
159             "}"
160         val mapper = ObjectMapper()
161         val actualObj: JsonNode = mapper.readTree(json)
162         val svc = bluePrintGrpcLibPropertyService
163             .blueprintGrpcClientService(actualObj)
164         assertTrue(svc is BasicAuthGrpcClientService)
165     }
166
167     @Test
168     fun testGrpcClientTLSProperties() {
169         val properties = bluePrintGrpcLibPropertyService
170             .grpcClientProperties("blueprintsprocessor.grpcclient.tls-sample") as TLSAuthGrpcClientProperties
171         assertNotNull(properties, "failed to create property bean")
172         assertNotNull(properties.host, "failed to get host property in property bean")
173         assertNotNull(properties.port, "failed to get host property in property bean")
174         assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
175         assertNotNull(properties.clientCertChain, "failed to get clientCertChain property in property bean")
176         assertNotNull(properties.clientPrivateKey, "failed to get clientPrivateKey property in property bean")
177
178         val configDsl = """{
179             "type" : "tls-auth",
180             "host" : "localhost",
181             "port" : "50505",
182             "trustCertCollection" : "server1.pem",
183             "clientCertChain" : "server1.key",
184             "clientPrivateKey" : "ca.pem"
185             }           
186         """.trimIndent()
187         val jsonProperties = bluePrintGrpcLibPropertyService
188             .grpcClientProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcClientProperties
189         assertNotNull(jsonProperties, "failed to create property bean from json")
190     }
191
192     @Test
193     fun testGrpcServerTLSProperties() {
194         val properties = bluePrintGrpcLibPropertyService
195             .grpcServerProperties("blueprintsprocessor.grpcserver.tls-sample") as TLSAuthGrpcServerProperties
196         assertNotNull(properties, "failed to create property bean")
197         assertNotNull(properties.port, "failed to get host property in property bean")
198         assertNotNull(properties.trustCertCollection, "failed to get trustCertCollection property in property bean")
199         assertNotNull(properties.certChain, "failed to get certChain property in property bean")
200         assertNotNull(properties.privateKey, "failed to get privateKey property in property bean")
201
202         val configDsl = """{
203             "type" : "tls-auth",
204             "port" : "50505",
205             "certChain" : "server1.pem",
206             "privateKey" : "server1.key",
207             "trustCertCollection" : "ca.pem"
208             }           
209         """.trimIndent()
210         val jsonProperties = bluePrintGrpcLibPropertyService
211             .grpcServerProperties(configDsl.jsonAsJsonType()) as TLSAuthGrpcServerProperties
212         assertNotNull(jsonProperties, "failed to create property bean from json")
213
214         val grpcServerService = bluePrintGrpcLibPropertyService.blueprintGrpcServerService("tls-sample")
215         assertNotNull(grpcServerService, "failed to get grpc server service")
216         Assert.assertEquals(TLSAuthGrpcServerService::class.java, grpcServerService.javaClass)
217     }
218 }