]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c113011d1924bc6845729ce0139ddfb633549e75
[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         assertStatusCode(refDocListResp, testName);\r
251 \r
252         AuthorityRefDocList list = refDocListResp.getEntity();\r
253 \r
254         // Optionally output additional data about list members for debugging.\r
255         boolean iterateThroughList = true;\r
256         int nIntakesFound = 0;\r
257         if(iterateThroughList && logger.isDebugEnabled()){\r
258             List<AuthorityRefDocList.AuthorityRefDocItem> items =\r
259                     list.getAuthorityRefDocItem();\r
260             int i = 0;\r
261             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);\r
262             for(AuthorityRefDocList.AuthorityRefDocItem item : items){\r
263                 logger.debug(testName + ": list-item[" + i + "] " +\r
264                                 item.getDocType() + "(" +\r
265                                 item.getDocId() + ") Name:[" +\r
266                                 item.getDocName() + "] Number:[" +\r
267                                 item.getDocNumber() + "] in field:[" +\r
268                                 item.getSourceField() + "]");\r
269                 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {\r
270                         nIntakesFound++;\r
271                 }\r
272                 i++;\r
273             }\r
274             //\r
275             Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");\r
276         }\r
277     }\r
278 \r
279 \r
280     // ---------------------------------------------------------------\r
281     // Cleanup of resources created during testing\r
282     // ---------------------------------------------------------------\r
283 \r
284     /**\r
285      * Deletes all resources created by tests, after all tests have been run.\r
286      *\r
287      * This cleanup method will always be run, even if one or more tests fail.\r
288      * For this reason, it attempts to remove all resources created\r
289      * at any point during testing, even if some of those resources\r
290      * may be expected to be deleted by certain tests.\r
291      */\r
292     @AfterClass(alwaysRun=true)\r
293     public void cleanUp() {\r
294         String noTest = System.getProperty("noTestCleanup");\r
295         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {\r
296             if (logger.isDebugEnabled()) {\r
297                 logger.debug("Skipping Cleanup phase ...");\r
298             }\r
299             return;\r
300         }\r
301         if (logger.isDebugEnabled()) {\r
302             logger.debug("Cleaning up temporary resources created for testing ...");\r
303         }\r
304         IntakeClient intakeClient = new IntakeClient();\r
305         // Note: Any non-success responses are ignored and not reported.\r
306         for (String resourceId : intakeIdsCreated) {\r
307             ClientResponse<Response> res = intakeClient.delete(resourceId);\r
308             res.releaseConnection();\r
309         }\r
310         // Delete persons before PersonAuth\r
311         OrgAuthorityClient personAuthClient = new OrgAuthorityClient();\r
312         for (String resourceId : orgIdsCreated) {\r
313             ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);\r
314             res.releaseConnection();\r
315         }\r
316         if (orgAuthCSID != null) {\r
317                 personAuthClient.delete(orgAuthCSID).releaseConnection();\r
318         }\r
319     }\r
320 \r
321     // ---------------------------------------------------------------\r
322     // Utility methods used by tests above\r
323     // ---------------------------------------------------------------\r
324     @Override\r
325     public String getServicePathComponent() {\r
326         return SERVICE_PATH_COMPONENT;\r
327     }\r
328 \r
329    private PoxPayloadOut createIntakeInstance(String entryNumber,\r
330                 String entryDate,\r
331                                 String currentOwner,\r
332                                 String depositor,\r
333                                 String conditionCheckerAssessor,\r
334                                 String insurer,\r
335                                 String Valuer ) {\r
336         IntakesCommon intake = new IntakesCommon();\r
337         intake.setEntryNumber(entryNumber);\r
338         intake.setEntryDate(entryDate);\r
339         intake.setCurrentOwner(currentOwner);\r
340         intake.setDepositor(depositor);\r
341         intake.setValuer(Valuer);\r
342 \r
343         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();\r
344         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();\r
345         checkersOrAssessors.add(conditionCheckerAssessor);\r
346         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);\r
347 \r
348         InsurerList insurerList = new InsurerList();\r
349         List<String> insurers = insurerList.getInsurer();\r
350         insurers.add(insurer);\r
351         intake.setInsurers(insurerList);\r
352 \r
353         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());\r
354         PayloadOutputPart commonPart =\r
355             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);\r
356         commonPart.setLabel(new IntakeClient().getCommonPartName());\r
357 \r
358         if(logger.isDebugEnabled()){\r
359             logger.debug("to be created, intake common");\r
360             logger.debug(objectAsXmlString(intake, IntakesCommon.class));\r
361         }\r
362 \r
363         return multipart;\r
364     }\r
365 }\r