package ca.ucalgary.services.test;

import ca.ucalgary.services.SoapServlet;
import ca.ucalgary.seahawk.gui.*;
import ca.ucalgary.seahawk.util.*;

import org.biomoby.service.test.*;

import junit.framework.*;

import java.io.*;
import java.net.URL;
import java.util.*;

/**
 * 
 */
public class SoapServletTestCase extends TestCase{
    // The class below should have a nullary c-tor
    private final static String TEST_DATARECORDER = "ca.ucalgary.services.util.PBERecorder";

    public SoapServletTestCase(String name){
	super(name);
    }
    
    /**
     * Checks against a KEGG service
     */
    public void testWSDLRpcEncExecute(){
	SoapServlet servlet = new SoapServlet();
	try{
	    servlet.init(new TestServletConfig("", new TestServletContext()));
	}catch(Exception e){
	    e.printStackTrace();
	    fail("Could not initialize the SoapServlet");
	}

	TestHttpServletRequest request = new TestHttpServletRequest();
	request.setParameter(SoapServlet.SRC_PARAM, "http://soap.genome.jp/KEGG.wsdl");
	request.setParameter(SoapServlet.SERVICE_SPEC_PARAM, "SOAP/KEGG KEGG SOAP/KEGG KEGGPort " +
			     "SOAP/KEGG get_compounds_by_enzyme SOAP/KEGG#get_compounds_by_enzyme " +
			     "get_compounds_by_enzyme rpc encoded");
	request.setParameter("enzyme_id", "ec:1.1.1.1");

	TestHttpServletResponse response = new TestHttpServletResponse();
	StringBufferServletOutputStream out = new StringBufferServletOutputStream();
	response.setOutputStream(out);
	try{
	    servlet.doPost(request, response);
	}
	catch(Exception e){
	    e.printStackTrace();
	    fail("Got exception while posting request for RPC/Encoded KEGG Web Service");
	}
	assertTrue("Did not find the expected data in the reponse from KEGG", 
		   out.getBuffer().indexOf("cpd:C00001") != -1);
    }
    
    /**
     * Checks against both NCBI and EBI services.
     */
    public void testWSDLDocLitExecute(){
	SoapServlet servlet = new SoapServlet();
	try{
	    servlet.init(new TestServletConfig("", new TestServletContext()));
	}catch(Exception e){
	    e.printStackTrace();
	    fail("Could not initialize the SoapServlet");
	}

	TestHttpServletRequest request = new TestHttpServletRequest();
	request.setParameter(SoapServlet.SRC_PARAM, "http://www.ebi.ac.uk/ontology-lookup/OntologyQuery.wsdl");
	request.setParameter(SoapServlet.SERVICE_SPEC_PARAM, "http://www.ebi.ac.uk/ontology-lookup/OntologyQuery QueryService " +
			     "http://www.ebi.ac.uk/ontology-lookup/OntologyQuery OntologyQuery " +
			     "http://www.ebi.ac.uk/ontology-lookup/OntologyQuery getTermsByName " +
			     "getTermsByName getTermsByName document literal");
	request.setParameter("partialName", "seed");
	request.setParameter("ontologyName", "GO");
	request.setParameter("reverseKeyOrder", "false");

	TestHttpServletResponse response = new TestHttpServletResponse();
	StringBufferServletOutputStream out = new StringBufferServletOutputStream();
	response.setOutputStream(out);
	try{
	    servlet.doPost(request, response);
	}
	catch(Exception e){
	    e.printStackTrace();
	    fail("Got exception while posting request for Document/Literal EBI Web Service");
	}
	if(out.getBuffer().indexOf("coat") == -1){
	    fail("Did not find the expected data in the reponse from EBI.  Response was:\n" + 
		 out.getBuffer().toString());
	}
	TestHttpServletRequest request2 = new TestHttpServletRequest();
	request2.setParameter(SoapServlet.SRC_PARAM, "http://www.ncbi.nlm.nih.gov/entrez/eutils/soap/v2.0/eutils.wsdl");
	request2.setParameter(SoapServlet.SERVICE_SPEC_PARAM, "http://www.ncbi.nlm.nih.gov/soap/eutils/ eUtilsService " +
			      "http://www.ncbi.nlm.nih.gov/soap/eutils/ eUtilsServiceSoap " +
			      "http://www.ncbi.nlm.nih.gov/soap/eutils/espell eSpellRequest " +
			      "espell run_eSpell document literal");
	request2.setParameter("db:opt", "");
	request2.setParameter("term:opt", "brest");
	request2.setParameter("tool:opt", "");
	request2.setParameter("email:opt", "");
	
	TestHttpServletResponse response2 = new TestHttpServletResponse();
	StringBufferServletOutputStream out2 = new StringBufferServletOutputStream();
	response2.setOutputStream(out2);
	try{
	    servlet.doPost(request2, response2);
	}
	catch(Exception e){
	    e.printStackTrace();
	    fail("Got exception while posting request for Document/Literal NCBI Web Service");
	}
	assertTrue("Did not find the expected data in the reponse from NCBI", 
		   out2.getBuffer().indexOf("breast") != -1);
    }

    public void testDataRecorderAutoLoad(){
	SoapServlet servlet = new SoapServlet();
	try{
	    TestServletConfig config = new TestServletConfig("", new TestServletContext());
	    config.addInitParameter(SoapServlet.DATARECORDER_CONTEXTPARAM, TEST_DATARECORDER);
	    servlet.init(config);
	}catch(Exception e){
	    e.printStackTrace();
	    fail("Could not initialize the SoapServlet");
	}	
    }

    /**
     * @return a test suite for all the test methods of this test case.
     *
     * Specifically, we see if a WSDL file loaded actually launches the wrapping servlet 
     * and a browser for the user to interact with in Programming-By-Example.
     */
    public static Test suite() {

	TestSuite suite = new TestSuite();
	suite.addTest(new SoapServletTestCase("testWSDLRpcEncExecute"));
	suite.addTest(new SoapServletTestCase("testWSDLDocLitExecute"));
	suite.addTest(new SoapServletTestCase("testDataRecorderAutoLoad"));
        return suite;
    }

    public static void main(String[] args){
	junit.textui.TestRunner.run(suite());
    }
}
