/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * 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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas.jtests.clients.wssecured;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.objectweb.jonas.jtests.util.JWebServicesTestCase;

/**
 * Test secured endpoints.
 * @author Guillaume Sauthier
 */
public class F_WsSecuredMultipleEndpoint extends JWebServicesTestCase {

	/**
	 * First proectedt endpoint.
	 */
    private static final String ADDRESSBOOK_URL = "/ws-secured/addressbook";

    /**
     * Seconf protected endpoint.
     */
    private static final String ADDRESSBOOK2_URL = "/ws-secured/addressbook2";

    /**
     * HTTP port to use (default to 9000).
     */
	private String port = "9000";

    /**
     * @param s
     */
    public F_WsSecuredMultipleEndpoint(String s) {
        super(s);
    }

    public static Test suite() {
        return new TestSuite(F_WsSecuredMultipleEndpoint.class);
    }

    public void setUp() throws Exception {
        super.setUp();
        port = System.getProperty("http.port");
        useWar("ws-secured");
    }

    public void tearDown() throws Exception {
        super.tearDown();
    }

    public void testAuthorizedAddressBookEndpoint1() throws Exception {

		Service service = ServiceFactory.newInstance().createService(new QName("jonas:AddressBook", "AddressBookService"));
        Call call = service.createCall(new QName("AddressBookPort"), new QName("isPresent"));
        call.setTargetEndpointAddress("http://localhost:" + port + ADDRESSBOOK_URL);
        call.setProperty(Call.USERNAME_PROPERTY, "jonas");
        call.setProperty(Call.PASSWORD_PROPERTY, "jonas");
        Boolean present = (Boolean) call.invoke(new Object[] {"JOnAS"});

        assertNotNull("ServiceEndpoint performed succesfully", present);
    }

    public void testUnauthorizedAddressBookEndpoint1() throws Exception {

		Service service = ServiceFactory.newInstance().createService(new QName("jonas:AddressBook", "AddressBookService"));
        Call call = service.createCall(new QName("AddressBookPort"), new QName("isPresent"));
        call.setTargetEndpointAddress("http://localhost:" + port + ADDRESSBOOK_URL);
        try {
        	call.invoke(new Object[] {"JOnAS"});
        	fail("AddressBookService was not URL protected !");
        } catch (RemoteException re) {
        	// expected Exception
        }
    }

    public void testAuthorizedAddressBookEndpoint2() throws Exception {

		Service service = ServiceFactory.newInstance().createService(new QName("jonas:AddressBook", "AddressBookService"));
        Call call = service.createCall(new QName("AddressBookPort"), new QName("isPresent"));
        call.setTargetEndpointAddress("http://localhost:" + port + ADDRESSBOOK2_URL);
        call.setProperty(Call.USERNAME_PROPERTY, "jonas");
        call.setProperty(Call.PASSWORD_PROPERTY, "jonas");
        Boolean present = (Boolean) call.invoke(new Object[] {"JOnAS"});

        assertNotNull("ServiceEndpoint performed succesfully", present);
    }

    public void testUnauthorizedAddressBookEndpoint2() throws Exception {

		Service service = ServiceFactory.newInstance().createService(new QName("jonas:AddressBook", "AddressBookService"));
        Call call = service.createCall(new QName("AddressBookPort"), new QName("isPresent"));
        call.setTargetEndpointAddress("http://localhost:" + port + ADDRESSBOOK2_URL);
        try {
        	call.invoke(new Object[] {"JOnAS"});
        	fail("AddressBookService was not URL protected !");
        } catch (RemoteException re) {
        	// expected Exception
        }
    }

}