/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2023 Nordix Foundation
+ * Copyright (C) 2023-2024 Nordix Foundation
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.read.ListAppender
-import org.junit.jupiter.api.AfterEach
-import org.junit.jupiter.api.BeforeEach
-import org.onap.cps.notification.NotificationErrorHandler
import org.slf4j.LoggerFactory
import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
class CpsNcmpTaskExecutorSpec extends Specification {
def logger = Spy(ListAppender<ILoggingEvent>)
def enoughTime = 100
- @BeforeEach
void setup() {
- ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).addAppender(logger);
- logger.start();
+ ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).addAppender(logger)
+ logger.start()
}
- @AfterEach
- void teardown() {
- ((Logger) LoggerFactory.getLogger(NotificationErrorHandler.class)).detachAndStopAllAppenders();
+ void cleanup() {
+ ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).detachAndStopAllAppenders()
}
def 'Execute successful task.'() {
when: 'task is executed'
objectUnderTest.executeTask(taskSupplier(), enoughTime)
- and: 'wait a little for async execution completion'
- Thread.sleep(10)
then: 'an event is logged with level INFO'
- def loggingEvent = getLoggingEvent()
- assert loggingEvent.level == Level.INFO
+ new PollingConditions().within(1) {
+ def loggingEvent = getLoggingEvent()
+ assert loggingEvent.level == Level.INFO
+ }
and: 'the log indicates the task completed successfully'
assert loggingEvent.formattedMessage == 'Async task completed successfully.'
}
def 'Execute failing task.'() {
when: 'task is executed'
objectUnderTest.executeTask(taskSupplierForFailingTask(), enoughTime)
- and: 'wait a little for async execution completion'
- Thread.sleep(10)
then: 'an event is logged with level ERROR'
- def loggingEvent = getLoggingEvent()
- assert loggingEvent.level == Level.ERROR
+ new PollingConditions().within(1) {
+ def loggingEvent = getLoggingEvent()
+ assert loggingEvent.level == Level.ERROR
+ }
and: 'the original error message is logged'
assert loggingEvent.formattedMessage.contains('original exception message')
}