/*
 * 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.common.xcontent.support;

import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matchers;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XContentHelperTests extends ESTestCase {

    Map<String, Object> getMap(Object... keyValues) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < keyValues.length; i++) {
            map.put((String) keyValues[i], keyValues[++i]);
        }
        return map;
    }

    Map<String, Object> getNamedMap(String name, Object... keyValues) {
        Map<String, Object> map = getMap(keyValues);

        Map<String, Object> namedMap = new HashMap<>(1);
        namedMap.put(name, map);
        return namedMap;
    }

    List<Object> getList(Object... values) {
        return Arrays.asList(values);
    }

    public void testMergingListValuesAreMapsOfOne() {
        Map<String, Object> defaults = getMap("test", getList(getNamedMap("name1", "t1", "1"), getNamedMap("name2", "t2", "2")));
        Map<String, Object> content = getMap("test", getList(getNamedMap("name2", "t3", "3"), getNamedMap("name4", "t4", "4")));
        Map<String, Object> expected = getMap("test",
                getList(getNamedMap("name2", "t2", "2", "t3", "3"), getNamedMap("name4", "t4", "4"), getNamedMap("name1", "t1", "1")));

        XContentHelper.mergeDefaults(content, defaults);

        assertThat(content, Matchers.equalTo(expected));
    }


}

@@ -871,31 +871,250 @@
 mon.
-xcontent.XContentHelper
+bytes.BytesReference;%0Aimport org.elasticsearch.common.xcontent.ToXContent;%0Aimport org.elasticsearch.common.xcontent.XContentHelper;%0Aimport org.elasticsearch.common.xcontent.XContentParser;%0Aimport org.elasticsearch.common.xcontent.XContentType
 ;%0Aim
@@ -1184,16 +1184,44 @@
 chers;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -2583,11 +2583,1105 @@
 %0A    %7D%0A%0A
+    public void testToXContentWrapInObject() throws IOException %7B%0A        boolean wrapInObject = randomBoolean();%0A        XContentType xContentType = randomFrom(XContentType.values());%0A        ToXContent toXContent = (builder, params) -%3E %7B%0A            if (wrapInObject == false) %7B%0A                builder.startObject();%0A            %7D%0A            builder.field(%22field%22, %22value%22);%0A            if (wrapInObject == false) %7B%0A                builder.endObject();%0A            %7D%0A            return builder;%0A        %7D;%0A        BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, wrapInObject);%0A        try (XContentParser parser = xContentType.xContent().createParser(bytes)) %7B%0A            assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());%0A            assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());%0A            assertTrue(parser.nextToken().isValue());%0A            assertEquals(%22value%22, parser.text());%0A            assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());%0A            assertNull(parser.nextToken());%0A        %7D%0A    %7D
 %0A%7D%0A

add unit test for XContentHelper#toXContent method