package org.jboss.test.webservice.jbws84;
import junit.framework.Test;
import org.jboss.test.webservice.WebserviceTestBase;
import org.w3c.dom.Document;
import javax.naming.InitialContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.Service;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
import java.io.ByteArrayInputStream;
public class JBWS84TestCase extends WebserviceTestBase
{
public JBWS84TestCase(String name)
{
super(name);
}
public static Test suite() throws Exception
{
return getDeploySetup(JBWS84TestCase.class, "ws4ee-jbws84.war, ws4ee-jbws84-client.jar");
}
public void testProcessSOAPElement() throws Exception
{
InitialContext iniCtx = getClientContext();
Service service = (Service)iniCtx.lookup("java:comp/env/service/MessageService");
Message port = (Message)service.getPort(Message.class);
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage reqMessage = factory.createMessage();
DocumentBuilder builder = getDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(Message.request.getBytes()));
reqMessage.getSOAPBody().addDocument(doc);
SOAPEnvelope soapEnvelope = reqMessage.getSOAPPart().getEnvelope();
SOAPBody soapBody = soapEnvelope.getBody();
SOAPElement reqElement = (SOAPElement)soapBody.getChildElements().next();
SOAPElement resElement = port.processSOAPElement(reqElement);
validateResponse(resElement);
}
private DocumentBuilder getDocumentBuilder() throws ParserConfigurationException
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
return builder;
}
private void validateResponse(SOAPElement soapElement)
throws SOAPException
{
SOAPFactory factory = SOAPFactory.newInstance();
Name expName = factory.createName("Response", Message.PREFIX, Message.NAMESPACE_URI);
Name elementName = soapElement.getElementName();
assertEquals(expName, elementName);
expName = factory.createName("POID");
soapElement = (SOAPElement)soapElement.getChildElements(expName).next();
elementName = soapElement.getElementName();
assertEquals(expName, elementName);
String elementValue = soapElement.getValue();
assertEquals("12345", elementValue);
expName = factory.createName("Status");
soapElement = (SOAPElement)soapElement.getNextSibling();
elementName = soapElement.getElementName();
assertEquals(expName, elementName);
elementValue = soapElement.getValue();
assertEquals("ok", elementValue);
}
}