package com.stratio.cassandra.index.schema.analysis;

import com.stratio.cassandra.util.JsonSerializer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.analysis.es.SpanishAnalyzer;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Andres de la Pena <adelapena@stratio.com>
 */
public class AnalysisTest {

    @Test
    public void testDefaultConstructor() {
        Analysis analysis = new Analysis();
        Analyzer analyzer = analysis.getAnalyzer("English");
        Assert.assertEquals(EnglishAnalyzer.class, analyzer.getClass());
        analysis.close();
    }

    @Test
    public void testRegularConstructor() {
        Map<String, AnalyzerBuilder> analyzers = new HashMap<>();
        analyzers.put("custom", new ClasspathAnalyzerBuilder("org.apache.lucene.analysis.es.SpanishAnalyzer"));
        Analysis analysis = new Analysis(analyzers);

        Analyzer englishAnalyzer = analysis.getAnalyzer("English");
        Assert.assertEquals(EnglishAnalyzer.class, englishAnalyzer.getClass());

        Analyzer customAnalyzer = analysis.getAnalyzer("custom");
        Assert.assertEquals(SpanishAnalyzer.class, customAnalyzer.getClass());

        analysis.close();
    }

    @Test
    public void testRegularConstructorWithNullAnalyzers() {
        Analysis analysis = new Analysis(null);
        Analyzer analyzer = analysis.getAnalyzer("English");
        Assert.assertEquals(EnglishAnalyzer.class, analyzer.getClass());
        analysis.close();
    }

    @Test
    public void testRegularConstructorWithEmptyAnalyzers() {
        Map<String, AnalyzerBuilder> analyzers = new HashMap<>();
        Analysis analysis = new Analysis(analyzers);
        Analyzer analyzer = analysis.getAnalyzer("English");
        Assert.assertEquals(EnglishAnalyzer.class, analyzer.getClass());
        analysis.close();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testGetAnalyzerNotExistent() {
        Analysis analysis = new Analysis();
        analysis.getAnalyzer("custom");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testGetAnalyzerNull() {
        Analysis analysis = new Analysis();
        analysis.getAnalyzer(null);
        analysis.close();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testGetAnalyzerEmpty() {
        Analysis analysis = new Analysis();
        analysis.getAnalyzer(" \t");
        analysis.close();
    }
}

@@ -700,40 +700,14 @@
 ));%0A
-        analysis.close();%0A
     %7D%0A
+
 %0A   
@@ -1285,35 +1285,8 @@
 ());
-%0A%0A        analysis.close();
 %0A   
@@ -1545,34 +1545,8 @@
 ));%0A
-        analysis.close();%0A
     
@@ -1876,34 +1876,8 @@
 ));%0A
-        analysis.close();%0A
     
@@ -2247,34 +2247,8 @@
 l);%0A
-        analysis.close();%0A
     
@@ -2380,32 +2380,32 @@
 new Analysis();%0A
+
         analysis
@@ -2429,34 +2429,8 @@
 %22);%0A
-        analysis.close();%0A
     

Fix analysis test.