]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
548a9b4d8aea6f3110b7af85f76393fa94cfd2fc
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23
24 package org.collectionspace.services.IntegrationTests.xmlreplay;
25
26 import org.apache.commons.httpclient.Header;
27 import org.collectionspace.services.common.api.Tools;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 /**
35  * User: laramie
36  * $LastChangedRevision:  $
37  * $LastChangedDate:  $
38  */
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){
65         vars.putAll(newVars);
66     }
67     private Map<String, TreeWalkResults> partSummaries = new HashMap<String, TreeWalkResults>();
68     public void addPartSummary(String label, TreeWalkResults list){
69         partSummaries.put(label, list);
70     }
71     public String partsSummary(boolean detailed){
72         StringBuffer buf = new StringBuffer();
73         if (!isDomWalkOK()){
74             if (detailed) buf.append("\r\nDOM CHECK FAILED:\r\n");
75             else buf.append("; DOM CHECK FAILED:");
76         }
77         for (Map.Entry<String,TreeWalkResults> entry : partSummaries.entrySet()) {
78             String key = entry.getKey();
79             TreeWalkResults value = entry.getValue();
80             buf.append(" label:"+key+": ");
81             if (detailed){
82                 buf.append("\r\n");
83                 buf.append(value.fullSummary());
84             } else {
85                 buf.append(value.miniSummary());
86             }
87
88         }
89         return buf.toString();
90     }
91     public boolean codeInSuccessRange(int code){
92         if (0<=code && code<200){
93             return false;
94         } else if (400<=code) {
95             return false;
96         }
97         return true;
98     }
99
100     public boolean isDomWalkOK(){
101         if (Tools.isEmpty(payloadStrictness)){
102             return true;
103         }
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; ";
110                 return false;
111             }
112             switch (strictness){
113             case STRICT:
114                 if (!value.isStrictMatch()) {
115                     failureReason = " : DOM NOT STRICT; ";
116                     return false;
117                 }
118                 break;
119             case ADDOK:
120                 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
121                     failureReason = " : DOM TEXT_DIFFERENT; ";
122                     return false;
123                 }
124                 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.R_MISSING)>0){
125                     failureReason = " : DOM R_MISSING; ";
126                     return false;
127                 }
128                 break;
129             case TEXT:
130                 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
131                     failureReason = " : DOM TEXT_DIFFERENT; ";
132                     return false;
133                 }
134                 break;
135             case TREE:
136                 if (!value.treesMatch()) {
137                     failureReason = " : DOM TREE MISMATCH; ";
138                     return false;
139                 }
140                 break;
141             case TREE_TEXT:
142                 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
143                     failureReason = " : DOM TEXT_DIFFERENT; ";
144                     return false;
145                 }
146                 if (!value.treesMatch()) {
147                     failureReason = " : DOM TREE MISMATCH; ";
148                     return false;
149                 }
150                 break;
151             case ZERO:
152                 break;
153             }
154         }
155         return true;
156     }
157
158     private boolean overrideExpectedResult = false;
159
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
162      *  signifies this.
163      */
164     public void overrideGotExpectedResult(){
165         overrideExpectedResult = true;
166     }
167
168     public boolean gotExpectedResult(){
169         if (overrideExpectedResult){
170             return true;
171         }
172         //if (Tools.notEmpty(failureReason)){
173         //    return false;
174         //}
175         for (Integer oneExpected : expectedCodes){
176             if (responseCode == oneExpected){
177                 failureReason = "";
178                 return isDomWalkOK();
179             }
180         }
181         if ( expectedCodes.size()>0 && codeInSuccessRange(responseCode)){ //none found, but result expected.
182             for (Integer oneExpected : expectedCodes){
183                 if ( ! codeInSuccessRange(oneExpected)){
184                     failureReason = "";
185                     return isDomWalkOK();
186                 }
187             }
188         }
189         boolean ok = codeInSuccessRange(responseCode);
190         if (ok) {
191             failureReason = "";
192             return isDomWalkOK();
193         }
194         failureReason = " : STATUS CODE UNEXPECTED; ";
195         return false;
196     }
197
198     //public static final String[] DUMP_OPTIONS = {"minimal", "detailed", "full"};
199     public static enum DUMP_OPTIONS {minimal, detailed, full, auto};
200
201     public static enum PAYLOAD_STRICTNESS {ZERO, ADDOK, TREE, TEXT, TREE_TEXT, STRICT};
202
203     public String toString(){
204         return detail(true);
205
206     }
207
208     private static final String LINE = "\r\n==================================";
209     private static final String CRLF = "\r\n";
210
211     public String detail(boolean includePayloads){
212         String res =  "{"
213                 + ( gotExpectedResult() ? "SUCCESS" : "FAILURE"  )
214                 + failureReason
215                 +"; "+method
216                 +"; "+responseCode
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 : "" )
222                 +"; URL:"+fullURL
223                 +"; auth: "+auth
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))
230                 +"}"
231                 + ( includePayloads && Tools.notBlank(requestPayload) ? LINE+"requestPayload:"+LINE+CRLF+requestPayload+LINE : "" )
232                 + ( includePayloads && Tools.notBlank(result) ? LINE+"result:"+LINE+CRLF+result : "" );
233         return res;
234     }
235
236     public String minimal(){
237         return minimal(false);
238     }
239
240     public String minimal(boolean verbosePartsSummary){
241         return "{"
242                 + ( gotExpectedResult() ? "SUCCESS" : "FAILURE"  )
243                 + failureReason
244                 + ( Tools.notEmpty(testID) ? "; "+testID : "" )
245                 +"; "+method
246                 +"; "+responseCode
247                 + (expectedCodes.size()>0 ? "; expected:"+expectedCodes : "")
248                 + ( Tools.notEmpty(responseMessage) ? "; msg:"+responseMessage : "" )
249                 +"; URL:"+fullURL
250                 //for auth, see detail()   +"; auth: "+auth
251                 + ( Tools.notEmpty(error) ? "; ERROR:"+error : "" )
252                 + (verbosePartsSummary ? partsSummary(true) : partsSummary(false) )
253                 +"}";
254     }
255     public String dump(ServiceResult.DUMP_OPTIONS opt, boolean hasError){
256         switch (opt){
257             case minimal:
258                 return minimal(false);
259             case detailed:
260                 return detail(false);
261             case full:
262                 return detail(true);
263             case auto:
264                 return minimal(hasError);
265             default:
266                 return toString();
267         }
268     }
269
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 {
272         try {
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)){
278                 return "";
279             }
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() : "";
282             return sr;
283         } catch (Exception e){
284             return "ERROR reading response value: "+e;
285         }
286     }
287
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 {
290         try {
291             String source = this.requestPayload; // REM - 5/9/2012 : Changing to requestPayload from requestPayloadsRaw to get actual sent payload 
292             if (source == null){
293                 return "ERROR:null:requestPayloadsRaw";
294             }
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() : "";
297             return sr;
298         } catch (Exception e){
299             return "ERROR reading request value: "+e;
300         }
301     }
302
303     public String get(String what){
304         if ("CSID".equals(what)){
305             return CSID;
306         } else if ("location".equals(what)){
307             return location;
308         } else if ("testID".equals(what)){
309             return testID;
310         } else if ("testGroupID".equals(what)){
311             return testGroupID;
312         } else if ("fullURL".equals(what)){
313             return fullURL;
314         } else if ("deleteURL".equals(what)){
315             return deleteURL;
316         } else if ("responseCode".equals(what)){
317             return ""+responseCode;
318         } else if ("method".equals(what)){
319             return method;
320         }
321         if (vars.containsKey(what)){
322             return vars.get(what);
323         }
324         return "";
325     }
326
327 }