]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
03cf89909d41cf2b5c33f48e414dbbb5ac9df5d9
[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.client.PayloadOutputPart;\r
39 import org.collectionspace.services.client.PoxPayloadOut;\r
40 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;\r
41 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;\r
42 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;\r
43 import org.collectionspace.services.intake.IntakesCommon;\r
44 import org.collectionspace.services.intake.InsurerList;\r
45 import org.collectionspace.services.jaxb.AbstractCommonList;\r
46 \r
47 import org.jboss.resteasy.client.ClientResponse;\r
48 \r
49 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
50 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
51 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
52 import org.testng.Assert;\r
53 import org.testng.annotations.AfterClass;\r
54 import org.testng.annotations.Test;\r
55 \r
56 import org.slf4j.Logger;\r
57 import org.slf4j.LoggerFactory;\r
58 \r
59 /**\r
60  * OrganizationAuthRefDocsTest, carries out tests against a\r
61  * deployed and running Organization Service.\r
62  *\r
63  * $LastChangedRevision: 1327 $\r
64  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $\r
65  */\r
66 public class OrganizationAuthRefDocsTest extends BaseServiceTest {\r
67 \r
68     private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();\r
69     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);\r
70 \r
71     // Instance variables specific to this test.\r
72     final String SERVICE_PATH_COMPONENT = "intakes";\r
73     final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";\r
74     private String knownIntakeId = null;\r
75     private List<String> intakeIdsCreated = new ArrayList<String>();\r
76     private List<String> orgIdsCreated = new ArrayList<String>();\r
77     private String orgAuthCSID = null; \r
78     //private String orgAuthRefName = null; \r
79     private String currentOwnerOrgCSID = null; \r
80     private String currentOwnerRefName = null;\r
81     private String depositorRefName = null;\r
82     private String conditionCheckerAssessorRefName = null;\r
83     private String insurerRefName = null;\r
84     private String valuerRefName = null;\r
85     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;\r
86     private final static String CURRENT_DATE_UTC =\r
87             GregorianCalendarDateTimeUtils.currentDateUTC();\r
88 \r
89         @Override\r
90         protected String getServiceName() {\r
91                 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498\r
92         }\r
93     \r
94     /* (non-Javadoc)\r
95      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()\r
96      */\r
97     @Override\r
98     protected CollectionSpaceClient getClientInstance() {\r
99         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class\r
100     }\r
101     \r
102     /* (non-Javadoc)\r
103      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)\r
104      */\r
105     @Override\r
106         protected AbstractCommonList getAbstractCommonList(\r
107                         ClientResponse<AbstractCommonList> response) {\r
108         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class\r
109     }\r
110 \r
111     // ---------------------------------------------------------------\r
112     // CRUD tests : CREATE tests\r
113     // ---------------------------------------------------------------\r
114     // Success outcomes\r
115     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)\r
116     public void createIntakeWithAuthRefs(String testName) throws Exception {\r
117 \r
118         if (logger.isDebugEnabled()) {\r
119             logger.debug(testBanner(testName, CLASS_NAME));\r
120         }\r
121         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);\r
122 \r
123         // Submit the request to the service and store the response.\r
124         String identifier = createIdentifier();\r
125         \r
126         // Create all the organization refs and entities\r
127         createOrgRefs();\r
128 \r
129         IntakeClient intakeClient = new IntakeClient();\r
130         PoxPayloadOut multipart = createIntakeInstance(\r
131                 "entryNumber-" + identifier,\r
132                 CURRENT_DATE_UTC,\r
133                 currentOwnerRefName,\r
134                 // Use currentOwnerRefName twice to test fix for CSPACE-2863\r
135                 currentOwnerRefName,    //depositorRefName,\r
136                 conditionCheckerAssessorRefName,\r
137                 insurerRefName,\r
138                 valuerRefName );\r
139 \r
140         ClientResponse<Response> res = intakeClient.create(multipart);\r
141         try {\r
142                 int statusCode = res.getStatus();\r
143         \r
144                 // Check the status code of the response: does it match\r
145                 // the expected response(s)?\r
146                 //\r
147                 // Specifically:\r
148                 // Does it fall within the set of valid status codes?\r
149                 // Does it exactly match the expected status code?\r
150                 if(logger.isDebugEnabled()){\r
151                     logger.debug(testName + ": status = " + statusCode);\r
152                 }\r
153                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
154                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
155                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);\r
156         } finally {\r
157                 res.releaseConnection();\r
158         }\r
159 \r
160         // Store the ID returned from the first resource created\r
161         // for additional tests below.\r
162         if (knownIntakeId == null){\r
163             knownIntakeId = extractId(res);\r
164             if (logger.isDebugEnabled()) {\r
165                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);\r
166             }\r
167         }\r
168         \r
169         // Store the IDs from every resource created by tests,\r
170         // so they can be deleted after tests have been run.\r
171         intakeIdsCreated.add(extractId(res));\r
172     }\r
173     \r
174     /**\r
175      * Creates the organization refs.\r
176      */\r
177     protected void createOrgRefs(){\r
178         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
179         //orgAuthRefName = \r
180         //      OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, null);\r
181         PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(\r
182                         ORGANIZATION_AUTHORITY_NAME, ORGANIZATION_AUTHORITY_NAME, orgAuthClient.getCommonPartName());\r
183         ClientResponse<Response> res = orgAuthClient.create(multipart);\r
184         int statusCode = res.getStatus();\r
185 \r
186         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
187                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
188         Assert.assertEquals(statusCode, STATUS_CREATED);\r
189         orgAuthCSID = extractId(res);\r
190         \r
191                 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");\r
192         orgIdsCreated.add(currentOwnerOrgCSID);\r
193         currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);\r
194         \r
195                 String newOrgCSID =\r
196                         createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");\r
197         depositorRefName = \r
198                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);\r
199         orgIdsCreated.add(newOrgCSID);\r
200         \r
201                 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");\r
202                 conditionCheckerAssessorRefName = \r
203                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);\r
204         orgIdsCreated.add(newOrgCSID);\r
205         \r
206                 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");\r
207                 insurerRefName = \r
208                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);\r
209         orgIdsCreated.add(newOrgCSID);\r
210         \r
211                 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");\r
212                 valuerRefName = \r
213                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);\r
214         orgIdsCreated.add(newOrgCSID);\r
215     }\r
216 \r
217     protected String createOrganization(String shortId, String shortName, String longName) {\r
218         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
219         Map<String, String> orgInfo = new HashMap<String,String>();\r
220         orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);\r
221         orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);\r
222         orgInfo.put(OrganizationJAXBSchema.LONG_NAME, longName);\r
223         PoxPayloadOut multipart = \r
224                 OrgAuthorityClientUtils.createOrganizationInstance(null, //orgAuthRefName,\r
225                                 orgInfo, orgAuthClient.getItemCommonPartName());\r
226         ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);\r
227         int statusCode = res.getStatus();\r
228 \r
229         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
230                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
231         Assert.assertEquals(statusCode, STATUS_CREATED);\r
232         return extractId(res);\r
233     }\r
234 \r
235     // Success outcomes\r
236     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,\r
237         dependsOnMethods = {"createIntakeWithAuthRefs"})\r
238     public void readAndCheckAuthRefDocs(String testName) throws Exception {\r
239 \r
240         if (logger.isDebugEnabled()) {\r
241             logger.debug(testBanner(testName, CLASS_NAME));\r
242         }\r
243         // Perform setup.\r
244         testSetup(STATUS_OK, ServiceRequestType.READ);\r
245         \r
246         // Get the auth ref docs and check them\r
247        OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
248        ClientResponse<AuthorityRefDocList> refDocListResp =\r
249                 orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);\r
250 \r
251         int statusCode = refDocListResp.getStatus();\r
252 \r
253         if(logger.isDebugEnabled()){\r
254             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);\r
255         }\r
256         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
257                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
258         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);\r
259         AuthorityRefDocList list = refDocListResp.getEntity();\r
260 \r
261         // Optionally output additional data about list members for debugging.\r
262         boolean iterateThroughList = true;\r
263         int nIntakesFound = 0;\r
264         if(iterateThroughList && logger.isDebugEnabled()){\r
265             List<AuthorityRefDocList.AuthorityRefDocItem> items =\r
266                     list.getAuthorityRefDocItem();\r
267             int i = 0;\r
268             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);\r
269             for(AuthorityRefDocList.AuthorityRefDocItem item : items){\r
270                 logger.debug(testName + ": list-item[" + i + "] " +\r
271                                 item.getDocType() + "(" +\r
272                                 item.getDocId() + ") Name:[" +\r
273                                 item.getDocName() + "] Number:[" +\r
274                                 item.getDocNumber() + "] in field:[" +\r
275                                 item.getSourceField() + "]");\r
276                 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {\r
277                         nIntakesFound++;\r
278                 }\r
279                 i++;\r
280             }\r
281             //\r
282             Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");\r
283         }\r
284     }\r
285 \r
286 \r
287     // ---------------------------------------------------------------\r
288     // Cleanup of resources created during testing\r
289     // ---------------------------------------------------------------\r
290 \r
291     /**\r
292      * Deletes all resources created by tests, after all tests have been run.\r
293      *\r
294      * This cleanup method will always be run, even if one or more tests fail.\r
295      * For this reason, it attempts to remove all resources created\r
296      * at any point during testing, even if some of those resources\r
297      * may be expected to be deleted by certain tests.\r
298      */\r
299     @AfterClass(alwaysRun=true)\r
300     public void cleanUp() {\r
301         String noTest = System.getProperty("noTestCleanup");\r
302         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {\r
303             if (logger.isDebugEnabled()) {\r
304                 logger.debug("Skipping Cleanup phase ...");\r
305             }\r
306             return;\r
307         }\r
308         if (logger.isDebugEnabled()) {\r
309             logger.debug("Cleaning up temporary resources created for testing ...");\r
310         }\r
311         IntakeClient intakeClient = new IntakeClient();\r
312         // Note: Any non-success responses are ignored and not reported.\r
313         for (String resourceId : intakeIdsCreated) {\r
314             ClientResponse<Response> res = intakeClient.delete(resourceId);\r
315             res.releaseConnection();\r
316         }\r
317         // Delete persons before PersonAuth\r
318         OrgAuthorityClient personAuthClient = new OrgAuthorityClient();\r
319         for (String resourceId : orgIdsCreated) {\r
320             ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);\r
321             res.releaseConnection();\r
322         }\r
323         if (orgAuthCSID != null) {\r
324                 personAuthClient.delete(orgAuthCSID).releaseConnection();\r
325         }\r
326     }\r
327 \r
328     // ---------------------------------------------------------------\r
329     // Utility methods used by tests above\r
330     // ---------------------------------------------------------------\r
331     @Override\r
332     public String getServicePathComponent() {\r
333         return SERVICE_PATH_COMPONENT;\r
334     }\r
335 \r
336    private PoxPayloadOut createIntakeInstance(String entryNumber,\r
337                 String entryDate,\r
338                                 String currentOwner,\r
339                                 String depositor,\r
340                                 String conditionCheckerAssessor,\r
341                                 String insurer,\r
342                                 String Valuer ) {\r
343         IntakesCommon intake = new IntakesCommon();\r
344         intake.setEntryNumber(entryNumber);\r
345         intake.setEntryDate(entryDate);\r
346         intake.setCurrentOwner(currentOwner);\r
347         intake.setDepositor(depositor);\r
348         intake.setValuer(Valuer);\r
349 \r
350         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();\r
351         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();\r
352         checkersOrAssessors.add(conditionCheckerAssessor);\r
353         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);\r
354 \r
355         InsurerList insurerList = new InsurerList();\r
356         List<String> insurers = insurerList.getInsurer();\r
357         insurers.add(insurer);\r
358         intake.setInsurers(insurerList);\r
359 \r
360         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());\r
361         PayloadOutputPart commonPart =\r
362             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);\r
363         commonPart.setLabel(new IntakeClient().getCommonPartName());\r
364 \r
365         if(logger.isDebugEnabled()){\r
366             logger.debug("to be created, intake common");\r
367             logger.debug(objectAsXmlString(intake, IntakesCommon.class));\r
368         }\r
369 \r
370         return multipart;\r
371     }\r
372 }\r