/* -*-Java-*-
********************************************************************************
*
* File:         NetworkTests.java
* RCS:          $Header: $
* Description:  
* Author:       Michael L. Creech
* Created:      Tue Oct 06 15:57:45 2009
* Modified:     Thu Oct 08 14:00:56 2009 (Michael L. Creech) creech@w235krbza760
* Language:     Java/l
* Package:      
* Status:       Experimental (Do Not Distribute)
*
* (c) Copyright 2009, Agilent Technologies, all rights reserved.
*
********************************************************************************
*/

package com.agilent.pwdemo.unittest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.rmi.RemoteException;
import java.util.Properties;

import javax.xml.rpc.ServiceException;

import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

// import org.pathvisio.wikipathways.webservice.WikiPathwaysLocator;
// import org.pathvisio.wikipathways.webservice.WikiPathwaysPortType;

/**
 * Test the connection to the wikiPathways server in various ways.
 * 
 * @author Michael L. Creech
 * @version 1.0
 */
public class NetworkTests extends TestCase {

    private final String _serverLoc = "http://137.120.14.24/wikipathways-test/wpi/webservice/webservice.php/listOrganisms";
    private static final String HTTP_PROXY_HOST  = "http.proxyHost";
    private static final String HTTP_NONPROXY_HOSTS = "http.nonProxHosts";
    private static final String HTTP_PROXY_PORT  = "http.proxyPort";
    private static final String HTTP_PROXY_USER  = "http.proxyUser";
    private static final String HTTP_PROXY_PASSWORD  = "http.proxyPassword";
    private static final String SOCKS_PROXY_HOST = "socksProxyHost";
    private static final String SOCKS_PROXY_PORT = "socksProxyPort";
    private static final String SOCKS_PROXY_USER="java.net.socks.username";
    private static final String SOCKS_PROXY_PASSWORD = "java.net.socks.password";

    public NetworkTests() {
	super();
    }

    /**
     * JUnit method for running tests for this class.
     * 
     * @return the Test to perform.
     */
    public static Test suite() {
	// Will dynamically add all methods as tests that begin with 'test'
	// and have no arguments:
	return new TestSuite(NetworkTests.class);
    }

    /**
     * Main for test.
     * 
     * @param args
     *            standard args to main program
     */
    public static void main(final String[] args) {
	junit.textui.TestRunner.run(suite());
    }

    public void setUp () {
        Properties sysProp = System.getProperties();
	System.out.println ("PROXY SETTINGS BEFORE TEST:");
	System.out.println ("     " + HTTP_PROXY_HOST + "= '" + sysProp.get(HTTP_PROXY_HOST) + "'");
	System.out.println ("     " + HTTP_PROXY_HOST + "= '" + sysProp.get(HTTP_NONPROXY_HOSTS) + "'");
	System.out.println ("     " + HTTP_PROXY_PORT + "= '" + sysProp.get(HTTP_PROXY_PORT) + "'");
	System.out.println ("     " + HTTP_PROXY_USER + "= '" + sysProp.get(HTTP_PROXY_USER) + "'");
	System.out.println ("     " + HTTP_PROXY_PASSWORD + "= '" + sysProp.get(HTTP_PROXY_PASSWORD) + "'");
	System.out.println ("     " + SOCKS_PROXY_HOST + "= '" + sysProp.get(SOCKS_PROXY_HOST) + "'");
	System.out.println ("     " + SOCKS_PROXY_PORT + "= '" + sysProp.get(SOCKS_PROXY_PORT) + "'");
	System.out.println ("     " + SOCKS_PROXY_USER + "= '" + sysProp.get(SOCKS_PROXY_USER) + "'");
	System.out.println ("     " + SOCKS_PROXY_PASSWORD + "= '" + sysProp.get(SOCKS_PROXY_PASSWORD) + "'");
    }

    /**
     * Simulate call to listOrganisms() on the wikiPathways server using direct HTTP call:
     */
    public void testListOrganismsUsingHTTP() {
	try {
	    URL  serverLocURL = new URL (_serverLoc);
	    URLConnection uc = serverLocURL.openConnection();
	    uc.setAllowUserInteraction(false);
	    uc.setUseCaches(false); // don't use a cached page
	    uc.setConnectTimeout(5000);
	    uc.connect();
	    BufferedReader input = new BufferedReader(new InputStreamReader(uc
		    .getInputStream()));
	    String line;
	    System.out.println ("testListOrganismsUsingHTTP organisms:");
	    while ((line = input.readLine()) != null) {
		System.out.println("     " + line);
	    }
	    System.out.println("testListOrganismsUsingHTTP test PASSED");
	}
	catch (MalformedURLException ex) {
	    ex.printStackTrace();
	    Assert.fail("Server location syntax is invalid: " + ex.getMessage());
	}
	catch (IOException e) {
	    // we couldn't connect:
	    e.printStackTrace();
	    Assert.fail("ERROR: " + e.getMessage());
	}
    }

