]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ed4e8c4e4f85d8c1cef899ff7e4faf695800cff1
[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
30 import javax.ws.rs.core.Response;
31
32 import org.collectionspace.services.PersonJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.ObjectExitClient;
35 import org.collectionspace.services.client.PayloadOutputPart;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.client.PoxPayloadIn;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
41 import org.collectionspace.services.common.authorityref.AuthorityRefList;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.collectionspace.services.objectexit.StructuredDateGroup;
44 import org.collectionspace.services.objectexit.ObjectexitCommon;
45 import org.collectionspace.services.person.PersonTermGroup;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * ObjectExitAuthRefsTest, carries out Authority References tests against a deployed and running ObjectExit (aka Loans Out) Service.
54  * $LastChangedRevision:  $
55  * $LastChangedDate:  $
56  */
57 public class ObjectExitAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
58
59     private final String CLASS_NAME = ObjectExitAuthRefsTest.class.getName();
60     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
61     final String PERSON_AUTHORITY_NAME = "ObjectexitPersonAuth";
62     private List<String> objectexitIdsCreated = new ArrayList<String>();
63     private List<String> personIdsCreated = new ArrayList<String>();
64     private String personAuthCSID = null;
65     private String depositorRefName = null;
66     private StructuredDateGroup exitDateGroup = new StructuredDateGroup();
67     private String exitNumber = null;
68     private final static String CURRENT_DATE_UTC =
69             GregorianCalendarDateTimeUtils.currentDateUTC();
70
71     @Override
72         public String getServicePathComponent() {
73                 return ObjectExitClient.SERVICE_PATH_COMPONENT;
74         }
75
76         @Override
77         protected String getServiceName() {
78                 return ObjectExitClient.SERVICE_NAME;
79         }
80     
81     @Override
82     protected CollectionSpaceClient getClientInstance() {
83         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
84     }
85
86         @Override
87         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
88         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
89         }
90
91         @Override
92     protected AbstractCommonList getCommonList(Response response) {
93         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
94     }
95
96     private PoxPayloadOut createObjectExitInstance(String depositorRefName, String exitNumber, String exitDateDisplayDate) throws Exception {
97         this.exitNumber = exitNumber;
98         this.depositorRefName = depositorRefName;
99         this.exitDateGroup.setDateDisplayDate(exitDateDisplayDate);
100         this.exitDateGroup.setDateDisplayDate(exitDateDisplayDate);
101         
102         ObjectexitCommon objectexit = new ObjectexitCommon();
103         StructuredDateGroup oeExitDateGroup = new StructuredDateGroup();
104
105         objectexit.setDepositor(depositorRefName);
106         objectexit.setExitNumber(exitNumber);
107         oeExitDateGroup.setDateDisplayDate(exitDateDisplayDate);
108         objectexit.setExitDateGroup(oeExitDateGroup);
109
110         PoxPayloadOut multipart = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
111         PayloadOutputPart commonPart = multipart.addPart(new ObjectExitClient().getCommonPartName(),
112                         objectexit);
113         logger.debug("to be created, objectexit common: " + objectAsXmlString(objectexit, ObjectexitCommon.class));
114         return multipart;
115     }
116
117     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
118     public void createWithAuthRefs(String testName) throws Exception {
119         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
120         String identifier = createIdentifier(); // Submit the request to the service and store the response.
121         createPersonRefs();// Create all the person refs and entities
122         // Create a new Loans In resource. One or more fields in this resource will be PersonAuthority
123         //    references, and will refer to Person resources by their refNames.
124         ObjectExitClient objectexitClient = new ObjectExitClient();
125         PoxPayloadOut multipart = createObjectExitInstance(depositorRefName,
126                 "exitNumber-" + identifier, CURRENT_DATE_UTC);
127         Response res = objectexitClient.create(multipart);
128         String newId = null;
129         try {
130                 assertStatusCode(res, testName);
131                 newId = extractId(res);
132                 Assert.assertNotNull(newId, "Could not create a new ObjectExit record.");
133                 if (knownResourceId == null) {// Store the ID returned from the first resource created for additional tests below.
134                     knownResourceId = newId;
135                 }
136         } finally {
137                 if (res != null) {
138                 res.close();
139             }
140         }
141         
142         objectexitIdsCreated.add(newId);// Store the IDs from every resource created; delete on cleanup
143     }
144
145     protected void createPersonRefs() throws Exception {
146         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
147         // Create a temporary PersonAuthority resource, and its corresponding refName by which it can be identified.
148         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
149                         PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
150         Response res = personAuthClient.create(multipart);
151         try {
152                 assertStatusCode(res, "createPersonRefs (not a surefire test)");
153                 personAuthCSID = extractId(res);
154         } finally {
155                 if (res != null) {
156                 res.close();
157             }
158         }
159         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
160         // Create temporary Person resources, and their corresponding refNames by which they can be identified.
161         String csid = "";
162
163         csid = createPerson("Owen the Cur", "Owner", "owenCurOwner", authRefName);
164         personIdsCreated.add(csid);
165         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
166
167         csid = createPerson("Davenport", "Depositor", "davenportDepositor", authRefName);
168         personIdsCreated.add(csid);
169         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
170     }
171
172     protected String createPerson(String firstName, String surName, String shortId, String authRefName) throws Exception {
173         String result = null;
174         
175         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
176         Map<String, String> personInfo = new HashMap<String, String>();
177         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
178         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
179         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
180         List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
181         PersonTermGroup term = new PersonTermGroup();
182         String termName = firstName + " " + surName;
183         term.setTermDisplayName(termName);
184         term.setTermName(termName);
185         personTerms.add(term);
186         PoxPayloadOut multipart =
187                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
188                     authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
189         Response res = personAuthClient.createItem(personAuthCSID, multipart);
190         try {
191                 assertStatusCode(res, "createPerson (not a surefire test)");
192                 result = extractId(res);
193         } finally {
194                 if (res != null) {
195                 res.close();
196             }
197         }
198         
199         return result;
200     }
201
202     @Test(dataProvider = "testName",
203                 dependsOnMethods = {"createWithAuthRefs"})
204     public void readAndCheckAuthRefs(String testName) throws Exception {
205         testSetup(STATUS_OK, ServiceRequestType.READ);
206         ObjectExitClient objectexitClient = new ObjectExitClient();
207         Response res = objectexitClient.read(knownResourceId);
208         ObjectexitCommon objectexit = null;
209         try {
210                 assertStatusCode(res, testName);
211                 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
212                 objectexit = (ObjectexitCommon) extractPart(input, objectexitClient.getCommonPartName(), ObjectexitCommon.class);
213                 Assert.assertNotNull(objectexit);
214                 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));
215         } finally {
216                 if (res != null) {
217                 res.close();
218             }
219         }
220
221         // Check a couple of fields
222         Assert.assertEquals(objectexit.getDepositor(), depositorRefName);
223         Assert.assertEquals(objectexit.getExitNumber(), exitNumber);
224
225         // Get the auth refs and check them
226         Response res2 = objectexitClient.getAuthorityRefs(knownResourceId);
227         AuthorityRefList list = null;
228         try {
229                 assertStatusCode(res2, testName);
230                 list = res2.readEntity(AuthorityRefList.class);
231         } finally {
232                 if (res2 != null) {
233                         res2.close();
234             }
235         }
236         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
237         int numAuthRefsFound = items.size();
238         logger.debug("Authority references, found " + numAuthRefsFound);
239         //Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
240         //                    "Did not find all expected authority references! " +
241         //                    "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
242         if (logger.isDebugEnabled()) {
243             int i = 0;
244             for (AuthorityRefList.AuthorityRefItem item : items) {
245                 logger.debug(testName + ": list-item[" + i + "] Field:" + item.getSourceField() + "= " + item.getAuthDisplayName() + item.getItemDisplayName());
246                 logger.debug(testName + ": list-item[" + i + "] refName=" + item.getRefName());
247                 logger.debug(testName + ": list-item[" + i + "] URI=" + item.getUri());
248                 i++;
249             }
250         }
251     }
252
253     /**
254      * Deletes all resources created by tests, after all tests have been run.
255      * <p/>
256      * This cleanup method will always be run, even if one or more tests fail.
257      * For this reason, it attempts to remove all resources created
258      * at any point during testing, even if some of those resources
259      * may be expected to be deleted by certain tests.
260      * @throws Exception 
261      */
262     @AfterClass(alwaysRun = true)
263     public void cleanUp() throws Exception {
264         String noTest = System.getProperty("noTestCleanup");
265         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
266             logger.debug("Skipping Cleanup phase ...");
267             return;
268         }
269         logger.debug("Cleaning up temporary resources created for testing ...");
270         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
271         // Delete Person resource(s) (before PersonAuthority resources).
272         for (String resourceId : personIdsCreated) {
273             // Note: Any non-success responses are ignored and not reported.
274             personAuthClient.deleteItem(personAuthCSID, resourceId).close();
275         }
276         // Delete PersonAuthority resource(s).
277         // Note: Any non-success response is ignored and not reported.
278         if (personAuthCSID != null) {
279             personAuthClient.delete(personAuthCSID).close();
280             // Delete Loans In resource(s).
281             ObjectExitClient objectexitClient = new ObjectExitClient();
282             for (String resourceId : objectexitIdsCreated) {
283                 // Note: Any non-success responses are ignored and not reported.
284                 objectexitClient.delete(resourceId).close();
285             }
286         }
287     }
288
289         @Override
290         protected Class<AbstractCommonList> getCommonListType() {
291                 // TODO Auto-generated method stub
292                 return null;
293         }
294
295 }