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