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

// $Id: OrderProcessDOCWrappedTestCase.java,v 1.1.2.2 2005/08/10 12:54:37 tdiesler Exp $

import junit.framework.Test;
import org.jboss.test.webservice.WebserviceTestBase;

import javax.naming.InitialContext;
import javax.xml.rpc.Service;

/**
 * Test access to a complex doc/literal endpoint
 *
 * @author Thomas.Diesler@jboss.org
 * @since 26-Apr-2004
 */
public class OrderProcessDOCWrappedTestCase extends WebserviceTestBase
{

   public OrderProcessDOCWrappedTestCase(String name)
   {
      super(name);
   }

   /** Deploy the test */
   public static Test suite() throws Exception
   {
      return getDeploySetup(OrderProcessDOCWrappedTestCase.class, "ws4ee-samples2-docwr.war, ws4ee-samples2-docwr-client.jar");
   }

   /** Test a valid access */
   public void testValidAccess() throws Exception
   {
      if (isWS4EEAvailable() == false)
      {
         System.out.println("FIXME: JBWS-323");
         return;
      }
      
      InitialContext iniCtx = getClientContext();
      Service service = (Service)iniCtx.lookup("java:comp/env/service/OrderProcess");
      OrderProcessWrapped endpoint = (OrderProcessWrapped)service.getPort(OrderProcessWrapped.class);

      Person p = new Person("Tom", 3);
      OrderItem i0 = new OrderItem("Ferrari", 1);
      OrderItem i1 = new OrderItem("Twix", 10);
      OrderItem i2 = new OrderItem("IceCream", 3);

      OrderProcess_processOrder_RequestStruct reqStr = new OrderProcess_processOrder_RequestStruct(new OrderItem[] { i0, i1, i2 }, p);
      OrderProcess_processOrder_ResponseStruct resStr = endpoint.processOrder(reqStr);
      OrderResponse res = resStr.getResult();
      assertEquals(3, res.getItems().length);
      assertEquals(i0, res.getItems()[0]);
      assertEquals(i1, res.getItems()[1]);
      assertEquals(i2, res.getItems()[2]);
      assertEquals("approved", res.getMessage());
   }

   /** Test a valid access */
   public void testNullPerson() throws Exception
   {
      if (isWS4EEAvailable() == false)
      {
         System.out.println("FIXME: JBWS-323");
         return;
      }

      InitialContext iniCtx = getClientContext();
      Service service = (Service)iniCtx.lookup("java:comp/env/service/OrderProcess");
      OrderProcessWrapped endpoint = (OrderProcessWrapped)service.getPort(OrderProcessWrapped.class);

      OrderItem i1 = new OrderItem("Ferrari", 1);
      OrderItem i2 = new OrderItem("Twix", 10);
      OrderItem i3 = new OrderItem("IceCream", 3);

      try
      {
         OrderProcess_processOrder_RequestStruct reqStr = new OrderProcess_processOrder_RequestStruct(new OrderItem[] { i1, i2, i3 }, null);
         endpoint.processOrder(reqStr);
         fail("OrderException expected");
      }
      catch (OrderException e)
      {
         // ignore expected exception
      }
   }

   /** Test a valid access */
   public void testTooManyItems() throws Exception
   {
      if (isWS4EEAvailable() == false)
      {
         System.out.println("FIXME: JBWS-323");
         return;
      }

      InitialContext iniCtx = getClientContext();
      Service service = (Service)iniCtx.lookup("java:comp/env/service/OrderProcess");
      OrderProcessWrapped endpoint = (OrderProcessWrapped)service.getPort(OrderProcessWrapped.class);

      Person p = new Person("Tom", 3);
      OrderItem i1 = new OrderItem("Ferrari", 1);
      OrderItem i2 = new OrderItem("Twix", 10);
      OrderItem i3 = new OrderItem("IceCream", 3);
      OrderItem i4 = new OrderItem("GameBoy", 1);

      try
      {
         OrderProcess_processOrder_RequestStruct reqStr = new OrderProcess_processOrder_RequestStruct(new OrderItem[] { i1, i2, i3, i4 }, p);
         endpoint.processOrder(reqStr);
         fail("OrderException expected");
      }
      catch (OrderException e)
      {
         // ignore expected exception
      }
   }
}
