/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

package org.elasticsearch.xpack.spatial.search;

import org.elasticsearch.Version;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.geo.GeoShapeIntegTestCase;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.containsString;

public class GeoShapeWithDocValuesIT extends GeoShapeIntegTestCase {

    @Override
    protected boolean addMockGeoShapeFieldMapper() {
        return false;
    }

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

    @Override
    protected void getGeoShapeMapping(XContentBuilder b) throws IOException {
        b.field("type", "geo_shape");
    }

    @Override
    protected Version randomSupportedVersion() {
        return VersionUtils.randomIndexCompatibleVersion(random());
    }

    @Override
    protected boolean allowExpensiveQueries() {
        return true;
    }

    @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/77755")
    public void testMappingUpdate() {
        // create index
        assertAcked(client().admin().indices().prepareCreate("test").setSettings(settings(randomSupportedVersion()).build())
            .setMapping("shape", "type=geo_shape").get());
        ensureGreen();

        String update ="{\n" +
            "  \"properties\": {\n" +
            "    \"shape\": {\n" +
            "      \"type\": \"geo_shape\"," +
            "      \"strategy\": \"recursive\"" +
            "    }\n" +
            "  }\n" +
            "}";

        IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> client().admin().indices()
            .preparePutMapping("test")
            .setSource(update, XContentType.JSON).get());
        assertThat(e.getMessage(), containsString("mapper [shape] of type [geo_shape] cannot change strategy from [BKD] to [recursive]"));
    }
}

@@ -443,16 +443,78 @@
 ntType;%0A
+import org.elasticsearch.index.mapper.MapperParsingException;%0A
 import o
@@ -1630,89 +1630,8 @@
  %7D%0A%0A
-    @AwaitsFix(bugUrl = %22https://github.com/elastic/elasticsearch/issues/77755%22)%0A
     
@@ -1688,16 +1688,68 @@
 e index%0A
+        Version version = randomSupportedVersion();%0A
         
@@ -1834,32 +1834,15 @@
 ngs(
-randomSupportedV
+v
 ersion
-()
 ).bu
@@ -2196,16 +2196,67 @@
   %22%7D%22;%0A%0A
+        if (version.before(Version.V_8_0_0)) %7B%0A    
         
@@ -2374,16 +2374,20 @@
         
+    
 .prepare
@@ -2405,16 +2405,20 @@
 %22test%22)%0A
+    
         
@@ -2459,32 +2459,36 @@
 e.JSON).get());%0A
+    
         assertTh
@@ -2505,16 +2505,32 @@
 ssage(),
+%0A               
  contain
@@ -2622,20 +2622,440 @@
 ursive%5D%22));%0A
+        %7D else %7B%0A            MapperParsingException e = expectThrows(MapperParsingException.class, () -%3E client().admin().indices()%0A                .preparePutMapping(%22test%22)%0A                .setSource(update, XContentType.JSON).get());%0A            assertThat(e.getMessage(),%0A                containsString(%22using deprecated parameters %5Bstrategy%5D in mapper %5Bshape%5D of type %5Bgeo_shape%5D is no longer allowed%22));%0A        %7D%0A
     %7D%0A%7D%0A

Fix GeoShapeWithDocValuesIT#testMappingUpdate (#77976)