    //    /**
    //     * This version does listOrganisms() without use of Cytoscape or
    //     * GPML Plugin. It uses WikiPathways API to obtain the data.
    //     */
    //    public void testListOrganismsUsingWikiPathwaysPortType() {
    //	try {
    //	    URL  serverLocURL = new URL (_serverLoc);
    //	    WikiPathwaysPortType port = new WikiPathwaysLocator().getWikiPathwaysSOAPPort_Http(serverLocURL);
    //	    String[] r = port.listOrganisms();
    //	    System.out.println ("testListOrganismsUsingWikiPathwaysPortType organisms:");
    //	    for (String organism : r) {
    //		System.out.println("     " + organism);
    //	    }
    //	    System.out.println("testListOrganismsUsingWikiPathwaysPortType test PASSED");
    //	}
    //	catch (ServiceException excep) {
    //	    excep.printStackTrace();
    //	    Assert.fail("Port Creation Failed: " + excep.getMessage());
    //	}
    //	catch (MalformedURLException e) {
    //	    e.printStackTrace();
    //	    Assert.fail("Server location syntax is invalid: " + e.getMessage());
    //	}
    //	catch (RemoteException ex) {
    //	    ex.printStackTrace();
    //	    Assert.fail("port listOrganisms failed: " + ex.getMessage());	    
    //	}
    //    }
    
    /**
     * This version does listOrganisms() without use of Cytoscape,
     * GPML Plugin, or WikiPathways. It directly uses of Axis to
     * obtain the data.
     */
    public void testListOrganismsUsingAxis() {
	try {
	    URL  serverLocURL = new URL (_serverLoc);
	    TestStub stub = new TestLocator().getWikiPathwaysSOAPPort_Http(serverLocURL);
	    String[] r = stub.listOrganisms();
	    System.out.println ("testListOrganismsUsingAxis organisms:");
	    for (String organism : r) {
		System.out.println("     " + organism);
	    }
	    System.out.println("testListOrganismsUsingAxis test PASSED");
	}
	catch (ServiceException excep) {
	    excep.printStackTrace();
	    Assert.fail("Axis Creation Failed: " + excep.getMessage());
	}
	catch (MalformedURLException e) {
	    e.printStackTrace();
	    Assert.fail("Server location syntax is invalid: " + e.getMessage());
	}
	catch (RemoteException ex) {
	    ex.printStackTrace();
	    Assert.fail("Axis listOrganisms failed: " + ex.getMessage());	    
	}
    }

    // maps to wikiPathways WikiPathwaysLocator.  When possible, the code here is copied from the WikiPathways API:
    private class TestLocator extends org.apache.axis.client.Service {
	private static final long serialVersionUID = -7538080100731369756L;
	private static final String WikiPathwaysSOAPPort_HttpWSDDServiceName = "WikiPathwaysSOAPPort_Http";
	public TestLocator() {
	}
	public TestStub getWikiPathwaysSOAPPort_Http(java.net.URL portAddress) throws javax.xml.rpc.ServiceException {

	    try {
		TestStub _stub = new TestStub (portAddress, this);
		_stub.setPortName(WikiPathwaysSOAPPort_HttpWSDDServiceName);
		return _stub;
	    }
	    catch (org.apache.axis.AxisFault e) {
		return null;
	    }
	}
    }

    // maps to WikiPathwaysSOAPBindingStub. When possible, the code here is copied from the WikiPathways API:
    private class TestStub extends org.apache.axis.client.Stub {
	private java.util.Vector cachedSerClasses = new java.util.Vector();
	private java.util.Vector cachedSerQNames = new java.util.Vector();
	private java.util.Vector cachedSerFactories = new java.util.Vector();
	private java.util.Vector cachedDeserFactories = new java.util.Vector();

	public TestStub(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
	    this(service);
	    super.cachedEndpoint = endpointURL;
	}
	    
