Merge "Use constants for magic numbers in perf tests"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / notification / NotificationErrorHandlerSpec.groovy
index 4cacc43..89e305a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation
+ *  Copyright (C) 2022-2023 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 
 package org.onap.cps.notification
 
+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.slf4j.LoggerFactory
+
 import spock.lang.Specification
 
 class NotificationErrorHandlerSpec extends Specification{
 
     NotificationErrorHandler objectUnderTest = new NotificationErrorHandler()
+    def logWatcher = Spy(ListAppender<ILoggingEvent>)
+
+    @BeforeEach
+    void setup() {
+        ((Logger) LoggerFactory.getLogger(NotificationErrorHandler.class)).addAppender(logWatcher);
+        logWatcher.start();
+    }
 
-    def 'Logging exception via notification error handler'() {
-        given: 'redirect system.out to a readable stream'
-            def systemOutAsStream = new ByteArrayOutputStream()
-            System.out = new PrintStream(systemOutAsStream)
-        when: 'some exception occurs'
-            objectUnderTest.onException(new Exception('sample exception'), 'some context')
+    @AfterEach
+    void teardown() {
+        ((Logger) LoggerFactory.getLogger(NotificationErrorHandler.class)).detachAndStopAllAppenders();
+    }
+
+    def 'Logging exception via notification error handler #scenario'() {
+        when: 'exception #scenario occurs'
+            objectUnderTest.onException(exception, 'some context')
         then: 'log output results contains the correct error details'
-            def systemOutAsString = systemOutAsStream.toString()
-            systemOutAsString.contains('Failed to process')
-            systemOutAsString.contains('Error cause: sample exception')
-            systemOutAsString.contains('Error context: [some context]')
+            def logMessage = logWatcher.list[0].getFormattedMessage()
+            assert logMessage.contains('Failed to process')
+            assert logMessage.contains("Error cause: ${exptectedCauseString}")
+            assert logMessage.contains("Error context: [some context]")
+        where:
+            scenario        | exception                                               || exptectedCauseString
+            'with cause'    | new Exception('message')                                || 'message'
+            'without cause' | new Exception('message', new RuntimeException('cause')) || 'java.lang.RuntimeException: cause'
     }
 }
-