/*
 * 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.action.allterms;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugin.TokenPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.junit.Test;

import java.util.Collection;


/**
 *
 */
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, transportClientRatio = 0)
public class AllTermsIT extends ESIntegTestCase {

    @Override
    protected Collection<Class<? extends Plugin>> nodePlugins() {
        return pluginList(TokenPlugin.class);
    }


    @Test
    public void testSimpleTestOneDoc() throws Exception {
        indexDocs();
        refresh();
        AllTermsResponse response = new AllTermsRequestBuilder(client()).index("test").field("field").size(10).execute().actionGet(1000000);
        String[] expected = {"always", "be", "careful", "don't", "ever", "forget"};
        assertArrayEquals(response.allTerms.toArray(new String[2]), expected);
    }

    private void indexDocs() {
        client().prepareIndex("test", "type", "1").setSource("field", "don't  be").execute().actionGet();
        client().prepareIndex("test", "type", "2").setSource("field", "ever always forget  be").execute().actionGet();
        client().prepareIndex("test", "type", "3").setSource("field", "careful careful").execute().actionGet();
        client().prepareIndex("test", "type", "4").setSource("field", "ever always careful careful don't be forget be").execute().actionGet();
    }

    @Test
    public void testSimpleTestOneDocWithFrom() throws Exception {
        indexDocs();
        refresh();
        AllTermsResponse response = new AllTermsRequestBuilder(client()).index("test").field("field").size(10).from("careful").execute().actionGet(10000);
        String[] expected = {"don't", "ever", "forget"};
        assertArrayEquals(response.allTerms.toArray(new String[3]), expected);

        response = new AllTermsRequestBuilder(client()).index("test").field("field").size(10).from("ces").execute().actionGet(10000);
        String[] expected2 = {"don't", "ever", "forget"};
        assertArrayEquals(response.allTerms.toArray(new String[3]), expected2);
    }

    @Test
    @TestLogging("org.elasticsearch.action.allterms:TRACE")
    public void testSimpleTestOneDocWithFromAndMinDocFreq() throws Exception {
        createIndex();
        indexDocs();
        refresh();
        AllTermsResponse response = new AllTermsRequestBuilder(client()).index("test").field("field").size(10).from(" be").minDocFreq(3).execute().actionGet(10000);
        String[] expected = {"be"};
        assertArrayEquals(response.allTerms.toArray(new String[1]), expected);

        response = new AllTermsRequestBuilder(client()).index("test").field("field").size(10).minDocFreq(3).from("arg").execute().actionGet(10000);
        String[] expected2 = {"be"};
        assertArrayEquals(response.allTerms.toArray(new String[1]), expected2);
    }

    private void createIndex() {
        client().admin().indices().prepareCreate("test").setSettings(Settings.settingsBuilder().put("index.number_of_shards", 1)).get();
        ensureYellow("test");
    }
}

@@ -1125,16 +1125,46 @@
 ection;%0A
+import java.util.Collections;%0A
 %0A%0A/**%0A *
@@ -1445,16 +1445,145 @@
 %0A    %7D%0A%0A
+    protected Collection%3CClass%3C? extends Plugin%3E%3E transportClientPlugins() %7B%0A        return pluginList(TokenPlugin.class);%0A    %7D%0A
 %0A    @Te

fix transport client plugin list