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