2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
24 package org.collectionspace.services.IntegrationTests.xmlreplay;
26 import org.apache.commons.httpclient.Header;
27 import org.collectionspace.services.common.api.Tools;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
36 * $LastChangedRevision: $
39 public class ServiceResult {
40 public String testID = "";
41 public String testGroupID = "";
42 public String fullURL = "";
43 public String deleteURL = "";
44 public String location = "";
45 public String CSID = "";
46 public String subresourceCSID = "";
47 public String requestPayload = ""; //just like requestPayloadRaw, but may have multipart boundary and headers.
48 public String requestPayloadsRaw = "";
49 public String result = "";
50 public int responseCode = 0;
51 public String responseMessage = "";
52 public String method = "";
53 public String error = "";
54 public String fromTestID = "";
55 public String auth = "";
56 public String boundary = "";
57 public String payloadStrictness = "";
58 public long contentLength = 0;
59 public String failureReason = "";
60 public String expectedContentExpanded = "";
61 public Header[] responseHeaders = new Header[0];
62 public List<Integer> expectedCodes = new ArrayList<Integer>();
63 public Map<String,String> vars = new HashMap<String,String>();
64 public void addVars(Map<String,String> newVars){
67 private Map<String, TreeWalkResults> partSummaries = new HashMap<String, TreeWalkResults>();
68 public void addPartSummary(String label, TreeWalkResults list){
69 partSummaries.put(label, list);
71 public String partsSummary(boolean detailed){
72 StringBuffer buf = new StringBuffer();
74 if (detailed) buf.append("\r\nDOM CHECK FAILED:\r\n");
75 else buf.append("; DOM CHECK FAILED:");
77 for (Map.Entry<String,TreeWalkResults> entry : partSummaries.entrySet()) {
78 String key = entry.getKey();
79 TreeWalkResults value = entry.getValue();
80 buf.append(" label:"+key+": ");
83 buf.append(value.fullSummary());
85 buf.append(value.miniSummary());
89 return buf.toString();
91 public boolean codeInSuccessRange(int code){
92 if (0<=code && code<200){
94 } else if (400<=code) {
100 public boolean isDomWalkOK(){
101 if (Tools.isEmpty(payloadStrictness)){
104 PAYLOAD_STRICTNESS strictness = PAYLOAD_STRICTNESS.valueOf(payloadStrictness);
105 for (Map.Entry<String,TreeWalkResults> entry : partSummaries.entrySet()) {
106 String key = entry.getKey();
107 TreeWalkResults value = entry.getValue();
108 if (value.hasDocErrors()){
109 failureReason = " : DOM DOC_ERROR; ";
114 if (!value.isStrictMatch()) {
115 failureReason = " : DOM NOT STRICT; ";
120 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
121 failureReason = " : DOM TEXT_DIFFERENT; ";
124 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.R_MISSING)>0){
125 failureReason = " : DOM R_MISSING; ";
130 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
131 failureReason = " : DOM TEXT_DIFFERENT; ";
136 if (!value.treesMatch()) {
137 failureReason = " : DOM TREE MISMATCH; ";
142 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
143 failureReason = " : DOM TEXT_DIFFERENT; ";
146 if (!value.treesMatch()) {
147 failureReason = " : DOM TREE MISMATCH; ";
158 private boolean overrideExpectedResult = false;
160 /** Call this method to create a ServiceResult mock object, for when you are doing autoDelete, and you come
161 * across a GET : GETs don't have a DELETE url, so they don't need to be autoDeleted, so an empty ServiceResult object
164 public void overrideGotExpectedResult(){
165 overrideExpectedResult = true;
168 public boolean gotExpectedResult(){
169 if (overrideExpectedResult){
172 //if (Tools.notEmpty(failureReason)){
175 for (Integer oneExpected : expectedCodes){
176 if (responseCode == oneExpected){
178 return isDomWalkOK();
181 if ( expectedCodes.size()>0 && codeInSuccessRange(responseCode)){ //none found, but result expected.
182 for (Integer oneExpected : expectedCodes){
183 if ( ! codeInSuccessRange(oneExpected)){
185 return isDomWalkOK();
189 boolean ok = codeInSuccessRange(responseCode);
192 return isDomWalkOK();
194 failureReason = " : STATUS CODE UNEXPECTED; ";
198 //public static final String[] DUMP_OPTIONS = {"minimal", "detailed", "full"};
199 public static enum DUMP_OPTIONS {minimal, detailed, full, auto};
201 public static enum PAYLOAD_STRICTNESS {ZERO, ADDOK, TREE, TEXT, TREE_TEXT, STRICT};
203 public String toString(){
208 private static final String LINE = "\r\n==================================";
209 private static final String CRLF = "\r\n";
211 public String detail(boolean includePayloads){
213 + ( gotExpectedResult() ? "SUCCESS" : "FAILURE" )
217 + ( (expectedCodes.size()>0) ? "; expectedCodes:"+expectedCodes : "" )
218 + ( Tools.notEmpty(testID) ? "; testID:"+testID : "" )
219 + ( Tools.notEmpty(testGroupID) ? "; testGroupID:"+testGroupID : "" )
220 + ( Tools.notEmpty(fromTestID) ? "; fromTestID:"+fromTestID : "" )
221 + ( Tools.notEmpty(responseMessage) ? "; msg:"+responseMessage : "" )
224 + ( Tools.notEmpty(deleteURL) ? "; deleteURL:"+deleteURL : "" )
225 + ( Tools.notEmpty(location) ? "; location.CSID:"+location : "" )
226 + ( Tools.notEmpty(error) ? "; ERROR:"+error : "" )
227 + "; gotExpected:"+gotExpectedResult()
228 //+";result:"+result+";"
229 + ( partsSummary(true))
231 + ( includePayloads && Tools.notBlank(requestPayload) ? LINE+"requestPayload:"+LINE+CRLF+requestPayload+LINE : "" )
232 + ( includePayloads && Tools.notBlank(result) ? LINE+"result:"+LINE+CRLF+result : "" );
236 public String minimal(){
237 return minimal(false);
240 public String minimal(boolean verbosePartsSummary){
242 + ( gotExpectedResult() ? "SUCCESS" : "FAILURE" )
244 + ( Tools.notEmpty(testID) ? "; "+testID : "" )
247 + (expectedCodes.size()>0 ? "; expected:"+expectedCodes : "")
248 + ( Tools.notEmpty(responseMessage) ? "; msg:"+responseMessage : "" )
250 //for auth, see detail() +"; auth: "+auth
251 + ( Tools.notEmpty(error) ? "; ERROR:"+error : "" )
252 + (verbosePartsSummary ? partsSummary(true) : partsSummary(false) )
255 public String dump(ServiceResult.DUMP_OPTIONS opt, boolean hasError){
258 return minimal(false);
260 return detail(false);
264 return minimal(hasError);
270 /** This method may be called from a test case, using a syntax like ${testID3.resValue("persons_common", "//refName")} */
271 public String got(String xpath) throws Exception {
273 //PayloadLogger.HttpTraffic traffic = PayloadLogger.readPayloads(this.result, this.boundary, this.contentLength);
274 //PayloadLogger.Part partFromServer = traffic.getPart(partName);
275 //String source = partFromServer.getContent();
276 String source = this.result;
277 if (Tools.isBlank(source)){
280 org.jdom.Element element = (org.jdom.Element) XmlCompareJdom.selectSingleNode(source, xpath, null); //todo: passing null for namespace may not work.
281 String sr = element != null ? element.getText() : "";
283 } catch (Exception e){
284 return "ERROR reading response value: "+e;
288 /** This method may be called from a test case, using a syntax like ${oe9.reqValue("personauthorities_common","//shortIdentifier")} */
289 public String sent(String xpath) throws Exception {
291 String source = this.requestPayload; // REM - 5/9/2012 : Changing to requestPayload from requestPayloadsRaw to get actual sent payload
293 return "ERROR:null:requestPayloadsRaw";
295 org.jdom.Element element = (org.jdom.Element) XmlCompareJdom.selectSingleNode(source, xpath, null); //e.g. "//shortIdentifier"); //todo: passing null for namespace may not work.
296 String sr = element != null ? element.getText() : "";
298 } catch (Exception e){
299 return "ERROR reading request value: "+e;
303 public String get(String what){
304 if ("CSID".equals(what)){
306 } else if ("location".equals(what)){
308 } else if ("testID".equals(what)){
310 } else if ("testGroupID".equals(what)){
312 } else if ("fullURL".equals(what)){
314 } else if ("deleteURL".equals(what)){
316 } else if ("responseCode".equals(what)){
317 return ""+responseCode;
318 } else if ("method".equals(what)){
321 if (vars.containsKey(what)){
322 return vars.get(what);