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