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