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.common.authorityref.AuthorityRefList;
39 //import org.collectionspace.services.common.authorityref.AuthorityRefList.AuthorityRefItem;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.loanin.LenderGroup;
42 import org.collectionspace.services.loanin.LenderGroupList;
43 import org.collectionspace.services.loanin.LoansinCommon;
44 //import org.collectionspace.services.loanin.LoansinCommonList;
46 import org.jboss.resteasy.client.ClientResponse;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
49 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
50 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
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_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 // FIXME: Value changed from 5 to 2 when repeatable / multivalue 'lenders'
83 // group was added to tenant-bindings.xml
84 private final int NUM_AUTH_REFS_EXPECTED = 2;
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 getAbstractCommonList(
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 {
110 if (logger.isDebugEnabled()) {
111 logger.debug(testBanner(testName, CLASS_NAME));
113 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
115 // Submit the request to the service and store the response.
116 String identifier = createIdentifier();
118 // Create all the person refs and entities
121 // Create a new Loans In resource.
123 // One or more fields in this resource will be PersonAuthority
124 // references, and will refer to Person resources by their refNames.
125 LoaninClient loaninClient = new LoaninClient();
126 MultipartOutput multipart = createLoaninInstance(
127 "loanInNumber-" + identifier,
128 "returnDate-" + identifier,
130 lendersAuthorizerRefName,
131 lendersContactRefName,
132 loanInContactRefName,
133 borrowersAuthorizerRefName);
134 ClientResponse<Response> response = loaninClient.create(multipart);
135 int statusCode = response.getStatus();
137 // Check the status code of the response: does it match
138 // the expected response(s)?
141 // Does it fall within the set of valid status codes?
142 // Does it exactly match the expected status code?
143 if(logger.isDebugEnabled()){
144 logger.debug(testName + ": status = " + statusCode);
146 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
148 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
150 // Store the ID returned from the first resource created
151 // for additional tests below.
152 if (knownResourceId == null){
153 knownResourceId = extractId(response);
154 if (logger.isDebugEnabled()) {
155 logger.debug(testName + ": knownResourceId=" + knownResourceId);
159 // Store the IDs from every resource created by tests,
160 // so they can be deleted after tests have been run.
161 loaninIdsCreated.add(extractId(response));
163 response.releaseConnection();
167 protected void createPersonRefs(){
169 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
170 // Create a temporary PersonAuthority resource, and its corresponding
171 // refName by which it can be identified.
172 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
173 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
174 ClientResponse<Response> res = personAuthClient.create(multipart);
175 int statusCode = res.getStatus();
177 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
178 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
179 Assert.assertEquals(statusCode, STATUS_CREATED);
180 personAuthCSID = extractId(res);
182 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
184 // Create temporary Person resources, and their corresponding refNames
185 // by which they can be identified.
186 String csid = createPerson("Linus", "Lender", "linusLender", authRefName);
187 personIdsCreated.add(csid);
188 lenderRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
190 csid = createPerson("Art", "Lendersauthorizor", "artLendersauthorizor", authRefName);
191 personIdsCreated.add(csid);
192 lendersAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
194 csid = createPerson("Larry", "Lenderscontact", "larryLenderscontact", authRefName);
195 personIdsCreated.add(csid);
196 lendersContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
198 csid = createPerson("Carrie", "Loanincontact", "carrieLoanincontact", authRefName);
199 personIdsCreated.add(csid);
200 loanInContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
202 csid = createPerson("Bonnie", "Borrowersauthorizer", "bonnieBorrowersauthorizer", authRefName);
203 personIdsCreated.add(csid);
204 borrowersAuthorizerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
206 // FIXME: Add instance(s) of 'lenders' field when we can work with
207 // repeatable / multivalued authority reference fields. Be sure to
208 // change the NUM_AUTH_REFS_EXPECTED constant accordingly, or otherwise
209 // revise check for numbers of authority fields expected in readAndCheckAuthRefs.
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 MultipartOutput 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<MultipartInput> res = loaninClient.read(knownResourceId);
244 int statusCode = res.getStatus();
246 // Check the status code of the response: does it match
247 // the expected response(s)?
248 if(logger.isDebugEnabled()){
249 logger.debug(testName + ".read: status = " + statusCode);
251 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
252 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
253 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255 MultipartInput input = (MultipartInput) res.getEntity();
256 LoansinCommon loanin = (LoansinCommon) extractPart(input,
257 loaninClient.getCommonPartName(), LoansinCommon.class);
258 Assert.assertNotNull(loanin);
259 if(logger.isDebugEnabled()){
260 logger.debug(objectAsXmlString(loanin, LoansinCommon.class));
262 // Check a couple of fields
263 // Assert.assertEquals(loanin.getLender(), lenderRefName);
264 // Assert.assertEquals(loanin.getLendersAuthorizer(), lendersAuthorizerRefName);
265 // Assert.assertEquals(loanin.getLendersContact(), lendersContactRefName);
266 Assert.assertEquals(loanin.getLoanInContact(), loanInContactRefName);
267 Assert.assertEquals(loanin.getBorrowersAuthorizer(), borrowersAuthorizerRefName);
269 // Get the auth refs and check them
270 ClientResponse<AuthorityRefList> res2 =
271 loaninClient.getAuthorityRefs(knownResourceId);
272 statusCode = res2.getStatus();
274 if(logger.isDebugEnabled()){
275 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
277 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
278 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
279 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
280 AuthorityRefList list = res2.getEntity();
282 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
283 int numAuthRefsFound = items.size();
284 if(logger.isDebugEnabled()){
285 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
286 " authority references, found " + numAuthRefsFound);
289 // Optionally output additional data about list members for debugging.
290 boolean iterateThroughList = true;
291 if(iterateThroughList && logger.isDebugEnabled()){
293 for(AuthorityRefList.AuthorityRefItem item : items){
294 logger.debug(testName + ": list-item[" + i + "] Field:" +
295 item.getSourceField() + "= " +
296 item.getAuthDisplayName() +
297 item.getItemDisplayName());
298 logger.debug(testName + ": list-item[" + i + "] refName=" +
300 logger.debug(testName + ": list-item[" + i + "] URI=" +
306 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
307 "Did not find all expected authority references! " +
308 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
313 // ---------------------------------------------------------------
314 // Cleanup of resources created during testing
315 // ---------------------------------------------------------------
318 * Deletes all resources created by tests, after all tests have been run.
320 * This cleanup method will always be run, even if one or more tests fail.
321 * For this reason, it attempts to remove all resources created
322 * at any point during testing, even if some of those resources
323 * may be expected to be deleted by certain tests.
325 @AfterClass(alwaysRun=true)
326 public void cleanUp() {
327 String noTest = System.getProperty("noTestCleanup");
328 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
329 if (logger.isDebugEnabled()) {
330 logger.debug("Skipping Cleanup phase ...");
334 if (logger.isDebugEnabled()) {
335 logger.debug("Cleaning up temporary resources created for testing ...");
337 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
338 // Delete Person resource(s) (before PersonAuthority resources).
340 for (String resourceId : personIdsCreated) {
341 // Note: Any non-success responses are ignored and not reported.
342 ClientResponse<Response> response =
343 personAuthClient.deleteItem(personAuthCSID, resourceId); // alternative to personAuthClient.deleteItem().releaseConnection();
344 response.releaseConnection();
347 // Delete PersonAuthority resource(s).
348 // Note: Any non-success response is ignored and not reported.
349 if (personAuthCSID != null) {
350 personAuthClient.delete(personAuthCSID);
351 // Delete Loans In resource(s).
352 LoaninClient loaninClient = new LoaninClient();
353 ClientResponse<Response> response = null;
354 for (String resourceId : loaninIdsCreated) {
355 // Note: Any non-success responses are ignored and not reported.
356 response = loaninClient.delete(resourceId); // alternative to loaninClient.delete(resourceId).releaseConnection();
357 response.releaseConnection();
362 // ---------------------------------------------------------------
363 // Utility methods used by tests above
364 // ---------------------------------------------------------------
366 public String getServicePathComponent() {
367 return SERVICE_PATH_COMPONENT;
370 private MultipartOutput createLoaninInstance(String loaninNumber,
373 String lendersAuthorizer,
374 String lendersContact,
375 String loaninContact,
376 String borrowersAuthorizer) {
377 LoansinCommon loanin = new LoansinCommon();
378 loanin.setLoanInNumber(loaninNumber);
379 loanin.setLoanInNumber(returnDate);
380 LenderGroupList lenderGroupList = new LenderGroupList();
381 LenderGroup lenderGroup = new LenderGroup();
382 lenderGroup.setLender(lender);
383 lenderGroup.setLendersAuthorizer(lendersAuthorizer);
384 lenderGroup.setLendersContact(lendersContact);
385 lenderGroupList.getLenderGroup().add(lenderGroup);
386 loanin.setLenderGroupList(lenderGroupList);
387 loanin.setLoanInContact(loaninContact);
388 loanin.setBorrowersAuthorizer(borrowersAuthorizer);
389 MultipartOutput multipart = new MultipartOutput();
390 OutputPart commonPart =
391 multipart.addPart(loanin, MediaType.APPLICATION_XML_TYPE);
392 commonPart.getHeaders().add("label", new LoaninClient().getCommonPartName());
394 if(logger.isDebugEnabled()){
395 logger.debug("to be created, loanin common");
396 logger.debug(objectAsXmlString(loanin, LoansinCommon.class));