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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.rest.executor
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
31 class CpsNcmpTaskExecutorSpec extends Specification {
33 def objectUnderTest = new CpsNcmpTaskExecutor()
34 def logger = Spy(ListAppender<ILoggingEvent>)
38 ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).addAppender(logger)
43 ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).detachAndStopAllAppenders()
46 def 'Execute successful task.'() {
47 when: 'task is executed'
48 objectUnderTest.executeTask(taskSupplier(), enoughTime)
49 then: 'an event is logged with level INFO'
50 new PollingConditions().within(1) {
51 def loggingEvent = getLoggingEvent()
52 assert loggingEvent.level == Level.INFO
54 and: 'the log indicates the task completed successfully'
55 assert loggingEvent.formattedMessage == 'Async task completed successfully.'
58 def 'Execute failing task.'() {
59 when: 'task is executed'
60 objectUnderTest.executeTask(taskSupplierForFailingTask(), enoughTime)
61 then: 'an event is logged with level ERROR'
62 new PollingConditions().within(1) {
63 def loggingEvent = getLoggingEvent()
64 assert loggingEvent.level == Level.ERROR
66 and: 'the original error message is logged'
67 assert loggingEvent.formattedMessage.contains('original exception message')
71 return () -> 'hello world'
74 def taskSupplierForFailingTask() {
75 return () -> { throw new RuntimeException('original exception message') }
78 def getLoggingEvent() {