/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.test.webservice.jbws167;

// $Id: JBWS167TestCase.java,v 1.1.2.5 2005/07/27 12:50:00 tdiesler Exp $

import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import junit.framework.Test;

import org.jboss.test.webservice.WebserviceTestBase;

/**
 * Tests that we can send and receive HTTP headers. Also test the we support multiple
 * values per header (like cookies)
 *
 * @author <a href="mailto:jason.greene@jboss.com">Jason T. Greene</a>
 * @since 05-Mar-2005
 */
public class JBWS167TestCase extends WebserviceTestBase
{
   /**
    * Construct the test case with a given name
    */
   public JBWS167TestCase(String name)
   {
      super(name);
   }

   /** Deploy the test */
   public static Test suite() throws Exception
   {
      return getDeploySetup(JBWS167TestCase.class, "ws4ee-jbws167.war");
   }

   public void testHttpHeaders() throws Exception
   {
      String NS_PREFIX = "ns1";
      String NS_URI = "http://org.jboss.test.webservice/jbws167";
      String rpcMethodName = "hello";

      MessageFactory mf = MessageFactory.newInstance();
      SOAPMessage msg = mf.createMessage();
      SOAPPart sp = msg.getSOAPPart();
      SOAPEnvelope envelope = sp.getEnvelope();
      SOAPBody bdy = envelope.getBody();
      SOAPBodyElement sbe = bdy.addBodyElement(envelope.createName(rpcMethodName, NS_PREFIX, NS_URI));

      // Add a some child elements
      sbe.addChildElement(envelope.createName("String_1")).addTextNode("Some hello");

      MimeHeaders headers = msg.getMimeHeaders();
      headers.addHeader("SOAPAction", "/foo/bar");
      headers.addHeader("testme", "testme");
      headers.addHeader("testme", "testme2");

      SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
      SOAPConnection con = conFactory.createConnection();
      SOAPMessage resMessage = con.call(msg, new URL("http://" + getServerHost() + ":8080/ws4ee-jbws167"));

      SOAPBody soapBody = resMessage.getSOAPBody();
      SOAPEnvelope soapEnvelope = (SOAPEnvelope) soapBody.getParentElement();

      Name rpcName = soapEnvelope.createName(rpcMethodName + "Response", NS_PREFIX, NS_URI);
      Iterator childElements = soapBody.getChildElements(rpcName);
      assertTrue("Expexted child: " + rpcName, childElements.hasNext());

      SOAPElement bodyChild = (SOAPElement) childElements.next();
      Name resName = soapEnvelope.createName("result");
      SOAPElement resElement = (SOAPElement) bodyChild.getChildElements(resName).next();
      String value = resElement.getValue();
      assertEquals("[pass]", value);

      verifyResponseHeaders(resMessage);
   }

   private void verifyResponseHeaders(SOAPMessage message)
   {
      MimeHeaders headers = message.getMimeHeaders();
      assertNotNull(headers);

      HashSet expectedValues = new HashSet();
      expectedValues.add("1");
      expectedValues.add("2");
      expectedValues.add("3");

      String[] values = headers.getHeader("server-test-header");
      assertTrue("Expected 3 values for server test header", values != null && values.length == 3);
      for (int i = 0; i < 3; i++)
      {
         assertTrue("Expected results did not match (got " + values[i] + " for a result)", expectedValues.contains(values[i]));
         expectedValues.remove(values[i]);
      }
    }
}
