// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile;

import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author baldersheim
 */
public class ChainedMapTestCase {
    @Test
    public void testIsEmpty() {
        assertTrue(new ChainedMap<String, String>(Map.of(), Map.of()).isEmpty());
        assertFalse(new ChainedMap<>(Map.of("k", "v"), Map.of()).isEmpty());
        assertFalse(new ChainedMap<>(Map.of(), Map.of("k", "v")).isEmpty());
        assertFalse(new ChainedMap<>(Map.of("k", "v"), Map.of("k", "v")).isEmpty());
    }
    @Test
    public void testSize() {
        assertEquals(0, new ChainedMap<String, String>(Map.of(), Map.of()).size());
        assertEquals(1, new ChainedMap<>(Map.of("k", "v"), Map.of()).size());
        assertEquals(1, new ChainedMap<>(Map.of(), Map.of("k", "v")).size());
        assertEquals(1, new ChainedMap<>(Map.of("k", "v"), Map.of("k", "v")).size());
        assertEquals(2, new ChainedMap<>(Map.of("k", "v"), Map.of("K", "v")).size());
        assertEquals(2, new ChainedMap<>(Map.of("k", "v"), Map.of("K", "v", "k", "v")).size());
    }
    @Test
    public void testGetUsesBoth() {
        Map<String, String> a = Map.of("a", "a_1");
        Map<String, String> b = Map.of("b", "b_1");
        Map<String, String> ab = Map.of("a", "a_2", "b", "b_2");
        Map<String, String> a_b = new ChainedMap<>(a, b);
        assertEquals("a_1", a_b.get("a"));
        assertEquals("b_1", a_b.get("b"));

        Map<String, String> b_a = new ChainedMap<>(a, b);
        assertEquals("a_1", b_a.get("a"));
        assertEquals("b_1", b_a.get("b"));

        Map<String, String> a_ab = new ChainedMap<>(a, ab);
        assertEquals("a_1", a_ab.get("a"));
        assertEquals("b_2", a_ab.get("b"));

        Map<String, String> ab_a = new ChainedMap<>(ab, a);
        assertEquals("a_2", ab_a.get("a"));
        assertEquals("b_2", ab_a.get("b"));
    }
}

@@ -184,16 +184,38 @@
 til.Map;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -2135,11 +2135,616 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testKeySet() %7B%0A        assertTrue(new ChainedMap%3CString, String%3E(Map.of(), Map.of()).keySet().isEmpty());%0A        Map%3CString, String%3E a = Map.of(%22a%22, %22a_1%22);%0A        Map%3CString, String%3E b = Map.of(%22b%22, %22b_1%22);%0A        Map%3CString, String%3E ab = Map.of(%22a%22, %22a_2%22, %22b%22, %22b_2%22);%0A        assertEquals(Set.of(%22a%22), new ChainedMap%3C%3E(a, Map.of()).keySet());%0A        assertEquals(Set.of(%22a%22), new ChainedMap%3C%3E(Map.of(), a).keySet());%0A        assertEquals(Set.of(%22a%22, %22b%22), new ChainedMap%3C%3E(a, b).keySet());%0A        assertEquals(Set.of(%22a%22, %22b%22), new ChainedMap%3C%3E(ab, b).keySet());%0A    %7D
 %0A%7D%0A

Test keySet