Enabling Code Formatter
[ccsdk/cds.git] / ms / blueprintsprocessor / application / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / uat / utils / UatExecutor.kt
index d120e71..7d0c875 100644 (file)
@@ -78,6 +78,7 @@ class UatExecutor(
 ) {
 
     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()
@@ -106,8 +107,8 @@ class UatExecutor(
     fun execute(uat: UatDefinition, cbaBytes: ByteArray): UatDefinition {
         val defaultHeaders = listOf(BasicHeader(HttpHeaders.AUTHORIZATION, clientAuthToken()))
         val httpClient = HttpClientBuilder.create()
-                .setDefaultHeaders(defaultHeaders)
-                .build()
+            .setDefaultHeaders(defaultHeaders)
+            .build()
         // Only if externalServices are defined
         val mockInterceptor = MockPreInterceptor()
         // Always defined and used, whatever the case
@@ -117,13 +118,13 @@ class UatExecutor(
             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 }
+                { 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 ->
@@ -134,14 +135,14 @@ class UatExecutor(
                     log.info("Executing process '${process.name}'")
                     val responseNormalizer = JsonNormalizer.getNormalizer(mapper, process.responseNormalizerSpec)
                     val actualResponse = processBlueprint(
-                            client, process.request,
-                            process.expectedResponse, responseNormalizer
+                        client, process.request,
+                        process.expectedResponse, responseNormalizer
                     )
                     ProcessDefinition(
-                            process.name,
-                            process.request,
-                            actualResponse,
-                            process.responseNormalizerSpec
+                        process.name,
+                        process.request,
+                        actualResponse,
+                        process.responseNormalizerSpec
                     )
                 }
             }
@@ -151,10 +152,10 @@ class UatExecutor(
                 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))
+                        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)
@@ -163,7 +164,7 @@ class UatExecutor(
             }
 
             val newExternalServices = spyInterceptor.getSpies()
-                    .map(SpyService::asServiceDefinition)
+                .map(SpyService::asServiceDefinition)
 
             return UatDefinition(newProcesses, newExternalServices)
         } finally {
@@ -181,43 +182,43 @@ class UatExecutor(
     }
 
     private fun createRestClientMock(restExpectations: List<ExpectationDefinition>):
-            BlueprintWebClientService {
-        val restClient = mock<BlueprintWebClientService>(
+        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()))
+            // 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(
+            for (expectation in restExpectations) {
+                var stubbing = whenever(
                     restClient.exchangeResource(
-                            eq(expectation.request.method),
-                            eq(expectation.request.path),
-                            any(),
-                            any()
+                        eq(expectation.request.method),
+                        eq(expectation.request.path),
+                        any(),
+                        any()
                     )
-            )
-            for (response in expectation.responses) {
-                stubbing = stubbing.thenReturn(WebClientResponse(response.status, response.body.toString()))
+                )
+                for (response in expectation.responses) {
+                    stubbing = stubbing.thenReturn(WebClientResponse(response.status, response.body.toString()))
+                }
             }
+            return restClient
         }
-        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()
+            .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
         }
@@ -254,7 +255,8 @@ class UatExecutor(
 
     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")
+            "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)
@@ -278,21 +280,25 @@ class UatExecutor(
     }
 
     private fun localServerPort(): Int =
-            (environment.getProperty("local.server.port")
-                    ?: environment.getRequiredProperty("blueprint.httpPort")).toInt()
+        (
+            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)
+                NOOP_PASSWORD_PREFIX.length
+            )
             else -> username
         }
         return "Basic " + Base64Utils.encodeToString("$username:$plainPassword".toByteArray())
     }
 
     private class MockPreInterceptor : BluePrintRestLibPropertyService.PreInterceptor {
+
         private val mocks = ConcurrentHashMap<String, BlueprintWebClientService>()
 
         override fun getInstance(jsonNode: JsonNode): BlueprintWebClientService? {
@@ -300,7 +306,7 @@ class UatExecutor(
         }
 
         override fun getInstance(selector: String): BlueprintWebClientService? =
-                mocks[selector]
+            mocks[selector]
 
         fun registerMock(selector: String, client: BlueprintWebClientService) {
             mocks[selector] = client
@@ -322,7 +328,7 @@ class UatExecutor(
         }
 
         fun getSpies(): List<SpyService> =
-                spies.values.toList()
+            spies.values.toList()
     }
 
     private class SpyService(
@@ -330,14 +336,15 @@ class UatExecutor(
         val selector: String,
         private val realService: BlueprintWebClientService
     ) :
-            BlueprintWebClientService by realService {
+        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
-                )
+            exchangeResource(
+                methodType, path, request,
+                DEFAULT_HEADERS
+            )
 
         override fun exchangeResource(
             methodType: String,
@@ -346,7 +353,7 @@ class UatExecutor(
             headers: Map<String, String>
         ): WebClientResponse<String> {
             val requestDefinition =
-                    RequestDefinition(methodType, path, headers, toJson(request))
+                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
@@ -354,12 +361,12 @@ class UatExecutor(
                 else -> null
             }
             val responseDefinition =
-                    ResponseDefinition(realAnswer.status, responseBody)
+                ResponseDefinition(realAnswer.status, responseBody)
             expectations.add(
-                    ExpectationDefinition(
-                            requestDefinition,
-                            responseDefinition
-                    )
+                ExpectationDefinition(
+                    requestDefinition,
+                    responseDefinition
+                )
             )
             return realAnswer
         }
@@ -369,7 +376,7 @@ class UatExecutor(
         }
 
         fun asServiceDefinition() =
-                ServiceDefinition(selector, expectations)
+            ServiceDefinition(selector, expectations)
 
         private fun toJson(str: String): JsonNode? {
             return when {
@@ -379,9 +386,10 @@ class UatExecutor(
         }
 
         companion object {
+
             private val DEFAULT_HEADERS = mapOf(
-                    HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
-                    HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE
+                HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
+                HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE
             )
         }
     }