package org.oasisOpen.docs.wsrf;
import junit.framework.TestCase;
import org.apache.axis.ConfigurationException;
import org.apache.axis.configuration.FileProvider;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.jndi.XmlBeanJndiUtils;
import org.apache.ws.util.soap.SoapClient;
import org.apache.ws.util.test.axis.AbstractOneAxisTestCase;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlInteger;
import org.apache.xmlbeans.XmlBoolean;
import org.oasisOpen.docs.wsrf.x2004.x11.wsrfWSResourceLifetime12Draft04.SetTerminationTimeDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.CreatePrinterResponseDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.CreationResponseDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl.JobPortPropertyQNames;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl.JobPortService;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl.PrinterPortPropertyQNames;
import org.xmlsoap.schemas.soap.envelope.Envelope;
import org.xmlsoap.schemas.soap.envelope.EnvelopeDocument;
import org.xmlsoap.schemas.soap.envelope.Header;
import org.xmlsoap.schemas.soap.envelope.Body;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.ReferencePropertiesType;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Calendar;
/**
* Created by IntelliJ IDEA.
* User: sacam
* Date: Mar 1, 2005
* Time: 10:20:29 AM
* To change this template use File | Settings | File Templates.
*/
public class InteropTestCase extends AbstractOneAxisTestCase
{
//this makes the test case not start the axis server...
/**
* Check system property, else the default is true
*/
private static boolean m_runAxisServer = System.getProperty("runAxis") != null ? Boolean.valueOf(System.getProperty("runAxis")).booleanValue() : true;
private static String PRINTER_FACTORY_PORT = "PrinterFactory";
private static final String PRINTER_PORT = "PrinterPort";
private static final String JOB_PORT = "JobPort";
//setup urls
private String m_baseURL = "http://localhost:8080/wsrf/services/";
/**
* check system property else the default is the baseurl
*/
private String m_printerFactoryURL = System.getProperty("printerFactoryURL") != null ? System.getProperty("printerFactoryURL") : m_baseURL + PRINTER_FACTORY_PORT;
private String m_printerURL = m_baseURL + PRINTER_PORT;
private String m_jobURL = m_baseURL + JOB_PORT;
//setup base path
/**
* check system property else the default is "."
*/
private File m_basePath = System.getProperty("basedir") != null ? new File(System.getProperty("basedir")) : new File(".");
//setup requests path
private File m_requestsDir = new File(m_basePath, "requests");
private static final String SOAP_REQ_CREATE_PRINTER = "CreatePrinter.soap";
private ReferencePropertiesType m_printerReferenceProperties;
public InteropTestCase()
{
super(m_runAxisServer);
}
protected void setUp() throws Exception
{
super.setUp();
//set the base url to the Axis one if running server embedded in unit test
if (m_runAxisServer)
{
m_baseURL = getAxisBaseUrl().toString();
//reinit urls for axis path
m_printerFactoryURL = m_baseURL + PRINTER_FACTORY_PORT;
//init jndi from config
XmlBeanJndiUtils.initializeFromInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(XmlBeanJndiUtils.JNDI_CONFIG_FILENAME));
}
//init the printer so its ready
//createPrinter();
}
/**
* Stops the embedded Axis server after the last test within this test case has finished running.
*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception
{
super.tearDown();
}
protected FileProvider getFileProvider() throws ConfigurationException
{
return new FileProvider(InteropTestCase.class.getResourceAsStream("/server-config.wsdd"));
}
public void createPrinter() throws IOException
{
String response = sendRequest(m_printerFactoryURL, SOAP_REQ_CREATE_PRINTER);
System.out.println("Create Printer Reponse: \n" + response);
assertTrue(response.indexOf("CreatePrinterResponse") > -1);
EnvelopeDocument envelopeDocument = loadResponseEnvelope(response);
CreatePrinterResponseDocument.CreatePrinterResponse createPrinter = (CreatePrinterResponseDocument.CreatePrinterResponse) XmlBeanUtils.getChildElements(envelopeDocument.getEnvelope().getBody(), new QName(PrinterPortPropertyQNames.PRINTER_REFERENCE.getNamespaceURI(), "CreatePrinterResponse"))[0];
setPrinterURL(createPrinter);
setPrinterReferenceProps(createPrinter);
}
//***** tests ********
public void testValidatePrinterInitialState() throws IOException
{
String response = sendPrinterRequest("GetPrinterPropertiesDoc.soap");
System.out.println("Printer Prop Doc: \n" + response);
assertTrue(response.indexOf("printer_properties") > -1);
EnvelopeDocument envelopeDocument = loadResponseEnvelope(response);
Body body = envelopeDocument.getEnvelope().getBody();
XmlObject[] childElements = XmlBeanUtils.getChildElements(body, new QName("http://docs.oasis-open.org/wsrf/2004/11/wsrf-WS-ResourceProperties-1.2-draft-05.xsd","GetResourcePropertyDocumentResponse"));
XmlObject childElement = childElements[0];
XmlObject[] printerprops = XmlBeanUtils.getChildElements(childElement, new QName(PrinterPortPropertyQNames.QUEUED_JOB_COUNT.getNamespaceURI(), "printer_properties"));
XmlInteger elem = (XmlInteger) XmlBeanUtils.getChildElements(printerprops[0], PrinterPortPropertyQNames.QUEUED_JOB_COUNT)[0];
assertTrue(elem.getBigIntegerValue().intValue() == 0);
XmlBoolean bool = (XmlBoolean) XmlBeanUtils.getChildElements(printerprops[0], PrinterPortPropertyQNames.PRINTER_IS_ACCEPTING_JOBS)[0];
assertTrue(bool.getBooleanValue() == false);
}
public void testGetPrinterQueueCount_SCENARIO_A1() throws IOException
{
String response = sendPrinterRequest("GetQueuedJobCount.soap");
assertTrue(response.indexOf("GetResourcePropertyResponse") > -1);
assertTrue(response.indexOf("draft-03.xsd\">0") > -1);
}
public void testGetPrinterStateAndQueueCount_SCENARIO_A2() throws IOException
{
String response = sendPrinterRequest("GetMulti.soap");
assertTrue(response.indexOf("GetMultipleResourcePropertiesResponse") > -1);
assertTrue(response.indexOf("Idle") > -1);
assertTrue(response.indexOf("raft-03.xsd\">0") > -1);
}
public void testGetPrinterResourcePropertiesDocument_SCENARIO_A3() throws IOException
{
String response = sendPrinterRequest("GetPrinterPropertiesDoc.soap");
assertTrue(response.indexOf("GetResourcePropertyDocumentResponse") > -1);
assertTrue(response.indexOf("printer_properties") > -1);
assertTrue(response.indexOf("operations_supported") > -1);
}
public void testQueryResourceProperties_SCENARIO_A4() throws IOException
{
String response = sendPrinterRequest("QueryPropertiesDoc.soap");
assertTrue(response.indexOf("QueryResourcePropertiesResponse") > -1);
assertTrue(response.indexOf("true") > -1);
}
/* public void testQueryCompletedJobs_SCENARIO_A4_VARIANT() throws IOException
{ //do this is after jobs are created
String response = sendRequest(m_printerURL, "QueryJobsCompleted.soap");
assertTrue(response.indexOf("QueryResourcePropertiesResponse") > -1);
// look at response to see how to validate
//assertTrue(response.indexOf("true") > -1);//check further once understand issue
}
*/
public void testUpdateResourceProperties_SCENARIO_B1() throws IOException
{
String response = sendPrinterRequest("UpdateProps.soap");
assertTrue(response.indexOf("UpdateResourcePropertiesResponse") > -1);
}
public void testFailInsertResourceProps_SCENARIO_B1_FAIL() throws IOException
{
String response = sendPrinterRequest("InsertProps.soap");
System.out.println("FAULT: " + response);
assertTrue(response.indexOf("InvalidInsertResourcePropertiesRequestContentFault") > -1);
}
public void testSetResourceProperties_SCENARIO_B2() throws IOException
{
String response = sendPrinterRequest("Set_UpdateDocFormat.soap");
assertTrue(response.indexOf("SetResourcePropertiesResponse") > -1);
response = sendPrinterRequest("GetPrinterPropertiesDoc.soap");
assertTrue(response.indexOf("text/plain") > -1);
assertTrue(response.indexOf("application/postscript") > -1);
// document_format_supported insert/delete should fail
response = sendPrinterRequest("Set_DeleteDocFormat.soap");
assertTrue(response.indexOf("fault") > -1);
response = sendPrinterRequest("Set_InsertDocFormat.soap");
assertTrue(response.indexOf("Fault") > -1);
System.out.println("");
}
public void testMultiSetResourceProps_SCENARIO_B3() throws IOException
{
//there are 2 calls in the spec
String response = sendPrinterRequest("Set_MultiInsertJobHold.soap");
assertTrue(response.indexOf("SetResourcePropertiesResponse") > -1);
//insert Overnight
response = sendPrinterRequest("Set_InsertJobHoldOvernight.soap");
assertTrue(response.indexOf("SetResourcePropertiesResponse") > -1);
response = sendPrinterRequest("GetPrinterPropertiesDoc.soap");
assertTrue(response.indexOf("Overnight") > -1);
}
public void testFailMultiSetResourceProps_SCENARIO_B3_VARIANT() throws IOException
{
String response = sendPrinterRequest("Set_Should_Fail.soap");
assertTrue(response.indexOf("fault") > -1);
}
public void testResetToInitialState_SCENARIO_B3_RESET() throws IOException
{
String response = sendPrinterRequest("Set_MultiDelete.soap");
assertTrue(response.indexOf("SetResourcePropertiesResponse") > -1);
response = sendPrinterRequest("GetPrinterPropertiesDoc.soap");
assertTrue(response.indexOf("job_hold_until_default") == -1);
assertTrue(response.indexOf("_hold_until_supported") == -1);
}
public void testPrintJob_SCENARIO_C() throws IOException
{
String response = printJob();
assertTrue(response.indexOf("CreationResponse") > -1);
assertTrue(response.indexOf("successful-ok") > -1);
}
public void testValidatePrinterJobProperties() throws IOException
{
String response = sendPrinterRequest("GetPrinterPropertiesDoc.soap");
assertTrue(response.indexOf("job_properties") > -1);
assertTrue(response.indexOf("job_reference") > -1);
assertTrue(response.indexOf("pending") > -1);
}
public void testDestroy_SCENARIO_D() throws IOException
{
String jobResponse = printJob();
EnvelopeDocument envelope = loadResponseEnvelope(jobResponse);
XmlBeanUtils.getChildElements(getCreationResponse(envelope));
setJobURL(envelope);
EnvelopeDocument destroyEnvelopeDocument = loadDocument("Destroy_Job.soap");
addReferencePropsToEnvelope(destroyEnvelopeDocument, getJobEndpointReference(envelope).getReferenceProperties());
System.out.println("Destroying JOB: ");
String response = sendJobRequest(destroyEnvelopeDocument);
assertTrue(response.indexOf("DestroyResponse") > -1);
//should fail
response = sendJobRequest(destroyEnvelopeDocument);
assertTrue(response.indexOf("fault") > -1);
}
private String sendJobRequest(EnvelopeDocument request) throws IOException
{
Header header = request.getEnvelope().getHeader();
setAddressingHeaders(header, m_jobURL);
return sendRequest(m_jobURL, request.newInputStream());
}
public void testCreateJob_SCENARIO_E() throws IOException
{
String response = createJob();
assertTrue(response.indexOf("CreationResponse") > -1);
assertTrue(response.indexOf("successful-ok") > -1);
response = sendPrinterRequest("GetPrinterPropertiesDoc.soap");
}
public void testSetTerminationTime_SCENARIO_E_CONTINUED() throws IOException
{
String response = createJob();
EnvelopeDocument responseEnvelope = loadResponseEnvelope(response);
setJobURL(responseEnvelope);
EnvelopeDocument envelopeDoc = null;
envelopeDoc = loadDocument("SetTerminationTime.soap");
addReferencePropsToEnvelope(envelopeDoc, getJobEndpointReference(responseEnvelope).getReferenceProperties());
Calendar instance = Calendar.getInstance();
instance.set(Calendar.MINUTE, instance.get(Calendar.MINUTE) + 5);
Envelope requestEnvelope = envelopeDoc.getEnvelope();
XmlObject[] childElements = XmlBeanUtils.getChildElements(requestEnvelope.getBody());
SetTerminationTimeDocument.SetTerminationTime childElement = (SetTerminationTimeDocument.SetTerminationTime) childElements[0];
childElement.setRequestedTerminationTime(instance);
System.out.println("Setting termination time on JOB: ");
System.out.println(envelopeDoc.toString());
response = sendJobRequest( envelopeDoc );
assertTrue(response.indexOf("SetTerminationTimeResponse") > -1);
}
public void testSendDocument_SCENARIO_E_CONTINUED() throws IOException
{
String response = createJob();
EnvelopeDocument responseEnvelope = loadResponseEnvelope(response);
setJobURL(responseEnvelope);
EnvelopeDocument envelopeDocument = loadDocument("SendDocument.soap");
addReferencePropsToEnvelope(envelopeDocument, getJobEndpointReference(responseEnvelope).getReferenceProperties());
response = sendJobRequest( envelopeDocument);
assertTrue(response.indexOf("CreationResponse") > -1);
assertTrue(response.indexOf("successful-ok") > -1);
}
public void testSendDocument_SCENARIO_E_IBM() throws IOException
{
createPrinter();//create printer
testUpdateResourceProperties_SCENARIO_B1();//set accepting job state to true
String response = sendPrinterRequest("Set_UpdateDocFormat.soap");
response = createJob(); //create Job
EnvelopeDocument responseEnvelope = loadResponseEnvelope(response);
setJobURL(responseEnvelope);
EnvelopeDocument setTerminationDoc = loadDocument("SetTerminationTime.soap");
addReferencePropsToEnvelope(setTerminationDoc, getJobEndpointReference(responseEnvelope).getReferenceProperties());
Calendar newTermTime = Calendar.getInstance();
newTermTime.set(Calendar.MINUTE, newTermTime.get(Calendar.MINUTE) + 1);
System.out.println("Setting termination time on JOB: ");
Envelope requestEnvelope = setTerminationDoc.getEnvelope();
XmlObject[] termElems = XmlBeanUtils.getChildElements(requestEnvelope.getBody());
SetTerminationTimeDocument.SetTerminationTime childElement = (SetTerminationTimeDocument.SetTerminationTime) termElems[0];
childElement.setRequestedTerminationTime(newTermTime);
response = sendJobRequest( setTerminationDoc );
//send a document
EnvelopeDocument envelopeDocument = loadDocument("SendDocument.soap");
addReferencePropsToEnvelope(envelopeDocument, getJobEndpointReference(responseEnvelope).getReferenceProperties());
response = sendJobRequest( envelopeDocument );
assertTrue(response.indexOf("Fault") == -1);
try
{
Thread.sleep(120000L);
}
catch (InterruptedException e)
{
;
}
//this request should be bad and return with a fault if the Job successfully timed out
envelopeDocument = loadDocument("SendDocument.soap");
addReferencePropsToEnvelope(envelopeDocument, getJobEndpointReference(responseEnvelope).getReferenceProperties());
response = sendJobRequest( envelopeDocument );
assertTrue(response.indexOf("ResourceUnknownFault") > -1);
}
//***** end tests ********
private String sendPrinterRequest(String fileName) throws IOException
{
EnvelopeDocument envelopeDocument = loadDocument(fileName);
addReferencePropsToEnvelope(envelopeDocument, m_printerReferenceProperties);
return sendRequest(m_printerURL, envelopeDocument.newInputStream());
}
private CreationResponseDocument.CreationResponse getCreationResponse(EnvelopeDocument envelope)
{
return (CreationResponseDocument.CreationResponse) XmlBeanUtils.getChildElements(envelope.getEnvelope().getBody(), new QName(PrinterPortPropertyQNames.PRINTER_REFERENCE.getNamespaceURI(), "CreationResponse"))[0];
}
private void setJobURL(EnvelopeDocument envelope)
{
EndpointReferenceType jobRef = getJobEndpointReference(envelope);
setJobURL(jobRef.getAddress().getStringValue());
}
private EndpointReferenceType getJobEndpointReference(EnvelopeDocument envelope)
{
EndpointReferenceType jobRef = (EndpointReferenceType) XmlBeanUtils.getChildElements(getCreationResponse(envelope), JobPortPropertyQNames.JOB_REFERENCE)[0];
return jobRef;
}
private void addReferencePropsToEnvelope(EnvelopeDocument envelopeDocument, ReferencePropertiesType referenceProperties)
{
Header header = envelopeDocument.getEnvelope().getHeader();
if (referenceProperties != null)
{
XmlObject[] childElements = XmlBeanUtils.getChildElements(referenceProperties);
for (int i = 0; i < childElements.length; i++)
{
XmlObject childElement = childElements[i];
XmlBeanUtils.addChildElement(header, childElement);
}
}
setAddressingHeaders(header, m_printerURL);
}
private void setAddressingHeaders(Header header, String url)
{
XmlObject[] childElements = XmlBeanUtils.getChildElements(header, new QName("http://schemas.xmlsoap.org/ws/2004/08/addressing", "To") );
AttributedURI childElement = (AttributedURI) childElements[0];
childElement.setStringValue(url);
}
private void setPrinterReferenceProps(CreatePrinterResponseDocument.CreatePrinterResponse createPrinter)
{
m_printerReferenceProperties = createPrinter.getPrinterReference().getReferenceProperties();
}
private void setPrinterURL(CreatePrinterResponseDocument.CreatePrinterResponse create)
{
EndpointReferenceType printerRef = (EndpointReferenceType) XmlBeanUtils.getChildElements(create, PrinterPortPropertyQNames.PRINTER_REFERENCE)[0];
m_printerURL = printerRef.getAddress().getStringValue();
}
private EnvelopeDocument loadResponseEnvelope(String response)
{
EnvelopeDocument envelope = null;
try
{
envelope = EnvelopeDocument.Factory.parse(response);
}
catch (XmlException e)
{
e.printStackTrace();
}
return envelope;
}
protected String createJob() throws IOException
{
return sendPrinterRequest("Create_Job.soap");
}
protected String printJob() throws IOException
{
return sendPrinterRequest("PrintJobRequest.soap");
}
private XmlObject getResourceIDFromJobReference(EnvelopeDocument envelope)
{
XmlObject[] childElements = XmlBeanUtils.getChildElements(envelope.getEnvelope().getBody(), new QName(JobPortPropertyQNames.JOB_REFERENCE.getNamespaceURI(), "CreationResponse"));
childElements = XmlBeanUtils.getChildElements(childElements[0], JobPortPropertyQNames.JOB_REFERENCE);
childElements = XmlBeanUtils.getChildElements(childElements[0], new QName("http://schemas.xmlsoap.org/ws/2004/08/addressing", "ReferenceProperties"));
return XmlBeanUtils.getChildElements(childElements[0], new QName(JobPortService.TARGET_NSURI, "ResourceID"))[0];
}
private EnvelopeDocument loadDocument(String requestFile)
throws IOException
{
EnvelopeDocument envelopeDoc = null;
try
{
envelopeDoc = (EnvelopeDocument) XmlObject.Factory.parse(new File(m_requestsDir, requestFile));
}
catch (XmlException e)
{
fail(e.getMessage());
}
return envelopeDoc;
}
public void setRunAxisServer(boolean RUN_AXIS_SERVER)
{
this.RUN_AXIS_SERVER = RUN_AXIS_SERVER;
}
public void setBaseURL(String m_baseURL)
{
this.m_baseURL = m_baseURL;
}
public void setPrinterFactoryURL(String m_printerFactoryURL)
{
this.m_printerFactoryURL = m_printerFactoryURL;
}
public void setPrinterURL(String m_printerURL)
{
this.m_printerURL = m_printerURL;
}
public void setJobURL(String m_jobURL)
{
this.m_jobURL = m_jobURL;
}
public void setRequestsDir(String requestsDir)
{
m_requestsDir = new File(requestsDir);
}
public String sendRequest(String requestURL, String requestFileName) throws IOException
{
return SoapClient.sendRequest(new URL(requestURL), new File(m_requestsDir, requestFileName), null);
}
public String sendRequest(String requestURL, InputStream is) throws IOException
{
String response = SoapClient.sendRequest(new URL(requestURL), is, null);
is.close();
return response;
}
}