]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4c7be741e119a8ac43815acd1c5a316f998865df
[tmp/jakarta-migration.git] /
1 /**\r
2  * This document is a part of the source code and related artifacts\r
3  * for CollectionSpace, an open source collections management system\r
4  * for museums and related institutions:\r
5  *\r
6  * http://www.collectionspace.org\r
7  * http://wiki.collectionspace.org\r
8  *\r
9  * Copyright © 2009 Regents of the University of California\r
10  *\r
11  * Licensed under the Educational Community License (ECL), Version 2.0.\r
12  * You may not use this file except in compliance with this License.\r
13  *\r
14  * You may obtain a copy of the ECL 2.0 License at\r
15  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
16  *\r
17  * Unless required by applicable law or agreed to in writing, software\r
18  * distributed under the License is distributed on an "AS IS" BASIS,\r
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
20  * See the License for the specific language governing permissions and\r
21  * limitations under the License.\r
22  */\r
23 package org.collectionspace.services.client.test;\r
24 \r
25 import java.util.ArrayList;\r
26 import java.util.HashMap;\r
27 import java.util.List;\r
28 import java.util.Map;\r
29 \r
30 import javax.ws.rs.core.MediaType;\r
31 import javax.ws.rs.core.Response;\r
32 \r
33 import org.collectionspace.services.OrganizationJAXBSchema;\r
34 import org.collectionspace.services.client.CollectionSpaceClient;\r
35 import org.collectionspace.services.client.IntakeClient;\r
36 import org.collectionspace.services.client.OrgAuthorityClient;\r
37 import org.collectionspace.services.client.OrgAuthorityClientUtils;\r
38 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;\r
39 import org.collectionspace.services.intake.IntakesCommon;\r
40 //import org.collectionspace.services.intake.IntakesCommonList;\r
41 import org.collectionspace.services.jaxb.AbstractCommonList;\r
42 \r
43 import org.jboss.resteasy.client.ClientResponse;\r
44 \r
45 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
48 import org.testng.Assert;\r
49 import org.testng.annotations.AfterClass;\r
50 import org.testng.annotations.Test;\r
51 \r
52 import org.slf4j.Logger;\r
53 import org.slf4j.LoggerFactory;\r
54 \r
55 /**\r
56  * OrganizationAuthRefDocsTest, carries out tests against a\r
57  * deployed and running Organization Service.\r
58  *\r
59  * $LastChangedRevision: 1327 $\r
60  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $\r
61  */\r
62 public class OrganizationAuthRefDocsTest extends BaseServiceTest {\r
63 \r
64    private final Logger logger =\r
65        LoggerFactory.getLogger(OrganizationAuthRefDocsTest.class);\r
66 \r
67     // Instance variables specific to this test.\r
68     final String SERVICE_PATH_COMPONENT = "intakes";\r
69     final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";\r
70     private String knownIntakeId = null;\r
71     private List<String> intakeIdsCreated = new ArrayList<String>();\r
72     private List<String> orgIdsCreated = new ArrayList<String>();\r
73     private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();\r
74     private int OK_STATUS = Response.Status.OK.getStatusCode();\r
75     private String orgAuthCSID = null; \r
76     private String currentOwnerOrgCSID = null; \r
77     private String currentOwnerRefName = null;\r
78     private String depositorRefName = null;\r
79     private String conditionCheckAssesorRefName = null;\r
80     private String insurerRefName = null;\r
81     private String fieldCollectorRefName = null;\r
82     private String valuerRefName = null;\r
83     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;\r
84 \r
85     /* (non-Javadoc)\r
86      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()\r
87      */\r
88     @Override\r
89     protected CollectionSpaceClient getClientInstance() {\r
90         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class\r
91     }\r
92     \r
93     /* (non-Javadoc)\r
94      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)\r
95      */\r
96     @Override\r
97         protected AbstractCommonList getAbstractCommonList(\r
98                         ClientResponse<AbstractCommonList> response) {\r
99         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class\r
100     }\r
101 \r
102     // ---------------------------------------------------------------\r
103     // CRUD tests : CREATE tests\r
104     // ---------------------------------------------------------------\r
105     // Success outcomes\r
106     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)\r
107     public void createIntakeWithAuthRefs(String testName) throws Exception {\r
108 \r
109         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);\r
110 \r
111         // Submit the request to the service and store the response.\r
112         String identifier = createIdentifier();\r
113         \r
114         // Create all the organization refs and entities\r
115         createOrgRefs();\r
116 \r
117         IntakeClient intakeClient = new IntakeClient();\r
118         MultipartOutput multipart = createIntakeInstance(\r
119                 "entryNumber-" + identifier,\r
120                 "entryDate-" + identifier,\r
121                                                                 currentOwnerRefName,\r
122                                                                 depositorRefName,\r
123                                                                 conditionCheckAssesorRefName,\r
124                                                                 insurerRefName,\r
125                                                                 fieldCollectorRefName,\r
126                                                                 valuerRefName );\r
127 \r
128         ClientResponse<Response> res = intakeClient.create(multipart);\r
129         try {\r
130                 int statusCode = res.getStatus();\r
131         \r
132                 // Check the status code of the response: does it match\r
133                 // the expected response(s)?\r
134                 //\r
135                 // Specifically:\r
136                 // Does it fall within the set of valid status codes?\r
137                 // Does it exactly match the expected status code?\r
138                 if(logger.isDebugEnabled()){\r
139                     logger.debug(testName + ": status = " + statusCode);\r
140                 }\r
141                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
142                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
143                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);\r
144         } finally {\r
145                 res.releaseConnection();\r
146         }\r
147 \r
148         // Store the ID returned from the first resource created\r
149         // for additional tests below.\r
150         if (knownIntakeId == null){\r
151             knownIntakeId = extractId(res);\r
152             if (logger.isDebugEnabled()) {\r
153                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);\r
154             }\r
155         }\r
156         \r
157         // Store the IDs from every resource created by tests,\r
158         // so they can be deleted after tests have been run.\r
159         intakeIdsCreated.add(extractId(res));\r
160     }\r
161     \r
162     /**\r
163      * Creates the organization refs.\r
164      */\r
165     protected void createOrgRefs(){\r
166         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
167         String authRefName = \r
168                 OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, false);\r
169         MultipartOutput multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(\r
170                         ORGANIZATION_AUTHORITY_NAME, authRefName, orgAuthClient.getCommonPartName());\r
171         ClientResponse<Response> res = orgAuthClient.create(multipart);\r
172         int statusCode = res.getStatus();\r
173 \r
174         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
175                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
176         Assert.assertEquals(statusCode, CREATED_STATUS);\r
177         orgAuthCSID = extractId(res);\r
178         \r
179         currentOwnerRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
180                                                                 authRefName, "Olivier Owner", true);\r
181                                 currentOwnerOrgCSID = createOrganization("Olivier", "Owner", currentOwnerRefName);\r
182         orgIdsCreated.add(currentOwnerOrgCSID);\r
183         \r
184         depositorRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
185                                                                         authRefName, "Debbie Depositor", true);\r
186         orgIdsCreated.add(createOrganization("Debbie", "Depositor", depositorRefName));\r
187         \r
188         conditionCheckAssesorRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
189                                                                         authRefName, "Andrew Assessor", true);\r
190         orgIdsCreated.add(createOrganization("Andrew", "Assessor", conditionCheckAssesorRefName));\r
191         \r
192         insurerRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
193                                                                         authRefName, "Ingrid Insurer", true);\r
194         orgIdsCreated.add(createOrganization("Ingrid", "Insurer", insurerRefName));\r
195         \r
196         fieldCollectorRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
197                                                                         authRefName, "Connie Collector", true);\r
198         orgIdsCreated.add(createOrganization("Connie", "Collector", fieldCollectorRefName));\r
199         \r
200         valuerRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
201                                                                         authRefName, "Vince Valuer", true);\r
202         orgIdsCreated.add(createOrganization("Vince", "Valuer", valuerRefName));\r
203         \r
204 \r
205     }\r
206     \r
207     protected String createOrganization(String shortName, String longName, String refName ) {\r
208         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
209         Map<String, String> orgInfo = new HashMap<String,String>();\r
210         orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);\r
211         orgInfo.put(OrganizationJAXBSchema.LONG_NAME, longName);\r
212         MultipartOutput multipart = \r
213                 OrgAuthorityClientUtils.createOrganizationInstance(orgAuthCSID, \r
214                                 refName, orgInfo, orgAuthClient.getItemCommonPartName());\r
215         ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);\r
216         int statusCode = res.getStatus();\r
217 \r
218         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
219                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
220         Assert.assertEquals(statusCode, CREATED_STATUS);\r
221         return extractId(res);\r
222     }\r
223 \r
224     // Success outcomes\r
225     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,\r
226         dependsOnMethods = {"createIntakeWithAuthRefs"})\r
227     public void readAndCheckAuthRefDocs(String testName) throws Exception {\r
228 \r
229         // Perform setup.\r
230         testSetup(OK_STATUS, ServiceRequestType.READ,testName);\r
231         \r
232         // Get the auth ref docs and check them\r
233        OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
234        ClientResponse<AuthorityRefDocList> refDocListResp =\r
235                 orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);\r
236 \r
237         int statusCode = refDocListResp.getStatus();\r
238 \r
239         if(logger.isDebugEnabled()){\r
240             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);\r
241         }\r
242         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
243                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
244         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);\r
245         AuthorityRefDocList list = refDocListResp.getEntity();\r
246 \r
247         // Optionally output additional data about list members for debugging.\r
248         boolean iterateThroughList = true;\r
249         boolean fFoundIntake = false;\r
250         if(iterateThroughList && logger.isDebugEnabled()){\r
251             List<AuthorityRefDocList.AuthorityRefDocItem> items =\r
252                     list.getAuthorityRefDocItem();\r
253             int i = 0;\r
254             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);\r
255             for(AuthorityRefDocList.AuthorityRefDocItem item : items){\r
256                 logger.debug(testName + ": list-item[" + i + "] " +\r
257                                 item.getDocType() + "(" +\r
258                                 item.getDocId() + ") Name:[" +\r
259                                 item.getDocName() + "] Number:[" +\r
260                                 item.getDocNumber() + "] in field:[" +\r
261                                 item.getSourceField() + "]");\r
262                 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {\r
263                         fFoundIntake = true;\r
264                 }\r
265                 i++;\r
266             }\r
267             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");\r
268         }\r
269     }\r
270 \r
271 \r
272     // ---------------------------------------------------------------\r
273     // Cleanup of resources created during testing\r
274     // ---------------------------------------------------------------\r
275 \r
276     /**\r
277      * Deletes all resources created by tests, after all tests have been run.\r
278      *\r
279      * This cleanup method will always be run, even if one or more tests fail.\r
280      * For this reason, it attempts to remove all resources created\r
281      * at any point during testing, even if some of those resources\r
282      * may be expected to be deleted by certain tests.\r
283      */\r
284     @AfterClass(alwaysRun=true)\r
285     public void cleanUp() {\r
286         String noTest = System.getProperty("noTestCleanup");\r
287         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {\r
288             if (logger.isDebugEnabled()) {\r
289                 logger.debug("Skipping Cleanup phase ...");\r
290             }\r
291             return;\r
292         }\r
293         if (logger.isDebugEnabled()) {\r
294             logger.debug("Cleaning up temporary resources created for testing ...");\r
295         }\r
296         IntakeClient intakeClient = new IntakeClient();\r
297         // Note: Any non-success responses are ignored and not reported.\r
298         for (String resourceId : intakeIdsCreated) {\r
299             ClientResponse<Response> res = intakeClient.delete(resourceId);\r
300             res.releaseConnection();\r
301         }\r
302         // Delete persons before PersonAuth\r
303         OrgAuthorityClient personAuthClient = new OrgAuthorityClient();\r
304         for (String resourceId : orgIdsCreated) {\r
305             ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);\r
306             res.releaseConnection();\r
307         }\r
308         if (orgAuthCSID != null) {\r
309                 personAuthClient.delete(orgAuthCSID).releaseConnection();\r
310         }\r
311     }\r
312 \r
313     // ---------------------------------------------------------------\r
314     // Utility methods used by tests above\r
315     // ---------------------------------------------------------------\r
316     @Override\r
317     public String getServicePathComponent() {\r
318         return SERVICE_PATH_COMPONENT;\r
319     }\r
320 \r
321    private MultipartOutput createIntakeInstance(String entryNumber,\r
322                 String entryDate,\r
323                                 String currentOwner,\r
324                                 String depositor,\r
325                                 String conditionCheckAssesor,\r
326                                 String insurer,\r
327                                 String fieldCollector,\r
328                                 String Valuer ) {\r
329         IntakesCommon intake = new IntakesCommon();\r
330         intake.setEntryNumber(entryNumber);\r
331         intake.setEntryDate(entryDate);\r
332         intake.setCurrentOwner(currentOwner);\r
333         intake.setDepositor(depositor);\r
334         intake.setConditionCheckAssesor(conditionCheckAssesor);\r
335         intake.setInsurer(insurer);\r
336         intake.setFieldCollector(fieldCollector);\r
337         intake.setValuer(Valuer);\r
338         MultipartOutput multipart = new MultipartOutput();\r
339         OutputPart commonPart =\r
340             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);\r
341         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());\r
342 \r
343         if(logger.isDebugEnabled()){\r
344             logger.debug("to be created, intake common");\r
345             logger.debug(objectAsXmlString(intake, IntakesCommon.class));\r
346         }\r
347 \r
348         return multipart;\r
349     }\r
350 }\r