]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
51b062e451f14e3682eadbce30b0349c692dd1e4
[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.intake.ConditionCheckerOrAssessorList;\r
42 import org.collectionspace.services.intake.IntakesCommon;\r
43 import org.collectionspace.services.intake.InsurerList;\r
44 import org.collectionspace.services.jaxb.AbstractCommonList;\r
45 \r
46 import org.jboss.resteasy.client.ClientResponse;\r
47 \r
48 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
49 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
50 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
51 import org.testng.Assert;\r
52 import org.testng.annotations.AfterClass;\r
53 import org.testng.annotations.Test;\r
54 \r
55 import org.slf4j.Logger;\r
56 import org.slf4j.LoggerFactory;\r
57 \r
58 /**\r
59  * OrganizationAuthRefDocsTest, carries out tests against a\r
60  * deployed and running Organization Service.\r
61  *\r
62  * $LastChangedRevision: 1327 $\r
63  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $\r
64  */\r
65 public class OrganizationAuthRefDocsTest extends BaseServiceTest {\r
66 \r
67     private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();\r
68     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);\r
69 \r
70     // Instance variables specific to this test.\r
71     final String SERVICE_PATH_COMPONENT = "intakes";\r
72     final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";\r
73     private String knownIntakeId = null;\r
74     private List<String> intakeIdsCreated = new ArrayList<String>();\r
75     private List<String> orgIdsCreated = new ArrayList<String>();\r
76     private String orgAuthCSID = null; \r
77     //private String orgAuthRefName = null; \r
78     private String currentOwnerOrgCSID = null; \r
79     private String currentOwnerRefName = null;\r
80     private String depositorRefName = null;\r
81     private String conditionCheckerAssessorRefName = null;\r
82     private String insurerRefName = null;\r
83     private String valuerRefName = null;\r
84     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;\r
85 \r
86         @Override\r
87         protected String getServiceName() {\r
88                 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498\r
89         }\r
90     \r
91     /* (non-Javadoc)\r
92      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()\r
93      */\r
94     @Override\r
95     protected CollectionSpaceClient getClientInstance() {\r
96         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class\r
97     }\r
98     \r
99     /* (non-Javadoc)\r
100      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)\r
101      */\r
102     @Override\r
103         protected AbstractCommonList getAbstractCommonList(\r
104                         ClientResponse<AbstractCommonList> response) {\r
105         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class\r
106     }\r
107 \r
108     // ---------------------------------------------------------------\r
109     // CRUD tests : CREATE tests\r
110     // ---------------------------------------------------------------\r
111     // Success outcomes\r
112     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)\r
113     public void createIntakeWithAuthRefs(String testName) throws Exception {\r
114 \r
115         if (logger.isDebugEnabled()) {\r
116             logger.debug(testBanner(testName, CLASS_NAME));\r
117         }\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                 "entryDate-" + identifier,\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(REQUEST_TYPE.isValidStatusCode(statusCode),\r
151                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
152                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);\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(REQUEST_TYPE.isValidStatusCode(statusCode),\r
184                 invalidStatusCodeMessage(REQUEST_TYPE, 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         orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);\r
219         orgInfo.put(OrganizationJAXBSchema.LONG_NAME, longName);\r
220         PoxPayloadOut multipart = \r
221                 OrgAuthorityClientUtils.createOrganizationInstance(null, //orgAuthRefName,\r
222                                 orgInfo, orgAuthClient.getItemCommonPartName());\r
223         ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);\r
224         int statusCode = res.getStatus();\r
225 \r
226         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
227                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
228         Assert.assertEquals(statusCode, STATUS_CREATED);\r
229         return extractId(res);\r
230     }\r
231 \r
232     // Success outcomes\r
233     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,\r
234         dependsOnMethods = {"createIntakeWithAuthRefs"})\r
235     public void readAndCheckAuthRefDocs(String testName) throws Exception {\r
236 \r
237         if (logger.isDebugEnabled()) {\r
238             logger.debug(testBanner(testName, CLASS_NAME));\r
239         }\r
240         // Perform setup.\r
241         testSetup(STATUS_OK, ServiceRequestType.READ);\r
242         \r
243         // Get the auth ref docs and check them\r
244        OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
245        ClientResponse<AuthorityRefDocList> refDocListResp =\r
246                 orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);\r
247 \r
248         int statusCode = refDocListResp.getStatus();\r
249 \r
250         if(logger.isDebugEnabled()){\r
251             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);\r
252         }\r
253         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),\r
254                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
255         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);\r
256         AuthorityRefDocList list = refDocListResp.getEntity();\r
257 \r
258         // Optionally output additional data about list members for debugging.\r
259         boolean iterateThroughList = true;\r
260         int nIntakesFound = 0;\r
261         if(iterateThroughList && logger.isDebugEnabled()){\r
262             List<AuthorityRefDocList.AuthorityRefDocItem> items =\r
263                     list.getAuthorityRefDocItem();\r
264             int i = 0;\r
265             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);\r
266             for(AuthorityRefDocList.AuthorityRefDocItem item : items){\r
267                 logger.debug(testName + ": list-item[" + i + "] " +\r
268                                 item.getDocType() + "(" +\r
269                                 item.getDocId() + ") Name:[" +\r
270                                 item.getDocName() + "] Number:[" +\r
271                                 item.getDocNumber() + "] in field:[" +\r
272                                 item.getSourceField() + "]");\r
273                 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {\r
274                         nIntakesFound++;\r
275                 }\r
276                 i++;\r
277             }\r
278             //\r
279             Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");\r
280         }\r
281     }\r
282 \r
283 \r
284     // ---------------------------------------------------------------\r
285     // Cleanup of resources created during testing\r
286     // ---------------------------------------------------------------\r
287 \r
288     /**\r
289      * Deletes all resources created by tests, after all tests have been run.\r
290      *\r
291      * This cleanup method will always be run, even if one or more tests fail.\r
292      * For this reason, it attempts to remove all resources created\r
293      * at any point during testing, even if some of those resources\r
294      * may be expected to be deleted by certain tests.\r
295      */\r
296     @AfterClass(alwaysRun=true)\r
297     public void cleanUp() {\r
298         String noTest = System.getProperty("noTestCleanup");\r
299         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {\r
300             if (logger.isDebugEnabled()) {\r
301                 logger.debug("Skipping Cleanup phase ...");\r
302             }\r
303             return;\r
304         }\r
305         if (logger.isDebugEnabled()) {\r
306             logger.debug("Cleaning up temporary resources created for testing ...");\r
307         }\r
308         IntakeClient intakeClient = new IntakeClient();\r
309         // Note: Any non-success responses are ignored and not reported.\r
310         for (String resourceId : intakeIdsCreated) {\r
311             ClientResponse<Response> res = intakeClient.delete(resourceId);\r
312             res.releaseConnection();\r
313         }\r
314         // Delete persons before PersonAuth\r
315         OrgAuthorityClient personAuthClient = new OrgAuthorityClient();\r
316         for (String resourceId : orgIdsCreated) {\r
317             ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);\r
318             res.releaseConnection();\r
319         }\r
320         if (orgAuthCSID != null) {\r
321                 personAuthClient.delete(orgAuthCSID).releaseConnection();\r
322         }\r
323     }\r
324 \r
325     // ---------------------------------------------------------------\r
326     // Utility methods used by tests above\r
327     // ---------------------------------------------------------------\r
328     @Override\r
329     public String getServicePathComponent() {\r
330         return SERVICE_PATH_COMPONENT;\r
331     }\r
332 \r
333    private PoxPayloadOut createIntakeInstance(String entryNumber,\r
334                 String entryDate,\r
335                                 String currentOwner,\r
336                                 String depositor,\r
337                                 String conditionCheckerAssessor,\r
338                                 String insurer,\r
339                                 String Valuer ) {\r
340         IntakesCommon intake = new IntakesCommon();\r
341         intake.setEntryNumber(entryNumber);\r
342         intake.setEntryDate(entryDate);\r
343         intake.setCurrentOwner(currentOwner);\r
344         intake.setDepositor(depositor);\r
345         intake.setValuer(Valuer);\r
346 \r
347         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();\r
348         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();\r
349         checkersOrAssessors.add(conditionCheckerAssessor);\r
350         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);\r
351 \r
352         InsurerList insurerList = new InsurerList();\r
353         List<String> insurers = insurerList.getInsurer();\r
354         insurers.add(insurer);\r
355         intake.setInsurers(insurerList);\r
356 \r
357         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());\r
358         PayloadOutputPart commonPart =\r
359             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);\r
360         commonPart.setLabel(new IntakeClient().getCommonPartName());\r
361 \r
362         if(logger.isDebugEnabled()){\r
363             logger.debug("to be created, intake common");\r
364             logger.debug(objectAsXmlString(intake, IntakesCommon.class));\r
365         }\r
366 \r
367         return multipart;\r
368     }\r
369 }\r