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.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.LoaninClient;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.client.PayloadOutputPart;
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.loanin.LenderGroup;
45 import org.collectionspace.services.loanin.LenderGroupList;
46 import org.collectionspace.services.loanin.LoansinCommon;
48 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.Test;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * LoaninAuthRefsTest, carries out Authority References tests against a
59 * deployed and running Loanin (aka Loans In) Service.
61 * $LastChangedRevision$
64 public class LoaninAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
66 private final String CLASS_NAME = LoaninAuthRefsTest.class.getName();
67 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
69 // Instance variables specific to this test.
70 final String SERVICE_NAME = "loansin";
71 final String SERVICE_PATH_COMPONENT = "loansin";
72 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
73 private String knownResourceId = null;
74 private List<String> loaninIdsCreated = new ArrayList<String>();
75 private List<String> personIdsCreated = new ArrayList<String>();
76 private String personAuthCSID = null;
77 private String lenderRefName = null;
78 private String lendersAuthorizerRefName = null;
79 private String lendersContactRefName = null;
80 private String loanInContactRefName = null;
81 private String borrowersAuthorizerRefName = null;
82 private final int NUM_AUTH_REFS_EXPECTED = 5;
83 private final static String CURRENT_DATE_UTC =
84 GregorianCalendarDateTimeUtils.currentDateUTC();
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
90 protected CollectionSpaceClient getClientInstance() {
91 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
95 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
98 protected AbstractCommonList getCommonList(
99 ClientResponse<AbstractCommonList> response) {
100 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
103 // ---------------------------------------------------------------
104 // CRUD tests : CREATE tests
105 // ---------------------------------------------------------------
107 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
108 public void createWithAuthRefs(String testName) throws Exception {
109 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
111 // Submit the request to the service and store the response.
112 String identifier = createIdentifier();
114 // Create all the person refs and entities
117 // Create a new Loans In resource.
119 // One or more fields in this resource will be PersonAuthority
120 // references, and will refer to Person resources by their refNames.
121 LoaninClient loaninClient = new LoaninClient();
122 PoxPayloadOut multipart = createLoaninInstance(
123 "loanInNumber-" + identifier,
126 lendersAuthorizerRefName,
127 lendersContactRefName,
128 loanInContactRefName,
129 borrowersAuthorizerRefName);
130 ClientResponse<Response> response = loaninClient.create(multipart);
131 int statusCode = response.getStatus();
133 // Check the status code of the response: does it match
134 // the expected response(s)?
137 // Does it fall within the set of valid status codes?
138 // Does it exactly match the expected status code?
139 if(logger.isDebugEnabled()){
140 logger.debug(testName + ": status = " + statusCode);
142 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
143 invalidStatusCodeMessage(testRequestType, statusCode));
144 Assert.assertEquals(statusCode, testExpectedStatusCode);
146 // Store the ID returned from the first resource created
147 // for additional tests below.
148 if (knownResourceId == null){
149 knownResourceId = extractId(response);
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ": knownResourceId=" + knownResourceId);
155 // Store the IDs from every resource created by tests,
156 // so they can be deleted after tests have been run.
157 loaninIdsCreated.add(extractId(response));
159 response.releaseConnection();
163 protected void createPersonRefs(){
165 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
166 // Create a temporary PersonAuthority resource, and its corresponding
167 // refName by which it can be identified.
168 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
169 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
170 ClientResponse<Response> res = personAuthClient.create(multipart);
171 int statusCode = res.getStatus();
173 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(testRequestType, statusCode));
175 Assert.assertEquals(statusCode, STATUS_CREATED);
176 personAuthCSID = extractId(res);
178 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
180 // Create temporary Person resources, and their corresponding refNames
181 // by which they can be identified.
182 String csid = createPerson("Linus", "Lender", "linusLender", authRefName);
183 personIdsCreated.add(csid);
184 lenderRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
186 csid = createPerson("Art", "Lendersauthorizor", "artLendersauthorizor", authRefName);
187 personIdsCreated.add(csid);
188 lendersAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
190 csid = createPerson("Larry", "Lenderscontact", "larryLenderscontact", authRefName);
191 personIdsCreated.add(csid);
192 lendersContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
194 csid = createPerson("Carrie", "Loanincontact", "carrieLoanincontact", authRefName);
195 personIdsCreated.add(csid);
196 loanInContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
198 csid = createPerson("Bonnie", "Borrowersauthorizer", "bonnieBorrowersauthorizer", authRefName);
199 personIdsCreated.add(csid);
200 borrowersAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
202 // FIXME: Add instance(s) of 'lenders' field when we can work with
203 // repeatable / multivalued authority reference fields. Be sure to
206 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
207 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
208 Map<String, String> personInfo = new HashMap<String,String>();
209 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
210 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
211 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
212 PoxPayloadOut multipart =
213 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
214 authRefName, personInfo, personAuthClient.getItemCommonPartName());
215 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
216 int statusCode = res.getStatus();
218 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(testRequestType, statusCode));
220 Assert.assertEquals(statusCode, STATUS_CREATED);
221 return extractId(res);
225 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
226 dependsOnMethods = {"createWithAuthRefs"})
227 public void readAndCheckAuthRefs(String testName) throws Exception {
229 testSetup(STATUS_OK, ServiceRequestType.READ);
231 // Submit the request to the service and store the response.
232 LoaninClient loaninClient = new LoaninClient();
233 ClientResponse<String> res = loaninClient.read(knownResourceId);
234 LoansinCommon loaninCommon = null;
236 assertStatusCode(res, testName);
237 // Extract the common part from the response.
238 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
239 loaninCommon = (LoansinCommon) extractPart(input,
240 loaninClient.getCommonPartName(), LoansinCommon.class);
241 Assert.assertNotNull(loaninCommon);
242 if(logger.isDebugEnabled()){
243 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
247 res.releaseConnection();
251 // Check a couple of fields
252 // Assert.assertEquals(loaninCommon.getLender(), lenderRefName);
253 // Assert.assertEquals(loaninCommon.getLendersAuthorizer(), lendersAuthorizerRefName);
254 // Assert.assertEquals(loaninCommon.getLendersContact(), lendersContactRefName);
256 Assert.assertEquals(loaninCommon.getLoanInContact(), loanInContactRefName);
257 Assert.assertEquals(loaninCommon.getBorrowersAuthorizer(), borrowersAuthorizerRefName);
259 // Get the auth refs and check them
260 ClientResponse<AuthorityRefList> res2 = loaninClient.getAuthorityRefs(knownResourceId);
261 AuthorityRefList list = null;
263 assertStatusCode(res2, testName);
264 list = res2.getEntity();
265 Assert.assertNotNull(list);
268 res2.releaseConnection();
272 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
273 int numAuthRefsFound = items.size();
274 if(logger.isDebugEnabled()){
275 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
276 " authority references, found " + numAuthRefsFound);
279 // Optionally output additional data about list members for debugging.
280 boolean iterateThroughList = true;
281 if(iterateThroughList && logger.isDebugEnabled()){
283 for(AuthorityRefList.AuthorityRefItem item : items){
284 logger.debug(testName + ": list-item[" + i + "] Field:" +
285 item.getSourceField() + "= " +
286 item.getAuthDisplayName() +
287 item.getItemDisplayName());
288 logger.debug(testName + ": list-item[" + i + "] refName=" +
290 logger.debug(testName + ": list-item[" + i + "] URI=" +
296 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
297 "Did not find all expected authority references! " +
298 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
303 // ---------------------------------------------------------------
304 // Cleanup of resources created during testing
305 // ---------------------------------------------------------------
308 * Deletes all resources created by tests, after all tests have been run.
310 * This cleanup method will always be run, even if one or more tests fail.
311 * For this reason, it attempts to remove all resources created
312 * at any point during testing, even if some of those resources
313 * may be expected to be deleted by certain tests.
315 @AfterClass(alwaysRun=true)
316 public void cleanUp() {
317 String noTest = System.getProperty("noTestCleanup");
318 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
319 if (logger.isDebugEnabled()) {
320 logger.debug("Skipping Cleanup phase ...");
324 if (logger.isDebugEnabled()) {
325 logger.debug("Cleaning up temporary resources created for testing ...");
327 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
328 // Delete Person resource(s) (before PersonAuthority resources).
330 for (String resourceId : personIdsCreated) {
331 // Note: Any non-success responses are ignored and not reported.
332 ClientResponse<Response> response =
333 personAuthClient.deleteItem(personAuthCSID, resourceId); // alternative to personAuthClient.deleteItem().releaseConnection();
334 response.releaseConnection();
337 // Delete PersonAuthority resource(s).
338 // Note: Any non-success response is ignored and not reported.
339 if (personAuthCSID != null) {
340 personAuthClient.delete(personAuthCSID);
341 // Delete Loans In resource(s).
342 LoaninClient loaninClient = new LoaninClient();
343 ClientResponse<Response> response = null;
344 for (String resourceId : loaninIdsCreated) {
345 // Note: Any non-success responses are ignored and not reported.
346 response = loaninClient.delete(resourceId); // alternative to loaninClient.delete(resourceId).releaseConnection();
347 response.releaseConnection();
352 // ---------------------------------------------------------------
353 // Utility methods used by tests above
354 // ---------------------------------------------------------------
355 public String getServiceName() {
360 public String getServicePathComponent() {
361 return SERVICE_PATH_COMPONENT;
364 private PoxPayloadOut createLoaninInstance(String loaninNumber,
367 String lendersAuthorizer,
368 String lendersContact,
369 String loaninContact,
370 String borrowersAuthorizer) {
371 LoansinCommon loaninCommon = new LoansinCommon();
372 loaninCommon.setLoanInNumber(loaninNumber);
373 loaninCommon.setLoanInNumber(returnDate);
374 LenderGroupList lenderGroupList = new LenderGroupList();
375 LenderGroup lenderGroup = new LenderGroup();
376 lenderGroup.setLender(lender);
377 lenderGroup.setLendersAuthorizer(lendersAuthorizer);
378 lenderGroup.setLendersContact(lendersContact);
379 lenderGroupList.getLenderGroup().add(lenderGroup);
380 loaninCommon.setLenderGroupList(lenderGroupList);
381 loaninCommon.setLoanInContact(loaninContact);
382 loaninCommon.setBorrowersAuthorizer(borrowersAuthorizer);
384 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
385 PayloadOutputPart commonPart =
386 multipart.addPart(new LoaninClient().getCommonPartName(), loaninCommon);
388 if(logger.isDebugEnabled()){
389 logger.debug("to be created, loanin common");
390 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
397 protected Class<AbstractCommonList> getCommonListType() {
398 return AbstractCommonList.class;