Refactor rest clients and support timeouts
[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     init {
40         mockServer = ClientAndServer.startClientAndServer(port.toInt())
41         headers = defaultHeaders()
42
43         // Create expected requests and responses
44         setRequest("GET", "/aai/v22/network/generic-vnfs/generic-vnf/123456")
45         setRequest(
46             "GET",
47             "/config/GENERIC-RESOURCE-API:services/service/10/service-data/vnfs/vnf/123456/" +
48                 "vnf-data/vnf-topology/vnf-parameters-data/param/vnf_name"
49         )
50         setRequestWithPayload(
51             "PUT", "/query",
52             "{\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}"
53         )
54     }
55
56     override fun getRestClientProperties(): RestClientProperties {
57         return restClientProperties
58     }
59
60     override fun defaultHeaders(): Map<String, String> {
61         val encodedCredentials = this.setBasicAuth("admin", "aaiTest")
62         return mapOf(
63             HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
64             HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE,
65             HttpHeaders.AUTHORIZATION to "Basic $encodedCredentials"
66         )
67     }
68
69     fun tearDown() {
70         mockServer.close()
71     }
72
73     override fun exchangeResource(
74         method: String,
75         path: String,
76         payload: String
77     ): BlueprintWebClientService.WebClientResponse<String> {
78         val header = arrayOf(BasicHeader(HttpHeaders.AUTHORIZATION, headers[HttpHeaders.AUTHORIZATION]))
79         return when (method) {
80             "POST" -> {
81                 post(path, payload, header, String::class.java)
82             }
83             "PUT" -> {
84                 put(path, payload, header, String::class.java)
85             }
86             else -> {
87                 get(path, header, String::class.java)
88             }
89         }
90     }
91
92     private fun setRequest(method: String, path: String) {
93         val requestResponse = when (method) {
94             "POST" -> {
95                 "Post response"
96             }
97             "PUT" -> {
98                 "Put response"
99             }
100             else -> {
101                 "Get response"
102             }
103         }
104         mockServer.`when`(
105             request().withHeaders(Header(HttpHeaders.AUTHORIZATION, headers[HttpHeaders.AUTHORIZATION]))
106                 .withMethod(method)
107                 .withPath(path)
108         ).respond(response().withStatusCode(200).withBody("{\"aai-resource\":\"$requestResponse\"}"))
109     }
110
111     private fun setRequestWithPayload(method: String, path: String, payload: String) {
112         val requestResponse = when (method) {
113             "POST" -> {
114                 "Post response"
115             }
116             "PUT" -> {
117                 "Put response"
118             }
119             else -> {
120                 "Get response"
121             }
122         }
123         mockServer.`when`(
124             request().withHeaders(Header(HttpHeaders.AUTHORIZATION, headers[HttpHeaders.AUTHORIZATION]))
125                 .withMethod(method)
126                 .withPath(path)
127                 .withQueryStringParameter("format", "resource")
128                 .withBody(payload)
129         ).respond(response().withStatusCode(200).withBody("{\"aai-resource\":\"$requestResponse\"}"))
130     }
131
132     private fun setBasicAuth(username: String, password: String): String {
133         val credentialsString = "$username:$password"
134         return Base64.getEncoder().encodeToString(
135             credentialsString.toByteArray(Charset.defaultCharset())
136         )
137     }
138 }