Skip to content

Commit

Permalink
Remove property expansions on WSDL and WADL import
Browse files Browse the repository at this point in the history
  • Loading branch information
spindelmanne committed Jan 10, 2014
1 parent 973f762 commit 6373165
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

package com.eviware.soapui.impl;

import javax.xml.namespace.QName;

import org.apache.log4j.Logger;

import com.eviware.soapui.SoapUI;
import com.eviware.soapui.config.InterfaceConfig;
import com.eviware.soapui.config.WsdlInterfaceConfig;
Expand All @@ -31,6 +27,12 @@
import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
import com.eviware.soapui.settings.WsdlSettings;
import com.eviware.soapui.support.SoapUIException;
import org.apache.log4j.Logger;

import javax.xml.namespace.QName;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class WsdlInterfaceFactory implements InterfaceFactory<WsdlInterface>
{
Expand Down Expand Up @@ -117,4 +119,9 @@ public static WsdlInterface[] importWsdl( WsdlProject project, String url, boole

return result;
}

public static void main( String[] args ) throws URISyntaxException, IOException
{
java.awt.Desktop.getDesktop().browse(new URI("http://www.sunet.se"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@

package com.eviware.soapui.impl.rest.support;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import com.eviware.soapui.impl.rest.RestMethod;
import com.eviware.soapui.impl.rest.RestRepresentation;
import com.eviware.soapui.impl.rest.RestRequestInterface;
import com.eviware.soapui.impl.rest.RestResource;
import com.eviware.soapui.impl.rest.RestService;
import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder.ParameterStyle;
import com.eviware.soapui.impl.support.definition.support.InvalidDefinitionException;
import com.eviware.soapui.impl.wsdl.support.Constants;
import com.eviware.soapui.impl.wsdl.support.UrlSchemaLoader;
import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
import com.eviware.soapui.support.StringUtils;
import com.eviware.soapui.support.Tools;
import com.eviware.soapui.support.UISupport;
import com.eviware.soapui.support.xml.XmlUtils;
import com.eviware.soapui.tools.PropertyExpansionRemover;
import net.java.dev.wadl.x2009.x02.ApplicationDocument;
import net.java.dev.wadl.x2009.x02.ApplicationDocument.Application;
import net.java.dev.wadl.x2009.x02.DocDocument.Doc;
Expand All @@ -37,7 +39,6 @@
import net.java.dev.wadl.x2009.x02.ResourceTypeDocument;
import net.java.dev.wadl.x2009.x02.ResourcesDocument.Resources;
import net.java.dev.wadl.x2009.x02.ResponseDocument.Response;

import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
Expand All @@ -47,20 +48,17 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.eviware.soapui.impl.rest.RestMethod;
import com.eviware.soapui.impl.rest.RestRepresentation;
import com.eviware.soapui.impl.rest.RestRequestInterface;
import com.eviware.soapui.impl.rest.RestResource;
import com.eviware.soapui.impl.rest.RestService;
import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder.ParameterStyle;
import com.eviware.soapui.impl.support.definition.support.InvalidDefinitionException;
import com.eviware.soapui.impl.wsdl.support.Constants;
import com.eviware.soapui.impl.wsdl.support.UrlSchemaLoader;
import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
import com.eviware.soapui.support.StringUtils;
import com.eviware.soapui.support.Tools;
import com.eviware.soapui.support.UISupport;
import com.eviware.soapui.support.xml.XmlUtils;
import javax.xml.namespace.QName;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WadlImporter
{
Expand All @@ -82,25 +80,23 @@ public void initFromWadl( String wadlUrl )
// XmlObject xmlObject = XmlObject.Factory.parse( new URL( wadlUrl ) );
XmlObject xmlObject = XmlUtils.createXmlObject( new URL( wadlUrl ) );

String content = xmlObject.xmlText();
Element element = ( ( Document )xmlObject.getDomNode() ).getDocumentElement();

// try to allow older namespaces
if( element.getLocalName().equals( "application" )
&& element.getNamespaceURI().startsWith( "http://research.sun.com/wadl" ) )
{
isWADL11 = false;
String content = xmlObject.xmlText();
content = content.replaceAll( "\"" + element.getNamespaceURI() + "\"", "\"" + Constants.WADL11_NS + "\"" );
xmlObject = ApplicationDocument.Factory.parse( content );
}
else if( !element.getLocalName().equals( "application" )
|| !element.getNamespaceURI().equals( Constants.WADL11_NS ) )
{
throw new Exception( "Document is not a WADL application with " + Constants.WADL11_NS + " namespace" );
}

ApplicationDocument applicationDocument = ( ApplicationDocument )xmlObject
.changeType( ApplicationDocument.type );
content = PropertyExpansionRemover.removeExpansions( content );
ApplicationDocument applicationDocument = ApplicationDocument.Factory.parse( content );
application = applicationDocument.getApplication();

resourcesList = application.getResourcesList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@

package com.eviware.soapui.impl.wsdl.support.wsdl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;

import org.apache.log4j.Logger;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.xml.sax.InputSource;

import com.eviware.soapui.SoapUI;
import com.eviware.soapui.impl.support.definition.support.AbstractDefinitionLoader;
import com.eviware.soapui.impl.support.definition.support.InvalidDefinitionException;
Expand All @@ -31,10 +20,21 @@
import com.eviware.soapui.support.StringUtils;
import com.eviware.soapui.support.Tools;
import com.eviware.soapui.support.xml.XmlUtils;
import com.eviware.soapui.tools.PropertyExpansionRemover;
import org.apache.log4j.Logger;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.xml.sax.InputSource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;

/**
* Abstract WSDLLocator for loading definitions from either URL or cache..
*
*
* @author ole.matzura
*/

Expand Down Expand Up @@ -110,16 +110,8 @@ public XmlObject loadXmlObject( String url, XmlOptions options ) throws Exceptio
monitor.setProgress( progressIndex, "Loading [" + url + "]" );

options.setLoadLineNumbers();

if( Boolean.TRUE.equals( ( ( Boolean )SoapUI.getSettings().getBoolean( WsdlSettings.TRIM_WSDL ) ) ) )
{
String content = Tools.readAll( load( url ), 0 ).toString().trim();
return XmlUtils.createXmlObject( new ByteArrayInputStream( content.getBytes() ), options );
}
else
{
return XmlUtils.createXmlObject( load( url ), options );
}
String content = readCleanWsdlFrom( url );
return XmlUtils.createXmlObject( new ByteArrayInputStream( content.getBytes() ), options );
}
catch( Exception e )
{
Expand All @@ -139,6 +131,17 @@ public XmlObject loadXmlObject( String url, XmlOptions options ) throws Exceptio
}
}

private String readCleanWsdlFrom( String url ) throws Exception
{
String content = Tools.readAll( load( url ), 0 ).toString();

if( SoapUI.getSettings().getBoolean( WsdlSettings.TRIM_WSDL ) )
{
content = content.trim();
}
return PropertyExpansionRemover.removeExpansions( content );
}

public String getBaseURI()
{
// log.debug( "Returning baseURI [" + url + "]" );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.eviware.soapui.tools;

/**
* Removes property expansions from an input string.
*/
public class PropertyExpansionRemover
{

public static final String EXPANSION_START = "${";

public static String removeExpansions( String input )
{
String output = input;
while (containsPropertyExpansion(output))
{
output = removeExpansionAt( output, output.indexOf( EXPANSION_START ) );
}
return output;
}

private static String removeExpansionAt( String input, int startIndex )
{
String output = input;
while (containsNestedExpansion(output, startIndex))
{
output = removeExpansionAt( output, output.indexOf( EXPANSION_START, startIndex + 1 ) );
}
int endIndex = output.indexOf('}', startIndex);
return output.substring(0, startIndex) + output.substring(endIndex + 1);
}

private static boolean containsNestedExpansion( String output, int startIndex )
{
String textToProcess = output.substring(startIndex + EXPANSION_START.length());
return textToProcess.contains( EXPANSION_START ) &&
textToProcess.indexOf( EXPANSION_START ) < textToProcess.indexOf( '}' );
}

private static boolean containsPropertyExpansion( String input )
{
if (!input.contains( EXPANSION_START ))
{
return false;
}
int startIndex = input.indexOf( EXPANSION_START );
return input.indexOf('}', startIndex) != -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@

package com.eviware.soapui.impl.rest.support;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.eviware.soapui.impl.rest.RestMethod;
import com.eviware.soapui.impl.rest.RestRequest;
import com.eviware.soapui.impl.rest.RestRequestInterface;
import com.eviware.soapui.impl.rest.RestResource;
import junit.framework.JUnit4TestAdapter;

import org.junit.Test;

import com.eviware.soapui.impl.rest.RestService;
import com.eviware.soapui.impl.rest.RestServiceFactory;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import junit.framework.JUnit4TestAdapter;
import org.junit.Test;

import static com.eviware.soapui.utils.CommonMatchers.anEmptyString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

public class WadlImporterTestCase
{
Expand Down Expand Up @@ -68,4 +70,18 @@ public void importsWadl() throws Exception
assertEquals( RestRequestInterface.RequestMethod.GET, request.getMethod() );
assertEquals( 9, request.getPropertyCount() );
}

@Test
public void removesPropertyExpansions() throws Exception
{
WsdlProject project = new WsdlProject();
RestService service = ( RestService )project.addNewInterface( "Test", RestServiceFactory.REST_TYPE );

new WadlImporter( service ).initFromWadl( RestUtilsTestCase.class.getResource(
"/wadl/YahooSearchWithExpansions.wadl" ).toURI().toString());
RestResource operation = ( RestResource )service.getAllOperations()[0];
RestMethod restMethod = operation.getRestMethodAt( 0 );
RestRequest request = restMethod.getRequestAt( 0 );
assertThat( request.getParams().getProperty( "language" ).getDefaultValue(), is( anEmptyString() ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.eviware.soapui.tools;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Unit tests for PropertyExpansionRemover.
*/
public class PropertyExpansionRemoverTest
{

@Test
public void removesPropertyExpansion() throws Exception
{
String stringWithPropertyExpansion = "<xsd:attribute name=\"name\" type=\"xsd:string\" default=\"${#Project#MyValue }\"/>";
assertThat( PropertyExpansionRemover.removeExpansions(stringWithPropertyExpansion ),
is("<xsd:attribute name=\"name\" type=\"xsd:string\" default=\"\"/>"));
}

@Test
public void removesDynamicPropertyExpansion() throws Exception
{
String stringWithDynamicPropertyExpansion = "<xsd:attribute name=\"name\" type=\"xsd:string\" default=\"${= new java.util.Date() }\"/>";
assertThat( PropertyExpansionRemover.removeExpansions(stringWithDynamicPropertyExpansion ),
is("<xsd:attribute name=\"name\" type=\"xsd:string\" default=\"\"/>"));
}

@Test
public void removesNestedPropertyExpansion() throws Exception
{
String stringWithDynamicPropertyExpansion = "<xsd:attribute name=\"name\" type=\"xsd:string\" default=\"${#testxml#${testxpath}}\"/>";
assertThat( PropertyExpansionRemover.removeExpansions(stringWithDynamicPropertyExpansion ),
is("<xsd:attribute name=\"name\" type=\"xsd:string\" default=\"\"/>"));
}

@Test
public void removesMultiplePropertyExpansions() throws Exception
{
String stringWithMultiplePropertyExpansions =
"<!-- ${= 5- + 2}--><xsd:attribute name=\"name\" type=\"xsd:string\" default=\"${#testxml#${testxpath}}\"/>";
assertThat( PropertyExpansionRemover.removeExpansions(stringWithMultiplePropertyExpansions ),
is("<!-- --><xsd:attribute name=\"name\" type=\"xsd:string\" default=\"\"/>"));
}

@Test
public void doesNotRemoveSpecialCharactersWhenNotPropertyExpansion() throws Exception
{
String stringWithSpecialCharacters = "<xsd:attribute name=\"name\" type=\"xsd:string\" default=\"$ { #testxml#$ {testxpath} } ${\"/>";
assertThat( PropertyExpansionRemover.removeExpansions(stringWithSpecialCharacters ),
is(stringWithSpecialCharacters));
}
}
Loading

0 comments on commit 6373165

Please sign in to comment.