Merge "Cm Subscription: Predicates optional now"
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / executor / CpsNcmpTaskExecutorSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.rest.executor
22
23 import ch.qos.logback.classic.Level
24 import ch.qos.logback.classic.Logger
25 import ch.qos.logback.classic.spi.ILoggingEvent
26 import ch.qos.logback.core.read.ListAppender
27 import org.slf4j.LoggerFactory
28 import spock.lang.Specification
29 import spock.util.concurrent.PollingConditions
30
31 class CpsNcmpTaskExecutorSpec extends Specification {
32
33     def objectUnderTest = new CpsNcmpTaskExecutor()
34     def logger = Spy(ListAppender<ILoggingEvent>)
35     def enoughTime = 100
36     def notEnoughTime = 10
37
38     void setup() {
39         ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).addAppender(logger)
40         logger.start()
41     }
42
43     void cleanup() {
44         ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).detachAndStopAllAppenders()
45     }
46
47     def 'Execute successful task.'() {
48         when: 'task is executed'
49             objectUnderTest.executeTask(taskSupplier(), enoughTime)
50         then: 'an event is logged with level INFO'
51             new PollingConditions().within(1) {
52                 def loggingEvent = getLoggingEvent()
53                 assert loggingEvent.level == Level.INFO
54             }
55         and: 'the log indicates the task completed successfully'
56             assert loggingEvent.formattedMessage == 'Async task completed successfully.'
57     }
58
59     def 'Execute failing task.'() {
60         when: 'task is executed'
61             objectUnderTest.executeTask(taskSupplierForFailingTask(), enoughTime)
62         then: 'an event is logged with level ERROR'
63             new PollingConditions().within(1) {
64                 def loggingEvent = getLoggingEvent()
65                 assert loggingEvent.level == Level.ERROR
66             }
67         and: 'the original error message is logged'
68             assert loggingEvent.formattedMessage.contains('original exception message')
69     }
70
71     def 'Task times out.'() {
72         when: 'task is executed without enough time to complete'
73             objectUnderTest.executeTask(taskSupplierForLongRunningTask(), notEnoughTime)
74         then: 'an event is logged with level ERROR'
75             new PollingConditions().within(1) {
76                 def loggingEvent = getLoggingEvent()
77                 assert loggingEvent.level == Level.ERROR
78             }
79         and: 'a timeout error message is logged'
80             assert loggingEvent.formattedMessage.contains('java.util.concurrent.TimeoutException')
81     }
82
83     def taskSupplier() {
84         return () -> 'hello world'
85     }
86
87     def taskSupplierForFailingTask() {
88         return () -> { throw new RuntimeException('original exception message') }
89     }
90
91     def taskSupplierForLongRunningTask() {
92         return () -> { sleep(enoughTime) }
93     }
94
95     def getLoggingEvent() {
96         return logger.list[0]
97     }
98
99 }