Implemented UAT runtime services
[ccsdk/cds.git] / ms / blueprintsprocessor / application / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / uat / logging / MockInvocationLogger.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.ccsdk.cds.blueprintsprocessor.uat.logging
21
22 import org.mockito.listeners.InvocationListener
23 import org.mockito.listeners.MethodInvocationReport
24 import org.slf4j.LoggerFactory
25 import org.slf4j.Marker
26 import java.util.concurrent.atomic.AtomicInteger
27
28 /**
29  * Logs all Mockito's mock/spy invocations.
30  *
31  * Used for debugging interactions with a mock.
32  */
33 class MockInvocationLogger(private val marker: Marker) : InvocationListener {
34
35     private val mockInvocationsCounter = AtomicInteger()
36
37     override fun reportInvocation(report: MethodInvocationReport) {
38         val sb = StringBuilder()
39         sb.appendln("Method invocation #${mockInvocationsCounter.incrementAndGet()} on mock/spy")
40         report.locationOfStubbing?.let { location ->
41             sb.append(INDENT).append("stubbed ").appendln(location)
42         }
43         sb.appendln(report.invocation)
44         sb.append(INDENT).append("invoked ").appendln(report.invocation.location)
45         if (report.threwException()) {
46             sb.append(INDENT).append("has thrown -> ").append(report.throwable.javaClass.name)
47             report.throwable.message?.let { message ->
48                 sb.append(" with message ").append(message)
49             }
50             sb.appendln()
51         } else {
52             sb.append(INDENT).append("has returned -> \"").append(report.returnedValue).append('"')
53             report.returnedValue?.let { value ->
54                 sb.append(" (").append(value.javaClass.name).append(')')
55             }
56             sb.appendln()
57         }
58         log.info(marker, sb.toString())
59     }
60
61     companion object {
62         private const val INDENT = "    "
63         private val log = LoggerFactory.getLogger(MockInvocationLogger::class.java)
64     }
65 }