]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4736f5da4cf76f3ba2aca8151a8c3aaf070e8500
[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 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){
67         vars.putAll(newVars);
68     }
69     private Map<String, TreeWalkResults> partSummaries = new HashMap<String, TreeWalkResults>();
70     public void addPartSummary(String label, TreeWalkResults list){
71         partSummaries.put(label, list);
72     }
73     public String partsSummary(boolean detailed){
74         StringBuffer buf = new StringBuffer();
75         if (!isDomWalkOK()){
76             if (detailed) buf.append("\r\nDOM CHECK FAILED:\r\n");
77             else buf.append("; DOM CHECK FAILED:");
78         }
79         for (Map.Entry<String,TreeWalkResults> entry : partSummaries.entrySet()) {
80             String key = entry.getKey();
81             TreeWalkResults value = entry.getValue();
82             buf.append(" label:"+key+": ");
83             if (detailed){
84                 buf.append("\r\n");
85                 buf.append(value.fullSummary());
86             } else {
87                 buf.append(value.miniSummary());
88             }
89
90         }
91         return buf.toString();
92     }
93     public boolean codeInSuccessRange(int code){
94         if (0<=code && code<200){
95             return false;
96         } else if (400<=code) {
97             return false;
98         }
99         return true;
100     }
101
102     public boolean isDomWalkOK(){
103         if (Tools.isEmpty(payloadStrictness)){
104             return true;
105         }
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; ";
112                 return false;
113             }
114             switch (strictness){
115             case STRICT:
116                 if (!value.isStrictMatch()) {
117                     failureReason = " : DOM NOT STRICT; ";
118                     return false;
119                 }
120                 break;
121             case ADDOK:
122                 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
123                     failureReason = " : DOM TEXT_DIFFERENT; ";
124                     return false;
125                 }
126                 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.R_MISSING)>0){
127                     failureReason = " : DOM R_MISSING; ";
128                     return false;
129                 }
130                 break;
131             case TEXT:
132                 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
133                     failureReason = " : DOM TEXT_DIFFERENT; ";
134                     return false;
135                 }
136                 break;
137             case TREE:
138                 if (!value.treesMatch()) {
139                     failureReason = " : DOM TREE MISMATCH; ";
140                     return false;
141                 }
142                 break;
143             case TREE_TEXT:
144                 if (value.countFor(TreeWalkResults.TreeWalkEntry.STATUS.TEXT_DIFFERENT)>0) {
145                     failureReason = " : DOM TEXT_DIFFERENT; ";
146                     return false;
147                 }
148                 if (!value.treesMatch()) {
149                     failureReason = " : DOM TREE MISMATCH; ";
150                     return false;
151                 }
152                 break;
153             case ZERO:
154                 break;
155             }
156         }
157         return true;
158     }
159
160     private boolean overrideExpectedResult = false;
161
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
164      *  signifies this.
165      */
166     public void overrideGotExpectedResult(){
167         overrideExpectedResult = true;
168     }
169
170     public boolean gotExpectedResult(){
171         if (overrideExpectedResult){
172             return true;
173         }
174         //if (Tools.notEmpty(failureReason)){
175         //    return false;
176         //}
177         for (Integer oneExpected : expectedCodes){
178             if (responseCode == oneExpected){
179                 failureReason = "";
180                 return isDomWalkOK();
181             }
182         }
183         if ( expectedCodes.size()>0 && codeInSuccessRange(responseCode)){ //none found, but result expected.
184             for (Integer oneExpected : expectedCodes){
185                 if ( ! codeInSuccessRange(oneExpected)){
186                     failureReason = "";
187                     return isDomWalkOK();
188                 }
189             }
190         }
191         boolean ok = codeInSuccessRange(responseCode);
192         if (ok) {
193             failureReason = "";
194             return isDomWalkOK();
195         }
196         failureReason = " : STATUS CODE UNEXPECTED; ";
197         return false;
198     }
199
200     //public static final String[] DUMP_OPTIONS = {"minimal", "detailed", "full"};
201     public static enum DUMP_OPTIONS {minimal, detailed, full, auto};
202
203     public static enum PAYLOAD_STRICTNESS {ZERO, ADDOK, TREE, TEXT, TREE_TEXT, STRICT};
204
205     public String toString(){
206         return detail(true);
207
208     }
209
210     private static final String LINE = "\r\n==================================";
211     private static final String CRLF = "\r\n";
212
213     public String detail(boolean includePayloads){
214         String res =  "{"
215                 + ( gotExpectedResult() ? "SUCCESS" : "FAILURE"  )
216                 + failureReason
217                 +"; "+method
218                 +"; "+responseCode
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 : "" )
224                 +"; URL:"+fullURL
225                 +"; auth: "+auth
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))
232                 +"}"
233                 + ( includePayloads && Tools.notBlank(requestPayload) ? LINE+"requestPayload:"+LINE+CRLF+requestPayload+LINE : "" )
234                 + ( includePayloads && Tools.notBlank(result) ? LINE+"result:"+LINE+CRLF+result : "" );
235         return res;
236     }
237
238     public String minimal(){
239         return minimal(false);
240     }
241
242     public String minimal(boolean verbosePartsSummary){
243         return "{"
244                 + ( gotExpectedResult() ? "SUCCESS" : "FAILURE"  )
245                 + failureReason
246                 + ( Tools.notEmpty(testID) ? "; "+testID : "" )
247                 +"; "+method
248                 +"; "+responseCode
249                 + (expectedCodes.size()>0 ? "; expected:"+expectedCodes : "")
250                 + ( Tools.notEmpty(responseMessage) ? "; msg:"+responseMessage : "" )
251                 +"; URL:"+fullURL
252                 //for auth, see detail()   +"; auth: "+auth
253                 + ( Tools.notEmpty(error) ? "; ERROR:"+error : "" )
254                 + (verbosePartsSummary ? partsSummary(true) : partsSummary(false) )
255                 +"}";
256     }
257     public String dump(ServiceResult.DUMP_OPTIONS opt, boolean hasError){
258         switch (opt){
259             case minimal:
260                 return minimal(false);
261             case detailed:
262                 return detail(false);
263             case full:
264                 return detail(true);
265             case auto:
266                 return minimal(hasError);
267             default:
268                 return toString();
269         }
270     }
271
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 {
274         try {
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)){
280                 return "";
281             }
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() : "";
284             return sr;
285         } catch (Exception e){
286             return "ERROR reading response value: "+e;
287         }
288     }
289
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 {
292         try {
293             String source = this.requestPayload; // REM - 5/9/2012 : Changing to requestPayload from requestPayloadsRaw to get actual sent payload 
294             if (source == null){
295                 return "ERROR:null:requestPayloadsRaw";
296             }
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() : "";
299             return sr;
300         } catch (Exception e){
301             return "ERROR reading request value: "+e;
302         }
303     }
304
305     public String get(String what){
306         if ("CSID".equals(what)){
307             return CSID;
308         } else if ("location".equals(what)){
309             return location;
310         } else if ("testID".equals(what)){
311             return testID;
312         } else if ("testGroupID".equals(what)){
313             return testGroupID;
314         } else if ("fullURL".equals(what)){
315             return fullURL;
316         } else if ("deleteURL".equals(what)){
317             return deleteURL;
318         } else if ("responseCode".equals(what)){
319             return ""+responseCode;
320         } else if ("method".equals(what)){
321             return method;
322         }
323         if (vars.containsKey(what)){
324             return vars.get(what);
325         }
326         return "";
327     }
328
329 }