/**
 * Tests all of the major functionality of MobyServlet, and calls ACDServlet's
 * main method to ensure classes that only it uses are tested too.
 */

package ca.ucalgary.seahawk.services.test;

import ca.ucalgary.seahawk.services.TextClient;
import org.biomoby.shared.data.*;

import junit.framework.*;
import java.net.URL;

public class TextClientTestCase extends TestCase{
    private final static String TEST_TEXT_TYPE = "seqall";
    private final static String TEST_MOBY_SIMPLE_XML = "ca/ucalgary/services/test/mobyDNASeq.xml";
    private final static String TEST_MOBY_XSLT = "ca/ucalgary/services/test/mobyRules.xsl";

    private TextClient client;

    public TextClientTestCase(String name){
	super(name);
    }

    /**
     * Sets up the test fixture.
     * Called before every test case method.
     */
    protected void setUp() {
	try{
	    client = new TextClient();
	} catch(Exception e){
	    fail("Could not initialize the TextClient class instance: " + e);
	}

	try{
	    client.addMappingsFromURL(getTestXSLT());
	} catch(Exception e){
	    fail("Could not load the test XSLT file into the TextClient: " + e);
	}
	
    }
    
    /**
     * Tears down the test fixture.
     * Called after every test case method.
     */
    protected void tearDown() {
    }

    private URL getTestXSLT(){
	URL u = getClass().getClassLoader().getResource(TEST_MOBY_XSLT);
	if(u == null){
	    fail("The XSLT rules file for testing could not be found (" + TEST_MOBY_XSLT + ")");
	}
	return u;
    }

    private MobyDataObject getTestSimpleData(){
	URL u = getClass().getClassLoader().getResource(TEST_MOBY_SIMPLE_XML);
	if(u == null){
	    fail("The test data representing Simple MOBY data cannot be found (" + TEST_MOBY_SIMPLE_XML +")");
	}

	MobyContentInstance contents = null;
	try{
	    contents = MobyDataUtils.fromXMLDocument(u.openStream());
	}
	catch(Exception e){
	    fail("The contents of the test data file was not valid MOBY XML (" + u + "): " +e);
	}

	MobyDataObjectSet objectSet = contents.retrieveObjects();
	if(objectSet.size() == 0){
	    fail("The test data representing Simple MOBY data has no objects instances in it (" + 
		 TEST_MOBY_SIMPLE_XML +")");
	}
	if(objectSet.size() > 1){
	    fail("The test data representing Simple MOBY data has more than one object (" + 
		 TEST_MOBY_SIMPLE_XML +")");
	}
	return (objectSet.getElementInstances())[0];
    }

    public void testSimpleToText(){
	MobyDataObject mobySimple = getTestSimpleData();
	if(!client.canProduceTextTypeFromMoby(TEST_TEXT_TYPE, mobySimple)){
	    fail("The text client says it cannot produce the given text type (" + TEST_TEXT_TYPE + 
		 ") with the given test input data (" + mobySimple.getDataType().getName() + ")");
	}
	
	String resultText = null;
	try{
	    resultText = client.getText(mobySimple, TEST_TEXT_TYPE);
	} catch(Exception e){
	    fail("TextClient encountered an error while transforming the test MOBY data Simple: " + e); 
	}

	if(resultText == null){
	    fail("TextClient returned null after tranforming the test Simple MOBY object, text was expected");
	}
	else if(resultText.length() == 0){
	    fail("TextClient returned a blank text result (transforming a MOBY Simple), expected real text contents");
	}
    }

    /**
     * @return a test suite for all the test methods of this test case.
     */
    public static Test suite() {

	TestSuite suite = new TestSuite();
 	suite.addTest(new TextClientTestCase("testSimpleToText"));
 	//suite.addTest(new MobyServletTestCase("testSimpleToTextSet"));
 	//suite.addTest(new MobyServletTestCase("testCollectionToText"));
 	//suite.addTest(new MobyServletTestCase("testCollectionToTextSet"));
        return suite;
    }

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