/*=============================================================================*
* 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.resource.properties.v1_2.porttype.impl;
import org.apache.ws.resource.ResourceContextException;
import org.apache.ws.resource.ResourceException;
import org.apache.ws.resource.properties.ResourceProperty;
import org.apache.ws.resource.properties.SushiCallback;
import org.apache.ws.resource.properties.SushiPlate;
import org.apache.ws.resource.properties.SushiPropertyQNames;
import org.apache.ws.resource.properties.SushiResource;
import org.apache.ws.resource.properties.SushiResourceContext;
import org.apache.ws.resource.properties.faults.InvalidResourcePropertyQNameFaultException;
import org.apache.ws.resource.properties.faults.InvalidSetResourcePropertiesRequestContentFaultException;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.DeleteDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.DeleteType;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.GetResourcePropertyResponseDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.InsertDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.InsertType;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.UpdateDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.UpdateType;
import javax.xml.namespace.QName;
import java.io.IOException;
/**
* @author Sal Campana, Ian Springer
*/
public class SetResourcePropertiesProviderTestCase
extends AbstractWsrpPortTypeImplTestCase
{
private SushiCallback m_callback;
protected void setUp() throws Exception
{
super.setUp();
m_callback = new SushiCallback( new SushiPlate() );
}
/* ===================================== TESTS FOR 'INSERT' ====================================== */
public void testInsertResourcePropertiesCallback() throws XmlException, ResourceException, ResourceContextException
{
m_resourceContext = new SushiResourceContext();
//build callback obj
//setup callback on resource prop
SushiResource resource = (SushiResource) m_resourceContext.getResource();
ResourceProperty resourceProp = resource.getResourcePropertySet().get( SushiPropertyQNames.EBI );
resourceProp.setCallback( m_callback );
SetResourcePropertiesProvider set_provider = new SetResourcePropertiesProvider( m_resourceContext );
InsertDocument insertDoc = InsertDocument.Factory.newInstance();
InsertType insertType = insertDoc.addNewInsert();
XmlObject xmlObject =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ "0" + "</foo:Ebi>" );
XmlBeanUtils.addChildElement( insertType, xmlObject );
set_provider.insertResourceProperty( insertType );
GetResourcePropertyResponseDocument.GetResourcePropertyResponse getResourcePropertyResponse = getResourceProperty( SushiPropertyQNames.EBI );
assertNotNull( getResourcePropertyResponse );
XmlObject[] childElements = XmlBeanUtils.getChildElements( getResourcePropertyResponse, SushiPropertyQNames.EBI );
assertEquals( 2, childElements.length );
assertTrue( m_callback.insertWasInvoked() );
}
public void testInsertResourceProperty()
throws XmlException
{
m_resourceContext = new SushiResourceContext();
SetResourcePropertiesProvider set_provider = new SetResourcePropertiesProvider( m_resourceContext );
InsertDocument insertDoc = InsertDocument.Factory.newInstance();
InsertType insertType = insertDoc.addNewInsert();
XmlObject xmlObject =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ "0" + "</foo:Ebi>" );
XmlBeanUtils.addChildElement( insertType, xmlObject );
set_provider.insertResourceProperty( insertType );
GetResourcePropertyResponseDocument.GetResourcePropertyResponse getResourcePropertyResponse = getResourceProperty( SushiPropertyQNames.EBI );
assertNotNull( getResourcePropertyResponse );
XmlObject[] childElements = XmlBeanUtils.getChildElements( getResourcePropertyResponse, SushiPropertyQNames.EBI );
assertEquals( 2, childElements.length );
}
public void testInsertAnyIntoOpenContent() throws XmlException
{
m_resourceContext = new SushiResourceContext();
insertXsdAnyPropElem();
assertPropHasOneElemWithGivenValue( SushiPropertyQNames.FUGU, INITIAL_PROP_VALUE_FUGU );
}
public void testInsertAnyIntoNonOpenContent() throws Exception
{
m_resourceContext = new SushiResourceContext( false );
try
{
insertXsdAnyPropElem();
fail();
}
catch ( InvalidResourcePropertyQNameFaultException expectedFault )
{
return;
}
}
/* ===================================== TESTS FOR 'DELETE' ====================================== */
public void testDeleteExistingOptionalProp()
throws Exception
{
m_resourceContext = new SushiResourceContext();
deleteResourceProperty( SushiPropertyQNames.OHTORO );
GetResourcePropertyResponseDocument.GetResourcePropertyResponse response = getResourceProperty( SushiPropertyQNames.OHTORO );
XmlObject[] ohtoroPropElems = XmlBeanUtils.getChildElements( response, SushiPropertyQNames.OHTORO );
assertNotNull( ohtoroPropElems );
assertEquals( 0, ohtoroPropElems.length );
}
public void testDeleteNonExistingOptionalProp()
throws Exception
{
m_resourceContext = new SushiResourceContext();
deleteResourceProperty( SushiPropertyQNames.UNI );
}
public void testDeleteExistingNonOptionalProp()
throws Exception
{
m_resourceContext = new SushiResourceContext();
try
{
deleteResourceProperty( SushiPropertyQNames.EBI );
fail();
}
catch ( InvalidSetResourcePropertiesRequestContentFaultException expectedFault )
{
return;
}
}
public void testDeleteOpenContent() throws IOException, XmlException
{
m_resourceContext = new SushiResourceContext( true );
SetResourcePropertiesProvider set_provider = new SetResourcePropertiesProvider( m_resourceContext );
DeleteDocument deleteDoc = DeleteDocument.Factory.newInstance();
DeleteType deleteType = deleteDoc.addNewDelete();
deleteType.setResourceProperty( SushiPropertyQNames.FUGU );
set_provider.deleteResourceProperty( deleteType );
GetResourcePropertyResponseDocument.GetResourcePropertyResponse getResourcePropertyResponse = getResourceProperty( SushiPropertyQNames.OHTORO );
//todo better way to validate
assertNotNull( getResourcePropertyResponse );
}
public void testDeleteResourcePropertiesCallback() throws ResourceException, ResourceContextException, XmlException
{
m_resourceContext = new SushiResourceContext();
SetResourcePropertiesProvider set_provider = new SetResourcePropertiesProvider( m_resourceContext );
InsertDocument insertDoc = InsertDocument.Factory.newInstance();
InsertType insertType = insertDoc.addNewInsert();
XmlObject xmlObject =
XmlObject.Factory.parse( "<foo:Ika xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ "my favorite!" + "</foo:Ika>" );
XmlBeanUtils.addChildElement( insertType, xmlObject );
set_provider.insertResourceProperty( insertType );
//setup callback on resource prop
SushiResource resource = (SushiResource) m_resourceContext.getResource();
ResourceProperty resourceProp = resource.getResourcePropertySet().get( SushiPropertyQNames.IKA );
resourceProp.setCallback( m_callback );
DeleteDocument deleteDoc = DeleteDocument.Factory.newInstance();
DeleteType deleteType = deleteDoc.addNewDelete();
deleteType.setResourceProperty( SushiPropertyQNames.IKA );
set_provider.deleteResourceProperty( deleteType );
GetResourcePropertyResponseDocument.GetResourcePropertyResponse getResourcePropertyResponse = getResourceProperty( SushiPropertyQNames.OHTORO );
assertNotNull( getResourcePropertyResponse );
assertTrue( m_callback.deleteWasInvoked() );
}
/* ===================================== TESTS FOR 'UPDATE' ====================================== */
/**
* DOCUMENT_ME
*/
public void testUpdateResourcePropertiesCallback() throws ResourceException, ResourceContextException, XmlException
{
String newValue = "99";
m_resourceContext = new SushiResourceContext();
SushiPlate plate = new SushiPlate();
SushiCallback callback = new SushiCallback( plate );
//setup callback on resource prop
SushiResource resource = (SushiResource) m_resourceContext.getResource();
ResourceProperty resourceProp = resource.getResourcePropertySet().get( SushiPropertyQNames.EBI );
resourceProp.setCallback( callback );
SetResourcePropertiesProvider set_provider = new SetResourcePropertiesProvider( m_resourceContext );
UpdateDocument updateDoc = UpdateDocument.Factory.newInstance();
UpdateType update = updateDoc.addNewUpdate();
XmlObject xmlObject =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ newValue + "</foo:Ebi>" );
XmlBeanUtils.addChildElement( update, xmlObject );
set_provider.updateResourceProperty( updateDoc.getUpdate() );
assertPropHasOneElemWithGivenValue( SushiPropertyQNames.EBI, newValue );
assertTrue( callback.updateWasInvoked() );
}
public void testUpdateOpenContent() throws XmlException
{
m_resourceContext = new SushiResourceContext( true );
insertXsdAnyPropElem();
assertPropHasOneElemWithGivenValue( SushiPropertyQNames.FUGU, INITIAL_PROP_VALUE_FUGU );
SetResourcePropertiesProvider set_provider = new SetResourcePropertiesProvider( m_resourceContext );
UpdateDocument updateDoc = UpdateDocument.Factory.newInstance();
UpdateType update = updateDoc.addNewUpdate();
XmlObject anyXBean = createXsdAnyPropElem();
final String newValue = "yummy!";
XmlBeanUtils.setValue( anyXBean, newValue );
XmlBeanUtils.addChildElement( update, anyXBean );
set_provider.updateResourceProperty( updateDoc.getUpdate() );
assertPropHasOneElemWithGivenValue( SushiPropertyQNames.FUGU, newValue );
}
/**
* DOCUMENT_ME
*/
public void testUpdateResourceProperty()
throws XmlException
{
final String newValue = "99";
m_resourceContext = new SushiResourceContext();
SetResourcePropertiesProvider set_provider = new SetResourcePropertiesProvider( m_resourceContext );
UpdateDocument updateDoc = UpdateDocument.Factory.newInstance();
UpdateType update = updateDoc.addNewUpdate();
XmlObject xmlObject =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ newValue + "</foo:Ebi>" );
XmlBeanUtils.addChildElement( update, xmlObject );
set_provider.updateResourceProperty( updateDoc.getUpdate() );
assertPropHasOneElemWithGivenValue( SushiPropertyQNames.EBI, newValue );
}
private void deleteResourceProperty( QName propName )
{
SetResourcePropertiesProvider set_provider = new SetResourcePropertiesProvider( m_resourceContext );
DeleteDocument deleteDoc = DeleteDocument.Factory.newInstance();
DeleteType deleteType = deleteDoc.addNewDelete();
deleteType.setResourceProperty( propName );
set_provider.deleteResourceProperty( deleteType );
}
}