package org.intermine.web.logic.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.intermine.model.testmodel.Company;
import org.intermine.util.DynamicUtil;

import junit.framework.TestCase;

/**
 * Tests an InlineList appearing on Report page
 * @author radek
 *
 */
public class InlineListTest extends TestCase
{

    Company company1;
    Company company2;
    Company company3;

    protected void setUp() throws Exception {
        super.setUp();

        // InterMine Objects
        company1 = (Company) DynamicUtil.createObject(Collections.singleton(Company.class));
        company1.setId(new Integer(1));
        company1.setName("Weyland Yutani");

        company2 = (Company) DynamicUtil.createObject(Collections.singleton(Company.class));
        company2.setId(new Integer(2));
        company2.setName("Initech");

        company3 = (Company) DynamicUtil.createObject(Collections.singleton(Company.class));
        company3.setId(new Integer(3));
        company3.setName("Umbrella Corp.");
    }

    /**
     * Duplo test name matches the complexity of the task contained...
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public void testDuplo() throws Exception {
        InlineList inlineList = new InlineList();
        inlineList.setListOfObjects(
                new HashSet(
                        new ArrayList<Object>(
                                Arrays.asList(company1, company2, company3))), "name");

        // traverse for compare
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (Object item : inlineList.getItems()) {
            Map<String, Object> resultElement = new LinkedHashMap<String, Object>();

            // Class type assertion
            assertTrue(item instanceof InlineListObject);

            // save
            resultElement.put("value", ((InlineListObject) item).getValue());
            resultElement.put("id", ((InlineListObject) item).getId());
            result.add(resultElement);
        }

        // [{value=Weyland Yutani, id=1}, {value=Initech, id=2}, {value=Umbrella Corp., id=3}]
        List<Map<String, Object>> l = new ArrayList<Map<String, Object>>();
        Map<String, Object> m1 = new LinkedHashMap<String, Object>();
        m1.put("value", "Weyland Yutani");
        m1.put("id", 1);
        l.add(m1);
        Map<String, Object> m2 = new LinkedHashMap<String, Object>();
        m2.put("value", "Initech");
        m2.put("id", 2);
        l.add(m2);
        Map<String, Object> m3 = new LinkedHashMap<String, Object>();
        m3.put("value", "Umbrella Corp.");
        m3.put("id", 3);
        l.add(m3);

        // count the number of elements as HashSet (next) will remove duplicates
        assertEquals(l.size(), result.size());
        // convert to HashSet for comparison
        assertEquals(new HashSet(l), new HashSet(result));
    }

}

@@ -221,16 +221,50 @@
 l.Map;%0A%0A
+import junit.framework.TestCase;%0A%0A
 import o
@@ -344,40 +344,57 @@
 il;%0A
-%0A
 import 
-junit.framework.TestCase
+org.intermine.web.logic.results.InlineList
 ;%0A%0A/
@@ -1421,102 +1421,39 @@
     
-InlineList inlineList = new InlineList();%0A        inlineList.setListOfObjects(%0A               
+HashSet%3CObject%3E listOfObjects =
  new
@@ -1460,20 +1460,26 @@
  HashSet
+%3CObject%3E
 (%0A
-  
         
@@ -1486,22 +1486,16 @@
         
-      
 new Arra
@@ -1505,32 +1505,24 @@
 st%3CObject%3E(%0A
-        
             
@@ -1582,16 +1582,96 @@
 3)))
-, %22name%22
+;%0A        InlineList inlineList = new InlineList(listOfObjects, %22name%22, true, null, null
 );%0A%0A

fix InlineList tests