Merge "Replace sleep with PollingConditions"
[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.junit.jupiter.api.AfterEach
28 import org.junit.jupiter.api.BeforeEach
29 import org.slf4j.LoggerFactory
30 import spock.lang.Specification
31 import spock.util.concurrent.PollingConditions
32
33 class CpsNcmpTaskExecutorSpec extends Specification {
34
35     def objectUnderTest = new CpsNcmpTaskExecutor()
36     def logger = Spy(ListAppender<ILoggingEvent>)
37     def enoughTime = 100
38
39     @BeforeEach
40     void setup() {
41         ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).addAppender(logger)
42         logger.start()
43     }
44
45     @AfterEach
46     void teardown() {
47         ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).detachAndStopAllAppenders()
48     }
49
50     def 'Execute successful task.'() {
51         when: 'task is executed'
52             objectUnderTest.executeTask(taskSupplier(), enoughTime)
53         then: 'an event is logged with level INFO'
54             new PollingConditions().within(1) {
55                 def loggingEvent = getLoggingEvent()
56                 assert loggingEvent.level == Level.INFO
57             }
58         and: 'the log indicates the task completed successfully'
59             assert loggingEvent.formattedMessage == 'Async task completed successfully.'
60     }
61
62     def 'Execute failing task.'() {
63         when: 'task is executed'
64             objectUnderTest.executeTask(taskSupplierForFailingTask(), enoughTime)
65         then: 'an event is logged with level ERROR'
66             new PollingConditions().within(1) {
67                 def loggingEvent = getLoggingEvent()
68                 assert loggingEvent.level == Level.ERROR
69             }
70         and: 'the original error message is logged'
71             assert loggingEvent.formattedMessage.contains('original exception message')
72     }
73
74     def taskSupplier() {
75         return () -> 'hello world'
76     }
77
78     def taskSupplierForFailingTask() {
79         return () -> { throw new RuntimeException('original exception message') }
80     }
81
82     def getLoggingEvent() {
83         return logger.list[0]
84     }
85
86 }