/*
 * Licensed to ElasticSearch and Shay Banon 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.index.engine.robin;

import org.elasticsearch.action.admin.indices.segments.IndexSegments;
import org.elasticsearch.action.admin.indices.segments.IndexShardSegments;
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse;
import org.elasticsearch.action.admin.indices.segments.ShardSegments;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.engine.Segment;
import org.elasticsearch.test.AbstractIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.Collection;

public class RobinEngineIntegrationTest extends AbstractIntegrationTest {

    @Test
    public void testSetIndexCompoundOnFlush() {
        client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.builder().put("number_of_replicas", 0));
        client().prepareIndex("test", "foo").setSource("field", "foo").get();
        refresh();
        assertTotalCompoundSegments(2, 2, "test");
        client().admin().indices().prepareUpdateSettings("test")
                .setSettings(ImmutableSettings.builder().put(RobinEngine.INDEX_COMPOUND_ON_FLUSH, false)).get();
        client().prepareIndex("test", "foo").setSource("field", "foo").get();
        refresh();
        assertTotalCompoundSegments(2, 4, "test");
        
        client().admin().indices().prepareUpdateSettings("test")
        .setSettings(ImmutableSettings.builder().put(RobinEngine.INDEX_COMPOUND_ON_FLUSH, true)).get();
        client().prepareIndex("test", "foo").setSource("field", "foo").get();
        refresh();
        assertTotalCompoundSegments(4, 6, "test");

    }

    private void assertTotalCompoundSegments(int i, int t, String index) {
        IndicesSegmentResponse indicesSegmentResponse = client().admin().indices().prepareSegments(index).get();
        IndexSegments indexSegments = indicesSegmentResponse.getIndices().get(index);
        Collection<IndexShardSegments> values = indexSegments.getShards().values();
        int compounds = 0;
        int total = 0;
        for (IndexShardSegments indexShardSegments : values) {
            for (ShardSegments s : indexShardSegments) {
                for (Segment segment : s.getSegments()) {
                    if (segment.isCompound()) {
                        compounds++;
                    }
                    total++;
                }
            }
        }
        assertThat(compounds, Matchers.equalTo(i));
        assertThat(total, Matchers.equalTo(t));

    }
}

@@ -1645,19 +1645,52 @@
 cas%22, 0)
+.put(%22number_of_shards%22, 1)).get(
 );%0A
-
         
@@ -1818,12 +1818,12 @@
 nts(
-2, 2
+1, 1
 , %22t
@@ -2144,12 +2144,12 @@
 nts(
-2, 4
+1, 2
 , %22t
@@ -2470,12 +2470,12 @@
 nts(
-4, 6
+2, 3
 , %22t
@@ -3061,26 +3061,90 @@
  : s
-.getSegments()) %7B%0A
+) %7B%0A                    if (segment.isSearch() && segment.getNumDocs() %3E 0) %7B%0A    
     
@@ -3211,16 +3211,20 @@
         
+    
 compound
@@ -3240,32 +3240,36 @@
                 
+    
 %7D%0A              
@@ -3274,16 +3274,20 @@
         
+    
 total++;
@@ -3283,24 +3283,46 @@
    total++;%0A
+                    %7D%0A
             

Fix RobinEngineIntegrationTest - missed to explicitly create the index