	public TestStub(javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
	    if (service == null) {
		super.service = new org.apache.axis.client.Service();
	    } else {
		super.service = service;
	    }
	    ((org.apache.axis.client.Service)super.service).setTypeMappingVersion("1.2");
	    // java.lang.Class cls;
	    // javax.xml.namespace.QName qName;
	    // javax.xml.namespace.QName qName2;
	    // java.lang.Class beansf = org.apache.axis.encoding.ser.BeanSerializerFactory.class;
	    // java.lang.Class beandf = org.apache.axis.encoding.ser.BeanDeserializerFactory.class;
	    // java.lang.Class enumsf = org.apache.axis.encoding.ser.EnumSerializerFactory.class;
	    // java.lang.Class enumdf = org.apache.axis.encoding.ser.EnumDeserializerFactory.class;
	    // java.lang.Class arraysf = org.apache.axis.encoding.ser.ArraySerializerFactory.class;
	    // java.lang.Class arraydf = org.apache.axis.encoding.ser.ArrayDeserializerFactory.class;
	    // java.lang.Class simplesf = org.apache.axis.encoding.ser.SimpleSerializerFactory.class;
	    // java.lang.Class simpledf = org.apache.axis.encoding.ser.SimpleDeserializerFactory.class;
	    // java.lang.Class simplelistsf = org.apache.axis.encoding.ser.SimpleListSerializerFactory.class;
	    // java.lang.Class simplelistdf = org.apache.axis.encoding.ser.SimpleListDeserializerFactory.class;
	    //	    qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSAuth");
	    //	    cachedSerQNames.add(qName);
	    //	    cls = org.pathvisio.wikipathways.webservice.WSAuth.class;
	    //	    cachedSerClasses.add(cls);
	    //	    cachedSerFactories.add(beansf);
	    //	    cachedDeserFactories.add(beandf);
	    //	    
	    //	    qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSCurationTag");
	    //	    cachedSerQNames.add(qName);
	    //	    cls = org.pathvisio.wikipathways.webservice.WSCurationTag.class;
	    //	    cachedSerClasses.add(cls);
	    //	    cachedSerFactories.add(beansf);
	    //	    cachedDeserFactories.add(beandf);
	    //	    
	    //	    qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSCurationTagHistory");
	    //	    cachedSerQNames.add(qName);
	    //	    cls = org.pathvisio.wikipathways.webservice.WSCurationTagHistory.class;
	    //	    cachedSerClasses.add(cls);
	    //	    cachedSerFactories.add(beansf);
	    //	    cachedDeserFactories.add(beandf);
	    //	    
	    //	    qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSHistoryRow");
	    //	    cachedSerQNames.add(qName);
	    //	    cls = org.pathvisio.wikipathways.webservice.WSHistoryRow.class;
	    //	    cachedSerClasses.add(cls);
	    //	    cachedSerFactories.add(beansf);
	    //	    cachedDeserFactories.add(beandf);
	    //	    
	    //	    qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSIndexField");
	    //	    cachedSerQNames.add(qName);
	    //	    cls = org.pathvisio.wikipathways.webservice.WSIndexField.class;
	    //	    cachedSerClasses.add(cls);
	    //	    cachedSerFactories.add(beansf);
	    //	    cachedDeserFactories.add(beandf);
	    //	    
	    //	    qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSPathway");
	    //	    cachedSerQNames.add(qName);
	    //	    cls = org.pathvisio.wikipathways.webservice.WSPathway.class;
	    //	    cachedSerClasses.add(cls);
	    //	    cachedSerFactories.add(beansf);
	    //	    cachedDeserFactories.add(beandf);
	    //	    
	    //	    qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSPathwayHistory");
	    //	    cachedSerQNames.add(qName);
	    //	    cls = org.pathvisio.wikipathways.webservice.WSPathwayHistory.class;
	    //	    cachedSerClasses.add(cls);
	    //	    cachedSerFactories.add(beansf);
	    //	    cachedDeserFactories.add(beandf);
	    //	    
	    //	    qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSPathwayInfo");
	    //	    cachedSerQNames.add(qName);
	    //      cls = org.pathvisio.wikipathways.webservice.WSPathwayInfo.class;
	    //      cachedSerClasses.add(cls);
	    //      cachedSerFactories.add(beansf);
	    //      cachedDeserFactories.add(beandf);
	    //
	    //     qName = new javax.xml.namespace.QName("http://www.wikipathways.org/webservice", "WSSearchResult");
	    //     cachedSerQNames.add(qName);
	    //     cls = org.pathvisio.wikipathways.webservice.WSSearchResult.class;
	    //     cachedSerClasses.add(cls);
	    //     cachedSerFactories.add(beansf);
	    //     cachedDeserFactories.add(beandf);
    }


