af3cbd89ae49837c2f19cf0231912045debf464b
[ccsdk/cds.git] /
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 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
23 import io.mockk.Runs
24 import io.mockk.coEvery
25 import io.mockk.coVerify
26 import io.mockk.every
27 import io.mockk.just
28 import io.mockk.mockk
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
53
54 @RunWith(SpringRunner::class)
55 @ContextConfiguration(
56     classes = [
57         MockServiceAction::class, SelfServiceApiTestConfiguration::class,
58         ErrorCatalogTestConfiguration::class
59     ]
60 )
61 @TestPropertySource(locations = ["classpath:application-test.properties"])
62 class ExecutionServiceHandlerTest {
63
64     @MockBean
65     lateinit var meterRegistry: MeterRegistry
66
67     @Autowired
68     lateinit var applicationContext: ApplicationContext
69
70     private val blueprintAuditStatusRepository =
71         mockk<BlueprintAuditStatusRepository>()
72
73     private val testDatabaseStoreAuditService = DatabaseStoreAuditService(blueprintAuditStatusRepository)
74
75     @Before
76     fun init() {
77         BluePrintDependencyService.inject(applicationContext)
78     }
79
80     @Test
81     fun testExecuteServiceFunction() {
82         val executionServiceInput = ExecutionServiceInput().apply {
83             commonHeader = CommonHeader().apply {
84                 requestId = "1234"
85                 subRequestId = "1234-12"
86                 originatorId = "cds-test"
87             }
88             actionIdentifiers = ActionIdentifiers().apply {
89                 blueprintName = "default"
90                 blueprintVersion = "1.0.0"
91                 actionName = "mock-service-action"
92             }
93         }
94         runBlocking {
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")
100         }
101     }
102
103     @Test
104     fun testPublishAuditFunction() {
105
106         val jsonContent = JacksonUtils.getClassPathFileContent("execution-input/sample-payload.json")
107         val json: ObjectNode = ObjectMapper().readTree(jsonContent) as ObjectNode
108
109         val executionServiceInput = ExecutionServiceInput().apply {
110             commonHeader = CommonHeader().apply {
111                 requestId = "1234"
112                 subRequestId = "1234-12"
113                 originatorId = "cds-test"
114             }
115             actionIdentifiers = ActionIdentifiers().apply {
116                 blueprintName = "default"
117                 blueprintVersion = "1.0.0"
118                 actionName = "mock-service-action"
119                 mode = "async"
120             }
121         }
122         executionServiceInput.payload = json
123         val publishAuditService = mockk<KafkaPublishAuditService>(relaxed = true)
124         val wfAudit = createWorkflowAuditStatusRecord(1000)
125
126         val executionServiceHandler = ExecutionServiceHandler(
127             mockk(),
128             mockk(),
129             mockk(),
130             publishAuditService,
131             testDatabaseStoreAuditService,
132             mockk(relaxed = true)
133         )
134         var testOutput: Long = 1000
135         coEvery { publishAuditService.publishExecutionInput(ExecutionServiceInput()) } just Runs
136
137         runBlocking {
138             every { blueprintAuditStatusRepository.findById(testOutput) } returns wfAudit
139             every { blueprintAuditStatusRepository.saveAndFlush(any<BlueprintWorkflowAuditStatus>()) } returns wfAudit
140         }
141
142         var executionServiceOutput: ExecutionServiceOutput? = null
143         runBlocking {
144             executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
145         }
146
147         coVerify {
148             publishAuditService.publishExecutionInput(executionServiceInput)
149             publishAuditService.publishExecutionOutput(executionServiceInput.correlationUUID, executionServiceOutput!!)
150             testOutput = testDatabaseStoreAuditService.storeExecutionInput(executionServiceInput)
151         }
152     }
153
154     private fun createWorkflowAuditStatusRecord(
155         id: Long
156     ): BlueprintWorkflowAuditStatus {
157
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" +
178             "      }\n" +
179             "    }\n" +
180             "  }"
181         blueprintWorkflowAuditStatus.workflowResponseContent = " "
182         return blueprintWorkflowAuditStatus
183     }
184 }
185
186 @Service("mock-service-action")
187 class MockServiceAction : AbstractServiceFunction() {
188
189     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
190         val responsePayload = """{"answer" : "correct"}""".jsonAsJsonType()
191         setResponsePayloadForAction(responsePayload)
192     }
193
194     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
195     }
196 }