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.LoanoutClient;
35 import org.collectionspace.services.client.PersonAuthorityClient;
36 import org.collectionspace.services.client.PersonAuthorityClientUtils;
37 import org.collectionspace.services.common.authorityref.AuthorityRefList;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList.AuthorityRefItem;
39 import org.collectionspace.services.loanout.LoansoutCommon;
40 import org.collectionspace.services.loanout.LoansoutCommonList;
42 import org.jboss.resteasy.client.ClientResponse;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * LoanoutAuthRefsTest, carries out Authority References tests against a
56 * deployed and running Loanout (aka Loans Out) Service.
58 * $LastChangedRevision: 1327 $
59 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
61 public class LoanoutAuthRefsTest extends BaseServiceTest {
63 private final Logger logger =
64 LoggerFactory.getLogger(LoanoutAuthRefsTest.class);
66 // Instance variables specific to this test.
67 final String SERVICE_PATH_COMPONENT = "loansout";
68 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
69 private String knownResourceId = null;
70 private List<String> loanoutIdsCreated = new ArrayList();
71 private List<String> personIdsCreated = new ArrayList();
72 private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
73 private int OK_STATUS = Response.Status.OK.getStatusCode();
74 private String personAuthCSID = null;
75 private String borrowersContactRefName = null;
76 private String lendersAuthorizerRefName = null;
77 private String lendersContactRefName = null;
79 // FIXME: Can add 'borrower' - likely to be an organization
80 // authority - as an authRef to tests below, and increase the
81 // number of expected authRefs to 4.
82 private final int NUM_AUTH_REFS_EXPECTED = 3;
84 // ---------------------------------------------------------------
85 // CRUD tests : CREATE tests
86 // ---------------------------------------------------------------
88 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
89 public void createWithAuthRefs(String testName) throws Exception {
91 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
93 // Submit the request to the service and store the response.
94 String identifier = createIdentifier();
96 // Create all the person refs and entities
99 // Create a new Loans In resource.
101 // One or more fields in this resource will be PersonAuthority
102 // references, and will refer to Person resources by their refNames.
103 LoanoutClient loanoutClient = new LoanoutClient();
104 MultipartOutput multipart = createLoanoutInstance(
105 "loanOutNumber-" + identifier,
106 "returnDate-" + identifier,
107 borrowersContactRefName,
108 lendersAuthorizerRefName,
109 lendersContactRefName);
110 ClientResponse<Response> res = loanoutClient.create(multipart);
111 int statusCode = res.getStatus();
113 // Check the status code of the response: does it match
114 // the expected response(s)?
117 // Does it fall within the set of valid status codes?
118 // Does it exactly match the expected status code?
119 if(logger.isDebugEnabled()){
120 logger.debug(testName + ": status = " + statusCode);
122 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
126 // Store the ID returned from the first resource created
127 // for additional tests below.
128 if (knownResourceId == null){
129 knownResourceId = extractId(res);
130 if (logger.isDebugEnabled()) {
131 logger.debug(testName + ": knownResourceId=" + knownResourceId);
135 // Store the IDs from every resource created by tests,
136 // so they can be deleted after tests have been run.
137 loanoutIdsCreated.add(extractId(res));
140 protected void createPersonRefs(){
142 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
143 // Create a temporary PersonAuthority resource, and its corresponding
144 // refName by which it can be identified.
146 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
147 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
148 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
149 ClientResponse<Response> res = personAuthClient.create(multipart);
150 int statusCode = res.getStatus();
152 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
153 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
154 Assert.assertEquals(statusCode, CREATED_STATUS);
155 personAuthCSID = extractId(res);
157 // Create temporary Person resources, and their corresponding refNames
158 // by which they can be identified.
159 borrowersContactRefName =
160 PersonAuthorityClientUtils.createPersonRefName(authRefName, "Bradley BorrowersContact", true);
161 personIdsCreated.add(createPerson("Bradley", "BorrowersContact", borrowersContactRefName));
163 lendersAuthorizerRefName =
164 PersonAuthorityClientUtils.createPersonRefName(authRefName, "Art Lendersauthorizor", true);
165 personIdsCreated.add(createPerson("Art", "Lendersauthorizor", lendersAuthorizerRefName));
167 lendersContactRefName =
168 PersonAuthorityClientUtils.createPersonRefName(authRefName, "Larry Lenderscontact", true);
169 personIdsCreated.add(createPerson("Larry", "Lenderscontact", lendersContactRefName));
172 protected String createPerson(String firstName, String surName, String refName ) {
173 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
174 Map<String, String> personInfo = new HashMap<String,String>();
175 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
176 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
177 MultipartOutput multipart =
178 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
179 refName, personInfo, personAuthClient.getItemCommonPartName());
180 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
181 int statusCode = res.getStatus();
183 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
184 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
185 Assert.assertEquals(statusCode, CREATED_STATUS);
186 return extractId(res);
190 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
191 dependsOnMethods = {"createWithAuthRefs"})
192 public void readAndCheckAuthRefs(String testName) throws Exception {
195 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
197 // Submit the request to the service and store the response.
198 LoanoutClient loanoutClient = new LoanoutClient();
199 ClientResponse<MultipartInput> res = loanoutClient.read(knownResourceId);
200 int statusCode = res.getStatus();
202 // Check the status code of the response: does it match
203 // the expected response(s)?
204 if(logger.isDebugEnabled()){
205 logger.debug(testName + ".read: status = " + statusCode);
207 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
208 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
209 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
211 MultipartInput input = (MultipartInput) res.getEntity();
212 LoansoutCommon loanout = (LoansoutCommon) extractPart(input,
213 loanoutClient.getCommonPartName(), LoansoutCommon.class);
214 Assert.assertNotNull(loanout);
215 if(logger.isDebugEnabled()){
216 logger.debug(objectAsXmlString(loanout, LoansoutCommon.class));
218 // Check a couple of fields
220 Assert.assertEquals(loanout.getLendersAuthorizer(), lendersAuthorizerRefName);
221 Assert.assertEquals(loanout.getLendersContact(), lendersContactRefName);
223 // Get the auth refs and check them
224 ClientResponse<AuthorityRefList> res2 =
225 loanoutClient.getAuthorityRefs(knownResourceId);
226 statusCode = res2.getStatus();
228 if(logger.isDebugEnabled()){
229 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
231 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
232 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
233 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
234 AuthorityRefList list = res2.getEntity();
236 // Optionally output additional data about list members for debugging.
237 boolean iterateThroughList = true;
238 if(iterateThroughList && logger.isDebugEnabled()){
239 List<AuthorityRefList.AuthorityRefItem> items =
240 list.getAuthorityRefItem();
242 for(AuthorityRefList.AuthorityRefItem item : items){
243 logger.debug(testName + ": list-item[" + i + "] Field:" +
244 item.getSourceField() + "= " +
245 item.getAuthDisplayName() +
246 item.getItemDisplayName());
247 logger.debug(testName + ": list-item[" + i + "] refName=" +
249 logger.debug(testName + ": list-item[" + i + "] URI=" +
253 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
258 // ---------------------------------------------------------------
259 // Cleanup of resources created during testing
260 // ---------------------------------------------------------------
263 * Deletes all resources created by tests, after all tests have been run.
265 * This cleanup method will always be run, even if one or more tests fail.
266 * For this reason, it attempts to remove all resources created
267 * at any point during testing, even if some of those resources
268 * may be expected to be deleted by certain tests.
270 @AfterClass(alwaysRun=true)
271 public void cleanUp() {
272 if (logger.isDebugEnabled()) {
273 logger.debug("Cleaning up temporary resources created for testing ...");
275 // Note: Any non-success responses are ignored and not reported.
277 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
278 // Delete Person resource(s) (before PersonAuthority resources).
279 ClientResponse<Response> res;
280 for (String resourceId : personIdsCreated) {
281 res = personAuthClient.deleteItem(personAuthCSID, resourceId);
283 // Delete PersonAuthority resource(s).
284 res = personAuthClient.delete(personAuthCSID);
285 // Delete Loans In resource(s).
286 LoanoutClient loanoutClient = new LoanoutClient();
287 for (String resourceId : loanoutIdsCreated) {
288 res = loanoutClient.delete(resourceId);
292 // ---------------------------------------------------------------
293 // Utility methods used by tests above
294 // ---------------------------------------------------------------
296 public String getServicePathComponent() {
297 return SERVICE_PATH_COMPONENT;
300 private MultipartOutput createLoanoutInstance(String loanoutNumber,
302 String borrowersContact,
303 String lendersAuthorizer,
304 String lendersContact) {
305 LoansoutCommon loanout = new LoansoutCommon();
306 loanout.setLoanOutNumber(loanoutNumber);
307 loanout.setLoanReturnDate(returnDate);
308 loanout.setBorrowersContact(lendersContact);
309 loanout.setLendersAuthorizer(lendersAuthorizer);
310 loanout.setLendersContact(lendersContact);
311 MultipartOutput multipart = new MultipartOutput();
312 OutputPart commonPart =
313 multipart.addPart(loanout, MediaType.APPLICATION_XML_TYPE);
314 commonPart.getHeaders().add("label", new LoanoutClient().getCommonPartName());
316 if(logger.isDebugEnabled()){
317 logger.debug("to be created, loanout common");
318 logger.debug(objectAsXmlString(loanout, LoansoutCommon.class));