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