package org.intermine.api.template;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import junit.framework.Test;

import org.intermine.api.profile.Profile;
import org.intermine.api.profile.ProfileManager;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.ObjectStoreWriterFactory;
import org.intermine.objectstore.StoreDataTestCase;

/**
 * Tests for the TemplateSummariser.
 */

public class TemplateSummariserTest extends StoreDataTestCase
{
    private Profile profile;
    private ProfileManager pm;
    private ObjectStore os;
    private ObjectStoreWriter osw, uosw;

    public TemplateSummariserTest(String arg) {
        super(arg);
    }

    public void setUp() throws Exception {
        super.setUp();
        osw = ObjectStoreWriterFactory.getObjectStoreWriter("osw.unittest");
        os = osw.getObjectStore();

        uosw =  ObjectStoreWriterFactory.getObjectStoreWriter("osw.userprofile-test");
        pm = new ProfileManager(os, uosw);
    }

    public void executeTest(String type) {
    }

    public void testQueries() throws Throwable {
    }
    
    public static void oneTimeSetUp() throws Exception {
        StoreDataTestCase.oneTimeSetUp();
    }
    
    public static Test suite() {
        return buildSuite(TemplateSummariserTest.class);
    }
    
    public void test1() throws Exception {
        profile = pm.getSuperuserProfile();
        if (profile == null) {
            fail("Superuser " + pm.getSuperuser() + " in " + pm.getProfileUserNames() + " is null");
        }
        if (profile.getSavedTemplates() == null) {
            fail("Templates map is null");
        }
        TemplateQuery t = profile.getSavedTemplates().get("employeesOfACertainAge");
        TemplateSummariser summariser = new TemplateSummariser(os, uosw);
        assertFalse(summariser.isSummarised(t));
        summariser.summarise(t);
        assertTrue(summariser.isSummarised(t));
        Map<String, List<Object>> possibleValues = summariser.getPossibleValues(t);
        assertEquals(1, possibleValues.size());
        assertEquals("Employee.age", possibleValues.keySet().iterator().next());
        Set<Object> expected = new HashSet<Object>(Arrays.asList(10, 20, 30, 40, 50, 60));
        assertEquals(expected, new HashSet<Object>(possibleValues.values().iterator().next()));
        //fail("" + possibleValues);
    }
}

@@ -353,16 +353,42 @@
 Arrays;%0A
+import java.util.HashMap;%0A
 import j
@@ -405,16 +405,16 @@
 ashSet;%0A
-
 import j
@@ -802,16 +802,149 @@
 estCase;
+%0Aimport org.intermine.pathquery.Constraints;%0Aimport org.intermine.pathquery.PathConstraint;%0Aimport org.intermine.pathquery.PathQuery;
 %0A%0A/**%0A *
@@ -1557,24 +1557,797 @@
 (os, uosw);%0A
+        profile = new Profile(pm, %22testUser%22, null, %22password%22, new HashMap(),%0A                new HashMap(), new HashMap());%0A        pm.createProfile(profile);%0A%0A        TemplateQuery twoConstraints = new TemplateQuery(%22twoConstraints%22, %22twoConstraints%22, %22%22, new PathQuery(model));%0A        twoConstraints.addViews(%22Employee.name%22, %22Employee.age%22);%0A        PathConstraint ageCon = Constraints.greaterThan(%22Employee.age%22, %2230%22);%0A        twoConstraints.addConstraint(ageCon);%0A        twoConstraints.setEditable(ageCon, true);%0A        PathConstraint depCon = Constraints.greaterThan(%22Employee.department.name%22, %22Finance%22);%0A        twoConstraints.addConstraint(depCon);%0A        twoConstraints.setEditable(depCon, true);%0A        profile.saveTemplate(%22template%22, twoConstraints);%0A
     %7D%0A%0A    p
@@ -2442,29 +2442,25 @@
 ble %7B%0A    %7D%0A
-    
 %0A
+
     public s
@@ -2544,37 +2544,33 @@
 eSetUp();%0A    %7D%0A
-    
 %0A
+
     public stati
@@ -2653,13 +2653,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2697,298 +2697,8 @@
 n %7B%0A
-        profile = pm.getSuperuserProfile();%0A        if (profile == null) %7B%0A            fail(%22Superuser %22 + pm.getSuperuser() + %22 in %22 + pm.getProfileUserNames() + %22 is null%22);%0A        %7D%0A        if (profile.getSavedTemplates() == null) %7B%0A            fail(%22Templates map is null%22);%0A        %7D%0A
     
@@ -2756,29 +2756,15 @@
 et(%22
+t
 empl
-oyeesOfACertainAg
+at
 e%22);
@@ -3073,17 +3073,17 @@
 tEquals(
-1
+2
 , possib
@@ -3268,24 +3268,24 @@
 , 50, 60));%0A
+
         asse
@@ -3372,45 +3372,8 @@
 ));%0A
-        //fail(%22%22 + possibleValues);%0A
     

Fix bug