/*
 * 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.test.integration.search.basic;

import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.Arrays;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.junit.Test;



public class SearchWhileCreatingIndexTests extends AbstractSharedClusterTest {


    protected int numberOfNodes() {
        return 1;
    }

    /**
     * This test basically verifies that search with a single shard active (cause we indexed to it) and other
     * shards possibly not active at all (cause they haven't allocated) will still work.
     */
    @Test
    public void searchWhileCreatingIndex() {
        cluster().ensureAtMostNumNodes(1); // this test is very fragile with more than one node...
        for (int i = 0; i < 20; i++) {
            run(prepareCreate("test").setSettings(settingsBuilder().put("index.number_of_shards", 10)));
            run(client().prepareIndex("test", "type1", "id:" + i).setSource("field", "test"));
            refresh();
            SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.termQuery("field", "test")).execute().actionGet();
            assertThat("Found unexpected number of hits ShardFailures:" + Arrays.toString(searchResponse.getShardFailures()) + " id: " + i, searchResponse.getHits().totalHits(), equalTo(1l));
            wipeIndex("test");
        }
    }
}
@@ -861,31 +861,24 @@
 ic;%0A%0Aimport 
-static 
 org.elastics
@@ -887,183 +887,53 @@
 rch.
-common.settings.ImmutableSettings.settingsBuilder;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.equalTo;%0A%0Aimport java.util.Arrays;%0A
+action.admin.indices.refresh.RefreshResponse;
 %0Aimp
@@ -1129,16 +1129,228 @@
 .Test;%0A%0A
+import java.util.Arrays;%0A%0Aimport static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;%0Aimport static org.hamcrest.Matchers.equalTo;%0Aimport static org.hamcrest.Matchers.greaterThanOrEqualTo;%0A
 %0A%0Apublic
@@ -1423,17 +1423,16 @@
 Test %7B%0A%0A
-%0A
     prot
@@ -1758,107 +1758,8 @@
 ) %7B%0A
-        cluster().ensureAtMostNumNodes(1); // this test is very fragile with more than one node...%0A
     
@@ -1805,20 +1805,16 @@
         
-run(
 prepareC
@@ -1891,17 +1891,16 @@
 s%22, 10))
-)
 ;%0A      
@@ -1909,12 +1909,8 @@
     
-run(
 clie
@@ -1985,33 +1985,297 @@
 st%22)
-);%0A            refresh();
+.execute().actionGet();%0A            RefreshResponse refreshResponse = client().admin().indices().prepareRefresh(%22test%22).execute().actionGet();%0A            assertThat(refreshResponse.getSuccessfulShards(), greaterThanOrEqualTo(1)); // at least one shard should be successful when refreshing
 %0A   
@@ -2444,17 +2444,17 @@
 rtThat(%22
-F
+f
 ound une
@@ -2479,22 +2479,70 @@
 hits
- S
+, s
 hard
-F
+_f
 ailures
+ (we expected to potentially non active ones!)
 :%22 +

improve test the test checks that even though some shards might not be active, search will still work