Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / ExecutionServiceHandlerTest.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
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
17 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
18
19 import io.micrometer.core.instrument.MeterRegistry
20 import io.mockk.coVerify
21 import io.mockk.Runs
22 import io.mockk.coEvery
23 import io.mockk.coVerify
24 import io.mockk.just
25 import io.mockk.mockk
26 import kotlinx.coroutines.runBlocking
27 import org.junit.Before
28 import org.junit.runner.RunWith
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
33 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractServiceFunction
34 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
35 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintDependencyService
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.test.mock.mockito.MockBean
38 import org.springframework.context.ApplicationContext
39 import org.springframework.stereotype.Service
40 import org.springframework.test.context.ContextConfiguration
41 import org.springframework.test.context.TestPropertySource
42 import org.springframework.test.context.junit4.SpringRunner
43 import kotlin.test.Test
44 import kotlin.test.assertNotNull
45 import kotlin.test.assertTrue
46
47 @RunWith(SpringRunner::class)
48 @ContextConfiguration(
49     classes = [
50         MockServiceAction::class, SelfServiceApiTestConfiguration::class,
51         ErrorCatalogTestConfiguration::class
52     ]
53 )
54 @TestPropertySource(locations = ["classpath:application-test.properties"])
55 class ExecutionServiceHandlerTest {
56
57     @MockBean
58     lateinit var meterRegistry: MeterRegistry
59
60     @Autowired
61     lateinit var applicationContext: ApplicationContext
62
63     @Before
64     fun init() {
65         BlueprintDependencyService.inject(applicationContext)
66     }
67
68     @Test
69     fun testExecuteServiceFunction() {
70         val executionServiceInput = ExecutionServiceInput().apply {
71             commonHeader = CommonHeader().apply {
72                 requestId = "1234"
73                 subRequestId = "1234-12"
74                 originatorId = "cds-test"
75             }
76             actionIdentifiers = ActionIdentifiers().apply {
77                 blueprintName = "default"
78                 blueprintVersion = "1.0.0"
79                 actionName = "mock-service-action"
80             }
81         }
82         runBlocking {
83             val executionServiceHandler = ExecutionServiceHandler(mockk(), mockk(), mockk(), mockk(), mockk(relaxed = true))
84             val isServiceFunction = executionServiceHandler.checkServiceFunction(executionServiceInput)
85             assertTrue(isServiceFunction, "failed to checkServiceFunction")
86             val executionServiceOutput = executionServiceHandler.executeServiceFunction(executionServiceInput)
87             assertNotNull(executionServiceOutput, "failed to get executionServiceOutput")
88         }
89     }
90
91     @Test
92     fun testPublishAuditFunction() {
93         val executionServiceInput = ExecutionServiceInput().apply {
94             commonHeader = CommonHeader().apply {
95                 requestId = "1234"
96                 subRequestId = "1234-12"
97                 originatorId = "cds-test"
98             }
99             actionIdentifiers = ActionIdentifiers().apply {
100                 blueprintName = "default"
101                 blueprintVersion = "1.0.0"
102                 actionName = "mock-service-action"
103             }
104         }
105
106         val publishAuditService = mockk<KafkaPublishAuditService>(relaxed = true)
107         val executionServiceHandler = ExecutionServiceHandler(
108             mockk(),
109             mockk(),
110             mockk(),
111             publishAuditService,
112             mockk(relaxed = true)
113         )
114
115         coEvery { publishAuditService.publishExecutionInput(ExecutionServiceInput()) } just Runs
116
117         var executionServiceOutput: ExecutionServiceOutput? = null
118         runBlocking {
119             executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
120         }
121
122         coVerify {
123             publishAuditService.publishExecutionInput(executionServiceInput)
124             publishAuditService.publishExecutionOutput(executionServiceInput.correlationUUID, executionServiceOutput!!)
125         }
126     }
127 }
128
129 @Service("mock-service-action")
130 class MockServiceAction : AbstractServiceFunction() {
131
132     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
133         val responsePayload = """{"answer" : "correct"}""".jsonAsJsonType()
134         setResponsePayloadForAction(responsePayload)
135     }
136
137     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
138     }
139 }