/** 
 * 
 * Copyright 2005 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.ws.addressing;

import junit.framework.TestCase;
import org.activemq.ws.xmlbeans.addressing.v2003_03.ActionDocument;
import org.activemq.ws.xmlbeans.addressing.v2003_03.AttributedURI;
import org.activemq.ws.xmlbeans.addressing.v2003_03.EndpointReferenceType;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.codehaus.activesoap.MessageExchange;
import org.codehaus.activesoap.SoapService;
import org.codehaus.activesoap.handler.xmlbeans.XMLBeansHandler;
import org.codehaus.activesoap.handler.xmlbeans.XMLBeansRegistry;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamWriter;
import java.io.InputStreamReader;
import java.io.StringWriter;

/**
 * @version $Revision: 1.1 $
 */
public class AddressingTest extends TestCase {
    private XmlObject invokeBody;

    public void testParse() throws Exception {
        XmlObject xmlObject = XmlObject.Factory.parse(getClass().getResourceAsStream("wsa.xml"));
        assertNotNull("null xmlObject", xmlObject);

        System.out.println("Parsed: " + xmlObject);
        System.out.println("Class: " + xmlObject.getClass());

        QName actionQName = ActionDocument.type.getName();

        XmlCursor cursor = xmlObject.newCursor();

        //System.out.println("current qname: " + cursor.getName());

        while (cursor.hasNextToken()) {
            XmlCursor.TokenType type = cursor.toNextToken();
            System.out.println("type: " + type);
            if (type.isStart()) {
                QName name = cursor.getName();
                System.out.println("at start of qname: " + name);
                if (name.getLocalPart().equals("Action")) {
                    break;
                }
            }
        }
        /*
        XmlObject foo = cursor.getObject();
        XmlObject[] xmlObjects = foo.selectChildren(actionQName);
        assertEquals("results", 1, xmlObjects.length);
        */

        XmlObject action = cursor.getObject();
        System.out.println("action is: " + action);

        /** TODO
        assertTrue("Should have AttributedURI  instead of: " + action.getClass(), action instanceof AttributedURI);
        AttributedURI actionReference = (AttributedURI) action;

        System.out.println("actionRefernce is: " + actionReference);
        assertEquals("actionURI", "http://docs.oasis-open.org/wsn/2004/06/WS-BaseNotification/Notify", actionReference.xmlText());
        */
    }

    public void testAddresssingContext() throws Exception {
        XMLBeansRegistry registry = new XMLBeansRegistry();
        registry.setDefaultHandler(new XMLBeansHandler() {
            protected void handleBody(MessageExchange exchange, XmlObject body, XMLStreamWriter out) throws Exception {
                invokeBody = body;
            }
        });

        SoapService service = new SoapService(registry);
        service.addPolicy(new AddressingPolicy());

        StringWriter buffer = new StringWriter();
        service.invoke(new InputStreamReader(getClass().getResourceAsStream("notify.xml")), buffer);

        String text = buffer.toString();
        System.out.println("Received response");
        System.out.println(text);

        assertNotNull("null invokeBody", invokeBody);

        /**
         *
        EndpointReferenceType endpointReference = AddressingContext.getEndpointReference();
        assertNotNull("null endpointReference", endpointReference);

        System.out.println("address: " + endpointReference.getAddress());
        System.out.println("portType: " + endpointReference.getPortType());
        System.out.println("serviceName: " + endpointReference.getServiceName());

        assertNotNull("null endpointReference.getAddress()", endpointReference.getAddress());
        assertNotNull("null endpointReference.getPortType()", endpointReference.getPortType());
        assertNotNull("null endpointReference.getServiceName()", endpointReference.getServiceName());
        */
    }

}
