]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d9e4f78d04e51cecaf4d1c2d7b673a5ddbe35620
[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.LoanoutClient;
35 import org.collectionspace.services.client.PersonAuthorityClient;
36 import org.collectionspace.services.client.PersonAuthorityClientUtils;
37 import org.collectionspace.services.common.authorityref.AuthorityRefList;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList.AuthorityRefItem;
39 import org.collectionspace.services.loanout.LoansoutCommon;
40 import org.collectionspace.services.loanout.LoansoutCommonList;
41
42 import org.jboss.resteasy.client.ClientResponse;
43
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
50
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * LoanoutAuthRefsTest, carries out Authority References tests against a
56  * deployed and running Loanout (aka Loans Out) Service.
57  *
58  * $LastChangedRevision: 1327 $
59  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60  */
61 public class LoanoutAuthRefsTest extends BaseServiceTest {
62
63    private final Logger logger =
64        LoggerFactory.getLogger(LoanoutAuthRefsTest.class);
65
66     // Instance variables specific to this test.
67     final String SERVICE_PATH_COMPONENT = "loansout";
68     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
69     private String knownResourceId = null;
70     private List<String> loanoutIdsCreated = new ArrayList();
71     private List<String> personIdsCreated = new ArrayList();
72     private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
73     private int OK_STATUS = Response.Status.OK.getStatusCode();
74     private String personAuthCSID = null;
75     private String borrowersContactRefName = null;
76     private String lendersAuthorizerRefName = null;
77     private String lendersContactRefName = null;
78
79     // FIXME: Can add 'borrower' - likely to be an organization
80     // authority - as an authRef to tests below, and increase the
81     // number of expected authRefs to 4.
82     private final int NUM_AUTH_REFS_EXPECTED = 3;
83
84     // ---------------------------------------------------------------
85     // CRUD tests : CREATE tests
86     // ---------------------------------------------------------------
87     // Success outcomes
88     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
89     public void createWithAuthRefs(String testName) throws Exception {
90
91         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
92
93         // Submit the request to the service and store the response.
94         String identifier = createIdentifier();
95
96         // Create all the person refs and entities
97         createPersonRefs();
98
99         // Create a new Loans In resource.
100         //
101         // One or more fields in this resource will be PersonAuthority
102         // references, and will refer to Person resources by their refNames.
103         LoanoutClient loanoutClient = new LoanoutClient();
104         MultipartOutput multipart = createLoanoutInstance(
105                 "loanOutNumber-" + identifier,
106                 "returnDate-" + identifier,
107                 borrowersContactRefName,
108                 lendersAuthorizerRefName,
109                 lendersContactRefName);
110         ClientResponse<Response> res = loanoutClient.create(multipart);
111         int statusCode = res.getStatus();
112
113         // Check the status code of the response: does it match
114         // the expected response(s)?
115         //
116         // Specifically:
117         // Does it fall within the set of valid status codes?
118         // Does it exactly match the expected status code?
119         if(logger.isDebugEnabled()){
120             logger.debug(testName + ": status = " + statusCode);
121         }
122         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
125
126         // Store the ID returned from the first resource created
127         // for additional tests below.
128         if (knownResourceId == null){
129             knownResourceId = extractId(res);
130             if (logger.isDebugEnabled()) {
131                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
132             }
133         }
134         
135         // Store the IDs from every resource created by tests,
136         // so they can be deleted after tests have been run.
137         loanoutIdsCreated.add(extractId(res));
138     }
139
140     protected void createPersonRefs(){
141
142         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
143         // Create a temporary PersonAuthority resource, and its corresponding
144         // refName by which it can be identified.
145         String authRefName = 
146                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
147         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
148             PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
149         ClientResponse<Response> res = personAuthClient.create(multipart);
150         int statusCode = res.getStatus();
151
152         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
153             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
154         Assert.assertEquals(statusCode, CREATED_STATUS);
155         personAuthCSID = extractId(res);
156
157         // Create temporary Person resources, and their corresponding refNames
158         // by which they can be identified.
159         borrowersContactRefName =
160             PersonAuthorityClientUtils.createPersonRefName(authRefName, "Bradley BorrowersContact", true);
161         personIdsCreated.add(createPerson("Bradley", "BorrowersContact", borrowersContactRefName));
162
163         lendersAuthorizerRefName =
164             PersonAuthorityClientUtils.createPersonRefName(authRefName, "Art Lendersauthorizor", true);
165         personIdsCreated.add(createPerson("Art", "Lendersauthorizor", lendersAuthorizerRefName));
166
167         lendersContactRefName =
168             PersonAuthorityClientUtils.createPersonRefName(authRefName, "Larry Lenderscontact", true);
169         personIdsCreated.add(createPerson("Larry", "Lenderscontact", lendersContactRefName));
170     }
171     
172     protected String createPerson(String firstName, String surName, String refName ) {
173         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
174         Map<String, String> personInfo = new HashMap<String,String>();
175         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
176         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
177         MultipartOutput multipart = 
178                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
179                                 refName, personInfo, personAuthClient.getItemCommonPartName());
180         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
181         int statusCode = res.getStatus();
182
183         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
184                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
185         Assert.assertEquals(statusCode, CREATED_STATUS);
186         return extractId(res);
187     }
188
189     // Success outcomes
190     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
191         dependsOnMethods = {"createWithAuthRefs"})
192     public void readAndCheckAuthRefs(String testName) throws Exception {
193
194         // Perform setup.
195         testSetup(OK_STATUS, ServiceRequestType.READ,testName);
196
197         // Submit the request to the service and store the response.
198         LoanoutClient loanoutClient = new LoanoutClient();
199         ClientResponse<MultipartInput> res = loanoutClient.read(knownResourceId);
200         int statusCode = res.getStatus();
201
202         // Check the status code of the response: does it match
203         // the expected response(s)?
204         if(logger.isDebugEnabled()){
205             logger.debug(testName + ".read: status = " + statusCode);
206         }
207         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
208             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
209         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
210
211         MultipartInput input = (MultipartInput) res.getEntity();
212         LoansoutCommon loanout = (LoansoutCommon) extractPart(input,
213             loanoutClient.getCommonPartName(), LoansoutCommon.class);
214         Assert.assertNotNull(loanout);
215         if(logger.isDebugEnabled()){
216             logger.debug(objectAsXmlString(loanout, LoansoutCommon.class));
217         }
218         // Check a couple of fields
219         // FIXME
220         Assert.assertEquals(loanout.getLendersAuthorizer(), lendersAuthorizerRefName);
221         Assert.assertEquals(loanout.getLendersContact(), lendersContactRefName);
222         
223         // Get the auth refs and check them
224         ClientResponse<AuthorityRefList> res2 =
225            loanoutClient.getAuthorityRefs(knownResourceId);
226         statusCode = res2.getStatus();
227
228         if(logger.isDebugEnabled()){
229             logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
230         }
231         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
232                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
233         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
234         AuthorityRefList list = res2.getEntity();
235
236         // Optionally output additional data about list members for debugging.
237         boolean iterateThroughList = true;
238         if(iterateThroughList && logger.isDebugEnabled()){
239             List<AuthorityRefList.AuthorityRefItem> items =
240                     list.getAuthorityRefItem();
241             int i = 0;
242             for(AuthorityRefList.AuthorityRefItem item : items){
243                 logger.debug(testName + ": list-item[" + i + "] Field:" +
244                                 item.getSourceField() + "= " +
245                         item.getAuthDisplayName() +
246                         item.getItemDisplayName());
247                 logger.debug(testName + ": list-item[" + i + "] refName=" +
248                         item.getRefName());
249                 logger.debug(testName + ": list-item[" + i + "] URI=" +
250                         item.getUri());
251                 i++;
252             }
253             Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
254         }
255     }
256
257
258     // ---------------------------------------------------------------
259     // Cleanup of resources created during testing
260     // ---------------------------------------------------------------
261
262     /**
263      * Deletes all resources created by tests, after all tests have been run.
264      *
265      * This cleanup method will always be run, even if one or more tests fail.
266      * For this reason, it attempts to remove all resources created
267      * at any point during testing, even if some of those resources
268      * may be expected to be deleted by certain tests.
269      */
270     @AfterClass(alwaysRun=true)
271     public void cleanUp() {
272         if (logger.isDebugEnabled()) {
273             logger.debug("Cleaning up temporary resources created for testing ...");
274         }
275         // Note: Any non-success responses are ignored and not reported.
276
277         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
278         // Delete Person resource(s) (before PersonAuthority resources).
279         ClientResponse<Response> res;
280         for (String resourceId : personIdsCreated) {
281             res = personAuthClient.deleteItem(personAuthCSID, resourceId);
282         }
283         // Delete PersonAuthority resource(s).
284         res = personAuthClient.delete(personAuthCSID);
285         // Delete Loans In resource(s).
286         LoanoutClient loanoutClient = new LoanoutClient();
287         for (String resourceId : loanoutIdsCreated) {
288             res = loanoutClient.delete(resourceId);
289         }
290     }
291
292     // ---------------------------------------------------------------
293     // Utility methods used by tests above
294     // ---------------------------------------------------------------
295     @Override
296     public String getServicePathComponent() {
297         return SERVICE_PATH_COMPONENT;
298     }
299
300    private MultipartOutput createLoanoutInstance(String loanoutNumber,
301                 String returnDate,
302                 String borrowersContact,
303                 String lendersAuthorizer,
304                 String lendersContact) {
305         LoansoutCommon loanout = new LoansoutCommon();
306         loanout.setLoanOutNumber(loanoutNumber);
307         loanout.setLoanReturnDate(returnDate);
308         loanout.setBorrowersContact(lendersContact);
309         loanout.setLendersAuthorizer(lendersAuthorizer);
310         loanout.setLendersContact(lendersContact);
311         MultipartOutput multipart = new MultipartOutput();
312         OutputPart commonPart =
313             multipart.addPart(loanout, MediaType.APPLICATION_XML_TYPE);
314         commonPart.getHeaders().add("label", new LoanoutClient().getCommonPartName());
315
316         if(logger.isDebugEnabled()){
317             logger.debug("to be created, loanout common");
318             logger.debug(objectAsXmlString(loanout, LoansoutCommon.class));
319         }
320
321         return multipart;
322     }
323 }