/*
 * Licensed to David Pilato (the "Author") under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Author licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package fr.pilato.elasticsearch.crawler.fs.settings;

import fr.pilato.elasticsearch.crawler.fs.test.framework.AbstractFSCrawlerTestCase;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import static fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.isIndexable;
import static fr.pilato.elasticsearch.crawler.fs.settings.Fs.DEFAULT_EXCLUDED;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class FsMatchFilesTest extends AbstractFSCrawlerTestCase {

    @Test
    public void exclude_only() {
        assertThat(isIndexable("test.doc", new ArrayList<>(), Collections.singletonList("*.doc")), is(false));
        assertThat(isIndexable("test.xls", new ArrayList<>(), Collections.singletonList("*.doc")), is(true));
        assertThat(isIndexable("my.doc.xls", new ArrayList<>(), Collections.singletonList("*.doc")), is(true));
        assertThat(isIndexable("my.doc.xls", new ArrayList<>(), Arrays.asList("*.doc", "*.xls")), is(false));
        assertThat(isIndexable("my.doc.xls", new ArrayList<>(), Collections.singletonList("my.d?c*.xls")), is(false));
        assertThat(isIndexable("my.douc.xls", new ArrayList<>(), Collections.singletonList("my.d?c*.xls")), is(true));
        assertThat(isIndexable(".snapshots", new ArrayList<>(), Collections.singletonList(".snapshots")), is(false));
    }

    @Test
    public void include_only() {
        assertThat(isIndexable("test.doc", Collections.singletonList("*.doc"), new ArrayList<>()), is(true));
        assertThat(isIndexable("test.xls", Collections.singletonList("*.doc"), new ArrayList<>()), is(false));
        assertThat(isIndexable("my.doc.xls", Collections.singletonList("*.doc"), new ArrayList<>()), is(false));
        assertThat(isIndexable("my.doc.xls", Collections.singletonList("my.d?c*.xls"), new ArrayList<>()), is(true));
        assertThat(isIndexable("my.douc.xls", Collections.singletonList("my.d?c*.xls"), new ArrayList<>()), is(false));
    }

    @Test
    public void include_exclude() {
        assertThat(isIndexable("test.doc", Collections.singletonList("*.xls"), Collections.singletonList("*.doc")), is(false));
        assertThat(isIndexable("test.xls", Collections.singletonList("*.xls"), Collections.singletonList("*.doc")), is(true));
        assertThat(isIndexable("my.doc.xls", Collections.singletonList("*.xls"), Collections.singletonList("*.doc")), is(true));
        assertThat(isIndexable("my.doc.xls", Collections.singletonList("*.xls"), Collections.singletonList("my.d?c*.xls")), is(false));
        assertThat(isIndexable("my.douc.xls", Collections.singletonList("*.xls"), Collections.singletonList("my.d?c*.xls")), is(true));
    }

    @Test
    public void default_ignored_file() {
        assertThat(isIndexable("~mydoc", new ArrayList<>(), DEFAULT_EXCLUDED), is(false));
        assertThat(isIndexable("~", new ArrayList<>(), DEFAULT_EXCLUDED), is(false));
        assertThat(isIndexable("adoc.doc", new ArrayList<>(), DEFAULT_EXCLUDED), is(true));
        assertThat(isIndexable("mydoc~", new ArrayList<>(), DEFAULT_EXCLUDED), is(true));
    }

    @Test
    public void case_sensitive() {
        // Excludes
        assertThat(isIndexable("test.doc", new ArrayList<>(), Collections.singletonList("*.DOC")), is(false));
        assertThat(isIndexable("test.xls", new ArrayList<>(), Collections.singletonList("*.DOC")), is(true));
        assertThat(isIndexable("my.doc.xls", new ArrayList<>(), Collections.singletonList("*.DOC")), is(true));
        assertThat(isIndexable("my.doc.xls", new ArrayList<>(), Arrays.asList("*.DOC", "*.XLS")), is(false));
        assertThat(isIndexable("my.doc.xls", new ArrayList<>(), Collections.singletonList("MY.D?C*.XLS")), is(false));
        assertThat(isIndexable("my.douc.xls", new ArrayList<>(), Collections.singletonList("MY.d?C*.XLS")), is(true));
        assertThat(isIndexable(".snapshots", new ArrayList<>(), Collections.singletonList(".SNAPSHOTS")), is(false));

        // Includes
        assertThat(isIndexable("test.doc", Collections.singletonList("*.DOC"), new ArrayList<>()), is(true));
        assertThat(isIndexable("test.xls", Collections.singletonList("*.DOC"), new ArrayList<>()), is(false));
        assertThat(isIndexable("my.doc.xls", Collections.singletonList("*.DOC"), new ArrayList<>()), is(false));
        assertThat(isIndexable("my.doc.xls", Collections.singletonList("MY.D?C*.XLS"), new ArrayList<>()), is(true));
        assertThat(isIndexable("my.douc.xls", Collections.singletonList("MY.D?C*.XLS"), new ArrayList<>()), is(false));
    }
}

@@ -2198,32 +2198,263 @@
 )), is(false));%0A
+        assertThat(isIndexable(%22doc.doc%22, new ArrayList%3C%3E(), Arrays.asList(%22*.pdf%22, %22*.xls%22, %22*.doc%22)), is(false));%0A        assertThat(isIndexable(%22doc.ppt%22, new ArrayList%3C%3E(), Arrays.asList(%22*.pdf%22, %22*.xls%22, %22*.doc%22)), is(true));%0A
     %7D%0A%0A    @Test
@@ -3055,24 +3055,255 @@
 is(false));%0A
+        assertThat(isIndexable(%22doc.doc%22, Arrays.asList(%22*.pdf%22, %22*.xls%22, %22*.doc%22), new ArrayList%3C%3E()), is(true));%0A        assertThat(isIndexable(%22doc.ppt%22, Arrays.asList(%22*.pdf%22, %22*.xls%22, %22*.doc%22), new ArrayList%3C%3E()), is(false));%0A
     %7D%0A%0A    @

Add more tests for includes/excludes parameters