package org.getopentest.logging;

import static org.testng.Assert.*;
import org.testng.annotations.Test;

public class BaseLoggerNGTest {

    public BaseLoggerNGTest() {
    }

    @Test
    public void addSecretContainingRegexpChars() {
        TestLogger testLogger = new TestLogger();
        String regexSpecialChars = "[{}()\\\\[\\\\].+*?^$\\\\\\\\|]";
        testLogger.addSecret("secret1");
        testLogger.addSecret(regexSpecialChars);
        testLogger.addSecret("secret3");
        
        testLogger.info("ABC secret1 DEF " + regexSpecialChars + " GHI secret3 JKL");
        
        assertTrue(!testLogger.lastLogText.contains("secret1"));
        assertTrue(!testLogger.lastLogText.contains(regexSpecialChars));
        assertTrue(!testLogger.lastLogText.contains("secret3"));
    }
    
    @Test
    public void addSecretByRegex() {
        TestLogger testLogger = new TestLogger();
        testLogger.addSecret("secret1");
        // Use a regex positive lookbehind
        testLogger.addSecretByRegex("(?<=prefix1 )regexp_secret\\d");
        
        testLogger.info("ABC secret1 DEF prefix1 regexp_secret1 regexp_secret2");
        
        assertTrue(!testLogger.lastLogText.contains("secret1"));
        assertTrue(!testLogger.lastLogText.contains("regexp_secret1"));
        assertTrue(testLogger.lastLogText.contains("regexp_secret2"));
    }
    
    @Test
    public void addSecretByRegex_multiple_regexes() {
        TestLogger testLogger = new TestLogger();
        testLogger.addSecret("secret1");
        // Use a regex positive lookbehind
        testLogger.addSecretByRegex("(?<=prefix )regexp_secret\\d");
        
        testLogger.info("ABC secret1 DEF prefix regexp_secret1 prefix regexp_secret2");
        
        assertTrue(!testLogger.lastLogText.contains("secret1"));
        assertTrue(!testLogger.lastLogText.contains("regexp_secret1"));
        assertTrue(!testLogger.lastLogText.contains("regexp_secret2"));
    }
    
    @Test
    public void clearSecrets() {
        TestLogger testLogger = new TestLogger();
        testLogger.setLevel(LogLevel.INFO);
        testLogger.addSecret("secret1");
        
        testLogger.info("ABC secret1 DEF");
        assertTrue(!testLogger.lastLogText.contains("secret1"));
        
        testLogger.clearSecrets();
        testLogger.info("ABC secret1 DEF");
        assertTrue(testLogger.lastLogText.contains("secret1"));
    }
    
    @Test
    public void debug() {
        TestLogger testLogger = new TestLogger();
        testLogger.setLevel(LogLevel.DEBUG);
        testLogger.addSecret("secret1");
        
        testLogger.debug("ABC secret1 DEF");
        
        assertTrue(!testLogger.lastLogText.contains("secret1"));
    }
    
    @Test
    public void error() {
        TestLogger testLogger = new TestLogger();
        testLogger.setLevel(LogLevel.ERROR);
        testLogger.addSecret("secret1");
        
        testLogger.error("ABC secret1 DEF");
        
        assertTrue(!testLogger.lastLogText.contains("secret1"));
    }
    
    @Test
    public void info() {
        TestLogger testLogger = new TestLogger();
        testLogger.setLevel(LogLevel.INFO);
        testLogger.addSecret("secret1");
        
        testLogger.info("ABC secret1 DEF");
        
        assertTrue(!testLogger.lastLogText.contains("secret1"));
    }
    
    @Test
    public void setMaskSecrets() {
        TestLogger testLogger = new TestLogger();
        testLogger.setLevel(LogLevel.INFO);
        testLogger.addSecret("secret1");
        
        testLogger.info("ABC secret1 DEF");
        assertTrue(!testLogger.lastLogText.contains("secret1"));
        
        testLogger.setMaskSecrets(false);
        testLogger.info("ABC secret1 DEF");
        assertTrue(testLogger.lastLogText.contains("secret1"));
    }
    
    @Test
    public void trace() {
        TestLogger testLogger = new TestLogger();
        testLogger.setLevel(LogLevel.TRACE);
        testLogger.addSecret("secret1");
        
        testLogger.trace("ABC secret1 DEF");
        
        assertTrue(!testLogger.lastLogText.contains("secret1"));
    }
    
    @Test
    public void warning() {
        TestLogger testLogger = new TestLogger();
        testLogger.setLevel(LogLevel.WARN);
        testLogger.addSecret("secret1");
        
        testLogger.warning("ABC secret1 DEF");
        
        assertTrue(!testLogger.lastLogText.contains("secret1"));
    }
}
