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