Correct use of Spock setup and cleanup methods (no need for @Before @After)
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / executor / CpsNcmpTaskExecutorSpec.groovy
index 870c36c..010eda9 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============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.
@@ -24,11 +24,9 @@ import ch.qos.logback.classic.Level
 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 {
 
@@ -36,25 +34,23 @@ 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.'
     }
@@ -62,11 +58,11 @@ class CpsNcmpTaskExecutorSpec extends Specification {
     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')
     }