
package com.sterlingcommerce.scc.agent.services.test;

import java.net.URL;
import java.util.Calendar;
import java.util.Date;

import com.sterlingcommerce.scc.agent.services.nodeClient.NodeAgentServiceServiceLocator;
import com.sterlingcommerce.scc.agent.services.nodeClient.NodeAgentServiceSoapBindingStub;
import com.sterlingcommerce.scc.agent.services.node.StatRecord;

import junit.framework.AssertionFailedError;

public class NodeAgentServiceTestCase extends junit.framework.TestCase
{
  static {
    System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
    System.setProperty("javax.net.ssl.trustStore","C:/tomcat5/tomcatKey"); 
  }
  
  /**
   * 
   */
  public static void main(String args[])
  {
    NodeAgentServiceTestCase obj;
    obj= new NodeAgentServiceTestCase("from main");
    
    try {
      obj.testNodeAgentServiceGetStats();
      obj.testNodeAgentClient();
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  /**
   * 
   */
  public NodeAgentServiceTestCase(java.lang.String name)
  {
    super(name);
  }

  /**
   * 
   */
  public void testNodeAgentServiceEchoString() throws Exception
  {
    NodeAgentServiceSoapBindingStub binding;
    try {
      binding = (NodeAgentServiceSoapBindingStub) new NodeAgentServiceServiceLocator().getnodeAgentService();
    }
    catch (javax.xml.rpc.ServiceException jre) {
      if (jre.getLinkedCause() != null)
        jre.getLinkedCause().printStackTrace();
      throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
    }
    assertNotNull("binding is null", binding);

    // Time out after a minute
    binding.setTimeout(60000);

    // Test operation
    String value = null;
    value = binding.echoString(new String(), new String());
    // TBD - validate results
  }

//  String secureAddress = "https://localhost:8442/nodeAgentWebapp/services/nodeAgentService";
//  String simpleAddress = "http://localhost:8080/nodeAgentWebapp/services/nodeAgentService";
//  String simpleAddress = "http://bbussell-tw:8080/nodeAgentWebapp/services/nodeAgentService";
  String simpleAddress = "http://dander:8080/nodeAgentWebapp/services/nodeAgentService";

  /**
   * 
   */
  public void testNodeAgentClient() throws Exception
  {
    
    NodeAgentClient obj= new NodeAgentClient("testNodeAgentClient", simpleAddress);
    int recordsRequested = 1000;
    StatRecord recordsProcessed[] = obj.getStats(recordsRequested);
    assertEquals(recordsProcessed.length, recordsRequested);
    validateStatRecords(recordsProcessed);
  }
  
  /**
   * 
   */
  public void testNodeAgentServiceGetStats() throws Exception
  {
    NodeAgentServiceSoapBindingStub binding;
    try {
      URL url = new URL(simpleAddress);

      binding = (NodeAgentServiceSoapBindingStub) new NodeAgentServiceServiceLocator().getnodeAgentService(url);
    }
    catch (javax.xml.rpc.ServiceException jre) {
      if (jre.getLinkedCause() != null)
        jre.getLinkedCause().printStackTrace();
      throw new AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
    }
    assertNotNull("binding is null", binding);

    // Time out after a minute
    binding.setTimeout(60000);

    // Test operation
    StatRecord[] statRecords = null;
    long start = System.currentTimeMillis();
    System.out.println("Starting Operation " + start);
    statRecords = binding.getStats(new Date(), 1000);
    long end = System.currentTimeMillis();
    System.out.println("Ending Operation "  + end + "  delta = "+(end - start));

    validateStatRecords(statRecords);
  }
  
  /**
   * 
   */
  private void validateStatRecords(StatRecord statRecords[])
  {
    System.out.println("Stat Record Array Length " + statRecords.length);
    for(int idx = 0; idx < statRecords.length; idx++) {
      assert "destFileName".equals(statRecords[idx].getDestFileName());
      assertEquals(statRecords[idx].getProcessId(), String.valueOf(idx));
      System.out.print(".");
    }
    System.out.println();
  }
}