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