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