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