/*=============================================================================*
* 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.v2004_06.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.ThrowsCallback;
import org.apache.ws.resource.properties.faults.InvalidResourcePropertyQNameFaultException;
import org.apache.ws.resource.properties.faults.InvalidSetResourcePropertiesRequestContentFaultException;
import org.apache.ws.resource.properties.faults.SetResourcePropertyRequestFailedFaultException;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.GetResourcePropertyResponseDocument;
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();
SushiResource resource = (SushiResource) m_resourceContext.getResource();
ResourceProperty resourceProp = resource.getResourcePropertySet().get( SushiPropertyQNames.EBI );
resourceProp.setCallback( m_callback );
XmlObject xBean =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ "0" + "</foo:Ebi>" );
insertResourceProperty( new XmlObject[]{xBean} );
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();
XmlObject xBean =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ "0" + "</foo:Ebi>" );
insertResourceProperty( new XmlObject[]{xBean} );
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; // success
}
}
public void testInsertResourcePropertiesFailedCallback() throws XmlException, ResourceException,
ResourceContextException
{
m_resourceContext = new SushiResourceContext();
SushiResource resource = (SushiResource) m_resourceContext.getResource();
ResourceProperty resourceProp = resource.getResourcePropertySet().get( SushiPropertyQNames.EBI );
resourceProp.setCallback( new ThrowsCallback() );
XmlObject xBean =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ "0" + "</foo:Ebi>" );
try
{
insertResourceProperty( new XmlObject[]{xBean} );
}
catch (RuntimeException e)
{
assertEquals(SetResourcePropertyRequestFailedFaultException.class.getName(), e.getClass().getName());
return;
}
fail();
}
/* ===================================== 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; // success
}
}
public void testDeleteOpenContent() throws IOException, XmlException
{
m_resourceContext = new SushiResourceContext( true );
deleteResourceProperty( SushiPropertyQNames.FUGU );
GetResourcePropertyResponseDocument.GetResourcePropertyResponse getResourcePropertyResponse = getResourceProperty(
SushiPropertyQNames.OHTORO );
//todo better way to validate
assertNotNull( getResourcePropertyResponse );
}
public void testDeleteResourcePropertiesCallback() throws ResourceException, ResourceContextException,
XmlException
{
m_resourceContext = new SushiResourceContext();
XmlObject xBean =
XmlObject.Factory.parse( "<foo:Ika xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ "my favorite!" + "</foo:Ika>" );
insertResourceProperty( new XmlObject[]{xBean} );
SushiResource resource = (SushiResource) m_resourceContext.getResource();
ResourceProperty resourceProp = resource.getResourcePropertySet().get( SushiPropertyQNames.IKA );
resourceProp.setCallback( m_callback );
deleteResourceProperty( SushiPropertyQNames.IKA );
GetResourcePropertyResponseDocument.GetResourcePropertyResponse getResourcePropertyResponse = getResourceProperty(
SushiPropertyQNames.OHTORO );
assertNotNull( getResourcePropertyResponse );
assertTrue( m_callback.deleteWasInvoked() );
}
public void testDeleteResourcePropertiesFailedCallback() throws ResourceException, ResourceContextException,
XmlException
{
m_resourceContext = new SushiResourceContext();
XmlObject xBean =
XmlObject.Factory.parse( "<foo:Ika xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ "my favorite!" + "</foo:Ika>" );
insertResourceProperty( new XmlObject[]{xBean} );
SushiResource resource = (SushiResource) m_resourceContext.getResource();
ResourceProperty resourceProp = resource.getResourcePropertySet().get( SushiPropertyQNames.IKA );
resourceProp.setCallback( new ThrowsCallback() );
try
{
deleteResourceProperty( SushiPropertyQNames.IKA );
}
catch (RuntimeException e)
{
assertEquals(SetResourcePropertyRequestFailedFaultException.class.getName(), e.getClass().getName());
return;
}
fail();
}
/* ===================================== 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 );
XmlObject xBean =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ newValue + "</foo:Ebi>" );
updateResourceProperty( new XmlObject[]{xBean} );
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 );
XmlObject anyXBean = createXsdAnyPropElem();
final String newValue = "yummy!";
XmlBeanUtils.setValue( anyXBean, newValue );
updateResourceProperty( new XmlObject[]{anyXBean} );
assertPropHasOneElemWithGivenValue( SushiPropertyQNames.FUGU, newValue );
}
/**
* DOCUMENT_ME
*/
public void testUpdateResourceProperty()
throws XmlException
{
final String newValue = "99";
m_resourceContext = new SushiResourceContext();
XmlObject xBean =
XmlObject.Factory.parse( "<foo:Ebi xmlns:foo=\"http://ws.apache.org/resource/properties/test/sushi\">"
+ newValue + "</foo:Ebi>" );
updateResourceProperty( new XmlObject[]{xBean} );
assertPropHasOneElemWithGivenValue( SushiPropertyQNames.EBI, newValue );
}
}