a85dc9ad7457b14e21637e1f961cca7db5c3d5a4
[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.Ignore
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
28 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration
29 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLBasicAuthRestClientProperties
30 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLRestClientProperties
31 import org.onap.ccsdk.cds.blueprintsprocessor.rest.SSLTokenAuthRestClientProperties
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.test.context.ContextConfiguration
34 import org.springframework.test.context.TestPropertySource
35 import org.springframework.test.context.junit4.SpringRunner
36 import kotlin.test.assertEquals
37 import kotlin.test.assertNotNull
38
39 @RunWith(SpringRunner::class)
40 @ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, BlueprintPropertyConfiguration::class, BluePrintProperties::class])
41 @TestPropertySource(properties =
42 ["blueprintsprocessor.restclient.sample.type=basic-auth",
43     "blueprintsprocessor.restclient.sample.url=http://localhost:8080",
44     "blueprintsprocessor.restclient.sample.userId=sampleuser",
45     "blueprintsprocessor.restclient.sslbasic.type=ssl-basic-auth",
46     "blueprintsprocessor.restclient.sslbasic.url=https://localhost:8443",
47     "blueprintsprocessor.restclient.sslbasic.username=admin",
48     "blueprintsprocessor.restclient.sslbasic.password=cds",
49     "blueprintsprocessor.restclient.sslbasic.keyStoreInstance=PKCS12",
50     "blueprintsprocessor.restclient.sslbasic.sslTrust=src/test/resources/keystore.p12",
51     "blueprintsprocessor.restclient.sslbasic.sslTrustPassword=changeit",
52     "blueprintsprocessor.restclient.ssltoken.type=ssl-token-auth",
53     "blueprintsprocessor.restclient.ssltoken.url=https://localhost:8443",
54     "blueprintsprocessor.restclient.ssltoken.token=72178473kjshdkjgvbsdkjv903274908",
55     "blueprintsprocessor.restclient.ssltoken.keyStoreInstance=PKCS12",
56     "blueprintsprocessor.restclient.ssltoken.sslTrust=src/test/resources/keystore.p12",
57     "blueprintsprocessor.restclient.ssltoken.sslTrustPassword=changeit",
58     "blueprintsprocessor.restclient.ssl.type=ssl-no-auth",
59     "blueprintsprocessor.restclient.ssl.url=https://localhost:8443",
60     "blueprintsprocessor.restclient.ssl.keyStoreInstance=PKCS12",
61     "blueprintsprocessor.restclient.ssl.sslTrust=src/test/resources/keystore.p12",
62     "blueprintsprocessor.restclient.ssl.sslTrustPassword=changeit",
63     "blueprintsprocessor.restclient.ssl.sslKey=src/test/resources/keystore.p12",
64     "blueprintsprocessor.restclient.ssl.sslKeyPassword=changeit"
65 ])
66 @Ignore
67 class BluePrintRestLibPropertyServiceTest {
68
69     @Autowired
70     lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
71
72     @Test
73     fun testRestClientProperties() {
74         val properties = bluePrintRestLibPropertyService.restClientProperties(
75                 "blueprintsprocessor.restclient.sample")
76         assertNotNull(properties, "failed to create property bean")
77         assertNotNull(properties.url, "failed to get url property in" +
78                 " property bean")
79     }
80
81     @Test
82     fun testSSLBasicProperties() {
83         val properties = bluePrintRestLibPropertyService.restClientProperties(
84                 "blueprintsprocessor.restclient.sslbasic")
85         assertNotNull(properties, "failed to create property bean")
86         val p: SSLBasicAuthRestClientProperties =
87                 properties as SSLBasicAuthRestClientProperties
88
89         assertEquals(p.basicAuth!!.username, "admin")
90         assertEquals(p.basicAuth!!.password, "cds")
91         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
92         assertEquals(p.sslTrustPassword, "changeit")
93         assertEquals(p.keyStoreInstance, "PKCS12")
94     }
95
96     @Test
97     fun testSSLTokenProperties() {
98         val properties = bluePrintRestLibPropertyService.restClientProperties(
99                 "blueprintsprocessor.restclient.ssltoken")
100         assertNotNull(properties, "failed to create property bean")
101
102         val p: SSLTokenAuthRestClientProperties =
103                 properties as SSLTokenAuthRestClientProperties
104
105         assertEquals(p.tokenAuth!!.token!!, "72178473kjshdkjgvbsdkjv903274908")
106         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
107         assertEquals(p.sslTrustPassword, "changeit")
108         assertEquals(p.keyStoreInstance, "PKCS12")
109     }
110
111     @Test
112     fun testSSLNoAuthProperties() {
113         val properties = bluePrintRestLibPropertyService.restClientProperties(
114                 "blueprintsprocessor.restclient.ssl")
115         assertNotNull(properties, "failed to create property bean")
116
117         val p: SSLRestClientProperties =
118                 properties as SSLRestClientProperties
119
120         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
121         assertEquals(p.sslTrustPassword, "changeit")
122         assertEquals(p.keyStoreInstance, "PKCS12")
123         assertEquals(p.sslKey, "src/test/resources/keystore.p12")
124         assertEquals(p.sslKeyPassword, "changeit")
125     }
126
127
128     @Test
129     fun testSSLBasicPropertiesAsJson() {
130         val json: String = "{\n" +
131                 "  \"type\" : \"ssl-basic-auth\",\n" +
132                 "  \"url\" : \"https://localhost:8443\",\n" +
133                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
134                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
135                 "  \"sslTrustPassword\" : \"changeit\",\n" +
136                 "  \"basicAuth\" : {\n" +
137                 "    \"username\" : \"admin\",\n" +
138                 "    \"password\" : \"cds\"\n" +
139                 "  }\n" +
140                 "}"
141         val mapper = ObjectMapper()
142         val actualObj: JsonNode = mapper.readTree(json)
143         val properties = bluePrintRestLibPropertyService.restClientProperties(
144                 actualObj)
145         assertNotNull(properties, "failed to create property bean")
146         val p: SSLBasicAuthRestClientProperties =
147                 properties as SSLBasicAuthRestClientProperties
148
149         assertEquals(p.basicAuth!!.username, "admin")
150         assertEquals(p.basicAuth!!.password, "cds")
151         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
152         assertEquals(p.sslTrustPassword, "changeit")
153         assertEquals(p.keyStoreInstance, "PKCS12")
154     }
155
156     @Test
157     fun testSSLTokenPropertiesAsJson() {
158         val json: String = "{\n" +
159                 "  \"type\" : \"ssl-token-auth\",\n" +
160                 "  \"url\" : \"https://localhost:8443\",\n" +
161                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
162                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
163                 "  \"sslTrustPassword\" : \"changeit\",\n" +
164                 "  \"tokenAuth\" : {\n" +
165                 "    \"token\" : \"72178473kjshdkjgvbsdkjv903274908\"\n" +
166                 "  }\n" +
167                 "}"
168         val mapper = ObjectMapper()
169         val actualObj: JsonNode = mapper.readTree(json)
170         val properties = bluePrintRestLibPropertyService.restClientProperties(
171                 actualObj)
172         assertNotNull(properties, "failed to create property bean")
173
174         val p: SSLTokenAuthRestClientProperties =
175                 properties as SSLTokenAuthRestClientProperties
176
177         assertEquals(p.tokenAuth!!.token!!, "72178473kjshdkjgvbsdkjv903274908")
178         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
179         assertEquals(p.sslTrustPassword, "changeit")
180         assertEquals(p.keyStoreInstance, "PKCS12")
181     }
182
183     @Test
184     fun testSSLNoAuthPropertiesAsJson() {
185         val json: String = "{\n" +
186                 "  \"type\" : \"ssl-basic-auth\",\n" +
187                 "  \"url\" : \"https://localhost:8443\",\n" +
188                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
189                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
190                 "  \"sslTrustPassword\" : \"changeit\",\n" +
191                 "  \"sslKey\" : \"src/test/resources/keystore.p12\",\n" +
192                 "  \"sslKeyPassword\" : \"changeit\"\n" +
193                 "}"
194         val mapper = ObjectMapper()
195         val actualObj: JsonNode = mapper.readTree(json)
196         val properties = bluePrintRestLibPropertyService.restClientProperties(
197                 actualObj)
198         assertNotNull(properties, "failed to create property bean")
199
200         val p: SSLRestClientProperties =
201                 properties as SSLRestClientProperties
202
203         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
204         assertEquals(p.sslTrustPassword, "changeit")
205         assertEquals(p.keyStoreInstance, "PKCS12")
206         assertEquals(p.sslKey, "src/test/resources/keystore.p12")
207         assertEquals(p.sslKeyPassword, "changeit")
208     }
209
210     @Test
211     fun testBlueprintWebClientService() {
212         val blueprintWebClientService = bluePrintRestLibPropertyService
213                 .blueprintWebClientService("sample")
214         assertNotNull(blueprintWebClientService, "failed to create blu" +
215                 "eprintWebClientService")
216     }
217
218 }
219