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