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