001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.teststeps.assertions;
014:
015: import java.io.BufferedReader;
016: import java.io.InputStreamReader;
017:
018: import junit.framework.TestCase;
019:
020: import org.apache.xmlbeans.XmlException;
021: import org.apache.xmlbeans.XmlObject;
022:
023: import com.eviware.soapui.config.RequestAssertionConfig;
024: import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
025:
026: public class XPathContainsAssertionTestCase extends TestCase {
027: private String testResponse;
028: private XPathContainsAssertion assertion;
029: private String testBody;
030:
031: protected void setUp() throws Exception {
032: testResponse = readResource("/testResponse.xml");
033: testBody = readResource("/testBody.xml");
034: assertion = new XPathContainsAssertion(
035: RequestAssertionConfig.Factory.newInstance(), null);
036: }
037:
038: public void testCreate() throws Exception {
039: RequestAssertionConfig config = createConfig("testPath",
040: "testContent");
041:
042: XPathContainsAssertion assertion = new XPathContainsAssertion(
043: config, null);
044:
045: assertEquals("testPath", assertion.getPath());
046: assertEquals("testContent", assertion.getExpectedContent());
047:
048: XmlObject conf = assertion.createConfiguration();
049: String str = conf.xmlText();
050:
051: assertEquals(
052: "<xml-fragment><path>testPath</path><content>testContent</content><allowWildcards>false</allowWildcards></xml-fragment>",
053: str);
054: }
055:
056: public void testFullContentMatch() throws Exception {
057: assertion.setPath("/");
058: assertion.setContent(testResponse);
059:
060: assertNotNull(assertion.assertContent(testResponse,
061: new WsdlSubmitContext(null), ""));
062: }
063:
064: public void testFullBodyMatch() throws Exception {
065: assertion
066: .setPath("declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';"
067: + "//urn:searchResponse");
068:
069: assertion.setContent(testBody);
070:
071: assertNotNull(assertion.assertContent(testResponse,
072: new WsdlSubmitContext(null), ""));
073: }
074:
075: public void testAttributeMatch() throws Exception {
076: assertion
077: .setPath("declare namespace env='http://schemas.xmlsoap.org/soap/envelope/';"
078: + "declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';"
079: + "declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';"
080: + "/env:Envelope/env:Body/urn:searchResponse/urn1:searchResult/@hitCount");
081: assertion.setContent("131");
082:
083: assertNotNull(assertion.assertContent(testResponse,
084: new WsdlSubmitContext(null), ""));
085: }
086:
087: public void testElementMatch() throws Exception {
088: assertion
089: .setPath("declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';"
090: + "declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';"
091: + "//urn:searchResponse/urn1:searchResult/company[2]/companyName");
092: assertion
093: .setContent("<companyName>Bonnier Otto Karl Adam</companyName>");
094:
095: assertNotNull(assertion.assertContent(testResponse,
096: new WsdlSubmitContext(null), ""));
097: }
098:
099: public void testElementTextMatch() throws Exception {
100: assertion
101: .setPath("declare namespace env='http://schemas.xmlsoap.org/soap/envelope/';"
102: + "declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';"
103: + "declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';"
104: + "/env:Envelope/env:Body/urn:searchResponse/urn1:searchResult/company[2]/companyName/text()");
105: assertion.setContent("Bonnier Otto Karl Adam");
106:
107: assertNotNull(assertion.assertContent(testResponse,
108: new WsdlSubmitContext(null), ""));
109: }
110:
111: public void testFragmentMatch() throws Exception {
112: assertion
113: .setPath("declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';"
114: + "declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';"
115: + "//urn:searchResponse/urn1:searchResult/company[4]");
116: assertion.setContent(readResource("/testFragment.xml"));
117:
118: assertNotNull(assertion.assertContent(testResponse,
119: new WsdlSubmitContext(null), ""));
120: }
121:
122: public void testAnyFragmentMatch() throws Exception {
123: assertion.setContent(readResource("/testFragment.xml"));
124: assertion.setPath("//company");
125:
126: assertNotNull(assertion.assertContent(testResponse,
127: new WsdlSubmitContext(null), ""));
128: }
129:
130: public void testLastElementTextMatch() throws Exception {
131: assertion.setPath("//company[last()]/companyName/text()");
132: assertion.setContent("Bonnier Zoo Förlag AB");
133:
134: assertNotNull(assertion.assertContent(testResponse,
135: new WsdlSubmitContext(null), ""));
136: }
137:
138: public void testElementCountMatch() throws Exception {
139: assertion.setPath("count(//company)");
140: assertion.setContent("20");
141:
142: assertNotNull(assertion.assertContent(testResponse,
143: new WsdlSubmitContext(null), ""));
144: }
145:
146: public void testAnyElementTextMatch() throws Exception {
147: assertion
148: .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.setContent("Bonnier Otto Karl Adam");
153:
154: assertNotNull(assertion.assertContent(testResponse,
155: new WsdlSubmitContext(null), ""));
156: }
157:
158: public void testAnyElementTextFail() throws Exception {
159: assertion
160: .setPath("declare namespace env='http://schemas.xmlsoap.org/soap/envelope/';"
161: + "declare namespace urn='urn:schema:v1:companyservice:applications:bis.bonnier.se';"
162: + "declare namespace urn1='urn:v1:companysearch:common:bis.bonnier.se';"
163: + "/env:Envelope/env:Body/urn:searchResponse/urn1:searchResult/company/companyName/text()");
164: assertion.setContent("Bonnier Otto Karl Adams");
165:
166: try {
167: assertNotNull(assertion.assertContent(testResponse,
168: new WsdlSubmitContext(null), ""));
169: assertFalse("assertion should have failed", true);
170: } catch (Exception e) {
171: }
172: }
173:
174: public void testComplexMatch() throws Exception {
175: String response = "<response><book>" + "<bookID>1012</bookID>"
176: + "<author type=\"humanBeing\" href=\"#ID_1\"/>"
177: + "<title type=\"string\">Birds</title>" + "</book>"
178: + "<humanBeing id=\"ID_1\">"
179: + "<name>Stephen King</name>"
180: + "</humanBeing></response>";
181:
182: assertion.setContent("Stephen King");
183: //assertion.setPath( "//*[@id=substring(//book/bookID[text()='1012']/following-sibling::author/@href,2)]" );
184:
185: assertion
186: .setPath("//*[@id=substring(//book/bookID[text()='1012']/following-sibling::author/@href,2)]/name/text()");
187: //assertion.setPath( "//*[@id='ID_1']/name/text()" );
188: assertNotNull(assertion.assertContent(response,
189: new WsdlSubmitContext(null), ""));
190: }
191:
192: private String readResource(String string) throws Exception {
193: BufferedReader reader = new BufferedReader(
194: new InputStreamReader(getClass().getResourceAsStream(
195: string)));
196: StringBuffer result = new StringBuffer();
197:
198: String line = reader.readLine();
199: while (line != null) {
200: result.append(line);
201: line = reader.readLine();
202: }
203:
204: return result.toString();
205: }
206:
207: private RequestAssertionConfig createConfig(String path,
208: String content) throws XmlException {
209: return RequestAssertionConfig.Factory
210: .parse("<con:configuration xmlns:con=\"http://eviware.com/soapui/config\">"
211: + "<path>"
212: + path
213: + "</path><content>"
214: + content + "</content></con:configuration>");
215: }
216:
217: }
|