/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.util;

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

import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.romix.scala.collection.concurrent.TrieMap;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Test;

public class ReadWriteTrieMapTest {

    @Test
    public void testMethodsOfReadWriteTrieMap() {
        final TrieMap<String, String> trieMap = new TrieMap<>();
        trieMap.put("0", "zero");
        trieMap.put("1", "one");

        final ReadWriteTrieMap<String, String> readWriteTrieMap = new ReadWriteTrieMap<String, String>(trieMap, 5);

        assertNotNull("Object readOnlyTrieMap shouldn't be 'null'.", readWriteTrieMap);

        assertEquals("Size of readOnlyTrieMap should be '5'.", 5, readWriteTrieMap.size());
        assertFalse("Object readOnlyTrieMap shouldn't be empty.", readWriteTrieMap.isEmpty());

        assertTrue("Object readOnlyTrieMap should have key '0'.", readWriteTrieMap.containsKey("0"));
        assertTrue("Object readOnlyTrieMap should have value 'zero'.", readWriteTrieMap.containsValue("zero"));
        assertEquals("Object readOnlyTrieMap should have value 'zero'.", "zero", readWriteTrieMap.get("0"));

        final Map<String, String> rwMap = readWriteTrieMap;
        rwMap.put("2", "two");
        rwMap.put("3", "three");

        assertEquals("Removed value from readOnlyTrieMap should be 'one'.", "one", rwMap.remove("1"));

        final Set<String> trieMapKeySet = readWriteTrieMap.keySet();
        assertEquals("Size of keySet should be '3'.", 3, Iterables.size(trieMapKeySet));

        final Collection<String> trieMapValues = readWriteTrieMap.values();
        assertEquals("Size of values should be '3'.", 3, Iterables.size(trieMapValues));

        assertTrue("Entry set of readWriteTrieMap and trieMap should by equals.", convertSetEntryToMap(readWriteTrieMap.entrySet()).equals(trieMap));

        trieMap.put("2", "two");
        final ReadWriteTrieMap<String, String> readWriteTrieMap2 = new ReadWriteTrieMap<String, String>(trieMap, 4);

        assertFalse("Objects readWriteTrieMap and readOnlyTrieMap2 should be different.", readWriteTrieMap.equals(readWriteTrieMap2));
        assertFalse("Hash codes of object readWriteTrieMap and readOnelyTrieMap2 should be different.", readWriteTrieMap.hashCode() == readWriteTrieMap2.hashCode());

        final Map<String, String> readOnlyTrieMap = readWriteTrieMap.toReadOnly();

        readWriteTrieMap.clear();
        assertEquals("Size of readOnlyTrieMap should be '0'.", 0, readWriteTrieMap.size());
    }

    public Map<String, String> convertSetEntryToMap(Set<Entry<String, String>> input) {
        Map<String, String> resultMap = Maps.newHashMap();
        for (Entry<String, String> entry : input) {
            resultMap.put(entry.getKey(), entry.getValue());
        }
        return resultMap;
    }
}

@@ -526,91 +526,8 @@
 e;%0A%0A
-import com.google.common.collect.Iterables;%0Aimport com.google.common.collect.Maps;%0A
 impo
@@ -605,16 +605,42 @@
 ection;%0A
+import java.util.HashMap;%0A
 import j
@@ -2026,39 +2026,24 @@
 e '3'.%22, 3, 
-Iterables.size(
 trieMapKeySe
@@ -2043,16 +2043,22 @@
 apKeySet
+.size(
 ));%0A%0A   
@@ -2191,23 +2191,8 @@
  3, 
-Iterables.size(
 trie
@@ -2200,16 +2200,22 @@
 apValues
+.size(
 ));%0A%0A   
@@ -2898,17 +2898,16 @@
 Only();%0A
-%0A
         
@@ -2958,36 +2958,37 @@
 ls(%22Size of read
-Only
+Write
 TrieMap should b
@@ -3021,24 +3021,115 @@
 ap.size());%0A
+        assertEquals(%22Size of readOnlyTrieMap should be '6'.%22, 6, readOnlyTrieMap.size());%0A
     %7D%0A%0A    p
@@ -3255,23 +3255,21 @@
 p = 
-Maps.
 new
+ 
 HashMap
+%3C%3E
 ();%0A

Fix TrieMap tests a bit