Merge "Complementary Junit test coverage"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / rest-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / rest / service / BluePrintRestLibPropertyServiceTest.kt
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
66 class BluePrintRestLibPropertyServiceTest {
67
68     @Autowired
69     lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
70
71     @Test
72     fun testRestClientProperties() {
73         val properties = bluePrintRestLibPropertyService.restClientProperties(
74                 "blueprintsprocessor.restclient.sample")
75         assertNotNull(properties, "failed to create property bean")
76         assertNotNull(properties.url, "failed to get url property in" +
77                 " property bean")
78     }
79
80     @Test
81     fun testSSLBasicProperties() {
82         val properties = bluePrintRestLibPropertyService.restClientProperties(
83                 "blueprintsprocessor.restclient.sslbasic")
84         assertNotNull(properties, "failed to create property bean")
85         val p: SSLBasicAuthRestClientProperties =
86                 properties as SSLBasicAuthRestClientProperties
87
88         assertEquals(p.basicAuth!!.username, "admin")
89         assertEquals(p.basicAuth!!.password, "cds")
90         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
91         assertEquals(p.sslTrustPassword, "changeit")
92         assertEquals(p.keyStoreInstance, "PKCS12")
93     }
94
95     @Test
96     fun testSSLTokenProperties() {
97         val properties = bluePrintRestLibPropertyService.restClientProperties(
98                 "blueprintsprocessor.restclient.ssltoken")
99         assertNotNull(properties, "failed to create property bean")
100
101         val p: SSLTokenAuthRestClientProperties =
102                 properties as SSLTokenAuthRestClientProperties
103
104         assertEquals(p.tokenAuth!!.token!!, "72178473kjshdkjgvbsdkjv903274908")
105         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
106         assertEquals(p.sslTrustPassword, "changeit")
107         assertEquals(p.keyStoreInstance, "PKCS12")
108     }
109
110     @Test
111     fun testSSLNoAuthProperties() {
112         val properties = bluePrintRestLibPropertyService.restClientProperties(
113                 "blueprintsprocessor.restclient.ssl")
114         assertNotNull(properties, "failed to create property bean")
115
116         val p: SSLRestClientProperties =
117                 properties as SSLRestClientProperties
118
119         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
120         assertEquals(p.sslTrustPassword, "changeit")
121         assertEquals(p.keyStoreInstance, "PKCS12")
122         assertEquals(p.sslKey, "src/test/resources/keystore.p12")
123         assertEquals(p.sslKeyPassword, "changeit")
124     }
125
126
127     @Test
128     fun testSSLBasicPropertiesAsJson() {
129         val json: String = "{\n" +
130                 "  \"type\" : \"ssl-basic-auth\",\n" +
131                 "  \"url\" : \"https://localhost:8443\",\n" +
132                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
133                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
134                 "  \"sslTrustPassword\" : \"changeit\",\n" +
135                 "  \"basicAuth\" : {\n" +
136                 "    \"username\" : \"admin\",\n" +
137                 "    \"password\" : \"cds\"\n" +
138                 "  }\n" +
139                 "}"
140         val mapper = ObjectMapper()
141         val actualObj: JsonNode = mapper.readTree(json)
142         val properties = bluePrintRestLibPropertyService.restClientProperties(
143                 actualObj)
144         assertNotNull(properties, "failed to create property bean")
145         val p: SSLBasicAuthRestClientProperties =
146                 properties as SSLBasicAuthRestClientProperties
147
148         assertEquals(p.basicAuth!!.username, "admin")
149         assertEquals(p.basicAuth!!.password, "cds")
150         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
151         assertEquals(p.sslTrustPassword, "changeit")
152         assertEquals(p.keyStoreInstance, "PKCS12")
153     }
154
155     @Test
156     fun testSSLTokenPropertiesAsJson() {
157         val json: String = "{\n" +
158                 "  \"type\" : \"ssl-token-auth\",\n" +
159                 "  \"url\" : \"https://localhost:8443\",\n" +
160                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
161                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
162                 "  \"sslTrustPassword\" : \"changeit\",\n" +
163                 "  \"tokenAuth\" : {\n" +
164                 "    \"token\" : \"72178473kjshdkjgvbsdkjv903274908\"\n" +
165                 "  }\n" +
166                 "}"
167         val mapper = ObjectMapper()
168         val actualObj: JsonNode = mapper.readTree(json)
169         val properties = bluePrintRestLibPropertyService.restClientProperties(
170                 actualObj)
171         assertNotNull(properties, "failed to create property bean")
172
173         val p: SSLTokenAuthRestClientProperties =
174                 properties as SSLTokenAuthRestClientProperties
175
176         assertEquals(p.tokenAuth!!.token!!, "72178473kjshdkjgvbsdkjv903274908")
177         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
178         assertEquals(p.sslTrustPassword, "changeit")
179         assertEquals(p.keyStoreInstance, "PKCS12")
180     }
181
182     @Test
183     fun testSSLNoAuthPropertiesAsJson() {
184         val json: String = "{\n" +
185                 "  \"type\" : \"ssl-basic-auth\",\n" +
186                 "  \"url\" : \"https://localhost:8443\",\n" +
187                 "  \"keyStoreInstance\" : \"PKCS12\",\n" +
188                 "  \"sslTrust\" : \"src/test/resources/keystore.p12\",\n" +
189                 "  \"sslTrustPassword\" : \"changeit\",\n" +
190                 "  \"sslKey\" : \"src/test/resources/keystore.p12\",\n" +
191                 "  \"sslKeyPassword\" : \"changeit\"\n" +
192                 "}"
193         val mapper = ObjectMapper()
194         val actualObj: JsonNode = mapper.readTree(json)
195         val properties = bluePrintRestLibPropertyService.restClientProperties(
196                 actualObj)
197         assertNotNull(properties, "failed to create property bean")
198
199         val p: SSLRestClientProperties =
200                 properties as SSLRestClientProperties
201
202         assertEquals(p.sslTrust, "src/test/resources/keystore.p12")
203         assertEquals(p.sslTrustPassword, "changeit")
204         assertEquals(p.keyStoreInstance, "PKCS12")
205         assertEquals(p.sslKey, "src/test/resources/keystore.p12")
206         assertEquals(p.sslKeyPassword, "changeit")
207     }
208
209     @Test
210     fun testBlueprintWebClientService() {
211         val blueprintWebClientService = bluePrintRestLibPropertyService
212                 .blueprintWebClientService("sample")
213         assertNotNull(blueprintWebClientService, "failed to create blu" +
214                 "eprintWebClientService")
215     }
216
217 }
218