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