/*
 * 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.indices.mapping;

import com.google.common.base.Predicate;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.junit.Test;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.*;

/**
 *
 */
public class SimpleDeleteMappingTests extends ElasticsearchIntegrationTest {

    @Test
    public void simpleDeleteMapping() throws Exception {
        for (int i = 0; i < 10; i++) {
            client().prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject()
                    .field("value", "test" + i)
                    .endObject()).execute().actionGet();
        }

        ensureGreen();
        refresh();

        for (int i = 0; i < 10; i++) {
            CountResponse countResponse = client().prepareCount().setQuery(matchAllQuery()).execute().actionGet();
            assertThat(countResponse.getCount(), equalTo(10l));
        }

        waitForMappingOnMaster("test", "type1");

        GetMappingsResponse mappingsResponse = client().admin().indices().prepareGetMappings("test").setTypes("type1").execute().actionGet();
        assertThat(mappingsResponse.getMappings().get("test").get("type1"), notNullValue());

        assertAcked(client().admin().indices().prepareDeleteMapping().setIndices("test").setType("type1"));

        for (int i = 0; i < 10; i++) {
            CountResponse countResponse = client().prepareCount().setQuery(matchAllQuery()).execute().actionGet();
            assertThat(countResponse.getCount(), equalTo(0l));
        }

        boolean applied = awaitBusy(new Predicate<Object>() {
            @Override
            public boolean apply(Object input) {
                GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").setTypes("type1").get();
                ImmutableOpenMap<String, MappingMetaData> mappings = response.getMappings().get("test");
                if (mappings == null) {
                    return true;
                }
                return !mappings.containsKey("type1");
            }
        });
        if (!applied) {
            fail("failed to wait for the mapping to be removed from the master cluster state");
        }

    }
    
    
    @Test
    public void deleteMappingAllowNoBlankIndexAndNoEmptyStrings() throws Exception {
        assertAcked(client().admin().indices().prepareCreate("index1").addMapping("1", "field1", "type=string").get());
        assertAcked(client().admin().indices().prepareCreate("1index").addMapping("1", "field1", "type=string").get());

        // Should succeed, since no wildcards
        client().admin().indices().prepareDeleteMapping("1index").setType("1").get();
        try {
            client().admin().indices().prepareDeleteMapping("_all").get();
            fail();
        } catch (ActionRequestValidationException e) {}
        
        try {
            client().admin().indices().prepareDeleteMapping("_all").setType("").get();
            fail();
        } catch (ActionRequestValidationException e) {}
        
        try {
            client().admin().indices().prepareDeleteMapping().setType("1").get();
            fail();
        } catch (ActionRequestValidationException e) {}
        
        try {
            client().admin().indices().prepareDeleteMapping("").setType("1").get();
            fail();
        } catch (ActionRequestValidationException e) {}

    }
    
}

@@ -1070,55 +1070,8 @@
 se;%0A
-import org.elasticsearch.cluster.ClusterState;%0A
 impo
@@ -1125,16 +1125,16 @@
 taData;%0A
+
 import o
@@ -1247,72 +1247,8 @@
 st;%0A
-import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;%0A
 impo
@@ -1536,17 +1536,73 @@
 atchers.
-*
+equalTo;%0Aimport static org.hamcrest.Matchers.notNullValue
 ;%0A%0A/**%0A 
@@ -1687,32 +1687,32 @@
 st %7B%0A%0A    @Test%0A
-
     public void 
@@ -1744,32 +1744,142 @@
 ows Exception %7B%0A
+        assertAcked(prepareCreate(%22test%22).addMapping(%22type1%22, %22value%22, %22type=string%22).execute().actionGet());%0A
         for (int

Fix SimpleDeleteMappingTests.