/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.apache.commons.configuration2;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration2.ex.ConfigurationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;

/**
 * Unit test for {@link JSONConfiguration} Not ideal: it uses the Jackson JSON parser just like
 * {@link JSONConfiguration} itself
 */
public class TestJSONConfiguration {
    /** The files that we test with. */
    private final String testJson = ConfigurationAssert.getTestFile("test.json").getAbsolutePath();

    private JSONConfiguration jsonConfiguration;

    @BeforeEach
    public void setUp() throws Exception {
        jsonConfiguration = new JSONConfiguration();
        jsonConfiguration.read(new FileReader(testJson));
    }

    @Test
    public void testCopyConstructor() {
        final BaseHierarchicalConfiguration c = new BaseHierarchicalConfiguration();
        c.addProperty("foo", "bar");

        jsonConfiguration = new JSONConfiguration(c);
        assertEquals("bar", jsonConfiguration.getString("foo"));
    }

    @Test
    public void testGetProperty_dictionary() {
        assertEquals("Martin D'vloper", jsonConfiguration.getProperty("martin.name"));
        assertEquals("Developer", jsonConfiguration.getProperty("martin.job"));
        assertEquals("Elite", jsonConfiguration.getProperty("martin.skill"));
    }

    @Test
    public void testGetProperty_dictionaryInList() {
        assertEquals("UK", jsonConfiguration.getString("capitals(1).country"));
        assertEquals("Washington", jsonConfiguration.getString("capitals(0).capital"));
    }

    @Test
    public void testGetProperty_integer() {
        final Object property = jsonConfiguration.getProperty("int1");
        assertInstanceOf(Integer.class, property);
        assertEquals(37, property);
    }

    @Test
    public void testGetProperty_nested() {
        assertEquals("value23", jsonConfiguration.getProperty("key2.key3"));
    }

    @Test
    public void testGetProperty_nested_with_list() {
        assertEquals(Arrays.asList("col1", "col2"), jsonConfiguration.getProperty("key4.key5"));
    }

    @Test
    public void testGetProperty_simple() {
        assertEquals("value1", jsonConfiguration.getProperty("key1"));
    }

    @Test
    public void testGetProperty_subset() {
        final Configuration subset = jsonConfiguration.subset("key4");
        assertEquals(Arrays.asList("col1", "col2"), subset.getProperty("key5"));
    }

    @Test
    public void testGetProperty_very_nested_properties() {
        final Object property = jsonConfiguration.getProperty("very.nested.properties");
        assertEquals(Arrays.asList("nested1", "nested2", "nested3"), property);
    }

    @Test
    public void testSave() throws IOException, ConfigurationException {
        // save the Configuration as a String...
        final StringWriter sw = new StringWriter();
        jsonConfiguration.write(sw);
        final String output = sw.toString();

        // ..and then try parsing it back
        final ObjectMapper mapper = new ObjectMapper();
        final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
        final Map<String, Object> parsed = mapper.readValue(output, type);
        assertEquals(7, parsed.entrySet().size());
        assertEquals("value1", parsed.get("key1"));

        final Map<?, ?> key2 = (Map<? , ?>) parsed.get("key2");
        assertEquals("value23", key2.get("key3"));

        final List<?> key5 = (List<?>) ((Map<?, ?>) parsed.get("key4")).get("key5");
        assertEquals(Arrays.asList("col1", "col2"), key5);

        final List<?> capitals = (List<?>) parsed.get("capitals");
        final Map<?, ?> capUk = (Map<?, ?>) capitals.get(1);
        assertEquals("London", capUk.get("capital"));
    }
}

@@ -901,16 +901,78 @@
 Equals;%0A
+import static org.junit.jupiter.api.Assertions.assertNotNull;%0A
 import s
@@ -1028,16 +1028,16 @@
 anceOf;%0A
-
 %0Aimport 
@@ -1293,16 +1293,55 @@
 reEach;%0A
+import org.junit.jupiter.api.Disabled;%0A
 import o
@@ -2308,32 +2308,243 @@
 %22foo%22));%0A    %7D%0A%0A
+    /**%0A     * Tests CONFIGURATION-793.%0A     */%0A    public void testGetList_nested_with_list() %7B%0A        assertEquals(Arrays.asList(%22col1%22, %22col2%22), jsonConfiguration.getList(String.class, %22key4.key5%22));%0A    %7D%0A%0A
     @Test%0A    pu
@@ -4094,24 +4094,24 @@
 operties%22);%0A
-
         asse
@@ -4177,32 +4177,682 @@
 operty);%0A    %7D%0A%0A
+    /**%0A     * Tests CONFIGURATION-793.%0A     */%0A    @Disabled%0A    @Test%0A    public void testListOfObjects() %7B%0A        final Configuration subset = jsonConfiguration.subset(%22capitals%22);%0A        assertNotNull(subset);%0A        assertEquals(2, subset.size());%0A%0A        final List%3CObject%3E list = jsonConfiguration.getList(%22capitals%22);%0A        assertNotNull(list);%0A        assertEquals(2, list.size());%0A%0A//        assertEquals(list.get(0).get(%22country%22), %22USA%22);%0A//        assertEquals(list.get(0).get(%22capital%22), %22Washington%22);%0A//        assertEquals(list.get(1).get(%22country%22), %22UK%22);%0A//        assertEquals(list.get(1).get(%22capital%22), %22London%22);%0A    %7D%0A%0A
     @Test%0A    pu

Add tests for CONFIGURATION-793