2 * Copyright © 2018-2019 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19 import com.fasterxml.jackson.databind.ObjectMapper
20 import com.fasterxml.jackson.databind.node.ObjectNode
21 import io.micrometer.core.instrument.MeterRegistry
22 import io.mockk.coVerify
24 import io.mockk.coEvery
25 import io.mockk.coVerify
29 import kotlinx.coroutines.runBlocking
30 import org.junit.Before
31 import org.junit.runner.RunWith
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
33 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
34 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
35 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
36 import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.DatabaseStoreAuditService
37 import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.db.BlueprintAuditStatusRepository
38 import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.db.BlueprintWorkflowAuditStatus
39 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractServiceFunction
40 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
41 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
42 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
43 import org.springframework.beans.factory.annotation.Autowired
44 import org.springframework.boot.test.mock.mockito.MockBean
45 import org.springframework.context.ApplicationContext
46 import org.springframework.stereotype.Service
47 import org.springframework.test.context.ContextConfiguration
48 import org.springframework.test.context.TestPropertySource
49 import org.springframework.test.context.junit4.SpringRunner
50 import kotlin.test.Test
51 import kotlin.test.assertNotNull
52 import kotlin.test.assertTrue
54 @RunWith(SpringRunner::class)
55 @ContextConfiguration(
57 MockServiceAction::class, SelfServiceApiTestConfiguration::class,
58 ErrorCatalogTestConfiguration::class
61 @TestPropertySource(locations = ["classpath:application-test.properties"])
62 class ExecutionServiceHandlerTest {
65 lateinit var meterRegistry: MeterRegistry
68 lateinit var applicationContext: ApplicationContext
70 private val blueprintAuditStatusRepository =
71 mockk<BlueprintAuditStatusRepository>()
73 private val testDatabaseStoreAuditService = DatabaseStoreAuditService(blueprintAuditStatusRepository)
77 BluePrintDependencyService.inject(applicationContext)
81 fun testExecuteServiceFunction() {
82 val executionServiceInput = ExecutionServiceInput().apply {
83 commonHeader = CommonHeader().apply {
85 subRequestId = "1234-12"
86 originatorId = "cds-test"
88 actionIdentifiers = ActionIdentifiers().apply {
89 blueprintName = "default"
90 blueprintVersion = "1.0.0"
91 actionName = "mock-service-action"
95 val executionServiceHandler = ExecutionServiceHandler(mockk(), mockk(), mockk(), mockk(), testDatabaseStoreAuditService, mockk(relaxed = true))
96 val isServiceFunction = executionServiceHandler.checkServiceFunction(executionServiceInput)
97 assertTrue(isServiceFunction, "failed to checkServiceFunction")
98 val executionServiceOutput = executionServiceHandler.executeServiceFunction(executionServiceInput)
99 assertNotNull(executionServiceOutput, "failed to get executionServiceOutput")
104 fun testPublishAuditFunction() {
106 val jsonContent = JacksonUtils.getClassPathFileContent("execution-input/sample-payload.json")
107 val json: ObjectNode = ObjectMapper().readTree(jsonContent) as ObjectNode
109 val executionServiceInput = ExecutionServiceInput().apply {
110 commonHeader = CommonHeader().apply {
112 subRequestId = "1234-12"
113 originatorId = "cds-test"
115 actionIdentifiers = ActionIdentifiers().apply {
116 blueprintName = "default"
117 blueprintVersion = "1.0.0"
118 actionName = "mock-service-action"
122 executionServiceInput.payload = json
123 val publishAuditService = mockk<KafkaPublishAuditService>(relaxed = true)
124 val wfAudit = createWorkflowAuditStatusRecord(1000)
126 val executionServiceHandler = ExecutionServiceHandler(
131 testDatabaseStoreAuditService,
132 mockk(relaxed = true)
134 var testOutput: Long = 1000
135 coEvery { publishAuditService.publishExecutionInput(ExecutionServiceInput()) } just Runs
138 every { blueprintAuditStatusRepository.findById(testOutput) } returns wfAudit
139 every { blueprintAuditStatusRepository.saveAndFlush(any<BlueprintWorkflowAuditStatus>()) } returns wfAudit
142 var executionServiceOutput: ExecutionServiceOutput? = null
144 executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
148 publishAuditService.publishExecutionInput(executionServiceInput)
149 publishAuditService.publishExecutionOutput(executionServiceInput.correlationUUID, executionServiceOutput!!)
150 testOutput = testDatabaseStoreAuditService.storeExecutionInput(executionServiceInput)
154 private fun createWorkflowAuditStatusRecord(
156 ): BlueprintWorkflowAuditStatus {
158 var blueprintWorkflowAuditStatus: BlueprintWorkflowAuditStatus =
159 BlueprintWorkflowAuditStatus()
160 blueprintWorkflowAuditStatus.id = id
161 blueprintWorkflowAuditStatus.originatorId = "SDNC_DG"
162 blueprintWorkflowAuditStatus.requestMode = "sync"
163 blueprintWorkflowAuditStatus.requestId = "ab543-3asd4"
164 blueprintWorkflowAuditStatus.subRequestId = "81c9-4910"
165 blueprintWorkflowAuditStatus.status = "In progress"
166 blueprintWorkflowAuditStatus.blueprintName = "multi-steps"
167 blueprintWorkflowAuditStatus.blueprintVersion = "1.0.0"
168 blueprintWorkflowAuditStatus.workflowName = "multi-steps-workflow"
169 blueprintWorkflowAuditStatus.updatedBy = "CBA"
170 blueprintWorkflowAuditStatus.requestMode = "sync"
171 blueprintWorkflowAuditStatus.workflowTaskContent = "{\n" +
172 " \"multi-steps-workflow-request\": {\n" +
173 " \"multi-steps-workflow-properties\": {\n" +
174 " \"prop1\": \"testing\",\n" +
175 " \"prop2\": \"testing description\",\n" +
176 " \"prop3\": \"user name \",\n" +
177 " \"prop4\" : \"test project\"\n" +
181 blueprintWorkflowAuditStatus.workflowResponseContent = " "
182 return blueprintWorkflowAuditStatus
186 @Service("mock-service-action")
187 class MockServiceAction : AbstractServiceFunction() {
189 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
190 val responsePayload = """{"answer" : "correct"}""".jsonAsJsonType()
191 setResponsePayloadForAction(responsePayload)
194 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {