/** 
 * 
 * Copyright 2004 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.notification.impl;

import junit.framework.TestCase;
import org.apache.xmlbeans.XmlObject;
import org.activemq.ws.xmlbeans.resource.properties.GetResourcePropertyDocument;
import org.activemq.ws.xmlbeans.addressing.v2003_03.EndpointReferenceType;
import org.activemq.ws.xmlbeans.notification.base.SubscribeDocument;
import org.activemq.ws.xmlbeans.notification.base.SubscribeResponseDocument;
import org.activemq.ws.xmlbeans.notification.base.NotifyDocument;
import org.activemq.ws.xmlbeans.notification.base.NotificationMessageHolderType;
import org.activemq.message.ActiveMQTopic;

import javax.xml.namespace.QName;

/**
 * @version $Revision: 1.1 $
 */
public abstract class TestSupport extends TestCase {
    protected ActiveMQTopic topic = new ActiveMQTopic("Test");

    protected XmlObject createMessage() {
        GetResourcePropertyDocument xml = GetResourcePropertyDocument.Factory.newInstance();
        QName property = new QName("test", "test");
        xml.setGetResourceProperty( property );
        return xml;
    }

    protected EndpointReferenceType addSubscription(ActiveMQNotificationBroker broker) {
        // Create a subscription

        // START SNIPPET: subscribe
        SubscribeDocument subrequest = SubscribeDocument.Factory.newInstance();
        SubscribeDocument.Subscribe subscribe = subrequest.addNewSubscribe();
        subscribe.setTopicExpression( TopicExpressionConverter.toTopicExpression(topic) );
        subscribe.setUseNotify(true);
        SubscribeResponseDocument subresponse = broker.Subscribe(subrequest);
        // END SNIPPET: subscribe


        System.out.println("Sub request: "+subrequest);
        System.out.println("Sub response: "+subresponse);
        assertNotNull(subresponse);
        
        return subresponse.getSubscribeResponse().getSubscriptionReference();
    }
    
    protected void sendNotification(ActiveMQNotificationBroker broker) {
        // START SNIPPET: notify
        NotifyDocument request = NotifyDocument.Factory.newInstance();
        NotifyDocument.Notify notify = request.addNewNotify();
        NotificationMessageHolderType messageHolder = notify.addNewNotificationMessage();
        messageHolder.setTopic( TopicExpressionConverter.toTopicExpression(topic) );

        // create some XMLBean for the payload
        XmlObject o = createMessage();
        messageHolder.setMessage(o);
        broker.notify(request);
        // END SNIPPET: notify

        assertValidMessage(request);

        System.out.println("Sending Notify: "+request);
    }

    protected void assertValidMessage(NotifyDocument document) {
        assertNotNull("null: document", document);

        NotifyDocument.Notify notify = document.getNotify();
        assertNotNull("null: notify", notify);

        NotificationMessageHolderType[] messageArray = notify.getNotificationMessageArray();
        assertNotNull("null: messageArray", messageArray);
        assertTrue("Must have at least one message entry", messageArray.length > 0);

        for (int i = 0; i < messageArray.length; i++) {
            NotificationMessageHolderType messageHolder = messageArray[i];
            assertNotNull("null: messageHolder[" + i + "]", messageHolder);

            XmlObject message = messageHolder.getMessage();
            assertNotNull("null: message[" + i + "]", message);
        }
    }
}
