package org.soap.sax.test;

import org.soap.SOAPFault;
import org.soap.WSRFFault;
import junit.framework.*;
import java.net.URL;

public class SOAPFaultTestCase extends TestCase{
    private final static String SOAP_MSG_RESOURCE = "org/soap/sax/test/SOAPMessage.xml";
    private final static String WSRF_MSG_RESOURCE = "org/soap/sax/test/WSRFMessage.xml";    

    private final static String SOAP_MSG_CODE = "soap:Server";  //oracle values
    private final static String WSRF_MSG_CODE = "soap:Server";  
    private final static String SOAP_MSG_CONTAINS = "/moby:FASTA"; 
    private final static String WSRF_MSG_DATE = "2007-03-02T18:24:33Z"; 
    private final static String WSRF_MSG_NAME = "ResourceUnknownFault";
    private final static String WSRF_MSG_CONTAINS = "36231822107560019179";
    /**
     * @param name Test case name.
     */
    public SOAPFaultTestCase(String name) {
        super(name);
    }

    public void testSOAP(){
	URL testSOAPURL = getClass().getClassLoader().getResource(SOAP_MSG_RESOURCE);
	assertNotNull("Could not find the test SOAP XML document (" + SOAP_MSG_RESOURCE + ")",
		      testSOAPURL);
	
	String soapMessage = getContent(testSOAPURL);
	assertTrue("The test SOAP message was blank", soapMessage != null && soapMessage.length() > 0);

	SOAPFault soapFault = null;
	try{
	    soapFault = SOAPFault.createFromXML(soapMessage);
	} catch(Exception e){
	    e.printStackTrace();
	    fail("Exception while creating a SOAPFault from the test data ("+testSOAPURL+"): "+e);
	}
	assertNotNull("SOAPFault.createFromXML() returned a null object after " +
		      "parsing the test data (" + testSOAPURL + ").", soapFault);
	
	assertFalse("The generated SOAPFault claims to be WSRF, but is not", soapFault.isWsrf());

	// Check that the fields are filled in
	assertTrue("The SOAPFault object did not have the expected code (expected "+
		   SOAP_MSG_CODE + ", but got " + soapFault.getCode() + ")", 
		   soapFault.getCode() != null && soapFault.getCode().equals(SOAP_MSG_CODE));
	assertNotNull("The message field of parsed SOAP fault ("+testSOAPURL+
		      ") is incorrectly reported as blank", soapFault.getMsg());
	assertTrue("The SOAPFault message does not contain an expected string ('"+SOAP_MSG_CONTAINS+"')",
		   soapFault.getMsg().indexOf(SOAP_MSG_CONTAINS) != -1);
    }

    public void testWSRF(){	
	URL testWSRFURL = getClass().getClassLoader().getResource(WSRF_MSG_RESOURCE);
	assertNotNull("Could not find the test SOAP XML document (" + WSRF_MSG_RESOURCE + ")",
		      testWSRFURL);
	
	String wsrfMessage = getContent(testWSRFURL);
	assertTrue("The test WSRF message was blank", wsrfMessage != null && wsrfMessage.length() > 0);

	SOAPFault soapFault = null;
	try{
	    soapFault = SOAPFault.createFromXML(wsrfMessage);
	} catch(Exception e){
	    e.printStackTrace();
	    fail("Exception while creating a WSRFFault from the test data ("+testWSRFURL+"): "+e);
	}
	assertNotNull("SOAPFault.createFromXML() returned a null object after " +
		      "parsing the test data (" + testWSRFURL + ").", soapFault);
	
	assertTrue("The generated SOAPFault claims NOT to be WSRF, but should be ("+testWSRFURL + ")", 
		   soapFault.isWsrf());

	// Check that the SOAP fields are filled in
	assertTrue("The SOAPFault object did not have the expected code (expected "+
		   SOAP_MSG_CODE + ", but got " + soapFault.getCode() + ")", 
		   soapFault.getCode() != null && soapFault.getCode().equals(SOAP_MSG_CODE));
	WSRFFault wsrfFault = soapFault.getWsrfFault();
	assertNotNull("The SOAPFault message claimed to be WSRF (via isWsrf()), but getWsrfFault() returned null", 
		      wsrfFault);

	// Check that the WSRF fields are okay
	assertTrue("The parsed WSRF fault ("+testWSRFURL+
		   ") does not have the correct timestamp (expected "+ WSRF_MSG_DATE +
		   ", but got " + wsrfFault.getTimestamp() + ")",
		   wsrfFault.getTimestamp() != null && wsrfFault.getTimestamp().equals(WSRF_MSG_DATE));
	assertTrue("The parsed WSRF fault ("+testWSRFURL+
		   ") does not have the correct name (expected "+ WSRF_MSG_NAME +
		   ", but got " + wsrfFault.getName() + ")",
		   wsrfFault.getName() != null && wsrfFault.getName().equals(WSRF_MSG_NAME));
	assertNotNull("The description field of parsed WSRF fault ("+testWSRFURL+
		      ") is incorrectly reported as blank", wsrfFault.getDescription());
	assertTrue("The parsed WSRF fault ("+testWSRFURL+
		   ") does not contain an expected string ('"+ WSRF_MSG_CONTAINS + "')",
		   wsrfFault.getDescription().indexOf(WSRF_MSG_CONTAINS) != -1);
	
    }

    private static String getContent(URL u){
	StringBuffer contents = new StringBuffer();
	try{
	    java.io.LineNumberReader reader = new java.io.LineNumberReader(new java.io.InputStreamReader(u.openStream()));
	    for(String line = reader.readLine(); line != null; line = reader.readLine()){
		contents.append(line+"\n");
	    }
	} catch(Exception e){
	    e.printStackTrace();
	    fail("Cannot load text data from URL " + u);
	}
	return contents.toString();
    }

    /**
     * @return a test suite for all the test methods of this test case.
     */
    public static Test suite() {
	TestSuite suite = new TestSuite();
 	suite.addTest(new SOAPFaultTestCase("testSOAP")); //done
 	suite.addTest(new SOAPFaultTestCase("testWSRF")); //done
        return suite;
    }

    /**
     * Runs the test suite when the class is invoked on the command line.    
     */
    public static void main(String[] args) throws Exception{
	junit.textui.TestRunner.run(suite());
    }
}
