summaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/application/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/uat/utils/UatExecutor.kt
blob: b97dbf7bb86c20493c602c1fde8d9fb57894f164 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.ccsdk.cds.blueprintsprocessor.uat.utils

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.argThat
import com.nhaarman.mockitokotlin2.atLeast
import com.nhaarman.mockitokotlin2.atMost
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
import com.nhaarman.mockitokotlin2.whenever
import org.apache.http.HttpHeaders
import org.apache.http.HttpStatus
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.StringEntity
import org.apache.http.entity.mime.HttpMultipartMode
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.message.BasicHeader
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.notNullValue
import org.hamcrest.MatcherAssert.assertThat
import org.mockito.Answers
import org.mockito.verification.VerificationMode
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService.WebClientResponse
import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.COLOR_MOCKITO
import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.LogColor.markerOf
import org.onap.ccsdk.cds.blueprintsprocessor.uat.logging.MockInvocationLogger
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.core.env.ConfigurableEnvironment
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.util.Base64Utils
import java.util.concurrent.ConcurrentHashMap

/**
 * Assumptions:
 *
 * - Application HTTP service is bound to loopback interface;
 * - Password is either defined in plain (with "{noop}" prefix), or it's the same of username.
 *
 * @author Eliezio Oliveira
 */
@Component
open class UatExecutor(
    private val environment: ConfigurableEnvironment,
    private val restClientFactory: BluePrintRestLibPropertyService,
    private val mapper: ObjectMapper
) {

    companion object {

        private const val NOOP_PASSWORD_PREFIX = "{noop}"
        private const val PROPERTY_IN_UAT = "IN_UAT"
        private val TIMES_SPEC_REGEX = "([<>]=?)?\\s*(\\d+)".toRegex()
        private val log: Logger = LoggerFactory.getLogger(UatExecutor::class.java)
        private val mockLoggingListener = MockInvocationLogger(markerOf(COLOR_MOCKITO))
    }

    // use lazy evaluation to postpone until localServerPort is injected by Spring
    private val baseUrl: String by lazy {
        "http://127.0.0.1:${localServerPort()}"
    }

    @Throws(AssertionError::class)
    fun execute(uatSpec: String, cbaBytes: ByteArray) {
        val uat = UatDefinition.load(mapper, uatSpec)
        execute(uat, cbaBytes)
    }

    /**
     *
     * The UAT can range from minimum to completely defined.
     *
     * @return an updated UAT with all NB and SB messages.
     */
    @Throws(AssertionError::class)
    fun execute(uat: UatDefinition, cbaBytes: ByteArray): UatDefinition {
        val defaultHeaders = listOf(BasicHeader(HttpHeaders.AUTHORIZATION, clientAuthToken()))
        val httpClient = HttpClientBuilder.create()
            .setDefaultHeaders(defaultHeaders)
            .build()
        // Only if externalServices are defined
        val mockInterceptor = MockPreInterceptor()
        // Always defined and used, whatever the case
        val spyInterceptor = SpyPostInterceptor(mapper)
        restClientFactory.setInterceptors(mockInterceptor, spyInterceptor)
        try {
            markUatBegin()
            // Configure mocked external services and save their expectations for further validation
            val expectationsPerClient = uat.externalServices.associateBy(
                { service ->
                    createRestClientMock(service.expectations).also { restClient ->
                        // side-effect: register restClient to override real instance
                        mockInterceptor.registerMock(service.selector, restClient)
                    }
                },
                { service -> service.expectations }
            )

            val newProcesses = httpClient.use { client ->
                uploadBlueprint(client, cbaBytes)

                // Run processes
                uat.processes.map { process ->
                    log.info("Executing process '${process.name}'")
                    val responseNormalizer = JsonNormalizer.getNormalizer(mapper, process.responseNormalizerSpec)
                    val actualResponse = processBlueprint(
                        client, process,
                        process.expectedResponse, responseNormalizer
                    )
                    ProcessDefinition(
                        process.name,
                        process.request,
                        actualResponse,
                        process.responseNormalizerSpec
                    )
                }
            }

            // Validate requests to external services
            for ((mockClient, expectations) in expectationsPerClient) {
                expectations.forEach { expectation ->
                    val request = expectation.request
                    verify(mockClient, evalVerificationMode(expectation.times)).exchangeResource(
                        eq(request.method),
                        eq(request.path),
                        argThat { assertJsonEquals(request.body, this) },
                        argThat(RequiredMapEntriesMatcher(request.headers))
                    )
                }
                // Don't mind the invocations to the overloaded exchangeResource(String, String, String)
                verify(mockClient, atLeast(0)).exchangeResource(any(), any(), any())
                verifyNoMoreInteractions(mockClient)
            }

            val newExternalServices = spyInterceptor.getSpies()
                .map(SpyService::asServiceDefinition)

            return UatDefinition(newProcesses, newExternalServices)
        } finally {
            restClientFactory.clearInterceptors()
            markUatEnd()
        }
    }

    private fun markUatBegin() {
        System.setProperty(PROPERTY_IN_UAT, "1")
    }

    private fun markUatEnd() {
        System.clearProperty(PROPERTY_IN_UAT)
    }

    private fun createRestClientMock(restExpectations: List<ExpectationDefinition>):
        BlueprintWebClientService {
            val restClient = mock<BlueprintWebClientService>(
                defaultAnswer = Answers.RETURNS_SMART_NULLS,
                // our custom verboseLogging handler
                invocationListeners = arrayOf(mockLoggingListener)
            )

            // Delegates to overloaded exchangeResource(String, String, String, Map<String, String>)
            whenever(restClient.exchangeResource(any(), any(), any()))
                .thenAnswer { invocation ->
                    val method = invocation.arguments[0] as String
                    val path = invocation.arguments[1] as String
                    val request = invocation.arguments[2] as String
                    restClient.exchangeResource(method, path, request, emptyMap())
                }
            for (expectation in restExpectations) {
                var stubbing = whenever(
                    restClient.exchangeResource(
                        eq(expectation.request.method),
                        eq(expectation.request.path),
                        any(),
                        any()
                    )
                )
                for (response in expectation.responses) {
                    stubbing = stubbing.thenReturn(WebClientResponse(response.status, response.body.toString()))
                }
            }
            return restClient
        }

    @Throws(AssertionError::class)
    private fun uploadBlueprint(client: HttpClient, cbaBytes: ByteArray) {
        val multipartEntity = MultipartEntityBuilder.create()
            .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
            .addBinaryBody("file", cbaBytes, ContentType.DEFAULT_BINARY, "cba.zip")
            .build()
        val request = HttpPost("$baseUrl/api/v1/blueprint-model/publish").apply {
            entity = multipartEntity
        }
        client.execute(request) { response ->
            val statusLine = response.statusLine
            assertThat(statusLine.statusCode, equalTo(HttpStatus.SC_OK))
        }
    }

    @Throws(AssertionError::class)
    private fun processBlueprint(
        client: HttpClient,
        process: ProcessDefinition,
        expectedResponse: JsonNode?,
        responseNormalizer: (String) -> String
    ): JsonNode {
        val stringEntity = StringEntity(mapper.writeValueAsString(process.request), ContentType.APPLICATION_JSON)
        val request = HttpPost("$baseUrl/api/v1/execution-service/process").apply {
            entity = stringEntity
        }
        val response = client.execute(request) { response ->
            val statusLine = response.statusLine
            val expectedCode = expectedResponse?.get("status")?.get("code")?.intValue()
            assertThat("${process.name}", statusLine.statusCode, equalTo(expectedCode ?: HttpStatus.SC_OK))
            val entity = response.entity
            assertThat("${process.name} Response contains no content", entity, notNullValue())
            entity.content.bufferedReader().use { it.readText() }
        }
        val actualResponse = responseNormalizer(response)
        if (expectedResponse != null) {
            assertJsonEquals(expectedResponse, actualResponse, process.name)
        }
        return mapper.readTree(actualResponse)!!
    }

    private fun evalVerificationMode(times: String): VerificationMode {
        val matchResult = TIMES_SPEC_REGEX.matchEntire(times) ?: throw InvalidUatDefinition(
            "Time specification '$times' does not follow expected format $TIMES_SPEC_REGEX"
        )
        val counter = matchResult.groups[2]!!.value.toInt()
        return when (matchResult.groups[1]?.value) {
            ">=" -> atLeast(counter)
            ">" -> atLeast(counter + 1)
            "<=" -> atMost(counter)
            "<" -> atMost(counter - 1)
            else -> times(counter)
        }
    }

    @Throws(AssertionError::class)
    private fun assertJsonEquals(expected: JsonNode?, actual: String, msg: String = ""): Boolean {
        // special case
        if ((expected == null) && actual.isBlank()) {
            return true
        }
        // general case
        JSONAssert.assertEquals(msg, expected?.toString(), actual, JSONCompareMode.LENIENT)
        // assertEquals throws an exception whenever match fails
        return true
    }

    private fun localServerPort(): Int =
        (
            environment.getProperty("local.server.port")
                ?: environment.getRequiredProperty("blueprint.httpPort")
            ).toInt()

    private fun clientAuthToken(): String {
        val username = environment.getRequiredProperty("security.user.name")
        val password = environment.getRequiredProperty("security.user.password")
        val plainPassword = when {
            password.startsWith(NOOP_PASSWORD_PREFIX) -> password.substring(
                NOOP_PASSWORD_PREFIX.length
            )
            else -> username
        }
        return "Basic " + Base64Utils.encodeToString("$username:$plainPassword".toByteArray())
    }

    open class MockPreInterceptor : BluePrintRestLibPropertyService.PreInterceptor {

        private val mocks = ConcurrentHashMap<String, BlueprintWebClientService>()

        override fun getInstance(jsonNode: JsonNode): BlueprintWebClientService? {
            TODO("jsonNode-keyed services not yet supported")
        }

        override fun getInstance(selector: String): BlueprintWebClientService? =
            mocks[selector]

        fun registerMock(selector: String, client: BlueprintWebClientService) {
            mocks[selector] = client
        }
    }

    open class SpyPostInterceptor(private val mapper: ObjectMapper) : BluePrintRestLibPropertyService.PostInterceptor {

        private val spies = ConcurrentHashMap<String, SpyService>()

        override fun getInstance(jsonNode: JsonNode, service: BlueprintWebClientService): BlueprintWebClientService {
            TODO("jsonNode-keyed services not yet supported")
        }

        override fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService {
            var spiedService = spies[selector]
            if (spiedService != null)
                return spiedService

            spiedService = SpyService(mapper, selector, service)
            spies[selector] = spiedService

            return spiedService
        }
        fun getSpies(): List<SpyService> =
            spies.values.toList()
    }

    open class SpyService(
        private val mapper: ObjectMapper,
        val selector: String,
        private val realService: BlueprintWebClientService
    ) :
        BlueprintWebClientService by realService {

        private val expectations: MutableList<ExpectationDefinition> = mutableListOf()

        override fun exchangeResource(methodType: String, path: String, request: String): WebClientResponse<String> =
            exchangeResource(
                methodType, path, request,
                DEFAULT_HEADERS
            )

        override fun exchangeResource(
            methodType: String,
            path: String,
            request: String,
            headers: Map<String, String>
        ): WebClientResponse<String> {
            val requestDefinition =
                RequestDefinition(methodType, path, headers, toJson(request))
            val realAnswer = realService.exchangeResource(methodType, path, request, headers)
            val responseBody = when {
                // TODO: confirm if we need to normalize the response here
                realAnswer.status == HttpStatus.SC_OK -> toJson(realAnswer.body)
                else -> null
            }
            val responseDefinition =
                ResponseDefinition(realAnswer.status, responseBody)
            expectations.add(
                ExpectationDefinition(
                    requestDefinition,
                    responseDefinition
                )
            )
            return realAnswer
        }

        override suspend fun <T> retry(times: Int, initialDelay: Long, delay: Long, block: suspend (Int) -> T): T {
            return super.retry(times, initialDelay, delay, block)
        }

        fun asServiceDefinition() =
            ServiceDefinition(selector, expectations)

        private fun toJson(str: String): JsonNode? {
            return when {
                str.isNotBlank() -> mapper.readTree(str)
                else -> null
            }
        }

        companion object {

            private val DEFAULT_HEADERS = mapOf(
                HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
                HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE
            )
        }
    }
}