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

// $Id: OrderProcessDOCTestCase.java,v 1.1.2.4 2005/07/27 12:50:01 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 OrderProcessDOCTestCase extends WebserviceTestBase
{

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

   /** Deploy the test */
   public static Test suite() throws Exception
   {
      return getDeploySetup(OrderProcessDOCTestCase.class, "ws4ee-samples2-doc.war, ws4ee-samples2-doc-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");
      OrderProcess endpoint = (OrderProcess)service.getPort(OrderProcess.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);

      OrderResponse res = endpoint.processOrder(new OrderItem[] { i0, i1, i2 }, p);
      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");
      OrderProcess endpoint = (OrderProcess)service.getPort(OrderProcess.class);

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

      try
      {
         endpoint.processOrder(new OrderItem[] { i1, i2, i3 }, null);
         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");
      OrderProcess endpoint = (OrderProcess)service.getPort(OrderProcess.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
      {
         endpoint.processOrder(new OrderItem[] { i1, i2, i3, i4 }, p);
         fail("OrderException expected");
      }
      catch (OrderException e)
      {
         // ignore expected exception
      }
   }
}
