/*
 * 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.cluster.metadata;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;

import java.util.Collection;
import java.util.Collections;

import static org.hamcrest.Matchers.equalTo;

public class MetaDataMappingServiceTests extends ESSingleNodeTestCase {

    @Override
    protected Collection<Class<? extends Plugin>> getPlugins() {
        return Collections.singleton(InternalSettingsPlugin.class);
    }

    public void testMappingClusterStateUpdateDoesntChangeExistingIndices() throws Exception {
        final IndexService indexService = createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type"));
        final CompressedXContent currentMapping = indexService.mapperService().documentMapper("type").mappingSource();

        final MetaDataMappingService mappingService = getInstanceFromNode(MetaDataMappingService.class);
        final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
        // TODO - it will be nice to get a random mapping generator
        final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest().type("type");
        request.source("{ \"properties\" { \"field\": { \"type\": \"text\" }}}");
        mappingService.putMappingExecutor.execute(clusterService.state(), Collections.singletonList(request));
        assertThat(indexService.mapperService().documentMapper("type").mappingSource(), equalTo(currentMapping));
    }

    public void testClusterStateIsNotChangedWithIdenticalMappings() throws Exception {
        createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type"));

        final MetaDataMappingService mappingService = getInstanceFromNode(MetaDataMappingService.class);
        final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
        final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest().type("type");
        request.source("{ \"properties\" { \"field\": { \"type\": \"text\" }}}");
        ClusterState result = mappingService.putMappingExecutor.execute(clusterService.state(), Collections.singletonList(request))
            .resultingState;

        assertFalse(result != clusterService.state());

        ClusterState result2 = mappingService.putMappingExecutor.execute(result, Collections.singletonList(request))
            .resultingState;

        assertSame(result, result2);
    }
}

@@ -782,16 +782,17 @@
 se.%0A */%0A
+%0A
 package 
@@ -970,16 +970,75 @@
 rState;%0A
+import org.elasticsearch.cluster.ClusterStateTaskExecutor;%0A
 import o
@@ -1147,16 +1147,54 @@
 ontent;%0A
+import org.elasticsearch.index.Index;%0A
 import o
@@ -1482,16 +1482,57 @@
 equalTo;
+%0Aimport static org.hamcrest.Matchers.not;
 %0A%0Apublic
@@ -2468,32 +2468,93 @@
 ).type(%22type%22);%0A
+        request.indices(new Index%5B%5D %7BindexService.index()%7D);%0A
         request.
@@ -2569,32 +2569,33 @@
 %7B %5C%22properties%5C%22
+:
  %7B %5C%22field%5C%22: %7B 
@@ -2632,110 +2632,765 @@
     
-mappingService.putMappingExecutor.execute(clusterService.state(), Collections.singletonList(request));
+final ClusterStateTaskExecutor.ClusterTasksResult%3CPutMappingClusterStateUpdateRequest%3E result =%0A                mappingService.putMappingExecutor.execute(clusterService.state(), Collections.singletonList(request));%0A        // the task completed successfully%0A        assertThat(result.executionResults.size(), equalTo(1));%0A        assertTrue(result.executionResults.values().iterator().next().isSuccess());%0A        // the task really was a mapping update%0A        assertThat(%0A                indexService.mapperService().documentMapper(%22type%22).mappingSource(),%0A                not(equalTo(result.resultingState.metaData().index(%22test%22).mapping(%22type%22).source())));%0A        // since we never committed the cluster state update, the in-memory state is unchanged
 %0A   
@@ -4487,16 +4487,17 @@
 sult2);%0A
-
     %7D%0A
+%0A
 %7D%0A

Fix a mappings update test (#33146)