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

import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceToParse;
import org.elasticsearch.test.ESSingleNodeTestCase;

public class ParentMappingTests extends ESSingleNodeTestCase {

    public void testParentSetInDocNotAllowed() throws Exception {
        String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
                .endObject().endObject().string();
        DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));

        try {
            docMapper.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder()
                .startObject().field("_parent", "1122").endObject().bytes()));
            fail("Expected failure to parse metadata field");
        } catch (MapperParsingException e) {
            assertTrue(e.getMessage(), e.getMessage().contains("Field [_parent] is a metadata field and cannot be added inside a document"));
        }
    }

    public void testParentSet() throws Exception {
        String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
                .startObject("_parent").field("type", "p_type").endObject()
                .endObject().endObject().string();
        DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));

        ParsedDocument doc = docMapper.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder()
                .startObject()
                .field("x_field", "x_value")
                .endObject()
                .bytes()).parent("1122"));

        assertEquals("1122", doc.rootDoc().getBinaryValue("_parent#p_type").utf8ToString());
    }
}

@@ -927,32 +927,348 @@
 mon.
-xcontent.XContentFactory
+io.stream.NamedWriteableRegistry;%0Aimport org.elasticsearch.common.settings.Settings;%0Aimport org.elasticsearch.common.xcontent.XContentBuilder;%0Aimport org.elasticsearch.common.xcontent.XContentFactory;%0Aimport org.elasticsearch.index.Index;%0Aimport org.elasticsearch.index.IndexSettings;%0Aimport org.elasticsearch.index.analysis.AnalysisService
 ;%0Aim
@@ -1381,16 +1381,69 @@
 eption;%0A
+import org.elasticsearch.index.mapper.MapperService;%0A
 import o
@@ -1570,33 +1570,397 @@
 rch.
-test.ESSingleNodeTestCase
+index.similarity.SimilarityService;%0Aimport org.elasticsearch.indices.IndicesModule;%0Aimport org.elasticsearch.test.ESSingleNodeTestCase;%0Aimport org.elasticsearch.test.IndexSettingsModule;%0A%0Aimport java.util.Collections;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport static java.util.Collections.emptyList;%0Aimport static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder
 ;%0A%0Ap
@@ -3605,10 +3605,1328 @@
 ;%0A    %7D%0A
+%0A%0A    @AwaitsFix(bugUrl = %22https://github.com/elastic/elasticsearch/issues/19389%22)%0A    public void testNoParentNullFieldCreatedIfNoParentSpecified() throws Exception %7B%0A        Index index = new Index(%22_index%22, %22testUUID%22);%0A        IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, Settings.EMPTY);%0A        AnalysisService analysisService = new AnalysisService(indexSettings, Collections.emptyMap(), Collections.emptyMap(),%0A            Collections.emptyMap(), Collections.emptyMap());%0A        SimilarityService similarityService = new SimilarityService(indexSettings, Collections.emptyMap());%0A        MapperService mapperService = new MapperService(indexSettings, analysisService, similarityService,%0A            new IndicesModule(new NamedWriteableRegistry(), emptyList()).getMapperRegistry(), () -%3E null);%0A        XContentBuilder mappingSource = jsonBuilder().startObject().startObject(%22some_type%22)%0A            .startObject(%22properties%22)%0A            .endObject()%0A            .endObject().endObject();%0A        mapperService.merge(%22some_type%22, new CompressedXContent(mappingSource.string()), MapperService.MergeReason.MAPPING_UPDATE, false);%0A        Set%3CString%3E allFields = new HashSet%3C%3E(mapperService.simpleMatchToIndexNames(%22*%22));%0A        assertFalse(allFields.contains(%22_parent#null%22));%0A    %7D%0A
 %7D%0A

Add test for #19389 new type always creates _parent#null field