]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7a4ecf7bb49156e180f09f349c4fa8b99b828054
[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.IntakesCommon;\r
40 //import org.collectionspace.services.intake.IntakesCommonList;\r
41 import org.collectionspace.services.jaxb.AbstractCommonList;\r
42 \r
43 import org.jboss.resteasy.client.ClientResponse;\r
44 \r
45 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
48 import org.testng.Assert;\r
49 import org.testng.annotations.AfterClass;\r
50 import org.testng.annotations.Test;\r
51 \r
52 import org.slf4j.Logger;\r
53 import org.slf4j.LoggerFactory;\r
54 \r
55 /**\r
56  * OrganizationAuthRefDocsTest, carries out tests against a\r
57  * deployed and running Organization Service.\r
58  *\r
59  * $LastChangedRevision: 1327 $\r
60  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $\r
61  */\r
62 public class OrganizationAuthRefDocsTest extends BaseServiceTest {\r
63 \r
64     private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();\r
65     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);\r
66 \r
67     // Instance variables specific to this test.\r
68     final String SERVICE_PATH_COMPONENT = "intakes";\r
69     final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";\r
70     private String knownIntakeId = null;\r
71     private List<String> intakeIdsCreated = new ArrayList<String>();\r
72     private List<String> orgIdsCreated = new ArrayList<String>();\r
73     private String orgAuthCSID = null; \r
74     private String currentOwnerOrgCSID = null; \r
75     private String currentOwnerRefName = null;\r
76     private String depositorRefName = null;\r
77     private String conditionCheckAssesorRefName = null;\r
78     private String insurerRefName = null;\r
79     private String fieldCollectorRefName = null;\r
80     private String valuerRefName = null;\r
81     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;\r
82 \r
83     /* (non-Javadoc)\r
84      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()\r
85      */\r
86     @Override\r
87     protected CollectionSpaceClient getClientInstance() {\r
88         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class\r
89     }\r
90     \r
91     /* (non-Javadoc)\r
92      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)\r
93      */\r
94     @Override\r
95         protected AbstractCommonList getAbstractCommonList(\r
96                         ClientResponse<AbstractCommonList> response) {\r
97         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class\r
98     }\r
99 \r
100     // ---------------------------------------------------------------\r
101     // CRUD tests : CREATE tests\r
102     // ---------------------------------------------------------------\r
103     // Success outcomes\r
104     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)\r
105     public void createIntakeWithAuthRefs(String testName) throws Exception {\r
106 \r
107         if (logger.isDebugEnabled()) {\r
108             logger.debug(testBanner(testName, CLASS_NAME));\r
109         }\r
110         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);\r
111 \r
112         // Submit the request to the service and store the response.\r
113         String identifier = createIdentifier();\r
114         \r
115         // Create all the organization refs and entities\r
116         createOrgRefs();\r
117 \r
118         IntakeClient intakeClient = new IntakeClient();\r
119         MultipartOutput multipart = createIntakeInstance(\r
120                 "entryNumber-" + identifier,\r
121                 "entryDate-" + identifier,\r
122                                                                 currentOwnerRefName,\r
123                                                                 depositorRefName,\r
124                                                                 conditionCheckAssesorRefName,\r
125                                                                 insurerRefName,\r
126                                                                 fieldCollectorRefName,\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         String authRefName = \r
169                 OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, false);\r
170         MultipartOutput multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(\r
171                         ORGANIZATION_AUTHORITY_NAME, authRefName, 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         currentOwnerRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
181                                                                 authRefName, "Olivier Owner", true);\r
182                                 currentOwnerOrgCSID = createOrganization("Olivier", "Owner", currentOwnerRefName);\r
183         orgIdsCreated.add(currentOwnerOrgCSID);\r
184         \r
185         depositorRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
186                                                                         authRefName, "Debbie Depositor", true);\r
187         orgIdsCreated.add(createOrganization("Debbie", "Depositor", depositorRefName));\r
188         \r
189         conditionCheckAssesorRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
190                                                                         authRefName, "Andrew Assessor", true);\r
191         orgIdsCreated.add(createOrganization("Andrew", "Assessor", conditionCheckAssesorRefName));\r
192         \r
193         insurerRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
194                                                                         authRefName, "Ingrid Insurer", true);\r
195         orgIdsCreated.add(createOrganization("Ingrid", "Insurer", insurerRefName));\r
196         \r
197         fieldCollectorRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
198                                                                         authRefName, "Connie Collector", true);\r
199         orgIdsCreated.add(createOrganization("Connie", "Collector", fieldCollectorRefName));\r
200         \r
201         valuerRefName = OrgAuthorityClientUtils.createOrganizationRefName(\r
202                                                                         authRefName, "Vince Valuer", true);\r
203         orgIdsCreated.add(createOrganization("Vince", "Valuer", valuerRefName));\r
204         \r
205 \r
206     }\r
207     \r
208     protected String createOrganization(String shortName, String longName, String refName ) {\r
209         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();\r
210         Map<String, String> orgInfo = new HashMap<String,String>();\r
211         orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);\r
212         orgInfo.put(OrganizationJAXBSchema.LONG_NAME, longName);\r
213         MultipartOutput multipart = \r
214                 OrgAuthorityClientUtils.createOrganizationInstance(orgAuthCSID, \r
215                                 refName, 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         boolean fFoundIntake = false;\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(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {\r
267                         fFoundIntake = true;\r
268                 }\r
269                 i++;\r
270             }\r
271             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");\r
272         }\r
273     }\r
274 \r
275 \r
276     // ---------------------------------------------------------------\r
277     // Cleanup of resources created during testing\r
278     // ---------------------------------------------------------------\r
279 \r
280     /**\r
281      * Deletes all resources created by tests, after all tests have been run.\r
282      *\r
283      * This cleanup method will always be run, even if one or more tests fail.\r
284      * For this reason, it attempts to remove all resources created\r
285      * at any point during testing, even if some of those resources\r
286      * may be expected to be deleted by certain tests.\r
287      */\r
288     @AfterClass(alwaysRun=true)\r
289     public void cleanUp() {\r
290         String noTest = System.getProperty("noTestCleanup");\r
291         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {\r
292             if (logger.isDebugEnabled()) {\r
293                 logger.debug("Skipping Cleanup phase ...");\r
294             }\r
295             return;\r
296         }\r
297         if (logger.isDebugEnabled()) {\r
298             logger.debug("Cleaning up temporary resources created for testing ...");\r
299         }\r
300         IntakeClient intakeClient = new IntakeClient();\r
301         // Note: Any non-success responses are ignored and not reported.\r
302         for (String resourceId : intakeIdsCreated) {\r
303             ClientResponse<Response> res = intakeClient.delete(resourceId);\r
304             res.releaseConnection();\r
305         }\r
306         // Delete persons before PersonAuth\r
307         OrgAuthorityClient personAuthClient = new OrgAuthorityClient();\r
308         for (String resourceId : orgIdsCreated) {\r
309             ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);\r
310             res.releaseConnection();\r
311         }\r
312         if (orgAuthCSID != null) {\r
313                 personAuthClient.delete(orgAuthCSID).releaseConnection();\r
314         }\r
315     }\r
316 \r
317     // ---------------------------------------------------------------\r
318     // Utility methods used by tests above\r
319     // ---------------------------------------------------------------\r
320     @Override\r
321     public String getServicePathComponent() {\r
322         return SERVICE_PATH_COMPONENT;\r
323     }\r
324 \r
325    private MultipartOutput createIntakeInstance(String entryNumber,\r
326                 String entryDate,\r
327                                 String currentOwner,\r
328                                 String depositor,\r
329                                 String conditionCheckAssesor,\r
330                                 String insurer,\r
331                                 String fieldCollector,\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.setConditionCheckAssesor(conditionCheckAssesor);\r
339         intake.setInsurer(insurer);\r
340         intake.setFieldCollector(fieldCollector);\r
341         intake.setValuer(Valuer);\r
342         MultipartOutput multipart = new MultipartOutput();\r
343         OutputPart commonPart =\r
344             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);\r
345         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());\r
346 \r
347         if(logger.isDebugEnabled()){\r
348             logger.debug("to be created, intake common");\r
349             logger.debug(objectAsXmlString(intake, IntakesCommon.class));\r
350         }\r
351 \r
352         return multipart;\r
353     }\r
354 }\r