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