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.jaxb.AbstractCommonList;
45 import org.collectionspace.services.loanin.LenderGroup;
46 import org.collectionspace.services.loanin.LenderGroupList;
47 import org.collectionspace.services.loanin.LoansinCommon;
49 import org.jboss.resteasy.client.ClientResponse;
51 import org.testng.Assert;
52 import org.testng.annotations.AfterClass;
53 import org.testng.annotations.Test;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 * LoaninAuthRefsTest, carries out Authority References tests against a
60 * deployed and running Loanin (aka Loans In) Service.
62 * $LastChangedRevision$
65 public class LoaninAuthRefsTest extends BaseServiceTest {
67 private final String CLASS_NAME = LoaninAuthRefsTest.class.getName();
68 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
70 // Instance variables specific to this test.
71 final String SERVICE_NAME = "loansin";
72 final String SERVICE_PATH_COMPONENT = "loansin";
73 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
74 private String knownResourceId = null;
75 private List<String> loaninIdsCreated = new ArrayList<String>();
76 private List<String> personIdsCreated = new ArrayList<String>();
77 private String personAuthCSID = null;
78 private String lenderRefName = null;
79 private String lendersAuthorizerRefName = null;
80 private String lendersContactRefName = null;
81 private String loanInContactRefName = null;
82 private String borrowersAuthorizerRefName = null;
83 // FIXME: Value changed from 5 to 2 when repeatable / multivalue 'lenders'
84 // group was added to tenant-bindings.xml
85 private final int NUM_AUTH_REFS_EXPECTED = 2;
88 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
91 protected CollectionSpaceClient getClientInstance() {
92 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
96 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
99 protected AbstractCommonList getAbstractCommonList(
100 ClientResponse<AbstractCommonList> response) {
101 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
104 // ---------------------------------------------------------------
105 // CRUD tests : CREATE tests
106 // ---------------------------------------------------------------
108 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
109 public void createWithAuthRefs(String testName) throws Exception {
111 if (logger.isDebugEnabled()) {
112 logger.debug(testBanner(testName, CLASS_NAME));
114 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
116 // Submit the request to the service and store the response.
117 String identifier = createIdentifier();
119 // Create all the person refs and entities
122 // Create a new Loans In resource.
124 // One or more fields in this resource will be PersonAuthority
125 // references, and will refer to Person resources by their refNames.
126 LoaninClient loaninClient = new LoaninClient();
127 PoxPayloadOut multipart = createLoaninInstance(
128 "loanInNumber-" + identifier,
129 "returnDate-" + identifier,
131 lendersAuthorizerRefName,
132 lendersContactRefName,
133 loanInContactRefName,
134 borrowersAuthorizerRefName);
135 ClientResponse<Response> response = loaninClient.create(multipart);
136 int statusCode = response.getStatus();
138 // Check the status code of the response: does it match
139 // the expected response(s)?
142 // Does it fall within the set of valid status codes?
143 // Does it exactly match the expected status code?
144 if(logger.isDebugEnabled()){
145 logger.debug(testName + ": status = " + statusCode);
147 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
149 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
151 // Store the ID returned from the first resource created
152 // for additional tests below.
153 if (knownResourceId == null){
154 knownResourceId = extractId(response);
155 if (logger.isDebugEnabled()) {
156 logger.debug(testName + ": knownResourceId=" + knownResourceId);
160 // Store the IDs from every resource created by tests,
161 // so they can be deleted after tests have been run.
162 loaninIdsCreated.add(extractId(response));
164 response.releaseConnection();
168 protected void createPersonRefs(){
170 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
171 // Create a temporary PersonAuthority resource, and its corresponding
172 // refName by which it can be identified.
173 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
174 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
175 ClientResponse<Response> res = personAuthClient.create(multipart);
176 int statusCode = res.getStatus();
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, STATUS_CREATED);
181 personAuthCSID = extractId(res);
183 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
185 // Create temporary Person resources, and their corresponding refNames
186 // by which they can be identified.
187 String csid = createPerson("Linus", "Lender", "linusLender", authRefName);
188 personIdsCreated.add(csid);
189 lenderRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
191 csid = createPerson("Art", "Lendersauthorizor", "artLendersauthorizor", authRefName);
192 personIdsCreated.add(csid);
193 lendersAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
195 csid = createPerson("Larry", "Lenderscontact", "larryLenderscontact", authRefName);
196 personIdsCreated.add(csid);
197 lendersContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
199 csid = createPerson("Carrie", "Loanincontact", "carrieLoanincontact", authRefName);
200 personIdsCreated.add(csid);
201 loanInContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
203 csid = createPerson("Bonnie", "Borrowersauthorizer", "bonnieBorrowersauthorizer", authRefName);
204 personIdsCreated.add(csid);
205 borrowersAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
207 // FIXME: Add instance(s) of 'lenders' field when we can work with
208 // repeatable / multivalued authority reference fields. Be sure to
209 // change the NUM_AUTH_REFS_EXPECTED constant accordingly, or otherwise
210 // revise check for numbers of authority fields expected in readAndCheckAuthRefs.
213 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
214 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
215 Map<String, String> personInfo = new HashMap<String,String>();
216 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
217 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
218 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
219 PoxPayloadOut multipart =
220 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
221 authRefName, personInfo, personAuthClient.getItemCommonPartName());
222 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
223 int statusCode = res.getStatus();
225 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
226 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
227 Assert.assertEquals(statusCode, STATUS_CREATED);
228 return extractId(res);
232 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
233 dependsOnMethods = {"createWithAuthRefs"})
234 public void readAndCheckAuthRefs(String testName) throws Exception {
236 if (logger.isDebugEnabled()) {
237 logger.debug(testBanner(testName, CLASS_NAME));
240 testSetup(STATUS_OK, ServiceRequestType.READ);
242 // Submit the request to the service and store the response.
243 LoaninClient loaninClient = new LoaninClient();
244 ClientResponse<String> res = loaninClient.read(knownResourceId);
245 int statusCode = res.getStatus();
247 // Check the status code of the response: does it match
248 // the expected response(s)?
249 if(logger.isDebugEnabled()){
250 logger.debug(testName + ".read: status = " + statusCode);
252 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
253 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
254 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
256 // Extract the common part from the response.
257 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
258 LoansinCommon loaninCommon = (LoansinCommon) extractPart(input,
259 loaninClient.getCommonPartName(), LoansinCommon.class);
260 Assert.assertNotNull(loaninCommon);
261 if(logger.isDebugEnabled()){
262 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
265 // Check a couple of fields
266 // Assert.assertEquals(loaninCommon.getLender(), lenderRefName);
267 // Assert.assertEquals(loaninCommon.getLendersAuthorizer(), lendersAuthorizerRefName);
268 // Assert.assertEquals(loaninCommon.getLendersContact(), lendersContactRefName);
269 Assert.assertEquals(loaninCommon.getLoanInContact(), loanInContactRefName);
270 Assert.assertEquals(loaninCommon.getBorrowersAuthorizer(), borrowersAuthorizerRefName);
272 // Get the auth refs and check them
273 ClientResponse<AuthorityRefList> res2 =
274 loaninClient.getAuthorityRefs(knownResourceId);
275 statusCode = res2.getStatus();
277 if(logger.isDebugEnabled()){
278 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
280 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
281 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
282 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
283 AuthorityRefList list = res2.getEntity();
285 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
286 int numAuthRefsFound = items.size();
287 if(logger.isDebugEnabled()){
288 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
289 " authority references, found " + numAuthRefsFound);
292 // Optionally output additional data about list members for debugging.
293 boolean iterateThroughList = true;
294 if(iterateThroughList && logger.isDebugEnabled()){
296 for(AuthorityRefList.AuthorityRefItem item : items){
297 logger.debug(testName + ": list-item[" + i + "] Field:" +
298 item.getSourceField() + "= " +
299 item.getAuthDisplayName() +
300 item.getItemDisplayName());
301 logger.debug(testName + ": list-item[" + i + "] refName=" +
303 logger.debug(testName + ": list-item[" + i + "] URI=" +
309 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
310 "Did not find all expected authority references! " +
311 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
316 // ---------------------------------------------------------------
317 // Cleanup of resources created during testing
318 // ---------------------------------------------------------------
321 * Deletes all resources created by tests, after all tests have been run.
323 * This cleanup method will always be run, even if one or more tests fail.
324 * For this reason, it attempts to remove all resources created
325 * at any point during testing, even if some of those resources
326 * may be expected to be deleted by certain tests.
328 @AfterClass(alwaysRun=true)
329 public void cleanUp() {
330 String noTest = System.getProperty("noTestCleanup");
331 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
332 if (logger.isDebugEnabled()) {
333 logger.debug("Skipping Cleanup phase ...");
337 if (logger.isDebugEnabled()) {
338 logger.debug("Cleaning up temporary resources created for testing ...");
340 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
341 // Delete Person resource(s) (before PersonAuthority resources).
343 for (String resourceId : personIdsCreated) {
344 // Note: Any non-success responses are ignored and not reported.
345 ClientResponse<Response> response =
346 personAuthClient.deleteItem(personAuthCSID, resourceId); // alternative to personAuthClient.deleteItem().releaseConnection();
347 response.releaseConnection();
350 // Delete PersonAuthority resource(s).
351 // Note: Any non-success response is ignored and not reported.
352 if (personAuthCSID != null) {
353 personAuthClient.delete(personAuthCSID);
354 // Delete Loans In resource(s).
355 LoaninClient loaninClient = new LoaninClient();
356 ClientResponse<Response> response = null;
357 for (String resourceId : loaninIdsCreated) {
358 // Note: Any non-success responses are ignored and not reported.
359 response = loaninClient.delete(resourceId); // alternative to loaninClient.delete(resourceId).releaseConnection();
360 response.releaseConnection();
365 // ---------------------------------------------------------------
366 // Utility methods used by tests above
367 // ---------------------------------------------------------------
368 public String getServiceName() {
373 public String getServicePathComponent() {
374 return SERVICE_PATH_COMPONENT;
378 private PoxPayloadOut createLoaninInstance(String loaninNumber,
381 String lendersAuthorizer,
382 String lendersContact,
383 String loaninContact,
384 String borrowersAuthorizer) {
385 LoansinCommon loaninCommon = new LoansinCommon();
386 loaninCommon.setLoanInNumber(loaninNumber);
387 loaninCommon.setLoanInNumber(returnDate);
388 LenderGroupList lenderGroupList = new LenderGroupList();
389 LenderGroup lenderGroup = new LenderGroup();
390 lenderGroup.setLender(lender);
391 lenderGroup.setLendersAuthorizer(lendersAuthorizer);
392 lenderGroup.setLendersContact(lendersContact);
393 lenderGroupList.getLenderGroup().add(lenderGroup);
394 loaninCommon.setLenderGroupList(lenderGroupList);
395 loaninCommon.setLoanInContact(loaninContact);
396 loaninCommon.setBorrowersAuthorizer(borrowersAuthorizer);
398 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
399 PayloadOutputPart commonPart =
400 multipart.addPart(loaninCommon, MediaType.APPLICATION_XML_TYPE);
401 commonPart.setLabel(new LoaninClient().getCommonPartName());
403 if(logger.isDebugEnabled()){
404 logger.debug("to be created, loanin common");
405 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));