2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
4 * Modifications Copyright © 2019 Huawei.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.fasterxml.jackson.databind.ObjectMapper
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
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"
65 class BluePrintRestLibPropertyServiceTest {
68 lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
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" +
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
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")
95 fun testSSLTokenProperties() {
96 val properties = bluePrintRestLibPropertyService.restClientProperties(
97 "blueprintsprocessor.restclient.ssltoken")
98 assertNotNull(properties, "failed to create property bean")
100 val p: SSLTokenAuthRestClientProperties =
101 properties as SSLTokenAuthRestClientProperties
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")
110 fun testSSLNoAuthProperties() {
111 val properties = bluePrintRestLibPropertyService.restClientProperties(
112 "blueprintsprocessor.restclient.ssl")
113 assertNotNull(properties, "failed to create property bean")
115 val p: SSLRestClientProperties =
116 properties as SSLRestClientProperties
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")
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" +
139 val mapper = ObjectMapper()
140 val actualObj: JsonNode = mapper.readTree(json)
141 val properties = bluePrintRestLibPropertyService.restClientProperties(
143 assertNotNull(properties, "failed to create property bean")
144 val p: SSLBasicAuthRestClientProperties =
145 properties as SSLBasicAuthRestClientProperties
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")
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" +
166 val mapper = ObjectMapper()
167 val actualObj: JsonNode = mapper.readTree(json)
168 val properties = bluePrintRestLibPropertyService.restClientProperties(
170 assertNotNull(properties, "failed to create property bean")
172 val p: SSLTokenAuthRestClientProperties =
173 properties as SSLTokenAuthRestClientProperties
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")
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" +
192 val mapper = ObjectMapper()
193 val actualObj: JsonNode = mapper.readTree(json)
194 val properties = bluePrintRestLibPropertyService.restClientProperties(
196 assertNotNull(properties, "failed to create property bean")
198 val p: SSLRestClientProperties =
199 properties as SSLRestClientProperties
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")
209 fun testBlueprintWebClientService() {
210 val blueprintWebClientService = bluePrintRestLibPropertyService
211 .blueprintWebClientService("sample")
212 assertNotNull(blueprintWebClientService, "failed to create blu" +
213 "eprintWebClientService")
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" +
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")