/*
 * Copyright 2020, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.testing.server.blackbox;

import io.spine.base.Error;
import io.spine.system.server.HandlerFailedUnexpectedly;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import java.util.logging.Logger;

import static com.google.common.truth.Truth.assertThat;
import static io.spine.base.Errors.causeOf;

@DisplayName("`ExceptionGuard` should")
final class ExceptionGuardTest extends ExceptionLoggingTest {

    private ExceptionGuard guard;

    @BeforeEach
    void initGuard() {
        guard = new ExceptionGuard();
    }

    @Test
    @DisplayName("log `HandlerFailedUnexpectedly` event")
    void tolerateException() {
        guard.tolerate();
        Error error = causeOf(new IllegalStateException("Test exception. Handler is fine."));
        guard.on(
                HandlerFailedUnexpectedly
                        .newBuilder()
                        .setEntity(entity())
                        .setError(error)
                        .vBuild()
        );
        assertLogged(error.getMessage());
    }

    @Test
    @DisplayName("fail test on `HandlerFailedUnexpectedly` event")
    @SuppressWarnings({"ThrowCaughtLocally", "ErrorNotRethrown"})
        // we're testing that guard is failing a test
    void failTest() {
        Error error = causeOf(new IllegalStateException("Test exception. Handler is fine."));
        try {
            guard.on(
                    HandlerFailedUnexpectedly
                            .newBuilder()
                            .setEntity(entity())
                            .setError(error)
                            .vBuild()
            );
            throw new IllegalStateException("The guard must have failed the test.");
        } catch (AssertionFailedError e) {
            assertThat(e.getMessage()).contains(error.getMessage());
        } catch (IllegalStateException e) {
            Assertions.fail(e);
        }
    }

    @Override
    protected Logger logger() {
        return Logger.getLogger(ExceptionGuard.class.getName());
    }
}

@@ -1138,49 +1138,8 @@
 ly;%0A
-import org.junit.jupiter.api.Assertions;%0A
 impo
@@ -1349,92 +1349,97 @@
 tic 
-com.google.common.truth.Truth.assertThat;%0Aimport static io.spine.base.Errors.causeOf
+io.spine.base.Errors.causeOf;%0Aimport static org.junit.jupiter.api.Assertions.assertThrows
 ;%0A%0A@
@@ -2237,128 +2237,8 @@
 t%22)%0A
-    @SuppressWarnings(%7B%22ThrowCaughtLocally%22, %22ErrorNotRethrown%22%7D)%0A        // we're testing that guard is failing a test%0A
     
@@ -2361,25 +2361,54 @@
     
-try %7B%0A           
+assertThrows(AssertionFailedError.class, () -%3E
  gua
@@ -2406,36 +2406,32 @@
 () -%3E guard.on(%0A
-    
                 
@@ -2472,36 +2472,32 @@
                 
-  
-  
 .newBuilder()%0A  
@@ -2486,36 +2486,32 @@
   .newBuilder()%0A
-    
                 
@@ -2555,36 +2555,32 @@
                 
-  
-  
 .setError(error)
@@ -2596,36 +2596,32 @@
                 
-    
 .vBuild()%0A      
@@ -2626,297 +2626,11 @@
     
- 
-   );%0A            throw new IllegalStateException(%22The guard must have failed the test.%22);%0A        %7D catch (AssertionFailedError e) %7B%0A            assertThat(e.getMessage()).contains(error.getMessage());%0A        %7D catch (IllegalStateException e) %7B%0A            Assertions.fail(e);%0A        %7D
+));
 %0A   

Use `assertThrows` over `try-catch`