package com.atlassian.bitbucket.mesh.logback;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.core.spi.FilterReply;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;

import static com.atlassian.bitbucket.mesh.logback.KnownMessageLevelAdjustingFilter.FILTERED;
import static org.hamcrest.Matchers.is;

@RunWith(Parameterized.class)
public class KnownMessageLevelAdjustingFilterTest {

    private final KnownMessageLevelAdjustingFilter underTest = new KnownMessageLevelAdjustingFilter();

    @Parameterized.Parameter(5)
    public FilterReply expected;
    @Parameterized.Parameter(4)
    public String format;
    @Parameterized.Parameter(3)
    public Level level;
    @Parameterized.Parameter(2)
    public String loggerName;
    @Parameterized.Parameter(1)
    public Marker marker;
    @Parameterized.Parameter(0)
    public String testName;

    @Parameterized.Parameters(name = "{0}")
    public static Object[][] data() {
        return new Object[][]{
                {"testAllNull", null, null, null, null, FilterReply.NEUTRAL},
                {"testNettyServerHandlerStreamError", null, "io.grpc.netty.NettyServerHandler", Level.WARN, "Stream Error", FilterReply.DENY},
                {"testNettyServerHandlerFiltering", FILTERED, "io.grpc.netty.NettyServerHandler", Level.WARN, "Stream Error", FilterReply.NEUTRAL},
                {"testNettyServerHandlerConnectionError", null, "io.grpc.netty.NettyServerHandler", Level.INFO, "Connection Error", FilterReply.NEUTRAL},
                {"testOtherLoggerWarn", null, "io.some.class", Level.WARN, "Some Error", FilterReply.NEUTRAL},
        };
    }

    @Test
    public void testFilter() {
        MatcherAssert.assertThat(underTest.decide(marker, loggerName == null ? null : (Logger) LoggerFactory.getLogger(loggerName), level, format, null, null), is(expected));
    }
}