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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.LoaninClient;
37 import org.collectionspace.services.client.PersonAuthorityClient;
38 import org.collectionspace.services.client.PersonAuthorityClientUtils;
39 import org.collectionspace.services.client.PayloadInputPart;
40 import org.collectionspace.services.client.PayloadOutputPart;
41 import org.collectionspace.services.client.PoxPayloadIn;
42 import org.collectionspace.services.client.PoxPayloadOut;
43 import org.collectionspace.services.common.authorityref.AuthorityRefList;
44 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
45 import org.collectionspace.services.jaxb.AbstractCommonList;
46 import org.collectionspace.services.loanin.LenderGroup;
47 import org.collectionspace.services.loanin.LenderGroupList;
48 import org.collectionspace.services.loanin.LoansinCommon;
50 import org.jboss.resteasy.client.ClientResponse;
52 import org.testng.Assert;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.Test;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * LoaninAuthRefsTest, carries out Authority References tests against a
61 * deployed and running Loanin (aka Loans In) Service.
63 * $LastChangedRevision$
66 public class LoaninAuthRefsTest extends BaseServiceTest {
68 private final String CLASS_NAME = LoaninAuthRefsTest.class.getName();
69 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71 // Instance variables specific to this test.
72 final String SERVICE_NAME = "loansin";
73 final String SERVICE_PATH_COMPONENT = "loansin";
74 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
75 private String knownResourceId = null;
76 private List<String> loaninIdsCreated = new ArrayList<String>();
77 private List<String> personIdsCreated = new ArrayList<String>();
78 private String personAuthCSID = null;
79 private String lenderRefName = null;
80 private String lendersAuthorizerRefName = null;
81 private String lendersContactRefName = null;
82 private String loanInContactRefName = null;
83 private String borrowersAuthorizerRefName = null;
84 private final int NUM_AUTH_REFS_EXPECTED = 5;
85 private final static String CURRENT_DATE_UTC =
86 GregorianCalendarDateTimeUtils.currentDateUTC();
89 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
92 protected CollectionSpaceClient getClientInstance() {
93 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
97 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
100 protected AbstractCommonList getAbstractCommonList(
101 ClientResponse<AbstractCommonList> response) {
102 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
105 // ---------------------------------------------------------------
106 // CRUD tests : CREATE tests
107 // ---------------------------------------------------------------
109 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
110 public void createWithAuthRefs(String testName) throws Exception {
112 if (logger.isDebugEnabled()) {
113 logger.debug(testBanner(testName, CLASS_NAME));
115 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
117 // Submit the request to the service and store the response.
118 String identifier = createIdentifier();
120 // Create all the person refs and entities
123 // Create a new Loans In resource.
125 // One or more fields in this resource will be PersonAuthority
126 // references, and will refer to Person resources by their refNames.
127 LoaninClient loaninClient = new LoaninClient();
128 PoxPayloadOut multipart = createLoaninInstance(
129 "loanInNumber-" + identifier,
132 lendersAuthorizerRefName,
133 lendersContactRefName,
134 loanInContactRefName,
135 borrowersAuthorizerRefName);
136 ClientResponse<Response> response = loaninClient.create(multipart);
137 int statusCode = response.getStatus();
139 // Check the status code of the response: does it match
140 // the expected response(s)?
143 // Does it fall within the set of valid status codes?
144 // Does it exactly match the expected status code?
145 if(logger.isDebugEnabled()){
146 logger.debug(testName + ": status = " + statusCode);
148 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
152 // Store the ID returned from the first resource created
153 // for additional tests below.
154 if (knownResourceId == null){
155 knownResourceId = extractId(response);
156 if (logger.isDebugEnabled()) {
157 logger.debug(testName + ": knownResourceId=" + knownResourceId);
161 // Store the IDs from every resource created by tests,
162 // so they can be deleted after tests have been run.
163 loaninIdsCreated.add(extractId(response));
165 response.releaseConnection();
169 protected void createPersonRefs(){
171 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
172 // Create a temporary PersonAuthority resource, and its corresponding
173 // refName by which it can be identified.
174 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
175 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
176 ClientResponse<Response> res = personAuthClient.create(multipart);
177 int statusCode = res.getStatus();
179 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
180 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
181 Assert.assertEquals(statusCode, STATUS_CREATED);
182 personAuthCSID = extractId(res);
184 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
186 // Create temporary Person resources, and their corresponding refNames
187 // by which they can be identified.
188 String csid = createPerson("Linus", "Lender", "linusLender", authRefName);
189 personIdsCreated.add(csid);
190 lenderRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
192 csid = createPerson("Art", "Lendersauthorizor", "artLendersauthorizor", authRefName);
193 personIdsCreated.add(csid);
194 lendersAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
196 csid = createPerson("Larry", "Lenderscontact", "larryLenderscontact", authRefName);
197 personIdsCreated.add(csid);
198 lendersContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
200 csid = createPerson("Carrie", "Loanincontact", "carrieLoanincontact", authRefName);
201 personIdsCreated.add(csid);
202 loanInContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
204 csid = createPerson("Bonnie", "Borrowersauthorizer", "bonnieBorrowersauthorizer", authRefName);
205 personIdsCreated.add(csid);
206 borrowersAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
208 // FIXME: Add instance(s) of 'lenders' field when we can work with
209 // repeatable / multivalued authority reference fields. Be sure to
212 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
213 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
214 Map<String, String> personInfo = new HashMap<String,String>();
215 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
216 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
217 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
218 PoxPayloadOut multipart =
219 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
220 authRefName, personInfo, personAuthClient.getItemCommonPartName());
221 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
222 int statusCode = res.getStatus();
224 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
225 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
226 Assert.assertEquals(statusCode, STATUS_CREATED);
227 return extractId(res);
231 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
232 dependsOnMethods = {"createWithAuthRefs"})
233 public void readAndCheckAuthRefs(String testName) throws Exception {
235 if (logger.isDebugEnabled()) {
236 logger.debug(testBanner(testName, CLASS_NAME));
239 testSetup(STATUS_OK, ServiceRequestType.READ);
241 // Submit the request to the service and store the response.
242 LoaninClient loaninClient = new LoaninClient();
243 ClientResponse<String> res = loaninClient.read(knownResourceId);
244 assertStatusCode(res, testName);
246 // Extract the common part from the response.
247 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
248 LoansinCommon loaninCommon = (LoansinCommon) extractPart(input,
249 loaninClient.getCommonPartName(), LoansinCommon.class);
250 Assert.assertNotNull(loaninCommon);
251 if(logger.isDebugEnabled()){
252 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
255 // Check a couple of fields
256 // Assert.assertEquals(loaninCommon.getLender(), lenderRefName);
257 // Assert.assertEquals(loaninCommon.getLendersAuthorizer(), lendersAuthorizerRefName);
258 // Assert.assertEquals(loaninCommon.getLendersContact(), lendersContactRefName);
259 Assert.assertEquals(loaninCommon.getLoanInContact(), loanInContactRefName);
260 Assert.assertEquals(loaninCommon.getBorrowersAuthorizer(), borrowersAuthorizerRefName);
262 // Get the auth refs and check them
263 ClientResponse<AuthorityRefList> res2 =
264 loaninClient.getAuthorityRefs(knownResourceId);
265 assertStatusCode(res2, testName);
266 AuthorityRefList list = res2.getEntity();
268 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
269 int numAuthRefsFound = items.size();
270 if(logger.isDebugEnabled()){
271 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
272 " authority references, found " + numAuthRefsFound);
275 // Optionally output additional data about list members for debugging.
276 boolean iterateThroughList = true;
277 if(iterateThroughList && logger.isDebugEnabled()){
279 for(AuthorityRefList.AuthorityRefItem item : items){
280 logger.debug(testName + ": list-item[" + i + "] Field:" +
281 item.getSourceField() + "= " +
282 item.getAuthDisplayName() +
283 item.getItemDisplayName());
284 logger.debug(testName + ": list-item[" + i + "] refName=" +
286 logger.debug(testName + ": list-item[" + i + "] URI=" +
292 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
293 "Did not find all expected authority references! " +
294 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
299 // ---------------------------------------------------------------
300 // Cleanup of resources created during testing
301 // ---------------------------------------------------------------
304 * Deletes all resources created by tests, after all tests have been run.
306 * This cleanup method will always be run, even if one or more tests fail.
307 * For this reason, it attempts to remove all resources created
308 * at any point during testing, even if some of those resources
309 * may be expected to be deleted by certain tests.
311 @AfterClass(alwaysRun=true)
312 public void cleanUp() {
313 String noTest = System.getProperty("noTestCleanup");
314 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
315 if (logger.isDebugEnabled()) {
316 logger.debug("Skipping Cleanup phase ...");
320 if (logger.isDebugEnabled()) {
321 logger.debug("Cleaning up temporary resources created for testing ...");
323 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
324 // Delete Person resource(s) (before PersonAuthority resources).
326 for (String resourceId : personIdsCreated) {
327 // Note: Any non-success responses are ignored and not reported.
328 ClientResponse<Response> response =
329 personAuthClient.deleteItem(personAuthCSID, resourceId); // alternative to personAuthClient.deleteItem().releaseConnection();
330 response.releaseConnection();
333 // Delete PersonAuthority resource(s).
334 // Note: Any non-success response is ignored and not reported.
335 if (personAuthCSID != null) {
336 personAuthClient.delete(personAuthCSID);
337 // Delete Loans In resource(s).
338 LoaninClient loaninClient = new LoaninClient();
339 ClientResponse<Response> response = null;
340 for (String resourceId : loaninIdsCreated) {
341 // Note: Any non-success responses are ignored and not reported.
342 response = loaninClient.delete(resourceId); // alternative to loaninClient.delete(resourceId).releaseConnection();
343 response.releaseConnection();
348 // ---------------------------------------------------------------
349 // Utility methods used by tests above
350 // ---------------------------------------------------------------
351 public String getServiceName() {
356 public String getServicePathComponent() {
357 return SERVICE_PATH_COMPONENT;
361 private PoxPayloadOut createLoaninInstance(String loaninNumber,
364 String lendersAuthorizer,
365 String lendersContact,
366 String loaninContact,
367 String borrowersAuthorizer) {
368 LoansinCommon loaninCommon = new LoansinCommon();
369 loaninCommon.setLoanInNumber(loaninNumber);
370 loaninCommon.setLoanInNumber(returnDate);
371 LenderGroupList lenderGroupList = new LenderGroupList();
372 LenderGroup lenderGroup = new LenderGroup();
373 lenderGroup.setLender(lender);
374 lenderGroup.setLendersAuthorizer(lendersAuthorizer);
375 lenderGroup.setLendersContact(lendersContact);
376 lenderGroupList.getLenderGroup().add(lenderGroup);
377 loaninCommon.setLenderGroupList(lenderGroupList);
378 loaninCommon.setLoanInContact(loaninContact);
379 loaninCommon.setBorrowersAuthorizer(borrowersAuthorizer);
381 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
382 PayloadOutputPart commonPart =
383 multipart.addPart(loaninCommon, MediaType.APPLICATION_XML_TYPE);
384 commonPart.setLabel(new LoaninClient().getCommonPartName());
386 if(logger.isDebugEnabled()){
387 logger.debug("to be created, loanin common");
388 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));