    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.impl.wsdl.teststeps.assertions;
    14
    15  import java.io.BufferedReader;
    16  import java.io.InputStreamReader;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.apache.xmlbeans.XmlException;
    21  import org.apache.xmlbeans.XmlObject;
    22
    23  import com.eviware.soapui.config.RequestAssertionConfig;
    24  import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
    25
    26  public class XPathContainsAssertionTestCase extends TestCase
    27  {
    28  	private String testResponse;
    29  	private XPathContainsAssertion assertion;
    30  	private String testBody;
    31
    32  	protected void setUp() throws Exception
    33  	{
    34  		testResponse = readResource( "/testResponse.xml" );
    35  		testBody = readResource( "/testBody.xml" );
    36  		assertion = new XPathContainsAssertion( RequestAssertionConfig.Factory.newInstance(), null );
    37  	}
    38
    39  	public void testCreate() throws Exception
    40  	{
    41  		RequestAssertionConfig config = createConfig( "testPath", "testContent" );
    42
    43  		XPathContainsAssertion assertion = new XPathContainsAssertion( config, null );
    44
    45  		assertEquals( "testPath", assertion.getPath() );
    46  		assertEquals( "testContent", assertion.getExpectedContent() );
    47
    48  		XmlObject conf = assertion.createConfiguration();
    49  		String str = conf.xmlText();
    50
    51        assertEquals( "<xml-fragment><path>testPath</path><content>testContent</content><allowWildcards>false</allowWildcards></xml-fragment>", str );
    52  	}
    53
    54  	public void testFullContentMatch() throws Exception
    55  	{
    56  		assertion.setPath( "/" );
    57  		assertion.setContent( testResponse );
    58
    59  		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    60  	}
    61
    62     public void testFullBodyMatch() throws Exception
    63  	{
    64  		assertion.setPath( "declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';" +
    65                 "//urn:searchResponse" );
    66
    67  		assertion.setContent( testBody );
    68
    69  		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    70  	}
    71
    72     public void testAttributeMatch() throws Exception
    73  	{
    74  		assertion.setPath( "declare namespace env='http://schemas.xmlsoap.org/soap/envelope/';" +
    75  				"declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';" +
    76  				"declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';" +
    77                 "/env:Envelope/env:Body/urn:searchResponse/urn1:searchResult/@hitCount" );
    78  		assertion.setContent( "131" );
    79
    80  		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    81  	}
    82
    83     public void testElementMatch() throws Exception
    84  	{
    85  		assertion.setPath( "declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';" +
    86  				"declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';" +
    87                 "//urn:searchResponse/urn1:searchResult/company[2]/companyName" );
    88  		assertion.setContent( "<companyName>Bonnier Otto Karl Adam</companyName>" );
    89
    90  		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    91  	}
    92
    93     public void testElementTextMatch() throws Exception
    94  	{
    95  		assertion.setPath( "declare namespace env='http://schemas.xmlsoap.org/soap/envelope/';" +
    96  				"declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';" +
    97  				"declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';" +
    98                 "/env:Envelope/env:Body/urn:searchResponse/urn1:searchResult/company[2]/companyName/text()" );
    99  		assertion.setContent( "Bonnier Otto Karl Adam" );
    100
    101 		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    102 	}
    103
    104    public void testFragmentMatch() throws Exception
    105    {
    106 		assertion.setPath( "declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';" +
    107 				"declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';" +
    108                "//urn:searchResponse/urn1:searchResult/company[4]" );
    109 		assertion.setContent( readResource( "/testFragment.xml") );
    110
    111 		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    112    }
    113
    114    public void testAnyFragmentMatch() throws Exception
    115    {
    116 		assertion.setContent( readResource( "/testFragment.xml") );
    117 		assertion.setPath( "//company" );
    118
    119 		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    120    }
    121
    122    public void testLastElementTextMatch() throws Exception
    123 	{
    124 		assertion.setPath( "//company[last()]/companyName/text()" );
    125 		assertion.setContent( "Bonnier Zoo Förlag AB" );
    126
    127 		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    128 	}
    129
    130    public void testElementCountMatch() throws Exception
    131 	{
    132 		assertion.setPath( "count(//company)" );
    133 		assertion.setContent( "20" );
    134
    135 		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    136 	}
    137
    138    public void testAnyElementTextMatch() throws Exception
    139 	{
    140 		assertion.setPath( "declare namespace env='http://schemas.xmlsoap.org/soap/envelope/';" +
    141 				"declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';" +
    142 				"declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';" +
    143                "/env:Envelope/env:Body/urn:searchResponse/urn1:searchResult/company/companyName/text()" );
    144 		assertion.setContent( "Bonnier Otto Karl Adam" );
    145
    146 		assertNotNull( assertion.assertContent( testResponse, new WsdlSubmitContext( null ), "" ));
    147 	}
    148
    149    public void testAnyElementTextFail() throws Exception
    150 	{
    151 		assertion.setPath( "declare namespace env='http://schemas.xmlsoap.org/soap/envelope/';" +
    152 				"declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';" +
    153 				"declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';" +
    154                "/env:Envelope/env:Body/urn:searchResponse/urn1:searchResult/company/companyName/text()" );
    155 		assertion.setContent( "Bonnier Otto Karl Adams" );
    156
    157 		try
    158 		{
    159 			assertNotNull( assertion.assertContent(testResponse, new WsdlSubmitContext( null ), ""));
    160 			assertFalse( "assertion should have failed", true );
    161 		}
    162 		catch (Exception e)
    163 		{
    164 		}
    165 	}
    166
    167    public void testComplexMatch() throws Exception
    168 	{
    169    	String response = "<response><book>" +
    170 		"<bookID>1012</bookID>" +
    171 		"<author type=\"humanBeing\" href=\"#ID_1\"/>" +
    172 		"<title type=\"string\">Birds</title>" +
    173 		"</book>" +
    174 		"<humanBeing id=\"ID_1\">" +
    175 		"<name>Stephen King</name>" +
    176 		"</humanBeing></response>";
    177
    178 		assertion.setContent( "Stephen King"  );
    179 		//assertion.setPath( "//*[@id=substring(//book/bookID[text()='1012']/following-sibling::author/@href,2)]" );
    180
    181 		assertion.setPath( "//*[@id=substring(//book/bookID[text()='1012']/following-sibling::author/@href,2)]/name/text()" );
    182 		//assertion.setPath( "//*[@id='ID_1']/name/text()" );
    183 		assertNotNull( assertion.assertContent( response, new WsdlSubmitContext( null ), "" ));
    184 	}
    185
    186 	private String readResource(String string) throws Exception
    187 	{
    188 		BufferedReader reader = new BufferedReader( new InputStreamReader( getClass().getResourceAsStream( string ) ));
    189 		StringBuffer result = new StringBuffer();
    190
    191 		String line = reader.readLine();
    192 		while( line != null )
    193 		{
    194 			result.append( line );
    195 			line = reader.readLine();
    196 		}
    197
    198 		return result.toString();
    199 	}
    200
    201 	private RequestAssertionConfig createConfig( String path, String content ) throws XmlException
    202 	{
    203 		return RequestAssertionConfig.Factory.parse(
    204 				"<con:configuration xmlns:con=\"http://eviware.com/soapui/config\">" +
    205 				"<path>" + path + "</path><content>" + content + "</content></con:configuration>" );
    206 	}
    207
    208 }