1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.model.impl.wsdl.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.teststeps.assertions.XPathContainsAssertion;
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.getContent() );
47
48 XmlObject conf = assertion.createConfiguration();
49 String str = conf.xmlText();
50
51 assertEquals( "<xml-fragment><path>testPath</path><content>testContent</content></xml-fragment>", str );
52 }
53
54 public void testFullContentMatch() throws Exception
55 {
56 assertion.setPath( "/" );
57 assertion.setContent( testResponse );
58
59 assertNotNull( assertion.assertResponse( testResponse ));
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.assertResponse( testResponse ));
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.assertResponse( testResponse ));
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.assertResponse( testResponse ));
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.assertResponse( testResponse ));
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.assertResponse( testResponse ));
112 }
113
114 public void testAnyFragmentMatch() throws Exception
115 {
116 assertion.setContent( readResource( "/testFragment.xml") );
117 assertion.setPath( "//company" );
118
119 assertNotNull( assertion.assertResponse( testResponse ));
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.assertResponse( testResponse ));
128 }
129
130 public void testElementCountMatch() throws Exception
131 {
132 assertion.setPath( "count(//company)" );
133 assertion.setContent( "20" );
134
135 assertNotNull( assertion.assertResponse( testResponse ));
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.assertResponse( testResponse ));
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.assertResponse(testResponse));
160 assertFalse( "assertion should have failed", true );
161 }
162 catch (Exception e)
163 {
164 }
165 }
166
167 private String readResource(String string) throws Exception
168 {
169 BufferedReader reader = new BufferedReader( new InputStreamReader( getClass().getResourceAsStream( string ) ));
170 StringBuffer result = new StringBuffer();
171
172 String line = reader.readLine();
173 while( line != null )
174 {
175 result.append( line );
176 line = reader.readLine();
177 }
178
179 return result.toString();
180 }
181
182 private RequestAssertionConfig createConfig( String path, String content ) throws XmlException
183 {
184 return RequestAssertionConfig.Factory.parse(
185 "<con:configuration xmlns:con=\"http://eviware.com/soapui/config\">" +
186 "<path>" + path + "</path><content>" + content + "</content></con:configuration>" );
187 }
188
189 }