package de.tblsoft.solr.pipeline.filter;

import de.tblsoft.solr.pipeline.test.AbstractFilterTest;
import org.junit.Test;

/**
 * Created by tblsoft on 26.04.16.
 */
public class MappingFilterTest extends AbstractFilterTest {

    @Override
    public void configure() {
        setClazz(MappingFilter.class);
    }

    @Test
    public void testPassthrough() {
        configure();
        createField("foo", "bar");
        runTest();
        assertNumberOfDocuments(0);
        assertInitWasDelegated();
        assertEndWasDelegated();
    }

    @Test
    public void testEmptyDocument() {
        configure();
        runTest();
        assertNumberOfDocuments(0);
        assertInitWasDelegated();
        assertEndWasDelegated();
    }

    @Test
    public void testJoin() {
        configure();
        addProperty("mapping", "join:name=${firstname} ${lastname}");
        createField("firstname", "John");
        createField("lastname", "Doe");
        runTest();
        assertFiled("name", "John Doe");
        assertNumberOfDocuments(1);
        assertNumberOfFields(1);
    }

    @Test
    public void testMapping() {
        configure();
        addProperty("mapping", "firstname->forename");
        addProperty("mapping", "lastname->surname");
        createField("foo", "bar");
        createField("firstname", "John");
        createField("lastname", "Doe");
        runTest();
        assertFiled("forename", "John");
        assertFiled("surname", "Doe");
        assertNumberOfDocuments(1);
        assertNumberOfFields(2);
    }


    @Test
    public void testMappingAddEmptyFieldIfNotExists() {
        configure();
        putProperty("addEmptyFieldIfNotExists", "true");
        addProperty("mapping", "firstname->forename");
        addProperty("mapping", "lastname->surname");
        addProperty("mapping", "strasse->street");
        addProperty("mapping", "strasse->street_additional");
        createField("foo", "bar");
        createField("firstname", "John");
        createField("lastname", "Doe");
        runTest();
        assertFiled("forename", "John");
        assertFiled("surname", "Doe");
        assertFiled("street", "");
        assertFiled("street_additional", "");
        assertNumberOfDocuments(1);
        assertNumberOfFields(4);
    }
    
    @Test
    public void testMappingTrimLowercaseMd5() {
        configure();
        addProperty("mapping", "noun->noun|trim|lowercase|md5");
        createField("noun", " Substantiv ");
        runTest();
        assertFiled("noun", "0f194a56105d09847fffcdf8c9eaf979");
        assertNumberOfDocuments(1);
        assertNumberOfFields(1);
    }
    
    @Test
    public void testMappingForMultipleFields() {
        configure();
        addProperty("mapping", "noun->title");
        addProperty("mapping", "noun->id|lowercase");
        addProperty("mapping", "noun->noun|trim|lowercase|md5");
        createField("noun", " Substantiv ");
        runTest();
        assertFiled("title", " Substantiv ");
        assertFiled("id", " substantiv ");
        assertFiled("noun", "0f194a56105d09847fffcdf8c9eaf979");
        assertNumberOfDocuments(1);
        assertNumberOfFields(3);
    }
    
    
    @Test(expected=IllegalArgumentException.class)
    public void testMappingFunctionNotImplemented() {
        configure();
        addProperty("mapping", "noun->noun|lowercase|md5|unknown");
        createField("noun", "Substantiv");
        runTest();
    }
    
    
}

@@ -109,20 +109,17 @@
 g.junit.
-Test
+*
 ;%0A%0A/**%0A 
@@ -2276,32 +2276,707 @@
 Fields(4);%0A    %7D
+%0A%0A    @Test%0A    public void testMappingSortFieldsByName() %7B%0A        configure();%0A        putProperty(%22sortFieldsByName%22, %22true%22);%0A        addProperty(%22mapping%22, %22a-%3Ed%22);%0A        addProperty(%22mapping%22, %22b-%3Ec%22);%0A        createField(%22foo%22, %22bar%22);%0A        createField(%22b%22, %22b_value%22);%0A        createField(%22a%22, %22a_value%22);%0A        runTest();%0A        assertFiled(%22d%22, %22a_value%22);%0A        assertFiled(%22c%22, %22b_value%22);%0A%0A        Assert.assertEquals(%22c%22,this.outputDocumentList.get(0).getFields().get(0).getName());%0A        Assert.assertEquals(%22d%22,this.outputDocumentList.get(0).getFields().get(1).getName());%0A        assertNumberOfDocuments(1);%0A        assertNumberOfFields(2);%0A    %7D
 %0A    %0A    @Test%0A

add a test for the sorting of the mapped fields by name