package org.ovirt.engine.core.utils.linq;

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;

public class LinqUtilsTest {
    private List<String> list = new LinkedList<String>() {
        {
            add("one");
            add("two");
            add("three");
        }
    };
    private List<String> cutlist = new LinkedList<String>() {
        {
            add("o");
            add("t");
            add("t");
        }
    };

    @Test
    public void exampleFirstOrNull() {
        String string = LinqUtils.firstOrNull(list, new Predicate<String>() {
            @Override
            public boolean eval(String s) {
                return s.equals("two");
            }
        });

        assertEquals("two", string);
    }

    @Test
    public void exampleForeach() {
        List<String> results = LinqUtils.transformToList(list, new Function<String, String>() {
            @Override
            public String eval(String s) {
                return s.substring(0, 1);
            }
        });

        assertEquals(results, cutlist);
    }

    @Test
    public void exampleFilter() {
        List<String> filteredList = LinqUtils.filter(list, new Predicate<String>() {
            @Override
            public boolean eval(String s) {
                return "two".equals(s);
            }
        });

        assertEquals("two", filteredList.get(0));
    }

    @Test
    public void exampleToMap() {
        Map<String, String> map = LinqUtils.toMap(list, new Mapper<String, String, String>() {
            @Override
            public String createKey(String s) {
                return s.substring(0, 1);
            }

            @Override
            public String createValue(String s) {
                return s;
            }
        });

        Map<String, String> example = new HashMap<String, String>() {
            {
                put("t", "three");
                put("o", "one");
            }
        };

        assertEquals(map, example);
    }

    @Test
    public void exampleToMapDefaultMapper() {
        Map<String, String> map = LinqUtils.toMap(list, new DefaultMapper<String, String>() {
            @Override
            public String createKey(String s) {
                return s.substring(0, 1);
            }
        });

        Map<String, String> example = new HashMap<String, String>() {
            {
                put("t", "three");
                put("o", "one");
            }
        };

        assertEquals(map, example);
    }

    @Test
    public void exampleAggregate() {
        String r = LinqUtils.aggregate(Arrays.asList("a", "b", "c"), new Aggregator<String>() {
            @Override
            public String process(String s, String s1) {
                return s + ":" + s1;
            }
        });

        assertEquals("a:b:c", r);
    }
}

@@ -294,60 +294,124 @@
 list
- = new LinkedList%3CString%3E() %7B%0A        %7B%0A    
+;%0A    private List%3CString%3E cutlist;%0A%0A    public LinqUtilsTest() %7B%0A        list  = new LinkedList%3CString%3E();%0A
         
 add(
@@ -402,24 +402,29 @@
 ();%0A        
+list.
 add(%22one%22);%0A
@@ -427,28 +427,29 @@
 %22);%0A        
-    
+list.
 add(%22two%22);%0A
@@ -452,28 +452,29 @@
 %22);%0A        
-  
-  
+list.
 add(%22three%22)
@@ -479,49 +479,16 @@
 %22);%0A
-        %7D
 %0A    
-%7D;%0A
    
- private List%3CString%3E
  cut
@@ -522,20 +522,9 @@
 g%3E()
- %7B%0A        %7B
+;
 %0A   
@@ -524,28 +524,32 @@
 ();%0A        
-    
+cutlist.
 add(%22o%22);%0A  
@@ -546,36 +546,40 @@
 d(%22o%22);%0A        
-    
+cutlist.
 add(%22t%22);%0A      
@@ -580,20 +580,24 @@
         
-    
+cutlist.
 add(%22t%22)
@@ -606,20 +606,9 @@
     
-    %7D%0A    %7D;
+%7D
 %0A%0A  
@@ -1999,34 +1999,33 @@
 tring, String%3E()
- %7B
+;
 %0A            %7B%0A 
@@ -2017,38 +2017,24 @@
         
-    %7B%0A                
+example.
 put(%22t%22,
@@ -2048,32 +2048,32 @@
 %22);%0A        
-    
-    
+example.
 put(%22o%22, %22on
@@ -2080,33 +2080,8 @@
 e%22);
-%0A            %7D%0A        %7D;
 %0A%0A  
@@ -2469,34 +2469,33 @@
 tring, String%3E()
- %7B
+;
 %0A            %7B%0A 
@@ -2491,30 +2491,16 @@
     
-    %7B%0A                
+example.
 put(
@@ -2522,24 +2522,24 @@
         
-    
-    
+example.
 put(%22o%22,
@@ -2550,33 +2550,8 @@
 e%22);
-%0A            %7D%0A        %7D;
 %0A%0A  

fix compiler warnings (unit test)