86ed0eab6187fc421472dfb4ace4a2f1ae587cef
[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.Runs
20 import io.mockk.coEvery
21 import io.mockk.coVerify
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(
46     classes = [
47         MockServiceAction::class, SelfServiceApiTestConfiguration::class,
48         ErrorCatalogTestConfiguration::class
49     ]
50 )
51 @TestPropertySource(locations = ["classpath:application-test.properties"])
52 class ExecutionServiceHandlerTest {
53
54     @Autowired
55     lateinit var applicationContext: ApplicationContext
56
57     @Before
58     fun init() {
59         BluePrintDependencyService.inject(applicationContext)
60     }
61
62     @Test
63     fun testExecuteServiceFunction() {
64         val executionServiceInput = ExecutionServiceInput().apply {
65             commonHeader = CommonHeader().apply {
66                 requestId = "1234"
67                 subRequestId = "1234-12"
68                 originatorId = "cds-test"
69             }
70             actionIdentifiers = ActionIdentifiers().apply {
71                 blueprintName = "default"
72                 blueprintVersion = "1.0.0"
73                 actionName = "mock-service-action"
74             }
75         }
76         runBlocking {
77             val executionServiceHandler = ExecutionServiceHandler(mockk(), mockk(), mockk(), mockk())
78             val isServiceFunction = executionServiceHandler.checkServiceFunction(executionServiceInput)
79             assertTrue(isServiceFunction, "failed to checkServiceFunction")
80             val executionServiceOutput = executionServiceHandler.executeServiceFunction(executionServiceInput)
81             assertNotNull(executionServiceOutput, "failed to get executionServiceOutput")
82         }
83     }
84
85     @Test
86     fun testPublishAuditFunction() {
87         val executionServiceInput = ExecutionServiceInput().apply {
88             commonHeader = CommonHeader().apply {
89                 requestId = "1234"
90                 subRequestId = "1234-12"
91                 originatorId = "cds-test"
92             }
93             actionIdentifiers = ActionIdentifiers().apply {
94                 blueprintName = "default"
95                 blueprintVersion = "1.0.0"
96                 actionName = "mock-service-action"
97             }
98         }
99
100         val publishAuditService = mockk<KafkaPublishAuditService>(relaxed = true)
101         val executionServiceHandler = ExecutionServiceHandler(
102             mockk(),
103             mockk(),
104             mockk(),
105             publishAuditService
106         )
107
108         coEvery { publishAuditService.publishExecutionInput(ExecutionServiceInput()) } just Runs
109
110         var executionServiceOutput: ExecutionServiceOutput? = null
111         runBlocking {
112             executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput)
113         }
114
115         coVerify {
116             publishAuditService.publishExecutionInput(executionServiceInput)
117             publishAuditService.publishExecutionOutput(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 }