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