/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

// $Id: WSRUnitTestCase.java,v 1.1.1.1.4.1 2004/08/09 09:54:43 tdiesler Exp $

package org.jboss.test.jbossnet.wsr;

import org.jboss.test.jbossnet.WSTests;
import org.xml.sax.InputSource;

import com.ibm.wsdl.factory.WSDLFactoryImpl;

import junit.framework.Test;

import java.io.InputStream;
import java.net.URL;

import javax.wsdl.Definition;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;

/**
 * Tests remote accessibility of "ordinary" (wsr-) deployed Axis services
 * @created 11. Oktober 2001
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @version $Revision: 1.1.1.1.4.1 $
 */
public class WSRUnitTestCase extends WSTests
{
   protected String ADDRESS_SOAP_ACTION="AddressBook";

   // Constructors --------------------------------------------------
   public WSRUnitTestCase(String name)
   {
      super(name);
   }

   /** prepared data */
   Address address;
   AddressBook book;

   /** sets up the test */
   public void setUp() throws Exception
   {
      super.setUp();

      interfaceMap.put(AddressBook.class, "http://net.jboss.org/samples/AddressBook");

      Phone phone = new Phone();
      phone.setExchange("(0)6897");
      phone.setNumber("6666");
      phone.setAreaCode(49);
      StateType state = StateType.fromString("TX");
      address = new Address();
      address.setStreetNum(42);
      address.setStreetName("Milky Way");
      address.setCity("Galactic City");
      address.setZip(2121);
      address.setState(state);
      address.setPhoneNumber(phone);

      book = (AddressBook) createAxisService(AddressBook.class,
        ADDRESS_SOAP_ACTION);
   }

   /** this is where the axis config is stored */
   protected String getAxisConfiguration()
   {
      return "jbossnet/wsr/client/" + super.getAxisConfiguration();
   }

   /** routes an address to the server and tests the result */
   public void testAddress() throws Exception
   {
      book.addEntry("George", address);
      assertEquals("Comparing addresses", address, book.
         getAddressFromName("George"));
   }


   /** tests wsdl access */
    public void testWsdl() throws Exception
    {
       // check if the WSDL is available
       URL urlWSDL = new URL(END_POINT +"/"+ADDRESS_SOAP_ACTION+ "?wsdl");
       InputStream isWSDL = urlWSDL.openStream();

       // check if it is a valid WSDL
       WSDLFactory wsdlFactory = new WSDLFactoryImpl();
       WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
       Definition wsdlDefinition = wsdlReader.readWSDL(null, new InputSource(isWSDL));
       assertNotNull("cannot obtain the wsdl", wsdlDefinition);
       //TODO test port address
    }
    
   /** suite method */
   public static Test suite() throws Exception
   {
      return getDeploySetup(WSRUnitTestCase.class, "wsr.wsr");
   }
}