package org.intermine.webservice.server.template.result;

/*
 * 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 junit.framework.TestCase;

import org.intermine.TestUtil;
import org.intermine.api.template.TemplateQuery;
import org.intermine.objectstore.query.ConstraintOp;
import org.intermine.pathquery.PathConstraint;
import org.intermine.pathquery.PathConstraintAttribute;
import org.intermine.pathquery.PathConstraintLookup;
import org.intermine.pathquery.PathQuery;
import org.intermine.webservice.server.WebServiceConstants;


/**
 * @author Jakub Kulaviak, dbutano
 **/
public class TemplateResultLinkGeneratorTest extends TestCase
{

    private String prefix = "http://localhost:8080/query/" + WebServiceConstants.MODULE_NAME;

    public void testExtraValueLink() {
        PathQuery ret = new PathQuery(TestUtil.getModel());
        PathConstraint c1 = new PathConstraintLookup("Gene.name", "zen", "Drosophila_melanogaster");
        TemplateQuery tmpl = new TemplateQuery("template1", "title", "comments", ret);
        tmpl.addConstraint(c1);
        tmpl.setEditable(c1, true);
        String link = new TemplateResultLinkGenerator().getHtmlLink("http://localhost:8080/query",
                                                                    tmpl);
        assertEquals(link, prefix + "/template/results?"
                + "name=template1&constraint1=Gene.name&op1=LOOKUP&value1=zen&"
                + "extra1=Drosophila_melanogaster&size="
                + TemplateResultLinkGenerator.DEFAULT_RESULT_SIZE + "&layout=minelink|paging",
                link);
    }

    public void testMultipleConstraintsLink() {
        PathQuery ret = new PathQuery(TestUtil.getModel());
        PathConstraint c1 = new PathConstraintAttribute("Gene.name", ConstraintOp.MATCHES, "zen");
        PathConstraint c2 = new PathConstraintAttribute("Gene.length", ConstraintOp.LESS_THAN,
                                                        "100");
        TemplateQuery tmpl = new TemplateQuery("template1", "title", "comments", ret);
        tmpl.addConstraint(c1);
        tmpl.setEditable(c1, true);
        tmpl.addConstraint(c2);
        tmpl.setEditable(c2, true);
        String link = new TemplateResultLinkGenerator()
                          .getHtmlLink("http://localhost:8080/query", tmpl);
        String expected = prefix + "/template/results?name=template1"
            + "&constraint1=Gene.name&op1=LIKE&value1=zen"
            + "&constraint2=Gene.length&op2=lt&value2=100"
            + "&size=" + TemplateResultLinkGenerator.DEFAULT_RESULT_SIZE
            + "&layout=minelink|paging";
        assertEquals(expected, link);
    }

}

@@ -383,16 +383,48 @@
 tCase;%0A%0A
+import org.apache.log4j.Logger;%0A
 import o
@@ -446,16 +446,16 @@
 stUtil;%0A
-
 import o
@@ -1011,17 +1011,111 @@
 E_NAME;%0A
+    private static final Logger LOG = Logger.getLogger(TemplateResultLinkGeneratorTest.class);
 %0A
-
     publ
@@ -1635,24 +1635,25 @@
  tmpl);%0A
+%0A
         
 assertEq
@@ -1648,26 +1648,25 @@
     
-assertEquals(link,
+String expected =
  pre
@@ -1690,37 +1690,16 @@
 results?
-%22%0A                + %22
 name=tem
@@ -1759,24 +1759,16 @@
         
-   
-     
 + %22extra
@@ -1799,32 +1799,16 @@
 r&size=%22
-%0A               
  + Templ
@@ -1877,17 +1877,18 @@
 %7Cpaging%22
-,
+;%0A
 %0A       
@@ -1888,23 +1888,161 @@
         
-       
+LOG.error(%22testExtraValueLink().expected:%22 + expected);%0A        LOG.error(%22testExtraValueLink().actual:%22 + link);%0A%0A        assertEquals(expected,
  link);%0A
@@ -3031,16 +3031,16 @@
 LT_SIZE%0A
-
         
@@ -3072,16 +3072,158 @@
 aging%22;%0A
+%0A        LOG.error(%22testMultipleConstraintsLink().expected:%22 + expected);%0A        LOG.error(%22testMultipleConstraintsLink().actual:%22 + link);%0A%0A
         

temporarily output full link. stupid test is truncating