/*
 * 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.index.mapper;

import com.google.common.io.Files;
import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.test.ElasticsearchTestCase;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;

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

@AwaitsFix(bugUrl = "")
public class FileBasedMappingsTests extends ElasticsearchTestCase {

    private static final String NAME = FileBasedMappingsTests.class.getSimpleName();

    private final ESLogger logger = Loggers.getLogger(FileBasedMappingsTests.class);

    public void testFileBasedMappings() throws Exception {
        File configDir = Files.createTempDir();
        File mappingsDir = new File(configDir, "mappings");
        File indexMappings = new File(new File(mappingsDir, "index"), "type.json");
        File defaultMappings = new File(new File(mappingsDir, "_default"), "type.json");
        try {
            indexMappings.getParentFile().mkdirs();
            defaultMappings.getParentFile().mkdirs();

            try (XContentBuilder builder = new XContentBuilder(JsonXContent.jsonXContent, new FileOutputStream(indexMappings))) {
                builder.startObject()
                        .startObject("type")
                            .startObject("properties")
                                .startObject("f")
                                    .field("type", "string")
                                .endObject()
                            .endObject()
                        .endObject()
                    .endObject();
            }

            try (XContentBuilder builder = new XContentBuilder(JsonXContent.jsonXContent, new FileOutputStream(defaultMappings))) {
                builder.startObject()
                        .startObject("type")
                            .startObject("properties")
                                .startObject("g")
                                    .field("type", "string")
                                .endObject()
                            .endObject()
                        .endObject()
                    .endObject();
            }

            Settings settings = ImmutableSettings.builder()
                    .put(ClusterName.SETTING, NAME)
                    .put("node.name", NAME)
                    .put("path.conf", configDir.getAbsolutePath())
                    .put("http.enabled", false)
                    .put("index.store.type", "ram")
                    .put("gateway.type", "none")
                    .build();

            try (Node node = NodeBuilder.nodeBuilder().local(true).data(true).settings(settings).build()) {
                node.start();

                assertAcked(node.client().admin().indices().prepareCreate("index").addMapping("type", "h", "type=string").get());
                final GetFieldMappingsResponse response = node.client().admin().indices().prepareGetFieldMappings("index").setTypes("type").setFields("f", "g", "h").get();
                System.out.println(response.mappings());
                for (String field : Arrays.asList("f", "g", "h")) {
                    logger.info("Checking field " + field);
                    FieldMappingMetaData fMappings = response.fieldMappings("index", "type", field);
                    assertNotNull(fMappings);
                    assertEquals(field, fMappings.fullName());
                }
            }
        } finally {
            FileSystemUtils.deleteRecursively(configDir);
        }
    }

}

@@ -853,156 +853,63 @@
 mon.
-io.Files;%0Aimport org.apache.lucene.util.LuceneTestCase.AwaitsFix;%0Aimport org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse
+collect.ImmutableSet;%0Aimport com.google.common.io.Files
 ;%0Aim
@@ -967,21 +967,16 @@
 .get.Get
-Field
 Mappings
@@ -987,29 +987,8 @@
 onse
-.FieldMappingMetaData
 ;%0Aim
@@ -1061,82 +1061,39 @@
 ch.c
-ommon.io.FileSystemUtils;%0Aimport org.elasticsearch.common.logging.ESLogger
+luster.metadata.MappingMetaData
 ;%0Aim
@@ -1126,22 +1126,25 @@
 mon.
-logging.Logger
+io.FileSystemUtil
 s;%0Ai
@@ -1575,22 +1575,19 @@
 va.util.
-Arrays
+Map
 ;%0A%0Aimpor
@@ -1669,32 +1669,8 @@
 d;%0A%0A
-@AwaitsFix(bugUrl = %22%22)%0A
 publ
@@ -1824,94 +1824,8 @@
 );%0A%0A
-    private final ESLogger logger = Loggers.getLogger(FileBasedMappingsTests.class);%0A%0A
     
@@ -4082,21 +4082,16 @@
 inal Get
-Field
 Mappings
@@ -4152,21 +4152,16 @@
 epareGet
-Field
 Mappings
@@ -4174,50 +4174,8 @@
 x%22).
-setTypes(%22type%22).setFields(%22f%22, %22g%22, %22h%22).
 get(
@@ -4197,26 +4197,18 @@
     
-System.out.println
+assertTrue
 (res
@@ -4227,143 +4227,64 @@
 gs()
-);%0A                for (String field : Arrays.asList(%22f%22, %22g%22, %22h%22)) %7B%0A                    logger.info(%22Checking field %22 + field);%0A    
+.toString(), response.mappings().containsKey(%22index%22));%0A
     
@@ -4299,13 +4299,8 @@
     
-Field
 Mapp
@@ -4311,18 +4311,17 @@
 etaData 
-fM
+m
 appings 
@@ -4335,22 +4335,17 @@
 nse.
-fieldM
+m
 appings(
 %22ind
@@ -4344,37 +4344,36 @@
 ngs(
+).get(
 %22index%22
-, %22type%22, field);%0A    
+).get(%22type%22);%0A
     
@@ -4398,18 +4398,17 @@
 NotNull(
-fM
+m
 appings)
@@ -4429,72 +4429,215 @@
     
-    assertEquals(field, fMappings.fullName());%0A                %7D
+Map%3C?, ?%3E properties = (Map%3C?, ?%3E) (mappings.getSourceAsMap().get(%22properties%22));%0A                assertNotNull(properties);%0A                assertEquals(ImmutableSet.of(%22f%22, %22g%22, %22h%22), properties.keySet());
 %0A   

Fix FileBasedMappingsTests by using the mappings API instead of field mappings.