]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e1761b457612d052126af18cbb2c07ff0615d5a9
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import javax.ws.rs.core.Response;
30
31 import org.collectionspace.services.PersonJAXBSchema;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.client.HeldintrustClient;
34 import org.collectionspace.services.client.PersonAuthorityClientUtils;
35 import org.collectionspace.services.client.PersonClient;
36 import org.collectionspace.services.client.PoxPayloadIn;
37 import org.collectionspace.services.client.PoxPayloadOut;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;
39 import org.collectionspace.services.heldintrust.HeldintrustsCommon;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.person.PersonTermGroup;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.testng.Assert;
45 import org.testng.annotations.AfterClass;
46 import org.testng.annotations.Test;
47
48 /**
49  * HitAuthRefsTest, carries out tests against a
50  * deployed and running Hit Service.
51  *
52  * $LastChangedRevision: 1327 $
53  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
54  */
55 public class HeldintrustAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
56
57     // Instance variables specific to this test.
58     final String SERVICE_PATH_COMPONENT = HeldintrustClient.SERVICE_PATH_COMPONENT;
59     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
60     private final String CLASS_NAME = HeldintrustAuthRefsTest.class.getName();
61     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
62
63     private final List<String> hitIdsCreated = new ArrayList<String>();
64     private final List<String> personIdsCreated = new ArrayList<String>();
65     private String personAuthCSID = null;
66     private String depositorContactRefName = null;
67     private String depositorRefName = null;
68     private String externalApprovalIndividualRefName = null;
69     private String correspondenceSenderRefName = null;
70     private String valuerRefName = null;
71
72     @Override
73     protected String getServiceName() {
74         throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
75     }
76
77     /* (non-Javadoc)
78      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79      */
80     @Override
81     protected CollectionSpaceClient getClientInstance() {
82         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
83     }
84
85     @Override
86     protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
87         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
88     }
89
90     /* (non-Javadoc)
91      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client
92      * .ClientResponse)
93      */
94     @Override
95     protected AbstractCommonList getCommonList(Response response) {
96         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
97     }
98
99     // ---------------------------------------------------------------
100     // CRUD tests : CREATE tests
101     // ---------------------------------------------------------------
102     // Success outcomes
103     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
104     public void createWithAuthRefs(String testName) throws Exception {
105         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
106
107         // Submit the request to the service and store the response.
108         String identifier = createIdentifier();
109
110         // Create all the person refs and entities
111         createPersonRefs();
112
113         // Submit the request to the service and store the response.
114         HeldintrustClient heldInTrustClient = new HeldintrustClient();
115         PoxPayloadOut multipart = createHitInstance(
116             "entryNumber-" + identifier,
117             depositorContactRefName,
118             depositorRefName,
119             externalApprovalIndividualRefName,
120             correspondenceSenderRefName);
121
122         String newId;
123         Response res = heldInTrustClient.create(multipart);
124         try {
125             int statusCode = res.getStatus();
126             // Check the status code of the response: does it match
127             // the expected response(s)?
128             //
129             // Specifically:
130             // Does it fall within the set of valid status codes?
131             // Does it exactly match the expected status code?
132             logger.debug("{}: status = {}", testName, statusCode);
133             Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
134                               invalidStatusCodeMessage(testRequestType, statusCode));
135             Assert.assertEquals(statusCode, testExpectedStatusCode);
136             newId = extractId(res);
137         } finally {
138             res.close();
139         }
140
141         // Store the ID returned from the first resource created
142         // for additional tests below.
143         if (knownResourceId == null) {
144             knownResourceId = newId;
145             logger.debug("{}: knownResourceId={}", testName, knownResourceId);
146         }
147
148         // Store the IDs from every resource created by tests,
149         // so they can be deleted after tests have been run.
150         hitIdsCreated.add(newId);
151     }
152
153     protected void createPersonRefs() throws Exception {
154         //
155         // First, create a new person authority
156         //
157         PersonClient personAuthClient = new PersonClient();
158         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
159             PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
160         Response res = personAuthClient.create(multipart);
161         try {
162             int statusCode = res.getStatus();
163             Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
164                               invalidStatusCodeMessage(testRequestType, statusCode));
165             Assert.assertEquals(statusCode, STATUS_CREATED);
166             personAuthCSID = extractId(res);
167         } finally {
168             res.close();
169         }
170
171         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
172         String csid = createPerson("Olivier", "Contact", "olivierContact", authRefName);
173         depositorContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
174         personIdsCreated.add(csid);
175
176         csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
177         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
178         personIdsCreated.add(csid);
179
180         csid = createPerson("Andrew", "ApprovalIndividual", "andrewApprovalIndividual", authRefName);
181         externalApprovalIndividualRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
182         personIdsCreated.add(csid);
183
184         csid = createPerson("Ingrid", "Sender", "ingridSender", authRefName);
185         correspondenceSenderRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
186         personIdsCreated.add(csid);
187
188         csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
189         valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
190         personIdsCreated.add(csid);
191     }
192
193     protected String createPerson(String firstName, String surName, String shortId, String authRefName)
194         throws Exception {
195         String result;
196
197         PersonClient personAuthClient = new PersonClient();
198         Map<String, String> personInfo = new HashMap<String, String>();
199         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
200         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
201         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
202         List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
203         PersonTermGroup term = new PersonTermGroup();
204         String termName = firstName + " " + surName;
205         term.setTermDisplayName(termName);
206         term.setTermName(termName);
207         personTerms.add(term);
208         PoxPayloadOut multipart =
209             PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
210                                                             authRefName, personInfo, personTerms,
211                                                             personAuthClient.getItemCommonPartName());
212
213         Response res = personAuthClient.createItem(personAuthCSID, multipart);
214         try {
215             int statusCode = res.getStatus();
216             Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
217                               invalidStatusCodeMessage(testRequestType, statusCode));
218             Assert.assertEquals(statusCode, STATUS_CREATED);
219             result = extractId(res);
220         } finally {
221             res.close();
222         }
223
224         return result;
225     }
226
227     // Success outcomes
228     @Test(dataProvider = "testName", dependsOnMethods = {"createWithAuthRefs"})
229     public void readAndCheckAuthRefs(String testName) throws Exception {
230         // Perform setup.
231         testSetup(STATUS_OK, ServiceRequestType.READ);
232
233         // Submit the request to the service and store the response.
234         HeldintrustClient heldInTrustClient = new HeldintrustClient();
235         Response res = heldInTrustClient.read(knownResourceId);
236         try {
237             assertStatusCode(res, testName);
238             PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
239             HeldintrustsCommon hit = (HeldintrustsCommon) extractPart(input, heldInTrustClient.getCommonPartName(),
240                                                                       HeldintrustsCommon.class);
241             Assert.assertNotNull(hit);
242             // Check a couple of fields
243         } finally {
244             if (res != null) {
245                 res.close();
246             }
247         }
248
249         // Get the auth refs and check them
250         res = heldInTrustClient.getAuthorityRefs(knownResourceId);
251         AuthorityRefList list;
252         try {
253             assertStatusCode(res, testName);
254             list = res.readEntity(AuthorityRefList.class);
255         } finally {
256             if (res != null) {
257                 res.close();
258             }
259         }
260
261         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
262         int numAuthRefsFound = items.size();
263         logger.debug("Expected {} authority references, found {}", personIdsCreated.size(), numAuthRefsFound);
264
265         // output additional data about list members for debugging.
266         int i = 0;
267         for (AuthorityRefList.AuthorityRefItem item : items) {
268             logger.debug("{}: list-item[{}] Field:{}={}{}", testName, i, item.getSourceField(),
269                          item.getAuthDisplayName(), item.getItemDisplayName());
270             logger.debug("{}: list-item[{}] refName={}", testName, i, item.getRefName());
271             logger.debug("{}: list-item[{}] URI={}", testName, i, item.getUri());
272             i++;
273         }
274
275         //
276         // Ensure we got the correct number of authRefs
277         Assert.assertEquals(numAuthRefsFound, personIdsCreated.size(),
278                             "Did not find all expected authority references! " + "Expected " + personIdsCreated.size() +
279                             ", found " + numAuthRefsFound);
280     }
281
282
283     // ---------------------------------------------------------------
284     // Cleanup of resources created during testing
285     // ---------------------------------------------------------------
286
287     /**
288      * Deletes all resources created by tests, after all tests have been run.
289      *
290      * This cleanup method will always be run, even if one or more tests fail.
291      * For this reason, it attempts to remove all resources created
292      * at any point during testing, even if some of those resources
293      * may be expected to be deleted by certain tests.
294      *
295      * @throws Exception
296      */
297     @AfterClass(alwaysRun = true)
298     public void cleanUp() throws Exception {
299         String noTest = System.getProperty("noTestCleanup");
300         if (Boolean.parseBoolean(noTest)) {
301             logger.debug("Skipping Cleanup phase ...");
302             return;
303         }
304
305         logger.debug("Cleaning up temporary resources created for testing ...");
306         HeldintrustClient heldInTrustClient = new HeldintrustClient();
307         // Note: Any non-success responses are ignored and not reported.
308         for (String resourceId : hitIdsCreated) {
309             heldInTrustClient.delete(resourceId).close();
310         }
311         //
312         // Delete all the person records then the parent resource
313         PersonClient personAuthClient = new PersonClient();
314         for (String resourceId : personIdsCreated) {
315             personAuthClient.deleteItem(personAuthCSID, resourceId).close();
316         }
317         personAuthClient.delete(personAuthCSID).close();
318     }
319
320     // ---------------------------------------------------------------
321     // Utility methods used by tests above
322     // ---------------------------------------------------------------
323     @Override
324     public String getServicePathComponent() {
325         return SERVICE_PATH_COMPONENT;
326     }
327
328     private PoxPayloadOut createHitInstance(String heldInTrustNumber,
329                                             String depositorContact,
330                                             String depositor,
331                                             String externalApprovalIndividual,
332                                             String correspondenceSender) throws Exception {
333         HeldintrustsCommon hit =
334             HeldintrustClientTestUtil.createHitInstance(heldInTrustNumber, depositorContact, depositor,
335                                                         externalApprovalIndividual, correspondenceSender);
336         hit.setHeldInTrustNumber(heldInTrustNumber);
337
338         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
339         multipart.addPart(new HeldintrustClient().getCommonPartName(), hit);
340
341         logger.debug("to be created, hit common");
342         logger.debug("{}", objectAsXmlString(hit, HeldintrustsCommon.class));
343
344         return multipart;
345     }
346 }