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