]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
497389fece17f20a1d97b66d3303a4dcb79f7f68
[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<AbstractCommonList> {\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 getCommonList(\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         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);\r
118 \r
119         // Submit the request to the service and store the response.\r
120         String identifier = createIdentifier();\r
121         \r
122         // Create all the organization refs and entities\r
123         createOrgRefs();\r
124 \r
125         IntakeClient intakeClient = new IntakeClient();\r
126         PoxPayloadOut multipart = createIntakeInstance(\r
127                 "entryNumber-" + identifier,\r
128                 CURRENT_DATE_UTC,\r
129                 currentOwnerRefName,\r
130                 // Use currentOwnerRefName twice to test fix for CSPACE-2863\r
131                 currentOwnerRefName,    //depositorRefName,\r
132                 conditionCheckerAssessorRefName,\r
133                 insurerRefName,\r
134                 valuerRefName );\r
135 \r
136         ClientResponse<Response> res = intakeClient.create(multipart);\r
137         try {\r
138                 int statusCode = res.getStatus();\r
139         \r
140                 // Check the status code of the response: does it match\r
141                 // the expected response(s)?\r
142                 //\r
143                 // Specifically:\r
144                 // Does it fall within the set of valid status codes?\r
145                 // Does it exactly match the expected status code?\r
146                 if(logger.isDebugEnabled()){\r
147                     logger.debug(testName + ": status = " + statusCode);\r
148                 }\r
149                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),\r
150                         invalidStatusCodeMessage(testRequestType, statusCode));\r
151                 Assert.assertEquals(statusCode, testExpectedStatusCode);\r
152         } finally {\r
153                 res.releaseConnection();\r
154         }\r
155 \r
156         // Store the ID returned from the first resource created\r
157         // for additional tests below.\r
158         if (knownIntakeId == null){\r
159             knownIntakeId = extractId(res);\r
160             if (logger.isDebugEnabled()) {\r
161                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);\r
162             }\r
163         }\r
164         \r
165         // Store the IDs from every resource created by tests,\r
166         // so they can be deleted after tests have been run.\r
167         intakeIdsCreated.add(extractId(res));\r
168     }\r
169     \r
170     /**\r
171      * Creates the organization refs.\r
172      */\r
173     protected void createOrgRefs(){\r
174         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
175         //orgAuthRefName = \r
176         //      OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, null);\r
177         PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(\r
178                         ORGANIZATION_AUTHORITY_NAME, ORGANIZATION_AUTHORITY_NAME, orgAuthClient.getCommonPartName());\r
179         ClientResponse<Response> res = orgAuthClient.create(multipart);\r
180         int statusCode = res.getStatus();\r
181 \r
182         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),\r
183                 invalidStatusCodeMessage(testRequestType, statusCode));\r
184         Assert.assertEquals(statusCode, STATUS_CREATED);\r
185         orgAuthCSID = extractId(res);\r
186         \r
187                 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");\r
188         orgIdsCreated.add(currentOwnerOrgCSID);\r
189         currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);\r
190         \r
191                 String newOrgCSID =\r
192                         createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");\r
193         depositorRefName = \r
194                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);\r
195         orgIdsCreated.add(newOrgCSID);\r
196         \r
197                 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");\r
198                 conditionCheckerAssessorRefName = \r
199                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);\r
200         orgIdsCreated.add(newOrgCSID);\r
201         \r
202                 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");\r
203                 insurerRefName = \r
204                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);\r
205         orgIdsCreated.add(newOrgCSID);\r
206         \r
207                 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");\r
208                 valuerRefName = \r
209                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);\r
210         orgIdsCreated.add(newOrgCSID);\r
211     }\r
212 \r
213     protected String createOrganization(String shortId, String shortName, String longName) {\r
214         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
215         Map<String, String> orgInfo = new HashMap<String,String>();\r
216         orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);\r
217         orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);\r
218         orgInfo.put(OrganizationJAXBSchema.LONG_NAME, longName);\r
219         PoxPayloadOut multipart = \r
220                 OrgAuthorityClientUtils.createOrganizationInstance(null, //orgAuthRefName,\r
221                                 orgInfo, orgAuthClient.getItemCommonPartName());\r
222         ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);\r
223         int statusCode = res.getStatus();\r
224 \r
225         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),\r
226                 invalidStatusCodeMessage(testRequestType, statusCode));\r
227         Assert.assertEquals(statusCode, STATUS_CREATED);\r
228         return extractId(res);\r
229     }\r
230 \r
231     // Success outcomes\r
232     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,\r
233         dependsOnMethods = {"createIntakeWithAuthRefs"})\r
234     public void readAndCheckAuthRefDocs(String testName) throws Exception {\r
235         // Perform setup.\r
236         testSetup(STATUS_OK, ServiceRequestType.READ);\r
237         \r
238         // Get the auth ref docs and check them\r
239        OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
240        ClientResponse<AuthorityRefDocList> refDocListResp =\r
241                 orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);\r
242        AuthorityRefDocList list = null;\r
243        try {\r
244            assertStatusCode(refDocListResp, testName);\r
245            list = refDocListResp.getEntity();\r
246            Assert.assertNotNull(list);\r
247        } finally {\r
248            if (refDocListResp != null) {\r
249                    refDocListResp.releaseConnection();\r
250            }\r
251        }\r
252 \r
253         // Optionally output additional data about list members for debugging.\r
254         boolean iterateThroughList = true;\r
255         int nIntakesFound = 0;\r
256         if(iterateThroughList && logger.isDebugEnabled()){\r
257             List<AuthorityRefDocList.AuthorityRefDocItem> items =\r
258                     list.getAuthorityRefDocItem();\r
259             int i = 0;\r
260             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);\r
261             for(AuthorityRefDocList.AuthorityRefDocItem item : items){\r
262                 logger.debug(testName + ": list-item[" + i + "] " +\r
263                                 item.getDocType() + "(" +\r
264                                 item.getDocId() + ") Name:[" +\r
265                                 item.getDocName() + "] Number:[" +\r
266                                 item.getDocNumber() + "] in field:[" +\r
267                                 item.getSourceField() + "]");\r
268                 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {\r
269                         nIntakesFound++;\r
270                 }\r
271                 i++;\r
272             }\r
273             //\r
274             Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");\r
275         }\r
276     }\r
277 \r
278 \r
279     // ---------------------------------------------------------------\r
280     // Cleanup of resources created during testing\r
281     // ---------------------------------------------------------------\r
282 \r
283     /**\r
284      * Deletes all resources created by tests, after all tests have been run.\r
285      *\r
286      * This cleanup method will always be run, even if one or more tests fail.\r
287      * For this reason, it attempts to remove all resources created\r
288      * at any point during testing, even if some of those resources\r
289      * may be expected to be deleted by certain tests.\r
290      */\r
291     @AfterClass(alwaysRun=true)\r
292     public void cleanUp() {\r
293         String noTest = System.getProperty("noTestCleanup");\r
294         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {\r
295             if (logger.isDebugEnabled()) {\r
296                 logger.debug("Skipping Cleanup phase ...");\r
297             }\r
298             return;\r
299         }\r
300         if (logger.isDebugEnabled()) {\r
301             logger.debug("Cleaning up temporary resources created for testing ...");\r
302         }\r
303         IntakeClient intakeClient = new IntakeClient();\r
304         // Note: Any non-success responses are ignored and not reported.\r
305         for (String resourceId : intakeIdsCreated) {\r
306             ClientResponse<Response> res = intakeClient.delete(resourceId);\r
307             res.releaseConnection();\r
308         }\r
309         // Delete persons before PersonAuth\r
310         OrgAuthorityClient personAuthClient = new OrgAuthorityClient();\r
311         for (String resourceId : orgIdsCreated) {\r
312             ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);\r
313             res.releaseConnection();\r
314         }\r
315         if (orgAuthCSID != null) {\r
316                 personAuthClient.delete(orgAuthCSID).releaseConnection();\r
317         }\r
318     }\r
319 \r
320     // ---------------------------------------------------------------\r
321     // Utility methods used by tests above\r
322     // ---------------------------------------------------------------\r
323     @Override\r
324     public String getServicePathComponent() {\r
325         return SERVICE_PATH_COMPONENT;\r
326     }\r
327 \r
328    private PoxPayloadOut createIntakeInstance(String entryNumber,\r
329                 String entryDate,\r
330                                 String currentOwner,\r
331                                 String depositor,\r
332                                 String conditionCheckerAssessor,\r
333                                 String insurer,\r
334                                 String Valuer ) {\r
335         IntakesCommon intake = new IntakesCommon();\r
336         intake.setEntryNumber(entryNumber);\r
337         intake.setEntryDate(entryDate);\r
338         intake.setCurrentOwner(currentOwner);\r
339         intake.setDepositor(depositor);\r
340         intake.setValuer(Valuer);\r
341 \r
342         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();\r
343         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();\r
344         checkersOrAssessors.add(conditionCheckerAssessor);\r
345         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);\r
346 \r
347         InsurerList insurerList = new InsurerList();\r
348         List<String> insurers = insurerList.getInsurer();\r
349         insurers.add(insurer);\r
350         intake.setInsurers(insurerList);\r
351 \r
352         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());\r
353         PayloadOutputPart commonPart =\r
354             multipart.addPart(new IntakeClient().getCommonPartName(), intake);\r
355 \r
356         if(logger.isDebugEnabled()){\r
357             logger.debug("to be created, intake common");\r
358             logger.debug(objectAsXmlString(intake, IntakesCommon.class));\r
359         }\r
360 \r
361         return multipart;\r
362     }\r
363 }\r