/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.ws.notification.base.v2004_06.porttype.impl;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.notification.base.impl.UnitExampleHome;
import org.apache.ws.notification.base.impl.UnitResource;
import org.apache.ws.notification.base.impl.UnitResourceContext;
import org.apache.ws.notification.base.v2004_06.BaseNotificationConstants;
import org.apache.ws.notification.base.v2004_06.porttype.NotificationProducerPortType;
import org.apache.ws.notification.topics.v2004_06.TopicsConstants;
import org.apache.ws.resource.PropertiesResource;
import org.apache.ws.resource.ResourceContext;
import org.apache.ws.resource.ResourceContextException;
import org.apache.ws.resource.ResourceException;
import org.apache.ws.resource.properties.ResourceProperty;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.test.PortListen;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.NotifyDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.SubscribeDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.SubscribeResponseDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.TopicExpressionDialectsDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.TopicExpressionType;
import org.xmlsoap.schemas.soap.envelope.Body;
import org.xmlsoap.schemas.soap.envelope.EnvelopeDocument;
import javax.xml.namespace.QName;
import java.util.Calendar;
/**
* Test case for {@link NotificationProducerPortTypeImpl}.
*
* @author Sal Campana
*/
public class NotificationProducerPortTypeImplTest
extends TestCase
{
/** DOCUMENT_ME */
protected static final int NOTIF_LISTENER_TIMEOUT = 20000;
/** DOCUMENT_ME */
protected static final int NOTIF_LISTENER_PORT = 9101;
/** DOCUMENT_ME */
ResourceContext m_resourceContext;
private NotificationProducerPortTypeImpl m_portTypeImpl;
private String m_consumerURL = "http://localhost:" + Integer.toString( NOTIF_LISTENER_PORT );
private QName m_topic =
new QName( UnitExampleHome.TARGET_NAMESPACE, UnitResource.TOPIC_NAME, UnitExampleHome.TARGET_NSPREFIX );
/**
* Creates a new {@link NotificationProducerPortTypeImplTest} object.
*
* @param name DOCUMENT_ME
*/
public NotificationProducerPortTypeImplTest( String name )
{
super( name );
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void setUp( )
throws Exception
{
super.setUp( );
m_resourceContext = new UnitResourceContext( );
m_portTypeImpl = new NotificationProducerPortTypeImpl( m_resourceContext );
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public static Test suite( )
{
return new TestSuite( NotificationProducerPortTypeImplTest.class );
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void tearDown( )
throws Exception
{
super.tearDown( );
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void testGetCurrentMessage( )
throws Exception
{
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void testSubscribeUnwrapped( )
throws Exception
{
/*subscribe(false);
PortListen listener = new PortListen(NOTIF_LISTENER_PORT, NOTIF_LISTENER_TIMEOUT);
triggerChangeEvent();
assertIncomingMessage(listener, false);*/
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void testSubscribeWrapped( )
throws Exception
{
/* subscribe(true);
PortListen listener = new PortListen(NOTIF_LISTENER_PORT, NOTIF_LISTENER_TIMEOUT);
triggerChangeEvent();
assertIncomingMessage(listener, true);*/
}
private void assertIncomingMessage( PortListen listener,
boolean isWrapped )
throws XmlException
{
String incomingMsg = listener.waitForIncomingMessage( );
assertTrue( incomingMsg, incomingMsg.indexOf( "ERROR" ) == -1 );
//get envelope
XmlObject xmlObject = XmlObject.Factory.parse( incomingMsg );
assertTrue( "The returned message was not a SOAP Envelope.", xmlObject instanceof EnvelopeDocument );
EnvelopeDocument envelope = (EnvelopeDocument) xmlObject;
Body body = envelope.getEnvelope( ).getBody( );
XmlObject event = null;
if ( isWrapped ) //wrapped
{
//get notify
XmlObject[] bodyElems =
XmlBeanUtils.getChildElements( body,
new QName( BaseNotificationConstants.NSURI_WSNT_SCHEMA, "Notify" ) );
assertEquals( "The SOAP Body does not contain exactly one wsnt:Notify element.", 1, bodyElems.length );
XmlObject notifyDoc = bodyElems[0];
assertTrue( "The SOAP Body does not contain a wrapped notification with a Notify element.",
notifyDoc instanceof NotifyDocument.Notify );
}
else //unwrapped
{ //see what this should be
XmlObject[] bodyElems =
XmlBeanUtils.getChildElements( body,
new QName( BaseNotificationConstants.NSURI_WSNT_SCHEMA, "Notify" ) );
}
}
private XmlBeansEndpointReference subscribe( boolean isWrapped )
{
SubscribeDocument requestDoc = SubscribeDocument.Factory.newInstance( );
SubscribeDocument.Subscribe subscribe = requestDoc.addNewSubscribe( );
subscribe.setUseNotify( true );
Calendar instance = Calendar.getInstance( );
instance.setTimeInMillis( instance.getTimeInMillis( ) + 20000 );
subscribe.setInitialTerminationTime( instance );
org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType consumerRef =
subscribe.addNewConsumerReference( );
org.xmlsoap.schemas.ws.x2003.x03.addressing.AttributedURI address = consumerRef.addNewAddress( );
address.setStringValue( m_consumerURL );
TopicExpressionType topicExpr = subscribe.addNewTopicExpression( );
topicExpr.setDialect( TopicsConstants.TOPIC_EXPR_DIALECT_SIMPLE );
XmlBeanUtils.setValueAsQName( topicExpr, m_topic );
XmlObject response = m_portTypeImpl.subscribe( requestDoc );
assertTrue( response instanceof SubscribeResponseDocument.SubscribeResponse );
SubscribeResponseDocument.SubscribeResponse subscribeResponse =
(SubscribeResponseDocument.SubscribeResponse) response;
XmlBeansEndpointReference xmlBeansEndpointReference =
new XmlBeansEndpointReference( subscribeResponse.getSubscriptionReference( ) );
return xmlBeansEndpointReference;
}
private void triggerChangeEvent( )
throws ResourceContextException,
ResourceException
{
PropertiesResource resource =
(PropertiesResource) m_resourceContext.getResourceHome( ).find( UnitExampleHome.RESOURCE_ID );
ResourceProperty resourceProperty =
resource.getResourcePropertySet( ).get( NotificationProducerPortType.PROP_QNAME_TOPIC_EXPRESSION_DIALECTS );
TopicExpressionDialectsDocument topicExpressionDialectsDocument =
TopicExpressionDialectsDocument.Factory.newInstance( );
topicExpressionDialectsDocument.setTopicExpressionDialects( "foo-dialect" );
resourceProperty.add( topicExpressionDialectsDocument );
}
}