/* 
 * QoS Discovery Component
 * Copyright (C) 2006 Sebastian Gerlach
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package ch.epfl.qosdisc.wsmx;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import ch.epfl.qosdisc.operators.TestFrame;
import ch.epfl.qosdisc.operators.Manager;

import javax.xml.namespace.QName;
import java.io.*;
import java.net.URL;

/**
 * WSMX test framework. This application programmatically invokes the web
 * services exposed by WSMX in order to test the QoS discovery components.
 *  
 * @author Sebastian Gerlach
 */
public class TestWSMX {
    
    /**
     * Read contents of a file to a string.
     * 
     * @param filename Name of file to read.
     * @return String with contents of file.
     */
    public static String readFile(String filename) {
        String ts = "";
        
        try {
            
            // Read a WSML file to a string.
            BufferedReader br = new BufferedReader(new InputStreamReader( new URL(Manager.rename(filename)).openStream()));
            String s;
            while((s = br.readLine())!=null) {
                ts=ts+s+"\n";
            }
            br.close();
        } catch(IOException ex) {
            ex.printStackTrace();
        }
        return ts;
    }
    
    /**
     * Main function.
     * 
     * @param args Command line arguments.
     */
    public static void main(String[] args) {
        
        // Create the test frame in order to ensure that the properties are loaded.        
        new TestFrame(".");
        
        try {
            Call call;
            String ret;
            
            // Where we can find WSMX (the axis endpoint).
            String endpoint = TestFrame.props.getProperty("wsmxhost");

            // Now get ready for some axis calls.
            Service service = new Service();

            // First the 'store' call.
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.setOperationName(new QName("http://soapinterop.org/","store"));
            
            // Add the services.
            for(int i = 1;;i++) {
                System.out.println("Adding services to WSMX store");
                String sn = TestFrame.props.getProperty("service"+i);
                if(sn == null)
                    break;
                System.out.println(" - "+sn);
                
                // Load the WSML file.
                sn = readFile(sn);
                if(sn != null) {

                    // Now try to store the WSML file in WSMX. 
                    ret = (String) call.invoke(new Object[] { sn } );
                    System.out.println(ret);
                }
            }
            
            // Next the 'achieveGoal' call
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.setOperationName(new QName("http://soapinterop.org/","achieveGoal"));

            // Pass a goal to WSMX
            String sn = TestFrame.props.getProperty("goal");
            if(sn != null) {
                System.out.println("Calling WSMX to achieve goal "+sn);
                sn = readFile(sn);
                if(sn != null) {
                    
                    // Call the WSMX web service entry point.
                    ret = (String) call.invoke(new Object[] { sn });
                    System.out.println(ret);
                }
            }
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
