]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c7b598880e3a96c741ecefa44c6ec7eea4657aa5
[tmp/jakarta-migration.git] /
1 /**     
2  * CollectionSpaceIntegrationTest.java
3  *
4  * {Purpose of This Class}
5  *
6  * {Other Notes Relating to This Class (Optional)}
7  *
8  * $LastChangedBy: $
9  * $LastChangedRevision: $
10  * $LastChangedDate: $
11  *
12  * This document is a part of the source code and related artifacts
13  * for CollectionSpace, an open source collections management system
14  * for museums and related institutions:
15  *
16  * http://www.collectionspace.org
17  * http://wiki.collectionspace.org
18  *
19  * Copyright © 2009 {Contributing Institution}
20  *
21  * Licensed under the Educational Community License (ECL), Version 2.0.
22  * You may not use this file except in compliance with this License.
23  *
24  * You may obtain a copy of the ECL 2.0 License at
25  * https://source.collectionspace.org/collection-space/LICENSE.txt
26  */
27 package org.collectionspace.services.IntegrationTests.test;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.Marshaller;
36
37 import org.collectionspace.services.client.PayloadInputPart;
38 import org.collectionspace.services.client.PoxPayloadIn;
39 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
40 import org.collectionspace.services.collectionobject.TitleGroup;
41 import org.collectionspace.services.collectionobject.TitleGroupList;
42 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
43 import org.collectionspace.services.intake.IntakesCommon;
44 import org.collectionspace.services.relation.RelationsCommon;
45 import org.jboss.resteasy.client.ClientResponse;
46
47 /**
48  * The Class CollectionSpaceIntegrationTest.
49  */
50 public abstract class CollectionSpaceIntegrationTest {
51
52         /*
53          * Package scoped methods.
54          */
55
56         /**
57          * Fill collection object.
58          * 
59          * @param co the co
60          * @param identifier the identifier
61          */
62         void fillCollectionObject(CollectionobjectsCommon co, String identifier) {
63                 fillCollectionObject(co, "objectNumber-" + identifier, "title-"
64                                 + identifier);
65         }
66
67         /**
68          * Fill collection object.
69          * 
70          * @param co the co
71          * @param objectNumber the object number
72          * @param title the object title
73          */
74         void fillCollectionObject(CollectionobjectsCommon co, String objectNumber,
75                         String title) {
76                 co.setObjectNumber(objectNumber);
77                 TitleGroupList titleGroupList = new TitleGroupList();
78                 List<TitleGroup> titleGroups = titleGroupList.getTitleGroup();
79                 TitleGroup titleGroup = new TitleGroup();
80                 titleGroup.setTitle(title);
81                 titleGroups.add(titleGroup);
82                 co.setTitleGroupList(titleGroupList);
83         }
84
85         /**
86          * Fill intake.
87          * 
88          * @param theIntake the the intake
89          * @param identifier the identifier
90          */
91         void fillIntake(IntakesCommon theIntake, String identifier) {
92                 String CURRENT_DATE_UTC = GregorianCalendarDateTimeUtils.currentDateUTC();
93                 fillIntake(theIntake, "entryNumber-" + identifier, CURRENT_DATE_UTC);
94         }
95
96         /**
97          * Fill intake.
98          * 
99          * @param theIntake the the intake
100          * @param entryNumber the entry number
101          * @param entryDate the entry date
102          */
103         void fillIntake(IntakesCommon theIntake, String entryNumber, String entryDate) {
104                 theIntake.setEntryNumber(entryNumber);
105                 theIntake.setEntryDate(entryDate);
106         }
107
108     /**
109      * Fill relation.
110      * 
111      * @param relation the relation
112      * @param subjectCsid the document id1
113      * @param subjectDocumentType the document type1
114      * @param objectCsid the document id2
115      * @param objectDocumentType the document type2
116      * @param rt the rt
117      */
118     void fillRelation(RelationsCommon relation, 
119             String subjectCsid, String subjectDocumentType,
120             String objectCsid, String objectDocumentType,
121             String rt)
122     {
123         relation.setSubjectCsid(subjectCsid);
124         relation.setSubjectDocumentType(subjectDocumentType);
125         relation.setObjectCsid(objectCsid);
126         relation.setObjectDocumentType(objectDocumentType);
127         
128         relation.setRelationshipType(rt);
129     }
130         
131         /**
132          * Creates the identifier.
133          * 
134          * @return the string
135          */
136         String createIdentifier() {
137                 long identifier = System.currentTimeMillis();
138                 return Long.toString(identifier);
139         }
140
141         /**
142          * Extract id.
143          * 
144          * @param res the res
145          * 
146          * @return the string
147          */
148         String extractId(ClientResponse<Response> res) {
149                 String result = null;
150                 
151                 MultivaluedMap mvm = res.getMetadata();
152                 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
153                 verbose("extractId:uri=" + uri);
154                 String[] segments = uri.split("/");
155                 result = segments[segments.length - 1];
156                 verbose("id=" + result);
157                 
158                 return result;
159         }
160         
161         String extractId(Response res) {
162                 String result = null;
163                 
164                 MultivaluedMap mvm = res.getMetadata();
165                 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
166                 verbose("extractId:uri=" + uri);
167                 String[] segments = uri.split("/");
168                 result = segments[segments.length - 1];
169                 verbose("id=" + result);
170                 
171                 return result;
172         }       
173
174         /**
175          * Extract part.
176          * 
177          * @param input
178          *            the input
179          * @param label
180          *            the label
181          * @param clazz
182          *            the clazz
183          * 
184          * @return the object
185          * 
186          * @throws Exception
187          *             the exception
188          */
189         static Object extractPart(PoxPayloadIn input, String label, Class clazz) {
190                 Object obj = null;
191
192                 PayloadInputPart payloadInputPart = input.getPart(label);
193                 if (payloadInputPart != null) {
194                         obj = payloadInputPart.getBody();
195                                 }
196
197                 return obj;
198         }
199         
200         /**
201          * Verbose.
202          * 
203          * @param msg the msg
204          */
205         void verbose(String msg) {
206                 System.out.println(msg);
207         }
208
209         /**
210          * Verbose.
211          * 
212          * @param msg the msg
213          * @param o the o
214          * @param clazz the clazz
215          */
216         void verbose(String msg, Object o, Class clazz) {
217                 try {
218                         verbose(msg);
219                         JAXBContext jc = JAXBContext.newInstance(clazz);
220                         Marshaller m = jc.createMarshaller();
221                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
222                         m.marshal(o, System.out);
223                 } catch (Exception e) {
224                         e.printStackTrace();
225                 }
226         }
227
228         /**
229          * Verbose map.
230          * 
231          * @param map the map
232          */
233         void verboseMap(MultivaluedMap map) {
234                 for (Object entry : map.entrySet()) {
235                         MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
236                         verbose("  name=" + mentry.getKey() + " value=" + mentry.getValue());
237                 }
238         }
239
240 }