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