	public java.lang.String[] listOrganisms() throws java.rmi.RemoteException {
	    if (super.cachedEndpoint == null) {
		throw new org.apache.axis.NoEndPointException();
	    }
	    org.apache.axis.description.OperationDesc oper;
	    oper = new org.apache.axis.description.OperationDesc();
	    oper.setName("listOrganisms");
	    oper.setReturnType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
	    oper.setReturnClass(java.lang.String[].class);
	    oper.setReturnQName(new javax.xml.namespace.QName("http://www.wso2.org/php/xsd", "organisms"));
	    oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
	    oper.setUse(org.apache.axis.constants.Use.LITERAL);
	    org.apache.axis.client.Call _call = createCall();
	    _call.setOperation(oper);
	    _call.setUseSOAPAction(true);
	    _call.setSOAPActionURI("http://137.120.14.24/wikipathways-test/wpi/webservice/webservice.php/listOrganisms");
	    _call.setEncodingStyle(null);
	    _call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
	    _call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
	    _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
	    _call.setOperationName(new javax.xml.namespace.QName("http://www.wso2.org/php/xsd", "listOrganisms"));
	    
	    setRequestHeaders(_call);
	    setAttachments(_call);
	    try {
		java.lang.Object _resp = _call.invoke(new java.lang.Object[] {});
		if (_resp instanceof java.rmi.RemoteException) {
		    throw (java.rmi.RemoteException)_resp;
		}
		else {
		    extractAttachments(_call);
		    try {
			return (java.lang.String[]) _resp;
		    } catch (java.lang.Exception _exception) {
			return (java.lang.String[]) org.apache.axis.utils.JavaUtils.convert(_resp, java.lang.String[].class);
		    }
		}
	    } catch (org.apache.axis.AxisFault axisFaultException) {
		throw axisFaultException;
	    }
	}

	protected org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {
	    try {
		org.apache.axis.client.Call _call = super._createCall();
		if (super.maintainSessionSet) {
		    _call.setMaintainSession(super.maintainSession);
		}
		if (super.cachedUsername != null) {
		    _call.setUsername(super.cachedUsername);
		}
		if (super.cachedPassword != null) {
		    _call.setPassword(super.cachedPassword);
		}
		if (super.cachedEndpoint != null) {
		    _call.setTargetEndpointAddress(super.cachedEndpoint);
		}
		if (super.cachedTimeout != null) {
		    _call.setTimeout(super.cachedTimeout);
		}
		if (super.cachedPortName != null) {
		    _call.setPortName(super.cachedPortName);
		}
		java.util.Enumeration keys = super.cachedProperties.keys();
		while (keys.hasMoreElements()) {
		    java.lang.String key = (java.lang.String) keys.nextElement();
		    _call.setProperty(key, super.cachedProperties.get(key));
		}
		// All the type mapping information is registered
		// when the first call is made.
		// The type mapping information is actually registered in
		// the TypeMappingRegistry of the service, which
		// is the reason why registration is only needed for the first call.
		synchronized (this) {
		    if (firstCall()) {
			// must set encoding style before registering serializers
			_call.setEncodingStyle(null);
			for (int i = 0; i < cachedSerFactories.size(); ++i) {
			    java.lang.Class cls = (java.lang.Class) cachedSerClasses.get(i);
			    javax.xml.namespace.QName qName =
                                (javax.xml.namespace.QName) cachedSerQNames.get(i);
			    java.lang.Object x = cachedSerFactories.get(i);
			    if (x instanceof Class) {
				java.lang.Class sf = (java.lang.Class)
				    cachedSerFactories.get(i);
				java.lang.Class df = (java.lang.Class)
				    cachedDeserFactories.get(i);
				_call.registerTypeMapping(cls, qName, sf, df, false);
			    }
			    else if (x instanceof javax.xml.rpc.encoding.SerializerFactory) {
				org.apache.axis.encoding.SerializerFactory sf = (org.apache.axis.encoding.SerializerFactory)
				    cachedSerFactories.get(i);
				org.apache.axis.encoding.DeserializerFactory df = (org.apache.axis.encoding.DeserializerFactory)
				    cachedDeserFactories.get(i);
				_call.registerTypeMapping(cls, qName, sf, df, false);
			    }
			}
		    }
		}
		return _call;
	    }
	    catch (java.lang.Throwable _t) {
		throw new org.apache.axis.AxisFault("Failure trying to get the Call object", _t);
	    }
	}
    }

}
