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.LoansinCommon;
42 //import org.collectionspace.services.loanin.LoansinCommonList;
44 import org.jboss.resteasy.client.ClientResponse;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
48 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * LoaninAuthRefsTest, carries out Authority References tests against a
58 * deployed and running Loanin (aka Loans In) Service.
60 * $LastChangedRevision$
63 public class LoaninAuthRefsTest extends BaseServiceTest {
65 private final String CLASS_NAME = LoaninAuthRefsTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
68 // Instance variables specific to this test.
69 final String SERVICE_PATH_COMPONENT = "loansin";
70 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
71 private String knownResourceId = null;
72 private List<String> loaninIdsCreated = new ArrayList<String>();
73 private List<String> personIdsCreated = new ArrayList<String>();
74 private String personAuthCSID = null;
75 private String lenderRefName = null;
76 private String lendersAuthorizerRefName = null;
77 private String lendersContactRefName = null;
78 private String loanInContactRefName = null;
79 // FIXME: May change when repeatable / multivalue 'lenders' field is added
80 // to tenant-bindings.xml
81 private final int NUM_AUTH_REFS_EXPECTED = 4;
84 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
87 protected CollectionSpaceClient getClientInstance() {
88 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
92 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
95 protected AbstractCommonList getAbstractCommonList(
96 ClientResponse<AbstractCommonList> response) {
97 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
100 // ---------------------------------------------------------------
101 // CRUD tests : CREATE tests
102 // ---------------------------------------------------------------
104 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
105 public void createWithAuthRefs(String testName) throws Exception {
107 if (logger.isDebugEnabled()) {
108 logger.debug(testBanner(testName, CLASS_NAME));
110 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
112 // Submit the request to the service and store the response.
113 String identifier = createIdentifier();
115 // Create all the person refs and entities
118 // Create a new Loans In resource.
120 // One or more fields in this resource will be PersonAuthority
121 // references, and will refer to Person resources by their refNames.
122 LoaninClient loaninClient = new LoaninClient();
123 MultipartOutput multipart = createLoaninInstance(
124 "loanInNumber-" + identifier,
125 "returnDate-" + identifier,
127 lendersAuthorizerRefName,
128 lendersContactRefName,
129 loanInContactRefName);
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(REQUEST_TYPE.isValidStatusCode(statusCode),
143 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
144 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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 MultipartOutput 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(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, 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 // FIXME: Add instance(s) of 'lenders' field when we can work with
199 // repeatable / multivalued authority reference fields. Be sure to
200 // change the NUM_AUTH_REFS_EXPECTED constant accordingly, or otherwise
201 // revise check for numbers of authority fields expected in readAndCheckAuthRefs.
204 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
205 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
206 Map<String, String> personInfo = new HashMap<String,String>();
207 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
208 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
209 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
210 MultipartOutput multipart =
211 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
212 authRefName, personInfo, personAuthClient.getItemCommonPartName());
213 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
214 int statusCode = res.getStatus();
216 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
217 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
218 Assert.assertEquals(statusCode, STATUS_CREATED);
219 return extractId(res);
223 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
224 dependsOnMethods = {"createWithAuthRefs"})
225 public void readAndCheckAuthRefs(String testName) throws Exception {
227 if (logger.isDebugEnabled()) {
228 logger.debug(testBanner(testName, CLASS_NAME));
231 testSetup(STATUS_OK, ServiceRequestType.READ);
233 // Submit the request to the service and store the response.
234 LoaninClient loaninClient = new LoaninClient();
235 ClientResponse<MultipartInput> res = loaninClient.read(knownResourceId);
236 int statusCode = res.getStatus();
238 // Check the status code of the response: does it match
239 // the expected response(s)?
240 if(logger.isDebugEnabled()){
241 logger.debug(testName + ".read: status = " + statusCode);
243 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
244 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
245 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
247 MultipartInput input = (MultipartInput) res.getEntity();
248 LoansinCommon loanin = (LoansinCommon) extractPart(input,
249 loaninClient.getCommonPartName(), LoansinCommon.class);
250 Assert.assertNotNull(loanin);
251 if(logger.isDebugEnabled()){
252 logger.debug(objectAsXmlString(loanin, LoansinCommon.class));
254 // Check a couple of fields
255 Assert.assertEquals(loanin.getLender(), lenderRefName);
256 Assert.assertEquals(loanin.getLendersAuthorizer(), lendersAuthorizerRefName);
257 Assert.assertEquals(loanin.getLendersContact(), lendersContactRefName);
258 Assert.assertEquals(loanin.getLoanInContact(), loanInContactRefName);
260 // Get the auth refs and check them
261 ClientResponse<AuthorityRefList> res2 =
262 loaninClient.getAuthorityRefs(knownResourceId);
263 statusCode = res2.getStatus();
265 if(logger.isDebugEnabled()){
266 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
268 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
271 AuthorityRefList list = res2.getEntity();
273 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
274 int numAuthRefsFound = items.size();
275 if(logger.isDebugEnabled()){
276 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
277 " authority references, found " + numAuthRefsFound);
280 // Optionally output additional data about list members for debugging.
281 boolean iterateThroughList = true;
282 if(iterateThroughList && logger.isDebugEnabled()){
284 for(AuthorityRefList.AuthorityRefItem item : items){
285 logger.debug(testName + ": list-item[" + i + "] Field:" +
286 item.getSourceField() + "= " +
287 item.getAuthDisplayName() +
288 item.getItemDisplayName());
289 logger.debug(testName + ": list-item[" + i + "] refName=" +
291 logger.debug(testName + ": list-item[" + i + "] URI=" +
297 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
298 "Did not find all expected authority references! " +
299 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
304 // ---------------------------------------------------------------
305 // Cleanup of resources created during testing
306 // ---------------------------------------------------------------
309 * Deletes all resources created by tests, after all tests have been run.
311 * This cleanup method will always be run, even if one or more tests fail.
312 * For this reason, it attempts to remove all resources created
313 * at any point during testing, even if some of those resources
314 * may be expected to be deleted by certain tests.
316 @AfterClass(alwaysRun=true)
317 public void cleanUp() {
318 String noTest = System.getProperty("noTestCleanup");
319 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
320 if (logger.isDebugEnabled()) {
321 logger.debug("Skipping Cleanup phase ...");
325 if (logger.isDebugEnabled()) {
326 logger.debug("Cleaning up temporary resources created for testing ...");
328 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
329 // Delete Person resource(s) (before PersonAuthority resources).
331 for (String resourceId : personIdsCreated) {
332 // Note: Any non-success responses are ignored and not reported.
333 ClientResponse<Response> response =
334 personAuthClient.deleteItem(personAuthCSID, resourceId); // alternative to personAuthClient.deleteItem().releaseConnection();
335 response.releaseConnection();
338 // Delete PersonAuthority resource(s).
339 // Note: Any non-success response is ignored and not reported.
340 if (personAuthCSID != null) {
341 personAuthClient.delete(personAuthCSID);
342 // Delete Loans In resource(s).
343 LoaninClient loaninClient = new LoaninClient();
344 ClientResponse<Response> response = null;
345 for (String resourceId : loaninIdsCreated) {
346 // Note: Any non-success responses are ignored and not reported.
347 response = loaninClient.delete(resourceId); // alternative to loaninClient.delete(resourceId).releaseConnection();
348 response.releaseConnection();
353 // ---------------------------------------------------------------
354 // Utility methods used by tests above
355 // ---------------------------------------------------------------
357 public String getServicePathComponent() {
358 return SERVICE_PATH_COMPONENT;
361 private MultipartOutput createLoaninInstance(String loaninNumber,
364 String lendersAuthorizer,
365 String lendersContact,
366 String loaninContact) {
367 LoansinCommon loanin = new LoansinCommon();
368 loanin.setLoanInNumber(loaninNumber);
369 loanin.setLoanInNumber(returnDate);
370 loanin.setLender(lender);
371 loanin.setLendersAuthorizer(lendersAuthorizer);
372 loanin.setLendersContact(lendersContact);
373 loanin.setLoanInContact(loaninContact);
374 MultipartOutput multipart = new MultipartOutput();
375 OutputPart commonPart =
376 multipart.addPart(loanin, MediaType.APPLICATION_XML_TYPE);
377 commonPart.getHeaders().add("label", new LoaninClient().getCommonPartName());
379 if(logger.isDebugEnabled()){
380 logger.debug("to be created, loanin common");
381 logger.debug(objectAsXmlString(loanin, LoansinCommon.class));