/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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 org.elasticsearch.ingest.processor;

import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.ingest.core.CompoundProcessor;
import org.elasticsearch.ingest.core.IngestDocument;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.elasticsearch.ingest.core.Processor;
import org.elasticsearch.ingest.core.TemplateService;
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.CoreMatchers;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class SplitProcessorTests extends ESTestCase {

    public void testSplit() throws Exception {
        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
        String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "127.0.0.1");
        Processor processor = new SplitProcessor(randomAsciiOfLength(10), fieldName, "\\.");
        processor.execute(ingestDocument);
        assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(Arrays.asList("127", "0", "0", "1")));
    }

    public void testSplitFieldNotFound() throws Exception {
        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
        String fieldName = RandomDocumentPicks.randomFieldName(random());
        Processor processor = new SplitProcessor(randomAsciiOfLength(10), fieldName, "\\.");
        try {
            processor.execute(ingestDocument);
            fail("split processor should have failed");
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), containsString("not present as part of path [" + fieldName + "]"));
        }
    }

    public void testSplitNullValue() throws Exception {
        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null));
        Processor processor = new SplitProcessor(randomAsciiOfLength(10), "field", "\\.");
        try {
            processor.execute(ingestDocument);
            fail("split processor should have failed");
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), equalTo("field [field] is null, cannot split."));
        }
    }

    public void testSplitNonStringValue() throws Exception {
        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
        String fieldName = RandomDocumentPicks.randomFieldName(random());
        ingestDocument.setFieldValue(fieldName, randomInt());
        Processor processor = new SplitProcessor(randomAsciiOfLength(10), fieldName, "\\.");
        try {
            processor.execute(ingestDocument);
            fail("split processor should have failed");
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]"));
        }
    }

    public void testSplitAppendable() throws Exception {
        TemplateService templateService = TestTemplateService.instance();
        Map splitConfig = new HashMap<>();
        splitConfig.put("field", "flags");
        splitConfig.put("separator", "\\|");
        Processor splitProcessor = (new SplitProcessor.Factory()).create(splitConfig);
        Map appendConfig = new HashMap<>();
        appendConfig.put("field", "flags");
        appendConfig.put("value", Collections.singletonList("additional_flag"));
        Processor appendProcessor = (new AppendProcessor.Factory(templateService)).create(appendConfig);
        CompoundProcessor compoundProcessor = new CompoundProcessor(splitProcessor, appendProcessor);
        Map<String, Object> source = new HashMap<>();
        source.put("flags", "new|hot|super|fun|interesting");
        IngestDocument ingestDocument = new IngestDocument(source, new HashMap<>());
        compoundProcessor.execute(ingestDocument);
        List<String> expectedFlags = Arrays.asList("new", "hot", "super", "fun", "interesting", "additional_flag");
        assertThat(ingestDocument.getFieldValue("flags", List.class), CoreMatchers.equalTo(expectedFlags));
    }
}

@@ -864,83 +864,27 @@
 est.
-TestTemplateService;%0Aimport org.elasticsearch.ingest.core.CompoundProcessor
+RandomDocumentPicks
 ;%0Aim
@@ -970,205 +970,64 @@
 est.
-RandomDocumentPicks;%0Aimport org.elasticsearch.ingest.core.Processor;%0Aimport org.elasticsearch.ingest.core.TemplateService;%0Aimport org.elasticsearch.test.ESTestCase;%0Aimport org.hamcrest.CoreMatchers
+core.Processor;%0Aimport org.elasticsearch.test.ESTestCase
 ;%0A%0Ai
@@ -3774,85 +3774,27 @@
     
-TemplateService templateService = TestTemplateService.instance();%0A        Map
+Map%3CString, Object%3E
  spl
@@ -4000,384 +4000,8 @@
 g);%0A
-        Map appendConfig = new HashMap%3C%3E();%0A        appendConfig.put(%22field%22, %22flags%22);%0A        appendConfig.put(%22value%22, Collections.singletonList(%22additional_flag%22));%0A        Processor appendProcessor = (new AppendProcessor.Factory(templateService)).create(appendConfig);%0A        CompoundProcessor compoundProcessor = new CompoundProcessor(splitProcessor, appendProcessor);%0A
     
@@ -4209,16 +4209,13 @@
     
-compound
+split
 Proc
@@ -4257,37 +4257,167 @@
     
-List%3CString%3E expectedFlags = 
+@SuppressWarnings(%22unchecked%22)%0A        List%3CString%3E flags = (List%3CString%3E)ingestDocument.getFieldValue(%22flags%22, List.class);%0A        assertThat(flags, equalTo(
 Arra
@@ -4469,16 +4469,68 @@
 resting%22
+)));%0A        ingestDocument.appendFieldValue(%22flags%22
 , %22addit
@@ -4617,42 +4617,93 @@
 s), 
-CoreMatchers.equalTo(expectedFlags
+equalTo(Arrays.asList(%22new%22, %22hot%22, %22super%22, %22fun%22, %22interesting%22, %22additional_flag%22)
 ));%0A

Simplify SplitProcessorTests#testSplitAppendable to not require compound and append processor