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