    1   package org.controlhaus.amazon;
    2
    3   import java.io.File;
    4   import java.lang.reflect.Method;
    5
    6   import org.apache.beehive.controls.api.bean.Control;
    7   import org.apache.beehive.controls.api.context.ControlBeanContext;
    8   import org.apache.beehive.controls.runtime.bean.ControlContainerContext;
    9   import org.apache.beehive.controls.api.events.EventHandler;
    10  import org.apache.xmlbeans.XmlObject;
    11  import org.codehaus.xfire.fault.XFireFault;
    12  import org.controlhaus.amazon.AmazonClientControl;
    13  import org.controlhaus.xfire.client.XFireClientControl;
    14
    15  import com.amazon.webservices.awseCommerceService.x20041019.ItemLookupDocument;
    16  import com.amazon.webservices.awseCommerceService.x20041019.ItemLookupRequest;
    17  import com.amazon.webservices.awseCommerceService.x20041019.ItemLookupResponseDocument;
    18  import com.amazon.webservices.awseCommerceService.x20041019.ItemLookupDocument.ItemLookup;
    19  import com.amazon.webservices.awseCommerceService.x20041019.ItemsDocument.Items;
    20
    21  import junit.framework.TestCase;
    22
    23  /***
    24   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    25   * @since Nov 2, 2004
    26   */
    27  public class AmazonClientTest
    28      extends AbstractControlTest
    29  {
    30      @Control AmazonClientControl amazon;
    31
    32      private boolean receivedResponse;
    33      private ItemLookupResponseDocument response;
    34
    35      public void testControl()
    36          throws Exception
    37      {
    38          assertNotNull(amazon);
    39
    40          ItemLookupDocument doc = ItemLookupDocument.Factory.newInstance();
    41          ItemLookup lookup = doc.addNewItemLookup();
    42          lookup.setAssociateTag("dandiephosblo-20");
    43          lookup.setSubscriptionId("1E5AY4ZG53H4AMC8QH82");
    44
    45          ItemLookupRequest request = lookup.addNewRequest();
    46          request.addItemId("0486411214");
    47          request.setIdType(ItemLookupRequest.IdType.ASIN);
    48
    49          response = amazon.ItemLookup(doc);
    50
    51          Items[] itemsArr = response.getItemLookupResponse().getItemsArray();
    52          assertEquals(1, itemsArr.length);
    53          System.out.println(response.xmlText());
    54          assertEquals("0486411214", itemsArr[0].getItemArray()[0].getASIN());
    55      }
    56
    57      public void testAsyncControl()
    58  	    throws Exception
    59  	{
    60  	    assertNotNull(amazon);
    61
    62  	    ItemLookupDocument doc = ItemLookupDocument.Factory.newInstance();
    63  	    ItemLookup lookup = doc.addNewItemLookup();
    64  	    lookup.setAssociateTag("dandiephosblo-20");
    65  	    lookup.setSubscriptionId("1E5AY4ZG53H4AMC8QH82");
    66
    67  	    ItemLookupRequest request = lookup.addNewRequest();
    68  	    request.addItemId("0486411214");
    69  	    request.setIdType(ItemLookupRequest.IdType.ASIN);
    70
    71  	    amazon.beginItemLookup(doc);
    72
    73  	    for ( int i = 0; i < 60; i++ )
    74  	    {
    75  	        if ( !receivedResponse )
    76  	            wait(500);
    77  	        else
    78  	            break;
    79  	    }
    80
    81  	    if ( !receivedResponse )
    82  	        fail("Didn't receive async response.");
    83
    84  	    Items[] itemsArr = response.getItemLookupResponse().getItemsArray();
    85          assertEquals(1, itemsArr.length);
    86          System.out.println(response.xmlText());
    87          assertEquals("0486411214", itemsArr[0].getItemArray()[0].getASIN());
    88  	}
    89
    90      @EventHandler(field="amazon",
    91                    eventSet=XFireClientControl.EndInvokeCallback.class,
    92                    eventName="endInvoke")
    93      public void endInvoke( XmlObject[] response, XmlObject[] responseHeaders )
    94      {
    95          receivedResponse = true;
    96          this.response = (ItemLookupResponseDocument) response[0];
    97      }
    98
    99      @EventHandler(field="amazon",
    100                   eventSet=XFireClientControl.EndInvokeCallback.class,
    101                   eventName="handleFault")
    102     public void handleFault( XFireFault fault )
    103     {
    104         receivedResponse = true;
    105     }
    106 }