aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockBlueprintWebClientService.kt
blob: 86a9ed05661496c5cdfc69f609fa1bcef2ab0c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright © 2019 IBM, Bell Canada.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.mock

import org.apache.http.message.BasicHeader
import org.mockserver.integration.ClientAndServer
import org.mockserver.model.Header
import org.mockserver.model.HttpRequest.request
import org.mockserver.model.HttpResponse.response
import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import java.nio.charset.Charset
import java.util.Base64

class MockBlueprintWebClientService(private var restClientProperties: RestClientProperties) :
    BlueprintWebClientService {

    private var mockServer: ClientAndServer
    private var port: String = if (restClientProperties.url.split(":")[2].isEmpty()) "8080"
    else restClientProperties.url.split(":")[2]
    private var headers: Map<String, String>

    init {
        mockServer = ClientAndServer.startClientAndServer(port.toInt())
        headers = defaultHeaders()

        // Create expected requests and responses
        setRequest("GET", "/aai/v22/network/generic-vnfs/generic-vnf/123456")
        setRequest(
            "GET",
            "/config/GENERIC-RESOURCE-API:services/service/10/service-data/vnfs/vnf/123456/" +
                "vnf-data/vnf-topology/vnf-parameters-data/param/vnf_name"
        )
        setRequestWithPayload(
            "PUT", "/query",
            "{\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}"
        )
    }

    override fun defaultHeaders(): Map<String, String> {
        val encodedCredentials = this.setBasicAuth("admin", "aaiTest")
        return mapOf(
            HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
            HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE,
            HttpHeaders.AUTHORIZATION to "Basic $encodedCredentials"
        )
    }

    override fun host(uri: String): String {
        return restClientProperties.url + uri
    }

    fun tearDown() {
        mockServer.close()
    }

    override fun exchangeResource(
        method: String,
        path: String,
        payload: String
    ): BlueprintWebClientService.WebClientResponse<String> {
        val header = arrayOf(BasicHeader(HttpHeaders.AUTHORIZATION, headers[HttpHeaders.AUTHORIZATION]))
        return when (method) {
            "POST" -> {
                post(path, payload, header, String::class.java)
            }
            "PUT" -> {
                put(path, payload, header, String::class.java)
            }
            else -> {
                get(path, header, String::class.java)
            }
        }
    }

    private fun setRequest(method: String, path: String) {
        val requestResponse = when (method) {
            "POST" -> {
                "Post response"
            }
            "PUT" -> {
                "Put response"
            }
            else -> {
                "Get response"
            }
        }
        mockServer.`when`(
            request().withHeaders(Header(HttpHeaders.AUTHORIZATION, headers[HttpHeaders.AUTHORIZATION]))
                .withMethod(method)
                .withPath(path)
        ).respond(response().withStatusCode(200).withBody("{\"aai-resource\":\"$requestResponse\"}"))
    }

    private fun setRequestWithPayload(method: String, path: String, payload: String) {
        val requestResponse = when (method) {
            "POST" -> {
                "Post response"
            }
            "PUT" -> {
                "Put response"
            }
            else -> {
                "Get response"
            }
        }
        mockServer.`when`(
            request().withHeaders(Header(HttpHeaders.AUTHORIZATION, headers[HttpHeaders.AUTHORIZATION]))
                .withMethod(method)
                .withPath(path)
                .withQueryStringParameter("format", "resource")
                .withBody(payload)
        ).respond(response().withStatusCode(200).withBody("{\"aai-resource\":\"$requestResponse\"}"))
    }

    private fun setBasicAuth(username: String, password: String): String {
        val credentialsString = "$username:$password"
        return Base64.getEncoder().encodeToString(
            credentialsString.toByteArray(Charset.defaultCharset())
        )
    }
}