8df218fe9084a489859d955d5af900c5e9a65874
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import org.junit.Test
22 import org.junit.runner.RunWith
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
25 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.BasicAuthGrpcClientProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.BluePrintGrpcLibConfiguration
27 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TokenAuthGrpcClientProperties
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     "blueprintsprocessor.grpcclient.token.type=token-auth",
46     "blueprintsprocessor.grpcclient.token.host=127.0.0.1",
47     "blueprintsprocessor.grpcclient.token.port=50505",
48     "blueprintsprocessor.grpcclient.token.username=sampleuser",
49     "blueprintsprocessor.grpcclient.token.password=sampleuser"
50 ])
51 class BluePrintGrpcLibPropertyServiceTest {
52
53     @Autowired
54     lateinit var bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService
55
56     /**
57      * Tests the GRPC client properties with selector for basic auth.
58      */
59     @Test
60     fun testGrpcClientProperties() {
61         val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
62                 "blueprintsprocessor.grpcclient.sample")
63                 as BasicAuthGrpcClientProperties
64         assertNotNull(properties, "failed to create property bean")
65         assertNotNull(properties.host, "failed to get host property" +
66                 " in property bean")
67         assertNotNull(properties.port, "failed to get host property" +
68                 " in property bean")
69         assertNotNull(properties.username, "failed to get host pro" +
70                 "perty in property bean")
71         assertNotNull(properties.password, "failed to get host pr" +
72                 "operty in property bean")
73     }
74
75     /**
76      * Tests the GRPC client properties with JSON node for token auth.
77      */
78     @Test
79     fun testGrpcClientPropertiesWithJson() {
80         val json: String = "{\n" +
81                 "  \"type\" : \"token-auth\",\n" +
82                 "  \"host\" : \"127.0.0.1\",\n" +
83                 "  \"port\" : \"50505\"\n" +
84                 "}"
85         val mapper = ObjectMapper()
86         val actualObj: JsonNode = mapper.readTree(json)
87         val properties = bluePrintGrpcLibPropertyService.grpcClientProperties(
88                 actualObj) as TokenAuthGrpcClientProperties
89         assertNotNull(properties, "failed to create property bean")
90         assertEquals(properties.host, "127.0.0.1")
91         assertNotNull(properties.port, "50505")
92     }
93
94     /**
95      * Tests the GRPC client service with selector for basic auth.
96      */
97     @Test
98     fun testGrpcClientServiceBasic() {
99         val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
100                 "sample")
101         assertTrue(svc is BasicAuthGrpcClientService)
102     }
103
104     /**
105      * Tests the GRPC client service with selector for token auth.
106      */
107     @Test
108     fun testGrpcClientServiceToken() {
109         val svc = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(
110                 "token")
111         assertTrue(svc is TokenAuthGrpcClientService)
112     }
113
114     /**
115      * Tests the GRPC client service with JSON node for basic auth.
116      */
117     @Test
118     fun testGrpcClientServiceWithJson() {
119         val json: String = "{\n" +
120                 "  \"type\" : \"basic-auth\",\n" +
121                 "  \"host\" : \"127.0.0.1\",\n" +
122                 "  \"port\" : \"50505\",\n" +
123                 "  \"username\" : \"sampleuser\",\n" +
124                 "  \"password\" : \"samplepwd\"\n" +
125                 "}"
126         val mapper = ObjectMapper()
127         val actualObj: JsonNode = mapper.readTree(json)
128         val svc = bluePrintGrpcLibPropertyService
129                 .blueprintGrpcClientService(actualObj)
130         assertTrue(svc is BasicAuthGrpcClientService)
131     }
132 }