Resolution processors tests and extendability
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / mock / MockBlueprintWebClientService.kt
1 /*
2  * Copyright © 2019 IBM, Bell Canada.
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 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.mock
17
18 import org.apache.http.message.BasicHeader
19 import org.mockserver.integration.ClientAndServer
20 import org.mockserver.model.Header
21 import org.mockserver.model.HttpRequest.request
22 import org.mockserver.model.HttpResponse.response
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BaseBlueprintWebClientService
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
26 import org.springframework.http.HttpHeaders
27 import org.springframework.http.MediaType
28 import java.nio.charset.Charset
29 import java.util.Base64
30
31 class MockBlueprintWebClientService(private var restClientProperties: RestClientProperties) :
32     BaseBlueprintWebClientService<RestClientProperties>() {
33
34     private var mockServer: ClientAndServer
35     private var port: String = if (restClientProperties.url.split(":")[2].isEmpty()) "8080"
36     else restClientProperties.url.split(":")[2]
37     private var headers: Map<String, String>
38
39     companion object {
40         const val JSON_OUTPUT: String = "{" +
41             "\"vnf-id\":\"123456\"," +
42             "\"param\": [{\"value\": \"vnf1\"}]," +
43             "\"vnf_name\":\"vnf1\"," +
44             "\"vnf-name\":\"vnf1\"" +
45             "}"
46     }
47
48     init {
49         mockServer = ClientAndServer.startClientAndServer(port.toInt())
50         headers = defaultHeaders()
51
52         // Create expected requests and responses
53         setRequest("GET", "/aai/v22/network/generic-vnfs/generic-vnf/123456")
54         setRequest(
55             "GET",
56             "/config/GENERIC-RESOURCE-API:services/service/10/service-data/vnfs/vnf/123456/" +
57                 "vnf-data/vnf-topology/vnf-parameters-data/param/vnf_name"
58         )
59         setRequestWithPayload(
60             "PUT", "/query",
61             "{\r\n\"start\": \"\\/nodes\\/vf-modules?vf-module-name=vf-module-name\",\r\n\"query\": \"\\/query\\/related-to?startingNodeType=vf-module&relatedToNodeType=generic-vnf\"\r\n}"
62         )
63     }
64
65     override fun getRestClientProperties(): RestClientProperties {
66         return restClientProperties
67     }
68
69     override fun defaultHeaders(): Map<String, String> {
70         val encodedCredentials = this.setBasicAuth("admin", "aaiTest")
71         return mapOf(
72             HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
73             HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE,
74             HttpHeaders.AUTHORIZATION to "Basic $encodedCredentials"
75         )
76     }
77
78     fun tearDown() {
79         mockServer.close()
80     }
81
82     override fun exchangeResource(
83         method: String,
84         path: String,
85         payload: String,
86         headers: Map<String, String>
87     ): BlueprintWebClientService.WebClientResponse<String> {
88         val header = arrayOf(BasicHeader(HttpHeaders.AUTHORIZATION, this.headers[HttpHeaders.AUTHORIZATION]))
89         return when (method) {
90             "POST" -> {
91                 post(path, payload, header, String::class.java)
92             }
93
94             "PUT" -> {
95                 put(path, payload, header, String::class.java)
96             }
97
98             else -> {
99                 get(path, header, String::class.java)
100             }
101         }
102     }
103
104     private fun setRequest(method: String, path: String) {
105         val requestResponse = when (method) {
106             "POST" -> {
107                 ""
108             }
109
110             "PUT" -> {
111                 ""
112             }
113
114             else -> {
115                 JSON_OUTPUT
116             }
117         }
118         mockServer.`when`(
119             request().withHeaders(Header(HttpHeaders.AUTHORIZATION, headers[HttpHeaders.AUTHORIZATION]))
120                 .withMethod(method)
121                 .withPath(path)
122         ).respond(response().withStatusCode(200).withBody(requestResponse))
123     }
124
125     private fun setRequestWithPayload(method: String, path: String, payload: String) {
126         val requestResponse = when (method) {
127             "POST" -> {
128                 ""
129             }
130
131             "PUT" -> {
132                 ""
133             }
134
135             else -> {
136                 JSON_OUTPUT
137             }
138         }
139         mockServer.`when`(
140             request().withHeaders(Header(HttpHeaders.AUTHORIZATION, headers[HttpHeaders.AUTHORIZATION]))
141                 .withMethod(method)
142                 .withPath(path)
143                 .withQueryStringParameter("format", "resource")
144                 .withBody(payload)
145         ).respond(response().withStatusCode(200).withBody(requestResponse))
146     }
147
148     private fun setBasicAuth(username: String, password: String): String {
149         val credentialsString = "$username:$password"
150         return Base64.getEncoder().encodeToString(
151             credentialsString.toByteArray(Charset.defaultCharset())
152         )
153     }
154 }