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 boolean autoDelete = true;
41 public String testID = "";
42 public String testGroupID = "";
43 public String fullURL = "";
44 public String deleteURL = "";
45 public String location = "";
46 public String CSID = "";
47 public String subresourceCSID = "";
48 public String requestPayload = ""; //just like requestPayloadRaw, but may have multipart boundary and headers.
49 public String requestPayloadsRaw = "";
50 public String result = "";
51 public int responseCode = 0;
52 public String responseMessage = "";
53 public String method = "";
54 public String error = "";
55 public String fromTestID = "";
56 public String auth = "";
57 public String adminAuth = "";
58 public String boundary = "";
59 public String payloadStrictness = "";
60 public long contentLength = 0;
61 public String failureReason = "";
62 public String expectedContentExpanded = "";
63 public Header[] responseHeaders = new Header[0];
64 public List<Integer> expectedCodes = new ArrayList<Integer>();
65 public Map<String,String> vars = new HashMap<String,String>();
66 public void addVars(Map<String,String> newVars){
69 private Map<String, TreeWalkResults> partSummaries = new HashMap<String, TreeWalkResults>();
70 public void addPartSummary(String label, TreeWalkResults list){
71 partSummaries.put(label, list);
73 public String partsSummary(boolean detailed){
74 StringBuffer buf = new StringBuffer();
76 if (detailed) buf.append("\r\nDOM CHECK FAILED:\r\n");
77 else buf.append("; DOM CHECK FAILED:");
79 for (Map.Entry<String,TreeWalkResults> entry : partSummaries.entrySet()) {
80 String key = entry.getKey();
81 TreeWalkResults value = entry.getValue();
82 buf.append(" label:"+key+": ");
85 buf.append(value.fullSummary());
87 buf.append(value.miniSummary());
91 return buf.toString();
93 public boolean codeInSuccessRange(int code){
94 if (0<=code && code<200){
96 } else if (400<=code) {
102 public boolean isDomWalkOK(){
103 if (Tools.isEmpty(payloadStrictness)){
106 PAYLOAD_STRICTNESS strictness = PAYLOAD_STRICTNESS.valueOf(payloadStrictness);
107 for (Map.Entry<String,TreeWalkResults> entry : partSummaries.entrySet()) {
108 String key = entry.getKey();
109 TreeWalkResults value = entry.getValue();
110 if (value.hasDocErrors()){
111 failureReason = " : DOM DOC_ERROR; ";
116 if (!value.isStrictMatch()) {
117 failureReason = " : DOM NOT STRICT; ";
122 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
123 failureReason = " : DOM TEXT_DIFFERENT; ";
126 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.R_MISSING)>0){
127 failureReason = " : DOM R_MISSING; ";
132 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
133 failureReason = " : DOM TEXT_DIFFERENT; ";
138 if (!value.treesMatch()) {
139 failureReason = " : DOM TREE MISMATCH; ";
144 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
145 failureReason = " : DOM TEXT_DIFFERENT; ";
148 if (!value.treesMatch()) {
149 failureReason = " : DOM TREE MISMATCH; ";
160 private boolean overrideExpectedResult = false;
162 /** Call this method to create a ServiceResult mock object, for when you are doing autoDelete, and you come
163 * across a GET : GETs don't have a DELETE url, so they don't need to be autoDeleted, so an empty ServiceResult object
166 public void overrideGotExpectedResult(){
167 overrideExpectedResult = true;
170 public boolean gotExpectedResult(){
171 if (overrideExpectedResult){
174 //if (Tools.notEmpty(failureReason)){
177 for (Integer oneExpected : expectedCodes){
178 if (responseCode == oneExpected){
180 return isDomWalkOK();
183 if ( expectedCodes.size()>0 && codeInSuccessRange(responseCode)){ //none found, but result expected.
184 for (Integer oneExpected : expectedCodes){
185 if ( ! codeInSuccessRange(oneExpected)){
187 return isDomWalkOK();
191 boolean ok = codeInSuccessRange(responseCode);
194 return isDomWalkOK();
196 failureReason = " : STATUS CODE UNEXPECTED; ";
200 //public static final String[] DUMP_OPTIONS = {"minimal", "detailed", "full"};
201 public static enum DUMP_OPTIONS {minimal, detailed, full, auto};
203 public static enum PAYLOAD_STRICTNESS {ZERO, ADDOK, TREE, TEXT, TREE_TEXT, STRICT};
205 public String toString(){
210 private static final String LINE = "\r\n==================================";
211 private static final String CRLF = "\r\n";
213 public String detail(boolean includePayloads){
215 + ( gotExpectedResult() ? "SUCCESS" : "FAILURE" )
219 + ( (expectedCodes.size()>0) ? "; expectedCodes:"+expectedCodes : "" )
220 + ( Tools.notEmpty(testID) ? "; testID:"+testID : "" )
221 + ( Tools.notEmpty(testGroupID) ? "; testGroupID:"+testGroupID : "" )
222 + ( Tools.notEmpty(fromTestID) ? "; fromTestID:"+fromTestID : "" )
223 + ( Tools.notEmpty(responseMessage) ? "; msg:"+responseMessage : "" )
226 + ( Tools.notEmpty(deleteURL) ? "; deleteURL:"+deleteURL : "" )
227 + ( Tools.notEmpty(location) ? "; location.CSID:"+location : "" )
228 + ( Tools.notEmpty(error) ? "; ERROR:"+error : "" )
229 + "; gotExpected:"+gotExpectedResult()
230 //+";result:"+result+";"
231 + ( partsSummary(true))
233 + ( includePayloads && Tools.notBlank(requestPayload) ? LINE+"requestPayload:"+LINE+CRLF+requestPayload+LINE : "" )
234 + ( includePayloads && Tools.notBlank(result) ? LINE+"result:"+LINE+CRLF+result : "" );
238 public String minimal(){
239 return minimal(false);
242 public String minimal(boolean verbosePartsSummary){
244 + ( gotExpectedResult() ? "SUCCESS" : "FAILURE" )
246 + ( Tools.notEmpty(testID) ? "; "+testID : "" )
249 + (expectedCodes.size()>0 ? "; expected:"+expectedCodes : "")
250 + ( Tools.notEmpty(responseMessage) ? "; msg:"+responseMessage : "" )
252 //for auth, see detail() +"; auth: "+auth
253 + ( Tools.notEmpty(error) ? "; ERROR:"+error : "" )
254 + (verbosePartsSummary ? partsSummary(true) : partsSummary(false) )
257 public String dump(ServiceResult.DUMP_OPTIONS opt, boolean hasError){
260 return minimal(false);
262 return detail(false);
266 return minimal(hasError);
272 /** This method may be called from a test case, using a syntax like ${testID3.resValue("persons_common", "//refName")} */
273 public String got(String xpath) throws Exception {
275 //PayloadLogger.HttpTraffic traffic = PayloadLogger.readPayloads(this.result, this.boundary, this.contentLength);
276 //PayloadLogger.Part partFromServer = traffic.getPart(partName);
277 //String source = partFromServer.getContent();
278 String source = this.result;
279 if (Tools.isBlank(source)){
282 org.jdom.Element element = (org.jdom.Element) XmlCompareJdom.selectSingleNode(source, xpath, null); //todo: passing null for namespace may not work.
283 String sr = element != null ? element.getText() : "";
285 } catch (Exception e){
286 return "ERROR reading response value: "+e;
290 /** This method may be called from a test case, using a syntax like ${oe9.reqValue("personauthorities_common","//shortIdentifier")} */
291 public String sent(String xpath) throws Exception {
293 String source = this.requestPayload; // REM - 5/9/2012 : Changing to requestPayload from requestPayloadsRaw to get actual sent payload
295 return "ERROR:null:requestPayloadsRaw";
297 org.jdom.Element element = (org.jdom.Element) XmlCompareJdom.selectSingleNode(source, xpath, null); //e.g. "//shortIdentifier"); //todo: passing null for namespace may not work.
298 String sr = element != null ? element.getText() : "";
300 } catch (Exception e){
301 return "ERROR reading request value: "+e;
305 public String get(String what){
306 if ("CSID".equals(what)){
308 } else if ("location".equals(what)){
310 } else if ("testID".equals(what)){
312 } else if ("testGroupID".equals(what)){
314 } else if ("fullURL".equals(what)){
316 } else if ("deleteURL".equals(what)){
318 } else if ("responseCode".equals(what)){
319 return ""+responseCode;
320 } else if ("method".equals(what)){
323 if (vars.containsKey(what)){
324 return vars.get(what);