package org.netmelody.cieye.server.configuration.test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.netmelody.cieye.core.domain.Sponsor;
import org.netmelody.cieye.server.configuration.RecordedKnownOffenders;
import org.netmelody.cieye.server.configuration.SettingsFile;

import static org.hamcrest.Matchers.is;


public final class RecordedKnownOffendersTest {

    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();
    private RecordedKnownOffenders offenders;

    @Before
    public void createOffendersFile() throws IOException {
        final File peeps = testFolder.newFile("peeps.txt");
        FileUtils.copyInputStreamToFile(RecordedKnownOffenders.class.getResourceAsStream("templates/pictures.txt.template"),
                                        peeps);
        offenders = new RecordedKnownOffenders(new SettingsFile(peeps));
    }
    
    @Test public void
    looksUpSimpleOffenderStrings() {
        assertThat(offenders.search("vlad"), contains(new Sponsor("", "/pictures/vlad.png")));
    }
    
    @Test public void
    looksUpWrappedOffenderStrings() {
        assertThat(offenders.search("-vlad-"), contains(new Sponsor("", "/pictures/vlad.png")));
    }
    
    @Test public void
    looksUpOffenderStringsAtTheBegginingOfALine() {
        assertThat(offenders.search("someguff\nvlad: did stuff"), contains(new Sponsor("", "/pictures/vlad.png")));
    }
    
    @Test public void
    looksUpMultipleOffenders() {
        assertThat(offenders.search("vlad/stupid:"), contains(new Sponsor("", "/pictures/vlad.png"),
                                                              new Sponsor("", "/pictures/stupid.png")));
    }
    
    @Ignore("pending implementation")
    @Test public void
    ignoresOffenderNamesAppearingInTheMiddleOfAWord() {
        assertThat(offenders.search("markovladies"), is(Matchers.<Sponsor>empty()));
    }
}

@@ -1366,32 +1366,214 @@
 est public void%0A
+    looksUpSimpleOffenderStringsCaseInsensitively() %7B%0A        assertThat(offenders.search(%22VlAd%22), contains(new Sponsor(%22%22, %22/pictures/vlad.png%22)));%0A    %7D%0A    %0A    @Test public void%0A
     looksUpWrapp
@@ -2333,20 +2333,20 @@
 h(%22marko
-vlad
+VLAD
 ies%22), i

test for case insensitive matching