37a797f789a78885f2e6a133178e74b13d85c1c1
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
4  * Modifications Copyright © 2019 Huawei.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
20
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties
29 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLRestClientProperties
30 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLTokenAuthRestClientProperties
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.test.context.ContextConfiguration
33 import org.springframework.test.context.TestPropertySource
34 import org.springframework.test.context.junit4.SpringRunner
35 import kotlin.test.assertEquals
36 import kotlin.test.assertNotNull
37
38 @RunWith(SpringRunner::class)
39 @ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, BlueprintPropertyConfiguration::class, BluePrintProperties::class])
40 @TestPropertySource(properties =
41 ["blueprintsprocessor.restclient.sample.type=basic-auth",
42     "blueprintsprocessor.restclient.sample.url=http://localhost:8080",
43     "blueprintsprocessor.restclient.sample.userId=sampleuser",
44     "blueprintsprocessor.restclient.sslbasic.type=ssl-basic-auth",
45     "blueprintsprocessor.restclient.sslbasic.url=https://localhost:8443",
46     "blueprintsprocessor.restclient.sslbasic.username=admin",
47     "blueprintsprocessor.restclient.sslbasic.password=cds",
48     "blueprintsprocessor.restclient.sslbasic.keyStoreInstance=PKCS12",
49     "blueprintsprocessor.restclient.sslbasic.sslTrust=src/test/resources/keystore.p12",
50     "blueprintsprocessor.restclient.sslbasic.sslTrustPassword=changeit",
51     "blueprintsprocessor.restclient.ssltoken.type=ssl-token-auth",
52     "blueprintsprocessor.restclient.ssltoken.url=https://localhost:8443",
53     "blueprintsprocessor.restclient.ssltoken.token=72178473kjshdkjgvbsdkjv903274908",
54     "blueprintsprocessor.restclient.ssltoken.keyStoreInstance=PKCS12",
55     "blueprintsprocessor.restclient.ssltoken.sslTrust=src/test/resources/keystore.p12",
56     "blueprintsprocessor.restclient.ssltoken.sslTrustPassword=changeit",
57     "blueprintsprocessor.restclient.ssl.type=ssl-no-auth",
58     "blueprintsprocessor.restclient.ssl.url=https://localhost:8443",
59     "blueprintsprocessor.restclient.ssl.keyStoreInstance=PKCS12",
60     "blueprintsprocessor.restclient.ssl.sslTrust=src/test/resources/keystore.p12",
61     "blueprintsprocessor.restclient.ssl.sslTrustPassword=changeit",
62     "blueprintsprocessor.restclient.ssl.sslKey=src/test/resources/keystore.p12",
63     "blueprintsprocessor.restclient.ssl.sslKeyPassword=changeit"
64 ])
65 class BluePrintRestLibPropertyServiceTest {
66
67     @Autowired
68     lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
69
70     @Test
71     fun testRestClientProperties() {
72         val properties = bluePrintRestLibPropertyService.restClientProperties(
73                 "blueprintsprocessor.restclient.sample")
74         assertNotNull(properties, "failed to create property bean")
75         assertNotNull(properties.url, "failed to get url property in" +
76                 " property bean")
77     }
78
79     @Test
80     fun testSSLBasicProperties() {
81         val properties = bluePrintRestLibPropertyService.restClientProperties(
82                 "blueprintsprocessor.restclient.sslbasic")
83         assertNotNull(properties, "failed to create property bean")
84         val p: SSLBasicAuthRestClientProperties =
85                 properties as SSLBasicAuthRestClientProperties
86
87         assertEquals(p.basicAuth!!.username, "admin")
88         assertEquals(p.basicAuth!!.password, "cds")
89         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
90         assertEquals(p.sslTrustPassword, "changeit")
91         assertEquals(p.keyStoreInstance, "PKCS12")
92     }
93
94     @Test
95     fun testSSLTokenProperties() {
96         val properties = bluePrintRestLibPropertyService.restClientProperties(
97                 "blueprintsprocessor.restclient.ssltoken")
98         assertNotNull(properties, "failed to create property bean")
99
100         val p: SSLTokenAuthRestClientProperties =
101                 properties as SSLTokenAuthRestClientProperties
102
103         assertEquals(p.tokenAuth!!.token!!, "72178473kjshdkjgvbsdkjv903274908")
104         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
105         assertEquals(p.sslTrustPassword, "changeit")
106         assertEquals(p.keyStoreInstance, "PKCS12")
107     }
108
109     @Test
110     fun testSSLNoAuthProperties() {
111         val properties = bluePrintRestLibPropertyService.restClientProperties(
112                 "blueprintsprocessor.restclient.ssl")
113         assertNotNull(properties, "failed to create property bean")
114
115         val p: SSLRestClientProperties =
116                 properties as SSLRestClientProperties
117
118         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
119         assertEquals(p.sslTrustPassword, "changeit")
120         assertEquals(p.keyStoreInstance, "PKCS12")
121         assertEquals(p.sslKey, "src/test/resources/keystore.p12")
122         assertEquals(p.sslKeyPassword, "changeit")
123     }
124
125
126     @Test
127     fun testSSLBasicPropertiesAsJson() {
128         val json: String = "{\n" +
129                 "  \"type\" : \"ssl-basic-auth\",\n" +
130                 "  \"url\" : \"https://localhost:8443\",\n" +
131                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
132                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
133                 "  \"sslTrustPassword\" : \"changeit\",\n" +
134                 "  \"basicAuth\" : {\n" +
135                 "    \"username\" : \"admin\",\n" +
136                 "    \"password\" : \"cds\"\n" +
137                 "  }\n" +
138                 "}"
139         val mapper = ObjectMapper()
140         val actualObj: JsonNode = mapper.readTree(json)
141         val properties = bluePrintRestLibPropertyService.restClientProperties(
142                 actualObj)
143         assertNotNull(properties, "failed to create property bean")
144         val p: SSLBasicAuthRestClientProperties =
145                 properties as SSLBasicAuthRestClientProperties
146
147         assertEquals(p.basicAuth!!.username, "admin")
148         assertEquals(p.basicAuth!!.password, "cds")
149         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
150         assertEquals(p.sslTrustPassword, "changeit")
151         assertEquals(p.keyStoreInstance, "PKCS12")
152     }
153
154     @Test
155     fun testSSLTokenPropertiesAsJson() {
156         val json: String = "{\n" +
157                 "  \"type\" : \"ssl-token-auth\",\n" +
158                 "  \"url\" : \"https://localhost:8443\",\n" +
159                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
160                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
161                 "  \"sslTrustPassword\" : \"changeit\",\n" +
162                 "  \"tokenAuth\" : {\n" +
163                 "    \"token\" : \"72178473kjshdkjgvbsdkjv903274908\"\n" +
164                 "  }\n" +
165                 "}"
166         val mapper = ObjectMapper()
167         val actualObj: JsonNode = mapper.readTree(json)
168         val properties = bluePrintRestLibPropertyService.restClientProperties(
169                 actualObj)
170         assertNotNull(properties, "failed to create property bean")
171
172         val p: SSLTokenAuthRestClientProperties =
173                 properties as SSLTokenAuthRestClientProperties
174
175         assertEquals(p.tokenAuth!!.token!!, "72178473kjshdkjgvbsdkjv903274908")
176         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
177         assertEquals(p.sslTrustPassword, "changeit")
178         assertEquals(p.keyStoreInstance, "PKCS12")
179     }
180
181     @Test
182     fun testSSLNoAuthPropertiesAsJson() {
183         val json: String = "{\n" +
184                 "  \"type\" : \"ssl-basic-auth\",\n" +
185                 "  \"url\" : \"https://localhost:8443\",\n" +
186                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
187                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
188                 "  \"sslTrustPassword\" : \"changeit\",\n" +
189                 "  \"sslKey\" : \"src/test/resources/keystore.p12\",\n" +
190                 "  \"sslKeyPassword\" : \"changeit\"\n" +
191                 "}"
192         val mapper = ObjectMapper()
193         val actualObj: JsonNode = mapper.readTree(json)
194         val properties = bluePrintRestLibPropertyService.restClientProperties(
195                 actualObj)
196         assertNotNull(properties, "failed to create property bean")
197
198         val p: SSLRestClientProperties =
199                 properties as SSLRestClientProperties
200
201         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
202         assertEquals(p.sslTrustPassword, "changeit")
203         assertEquals(p.keyStoreInstance, "PKCS12")
204         assertEquals(p.sslKey, "src/test/resources/keystore.p12")
205         assertEquals(p.sslKeyPassword, "changeit")
206     }
207
208     @Test
209     fun testBlueprintWebClientService() {
210         val blueprintWebClientService = bluePrintRestLibPropertyService
211                 .blueprintWebClientService("sample")
212         assertNotNull(blueprintWebClientService, "failed to create blu" +
213                 "eprintWebClientService")
214     }
215
216     @Test
217     fun testBlueprintWebClientServiceWithJsonNode() {
218         val json: String = "{\n" +
219                 "  \"type\" : \"ssl-basic-auth\",\n" +
220                 "  \"url\" : \"https://localhost:8443\",\n" +
221                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
222                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
223                 "  \"sslTrustPassword\" : \"changeit\",\n" +
224                 "  \"basicAuth\" : {\n" +
225                 "    \"username\" : \"admin\",\n" +
226                 "    \"password\" : \"cds\"\n" +
227                 "  }\n" +
228                 "}"
229         val mapper = ObjectMapper()
230         val actualObj: JsonNode = mapper.readTree(json)
231         val blueprintWebClientService = bluePrintRestLibPropertyService
232                 .blueprintWebClientService(actualObj)
233         assertNotNull(blueprintWebClientService, "failed to create blu" +
234                 "eprintWebClientService")
235     }
236 }
237