/*=============================================================================*
* Copyright 2005 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.muws.interop.client;
import org.apache.ws.muws.v1_0.MuwsConstants;
import org.apache.ws.muws.v1_0.capability.IdentityCapability;
import org.apache.ws.muws.v1_0.capability.ManageabilityCharacteristicsCapability;
import org.apache.ws.muws.v1_0.capability.OperationalStatusCapability;
import org.apache.ws.notification.base.v2004_06.BaseNotificationConstants;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.jndi.XmlBeanJndiUtils;
import org.apache.ws.util.test.PortListen;
import org.apache.ws.util.test.axis.AbstractOneAxisTestCase;
import org.apache.xmlbeans.XmlAnyURI;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart1.ManagementEventType;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.OperationalStatusDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.NotificationMessageHolderType;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.NotifyDocument;
import org.xmlsoap.schemas.soap.envelope.Body;
import org.xmlsoap.schemas.soap.envelope.EnvelopeDocument;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceDocument;
import javax.xml.namespace.QName;
import java.net.InetAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* TODO
*/
public abstract class AbstractWsdmInteropTestCase extends AbstractOneAxisTestCase
{
protected static final int NOTIF_LISTENER_TIMEOUT = 20000;
protected static final int NOTIF_LISTENER_PORT = 9101;
protected static String s_consumerURL;
private static final boolean RUN_AXIS_SERVER = Boolean.getBoolean( "runAxis" );
static final boolean DEBUG = true;//Boolean.getBoolean( "debug" );
public AbstractWsdmInteropTestCase()
{
super( RUN_AXIS_SERVER );
}
protected void setUp() throws Exception
{
super.setUp();
s_consumerURL = "http://" + InetAddress.getLocalHost().getHostAddress() + ":" + NOTIF_LISTENER_PORT + "/";
if ( RUN_AXIS_SERVER )
{
//init jndi from config
XmlBeanJndiUtils.initializeFromInputStream( Thread.currentThread().getContextClassLoader()
.getResourceAsStream( XmlBeanJndiUtils.JNDI_CONFIG_FILENAME ) );
}
}
protected void performMuwsIdentityCharacteristicsAct( ResourceStub resource ) throws FaultException
{
XmlObject idPropElem = getSingleProperty( resource, IdentityCapability.PROP_NAME_RESOURCE_ID );
assertTrue( idPropElem instanceof XmlAnyURI );
XmlObject[] capabilityElems = resource.getResourceProperty(
ManageabilityCharacteristicsCapability.PROP_NAME_MANAGEABILITY_CAPABILITY );
assertContainsURI( capabilityElems, IdentityCapability.URI );
}
protected void performMuwsStatusAct( ResourceStub resource ) throws FaultException
{
XmlObject[] capabilityPropElems = resource.getResourceProperty(
ManageabilityCharacteristicsCapability.PROP_NAME_MANAGEABILITY_CAPABILITY );
assertContainsURI( capabilityPropElems, OperationalStatusCapability.URI );
XmlObject[] statusPropElems = resource.getResourceProperty(
OperationalStatusCapability.PROP_NAME_OPERATIONAL_STATUS );
assertEquals( 1, statusPropElems.length );
assertTrue( statusPropElems[0] instanceof OperationalStatusDocument.OperationalStatus );
}
protected XmlObject getSingleProperty( ResourceStub resource, QName propName )
throws FaultException
{
XmlObject[] propElems = resource.getResourceProperty( propName );
assertEquals(
"Incorrect number of " + propName.getPrefix() + ":" + propName.getLocalPart() + " property elements -",
1, propElems.length );
return propElems[0];
}
protected void setSingleProperty( ResourceStub resource, XmlObject propElem )
throws FaultException
{
resource.updateResourceProperty( new XmlObject[]{propElem} );
}
protected void assertContainsURI( XmlObject[] capabilityPropElems, String uri )
{
boolean result = false;
for ( int i = 0; i < capabilityPropElems.length; i++ )
{
XmlAnyURI capabilityElem = (XmlAnyURI) capabilityPropElems[i];
if ( capabilityElem.getStringValue().equals( uri ) )
{
result = true;
break;
}
}
assertTrue( "List of manageability capabilities does not include the URI: " + uri, result );
}
/**
* Reads a wsa03:EndpointReference XML document from the specified URL.
*
* @param url
*
* @return
*
* @throws Exception
*/
protected EndpointReferenceDocument getEndpointReference( URL url ) throws Exception
{
XmlObject stuff=XmlObject.Factory.parse( url );
return (EndpointReferenceDocument)stuff;
}
protected ManagementEventType waitForManagementEvent( PortListen notifListener ) throws XmlException
{
String incomingMsg = notifListener.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 mgmtEventElem = null;
if ( ResourceStub.USE_WRAPPED_NOTIFICATIONS ) //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 );
NotifyDocument.Notify notify = (NotifyDocument.Notify) notifyDoc;
//get contained management event
NotificationMessageHolderType notificationMessageType = notify.getNotificationMessageArray( 0 );
XmlObject message = notificationMessageType.getMessage();
XmlObject mgmtEventElems[] = XmlBeanUtils.getChildElements( message,
new QName( MuwsConstants.NSURI_MUWS_PART1_SCHEMA, "ManagementEvent" ) );
assertEquals( "The Notify element does not contain exactly one muws-p1-xs:ManagementEvent element.", 1,
mgmtEventElems.length );
mgmtEventElem = mgmtEventElems[0];
assertTrue( "The Notify element does not contain a ManagementEvent.",
mgmtEventElem instanceof ManagementEventType );
}
else //unwrapped
{
XmlObject mgmtEventElems[] = XmlBeanUtils.getChildElements( body,
new QName( MuwsConstants.NSURI_MUWS_PART1_SCHEMA, "ManagementEvent" ) );
assertEquals( "The SOAP Body does not contain exactly one muws-p1-xs:ManagementEvent element.", 1,
mgmtEventElems.length );
mgmtEventElem = mgmtEventElems[0];
assertTrue( "The SOAP Body did not contain an unwrapped ManagementEvent.",
mgmtEventElem instanceof ManagementEventType );
}
ManagementEventType mgmtEvent = (ManagementEventType) mgmtEventElem;
return mgmtEvent;
}
protected static boolean validate( XmlObject xBean )
{
// Create an XmlOptions instance and set the error listener.
XmlOptions validateOptions = new XmlOptions();
List errorList = new ArrayList();
validateOptions.setErrorListener( errorList );
// Validate the XML.
boolean isValid = xBean.validate( validateOptions );
// If the XML isn't valid, loop through the listener's contents,
// printing contained messages.
if ( !isValid )
{
for ( int i = 0; i < errorList.size(); i++ )
{
XmlError error = (XmlError) errorList.get( i );
System.out.println( "\n" );
System.out.println( "Message: " + error.getMessage() + "\n" );
System.out.println( "Location of invalid XML: " +
error.getCursorLocation().xmlText() + "\n" );
}
}
return isValid;
}
}