package gumi.builders;

import gumi.builders.url.UrlParameterMultimap;
import org.junit.Test;

import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * Ideally, these tests should provide 100% coverage for UrlParameterMultimap.
 */
public class MultimapTest {

    private static Map.Entry<String, String> newEntry(final String key, final String value) {
        return new AbstractMap.SimpleImmutableEntry<String, String>(key, value);
    }

    @Test
    public void createMultimap() {
        final UrlParameterMultimap m1 = UrlParameterMultimap.newMultimap();
    }

    @Test
    public void createImmutable() {
        final UrlParameterMultimap.Immutable m1 = UrlParameterMultimap.newMultimap().immutable();
    }

    @Test
    public void putArray() {
        final UrlParameterMultimap m1 = UrlParameterMultimap.newMultimap();
        m1.put("a", Arrays.asList("1", "2", "3"));
        m1.put("b", Arrays.asList("4", "5", "6"));
        m1.put("a", Arrays.asList("2", "3", "7"));
        assertEquals(Arrays.asList(newEntry("a", "2"), newEntry("a", "3"), newEntry("b", "4"),
                newEntry("b", "5"), newEntry("b", "6"), newEntry("a", "7")),
                m1.flatEntryList());
    }

    @Test
    public void addEntries() {
        final UrlParameterMultimap m1 = UrlParameterMultimap.newMultimap();
        m1.add("a", "1").add("b", "2").add("c", "3");
        assertEquals(m1.flatEntryList().get(0), newEntry("a", "1"));
        assertEquals(m1.flatEntryList().get(1), newEntry("b", "2"));
        assertEquals(m1.flatEntryList().get(2), newEntry("c", "3"));
    }

    @Test(expected = UnsupportedOperationException.class)
    public void mutateImmutableParameterMap() {
        final UrlParameterMultimap m1 = UrlParameterMultimap.newMultimap();
        m1.add("a", "1").add("b", "2").add("c", "3");
        final UrlParameterMultimap.Immutable i1 = m1.immutable();
        i1.add("d", "4");
    }
    
    @Test
    public void containsKey() {
        UrlParameterMultimap parameterMap = givenParameterMapWithKeyAndValue();
        assertTrue(parameterMap.containsKey("key1"));
        assertFalse(parameterMap.containsKey("key2"));
    }

    @Test
    public void containsValue() {
        UrlParameterMultimap parameterMap = givenParameterMapWithKeyAndValue();
        assertTrue(parameterMap.containsValue("value1"));
        assertFalse(parameterMap.containsValue("value2"));
    }

    @Test
    public void replaceValues() {
        UrlParameterMultimap parameterMap = givenParameterMapWithKeyAndValue();
        parameterMap.replaceValues("key1", "value2");
        assertFalse(parameterMap.containsValue("value1"));
        assertTrue(parameterMap.containsValue("value2"));
    }

    private static UrlParameterMultimap givenParameterMapWithKeyAndValue() {
        return UrlParameterMultimap.newMultimap().add("key1", "value1");
    }
    
}

@@ -164,16 +164,39 @@
 til.Map;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -2793,24 +2793,1946 @@
 %22));%0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void nullParameterShouldTriggerAnIllegalArgumentException() %7B%0A        final UrlParameterMultimap parameterMap = givenParameterMapWithKeyAndValue();%0A        parameterMap.containsKey(null);%0A        parameterMap.containsValue(null);%0A        parameterMap.remove(null);%0A        parameterMap.remove(null, null);%0A        parameterMap.remove(%22key1%22, null);%0A        parameterMap.remove(null, %22value1%22);%0A    %7D%0A%0A    @Test%0A    public void removeKey() %7B%0A        final UrlParameterMultimap parameterMap = givenParameterMapWithKeyAndValue();%0A        final List%3CString%3E removedMap = parameterMap.remove(%22key1%22);%0A        assertFalse(parameterMap.containsKey(%22key1%22));%0A        assertTrue(removedMap.contains(%22value1%22));%0A    %7D%0A%0A    @Test%0A    public void removeKeyAndValue() %7B%0A        final UrlParameterMultimap parameterMap = givenParameterMapWithKeyAndValue();%0A        parameterMap.add(%22key1%22, %22value2%22);%0A        parameterMap.remove(%22key1%22, %22value1%22);%0A        assertTrue(parameterMap.containsKey(%22key1%22));%0A        parameterMap.remove(%22key1%22, %22value2%22);%0A        assertFalse(parameterMap.containsKey(%22key1%22));%0A    %7D%0A%0A    @Test%0A    public void checkSizeIsEmptyAndClear() %7B%0A        final UrlParameterMultimap parameterMap = givenParameterMapWithKeyAndValue();%0A        assertFalse(parameterMap.isEmpty());%0A        assertEquals(1, parameterMap.size());%0A        parameterMap.clear();%0A        assertTrue(parameterMap.isEmpty());%0A        assertEquals(0, parameterMap.size());%0A    %7D%0A%0A    @Test%0A    public void removeAllValues() %7B%0A        final UrlParameterMultimap parameterMap = givenParameterMapWithKeyAndValue();%0A        parameterMap.add(%22key1%22, %22value2%22);%0A        parameterMap.removeAllValues(%22key1%22);%0A        assertFalse(parameterMap.containsKey(%22key1%22));%0A        assertFalse(parameterMap.containsValue(%22value1%22));%0A        assertFalse(parameterMap.containsValue(%22value2%22));%0A    %7D%0A%0A
     private 

Add new tests for UrlParameterMultimap class