]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
03f39e9607555986223f749efa47fb1d20a06fd5
[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 \r
31 import javax.ws.rs.core.MultivaluedMap;\r
32 import javax.ws.rs.core.Response;\r
33 import javax.xml.bind.JAXBContext;\r
34 import javax.xml.bind.Marshaller;\r
35 \r
36 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;\r
37 import org.collectionspace.services.intake.IntakesCommon;\r
38 import org.collectionspace.services.relation.RelationsCommon;\r
39 import org.collectionspace.services.relation.RelationshipType;\r
40 import org.jboss.resteasy.client.ClientResponse;\r
41 import org.jboss.resteasy.plugins.providers.multipart.InputPart;\r
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
43 \r
44 /**\r
45  * The Class CollectionSpaceIntegrationTest.\r
46  */\r
47 public abstract class CollectionSpaceIntegrationTest {\r
48 \r
49         /*\r
50          * Package scoped methods.\r
51          */\r
52 \r
53         /**\r
54          * Fill collection object.\r
55          * \r
56          * @param co the co\r
57          * @param identifier the identifier\r
58          */\r
59         void fillCollectionObject(CollectionobjectsCommon co, String identifier) {\r
60                 fillCollectionObject(co, "objectNumber-" + identifier, "title-"\r
61                                 + identifier);\r
62         }\r
63 \r
64         /**\r
65          * Fill collection object.\r
66          * \r
67          * @param co the co\r
68          * @param objectNumber the object number\r
69          * @param title the object title\r
70          */\r
71         void fillCollectionObject(CollectionobjectsCommon co, String objectNumber,\r
72                         String title) {\r
73                 co.setObjectNumber(objectNumber);\r
74                 co.setTitle(title);\r
75         }\r
76 \r
77         /**\r
78          * Fill intake.\r
79          * \r
80          * @param theIntake the the intake\r
81          * @param identifier the identifier\r
82          */\r
83         void fillIntake(IntakesCommon theIntake, String identifier) {\r
84                 fillIntake(theIntake, "entryNumber-" + identifier, "entryDate-"\r
85                                 + identifier);\r
86         }\r
87 \r
88         /**\r
89          * Fill intake.\r
90          * \r
91          * @param theIntake the the intake\r
92          * @param entryNumber the entry number\r
93          * @param entryDate the entry date\r
94          */\r
95         void fillIntake(IntakesCommon theIntake, String entryNumber, String entryDate) {\r
96                 theIntake.setEntryNumber(entryNumber);\r
97                 theIntake.setEntryDate(entryDate);\r
98         }\r
99 \r
100     /**\r
101      * Fill relation.\r
102      * \r
103      * @param relation the relation\r
104      * @param documentId1 the document id1\r
105      * @param documentType1 the document type1\r
106      * @param documentId2 the document id2\r
107      * @param documentType2 the document type2\r
108      * @param rt the rt\r
109      */\r
110     void fillRelation(RelationsCommon relation, String documentId1, String documentType1,\r
111                 String documentId2, String documentType2, RelationshipType rt)\r
112     {\r
113         relation.setDocumentId1(documentId1);\r
114         relation.setDocumentType1(documentType1);\r
115         relation.setDocumentId2(documentId2);\r
116         relation.setDocumentType2(documentType2);\r
117         \r
118         relation.setRelationshipType(rt);\r
119     }\r
120         \r
121         /**\r
122          * Creates the identifier.\r
123          * \r
124          * @return the string\r
125          */\r
126         String createIdentifier() {\r
127                 long identifier = System.currentTimeMillis();\r
128                 return Long.toString(identifier);\r
129         }\r
130 \r
131         /**\r
132          * Extract id.\r
133          * \r
134          * @param res the res\r
135          * \r
136          * @return the string\r
137          */\r
138         String extractId(ClientResponse<Response> res) {\r
139                 String result = null;\r
140                 \r
141                 MultivaluedMap mvm = res.getMetadata();\r
142                 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);\r
143                 verbose("extractId:uri=" + uri);\r
144                 String[] segments = uri.split("/");\r
145                 result = segments[segments.length - 1];\r
146                 verbose("id=" + result);\r
147                 \r
148                 return result;\r
149         }\r
150 \r
151         /**\r
152          * Extract part.\r
153          * \r
154          * @param input\r
155          *            the input\r
156          * @param label\r
157          *            the label\r
158          * @param clazz\r
159          *            the clazz\r
160          * \r
161          * @return the object\r
162          * \r
163          * @throws Exception\r
164          *             the exception\r
165          */\r
166         static Object extractPart(MultipartInput input, String label, Class clazz) {\r
167                 Object obj = null;\r
168                 \r
169                 try {\r
170                         for (InputPart part : input.getParts()) {\r
171                                 String partLabel = part.getHeaders().getFirst("label");\r
172                                 if (label.equalsIgnoreCase(partLabel)) {\r
173                                         String partStr = part.getBodyAsString();\r
174                                         obj = part.getBody(clazz, null);\r
175                                         break;\r
176                                 }\r
177                         }\r
178                 } catch (Exception e) {\r
179                         e.printStackTrace();\r
180                 }\r
181 \r
182                 return obj;\r
183         }\r
184         \r
185         /**\r
186          * Verbose.\r
187          * \r
188          * @param msg the msg\r
189          */\r
190         void verbose(String msg) {\r
191                 System.out.println(msg);\r
192         }\r
193 \r
194         /**\r
195          * Verbose.\r
196          * \r
197          * @param msg the msg\r
198          * @param o the o\r
199          * @param clazz the clazz\r
200          */\r
201         void verbose(String msg, Object o, Class clazz) {\r
202                 try {\r
203                         verbose(msg);\r
204                         JAXBContext jc = JAXBContext.newInstance(clazz);\r
205                         Marshaller m = jc.createMarshaller();\r
206                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);\r
207                         m.marshal(o, System.out);\r
208                 } catch (Exception e) {\r
209                         e.printStackTrace();\r
210                 }\r
211         }\r
212 \r
213         /**\r
214          * Verbose map.\r
215          * \r
216          * @param map the map\r
217          */\r
218         void verboseMap(MultivaluedMap map) {\r
219                 for (Object entry : map.entrySet()) {\r
220                         MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;\r
221                         verbose("  name=" + mentry.getKey() + " value=" + mentry.getValue());\r
222                 }\r
223         }\r
224 \r
225 }\r