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