package com.wikia.pandora.core.metrics;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import javax.validation.constraints.AssertTrue;

import static org.junit.Assert.*;

public class RegexFilterTest {

  @Test
  public void testIncludeMatches() throws Exception {
    ImmutableSet<String> includes = new ImmutableSet.Builder<String>()
        .add("([a-z]){5}")
        .add("([0-4]){3}")
        .add("simple")
        .add("([a-z]+)[0-9]{2}")
        .build();

    RegexFilter regexFilter = new RegexFilter(includes, null);

    assertTrue(regexFilter.matches("abcde", null));
    assertTrue(regexFilter.matches("simple", null));
    assertTrue(regexFilter.matches("test09", null));
    assertTrue(regexFilter.matches("012", null));

    assertFalse(regexFilter.matches("abcdef", null));
    assertFalse(regexFilter.matches("4567", null));
    assertFalse(regexFilter.matches("", null));
  }


  @Test
  public void testExcludeMatches() throws Exception {
    ImmutableSet<String> excludes = new ImmutableSet.Builder<String>()
        .add("([a-z]){5}")
        .add("([0-4]){3}")
        .add("simple")
        .add("([a-z]+)[0-9]{2}")
        .build();

    RegexFilter regexFilter = new RegexFilter(null, excludes);

    assertTrue(regexFilter.matches("abcdef", null));
    assertTrue(regexFilter.matches("4567", null));
    assertTrue(regexFilter.matches("", null));

    assertFalse(regexFilter.matches("abcde", null));
    assertFalse(regexFilter.matches("simple", null));
    assertFalse(regexFilter.matches("test09", null));
    assertFalse(regexFilter.matches("012", null));

  }

  @Test
  public void testIncludeFromExcludeMatches() throws Exception {
    ImmutableSet<String> includes = new ImmutableSet.Builder<String>()
        .add("([a-z]){5}[0-9]{2}")
        .add("([0-4]){2}")
        .build();
    ImmutableSet<String> excludes = new ImmutableSet.Builder<String>()
        .add("([a-z])+[0-9]{2}")
        .add("([0-9]+)")
        .build();

    RegexFilter regexFilter = new RegexFilter(includes, excludes);

    assertTrue(regexFilter.matches("abcde12", null));
    assertTrue(regexFilter.matches("12", null));

    assertFalse(regexFilter.matches("abcdef12", null));
  }

  @Test
  public void testExcludeFromIncludeMatches() throws Exception {
    ImmutableSet<String> includes = new ImmutableSet.Builder<String>()
        .add("([a-z]+)[0-9]{2}")
        .add("([0-9]+)")
        .build();
    ImmutableSet<String> excludes = new ImmutableSet.Builder<String>()
        .add("([a-z]){5}[0-9]{2}")
        .add("([0-4]){2}")
        .build();

    RegexFilter regexFilter = new RegexFilter(includes, excludes, true);

    assertTrue(regexFilter.matches("abcdef12", null));
    
    assertFalse(regexFilter.matches("12", null));
    assertFalse(regexFilter.matches("abcde12", null));
  }
}

@@ -2880,13 +2880,9 @@
 ));%0A
-    
 %0A
+
     
@@ -2919,32 +2919,32 @@
 s(%2212%22, null));%0A
-
     assertFalse(
@@ -2985,11 +2985,497 @@
 l));%0A  %7D
+%0A%0A  @Test%0A  public void testMatchesWithoutIncludeOrExclude() %7B%0A    RegexFilter regexFilter = new RegexFilter(null, null);%0A%0A    assertTrue(regexFilter.matches(%22abcdef%22, null));%0A    assertTrue(regexFilter.matches(%224567%22, null));%0A    assertTrue(regexFilter.matches(%22%22, null));%0A    assertTrue(regexFilter.matches(%22abcde%22, null));%0A    assertTrue(regexFilter.matches(%22simple%22, null));%0A    assertTrue(regexFilter.matches(%22test09%22, null));%0A    assertTrue(regexFilter.matches(%22012%22, null));%0A%0A  %7D
 %0A%7D%0A

add RegexFilter test with no include, or exclude list