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.LoaninClient;
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.loanin.LoansinCommon;
40 import org.collectionspace.services.loanin.LoansinCommonList;
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 * LoaninAuthRefsTest, carries out Authority References tests against a
56 * deployed and running Loanin (aka Loans In) Service.
58 * $LastChangedRevision: 1327 $
59 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
61 public class LoaninAuthRefsTest extends BaseServiceTest {
63 private final Logger logger =
64 LoggerFactory.getLogger(LoaninAuthRefsTest.class);
66 // Instance variables specific to this test.
67 final String SERVICE_PATH_COMPONENT = "loansin";
68 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
69 private String knownResourceId = null;
70 private List<String> loaninIdsCreated = 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 lendersAuthorizerRefName = null;
76 private String lendersContactRefName = null;
77 private String loanInContactRefName = null;
78 // FIXME: May change when repeatable / multivalue 'lenders' field is added
79 // to tenant-bindings.xml
80 private final int NUM_AUTH_REFS_EXPECTED = 3;
82 // ---------------------------------------------------------------
83 // CRUD tests : CREATE tests
84 // ---------------------------------------------------------------
86 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
87 public void createWithAuthRefs(String testName) throws Exception {
89 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
91 // Submit the request to the service and store the response.
92 String identifier = createIdentifier();
94 // Create all the person refs and entities
97 // Create a new Loans In resource.
99 // One or more fields in this resource will be PersonAuthority
100 // references, and will refer to Person resources by their refNames.
101 LoaninClient loaninClient = new LoaninClient();
102 MultipartOutput multipart = createLoaninInstance(
103 "loanInNumber-" + identifier,
104 "returnDate-" + identifier,
105 lendersAuthorizerRefName,
106 lendersContactRefName,
107 loanInContactRefName);
108 ClientResponse<Response> res = loaninClient.create(multipart);
109 int statusCode = res.getStatus();
111 // Check the status code of the response: does it match
112 // the expected response(s)?
115 // Does it fall within the set of valid status codes?
116 // Does it exactly match the expected status code?
117 if(logger.isDebugEnabled()){
118 logger.debug(testName + ": status = " + statusCode);
120 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
121 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
122 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
124 // Store the ID returned from the first resource created
125 // for additional tests below.
126 if (knownResourceId == null){
127 knownResourceId = extractId(res);
128 if (logger.isDebugEnabled()) {
129 logger.debug(testName + ": knownResourceId=" + knownResourceId);
133 // Store the IDs from every resource created by tests,
134 // so they can be deleted after tests have been run.
135 loaninIdsCreated.add(extractId(res));
138 protected void createPersonRefs(){
140 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
141 // Create a temporary PersonAuthority resource, and its corresponding
142 // refName by which it can be identified.
144 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
145 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
146 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
147 ClientResponse<Response> res = personAuthClient.create(multipart);
148 int statusCode = res.getStatus();
150 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
151 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
152 Assert.assertEquals(statusCode, CREATED_STATUS);
153 personAuthCSID = extractId(res);
155 // Create temporary Person resources, and their corresponding refNames
156 // by which they can be identified.
157 lendersAuthorizerRefName =
158 PersonAuthorityClientUtils.createPersonRefName(authRefName, "Art Lendersauthorizor", true);
159 personIdsCreated.add(createPerson("Art", "Lendersauthorizor", lendersAuthorizerRefName));
161 lendersContactRefName =
162 PersonAuthorityClientUtils.createPersonRefName(authRefName, "Larry Lenderscontact", true);
163 personIdsCreated.add(createPerson("Larry", "Lenderscontact", lendersContactRefName));
165 loanInContactRefName =
166 PersonAuthorityClientUtils.createPersonRefName(authRefName, "Carrie Loanincontact", true);
167 personIdsCreated.add(createPerson("Carrie", "Loanincontact", loanInContactRefName));
169 // FIXME: Add instance(s) of 'lenders' field when we can work with
170 // repeatable / multivalued authority reference fields. Be sure to
171 // change the NUM_AUTH_REFS_EXPECTED constant accordingly, or otherwise
172 // revise check for numbers of authority fields expected in readAndCheckAuthRefs.
175 protected String createPerson(String firstName, String surName, String refName ) {
176 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
177 Map<String, String> personInfo = new HashMap<String,String>();
178 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
179 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
180 MultipartOutput multipart =
181 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
182 refName, personInfo, personAuthClient.getItemCommonPartName());
183 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
184 int statusCode = res.getStatus();
186 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
187 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
188 Assert.assertEquals(statusCode, CREATED_STATUS);
189 return extractId(res);
193 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
194 dependsOnMethods = {"createWithAuthRefs"})
195 public void readAndCheckAuthRefs(String testName) throws Exception {
198 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
200 // Submit the request to the service and store the response.
201 LoaninClient loaninClient = new LoaninClient();
202 ClientResponse<MultipartInput> res = loaninClient.read(knownResourceId);
203 int statusCode = res.getStatus();
205 // Check the status code of the response: does it match
206 // the expected response(s)?
207 if(logger.isDebugEnabled()){
208 logger.debug(testName + ".read: status = " + statusCode);
210 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
211 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
212 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
214 MultipartInput input = (MultipartInput) res.getEntity();
215 LoansinCommon loanin = (LoansinCommon) extractPart(input,
216 loaninClient.getCommonPartName(), LoansinCommon.class);
217 Assert.assertNotNull(loanin);
218 if(logger.isDebugEnabled()){
219 logger.debug(objectAsXmlString(loanin, LoansinCommon.class));
221 // Check a couple of fields
223 Assert.assertEquals(loanin.getLendersAuthorizer(), lendersAuthorizerRefName);
224 Assert.assertEquals(loanin.getLendersContact(), lendersContactRefName);
225 Assert.assertEquals(loanin.getLoanInContact(), loanInContactRefName);
227 // Get the auth refs and check them
228 ClientResponse<AuthorityRefList> res2 =
229 loaninClient.getAuthorityRefs(knownResourceId);
230 statusCode = res2.getStatus();
232 if(logger.isDebugEnabled()){
233 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
235 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
236 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
237 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238 AuthorityRefList list = res2.getEntity();
240 // Optionally output additional data about list members for debugging.
241 boolean iterateThroughList = true;
242 if(iterateThroughList && logger.isDebugEnabled()){
243 List<AuthorityRefList.AuthorityRefItem> items =
244 list.getAuthorityRefItem();
246 for(AuthorityRefList.AuthorityRefItem item : items){
247 logger.debug(testName + ": list-item[" + i + "] Field:" +
248 item.getSourceField() + "= " +
249 item.getAuthDisplayName() +
250 item.getItemDisplayName());
251 logger.debug(testName + ": list-item[" + i + "] refName=" +
253 logger.debug(testName + ": list-item[" + i + "] URI=" +
257 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
262 // ---------------------------------------------------------------
263 // Cleanup of resources created during testing
264 // ---------------------------------------------------------------
267 * Deletes all resources created by tests, after all tests have been run.
269 * This cleanup method will always be run, even if one or more tests fail.
270 * For this reason, it attempts to remove all resources created
271 * at any point during testing, even if some of those resources
272 * may be expected to be deleted by certain tests.
274 @AfterClass(alwaysRun=true)
275 public void cleanUp() {
276 String noTest = System.getProperty("noTestCleanup");
277 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
278 if (logger.isDebugEnabled()) {
279 logger.debug("Skipping Cleanup phase ...");
283 if (logger.isDebugEnabled()) {
284 logger.debug("Cleaning up temporary resources created for testing ...");
286 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
287 // Delete Person resource(s) (before PersonAuthority resources).
288 ClientResponse<Response> res;
289 for (String resourceId : personIdsCreated) {
290 // Note: Any non-success responses are ignored and not reported.
291 res = personAuthClient.deleteItem(personAuthCSID, resourceId);
293 // Delete PersonAuthority resource(s).
294 // Note: Any non-success response is ignored and not reported.
295 res = personAuthClient.delete(personAuthCSID);
296 // Delete Loans In resource(s).
297 LoaninClient loaninClient = new LoaninClient();
298 for (String resourceId : loaninIdsCreated) {
299 // Note: Any non-success responses are ignored and not reported.
300 res = loaninClient.delete(resourceId);
304 // ---------------------------------------------------------------
305 // Utility methods used by tests above
306 // ---------------------------------------------------------------
308 public String getServicePathComponent() {
309 return SERVICE_PATH_COMPONENT;
312 private MultipartOutput createLoaninInstance(String loaninNumber,
314 String lendersAuthorizer,
315 String lendersContact,
316 String loaninContact) {
317 LoansinCommon loanin = new LoansinCommon();
318 loanin.setLoanInNumber(loaninNumber);
319 loanin.setLoanInNumber(returnDate);
320 loanin.setLendersAuthorizer(lendersAuthorizer);
321 loanin.setLendersContact(lendersContact);
322 loanin.setLoanInContact(loaninContact);
323 MultipartOutput multipart = new MultipartOutput();
324 OutputPart commonPart =
325 multipart.addPart(loanin, MediaType.APPLICATION_XML_TYPE);
326 commonPart.getHeaders().add("label", new LoaninClient().getCommonPartName());
328 if(logger.isDebugEnabled()){
329 logger.debug("to be created, loanin common");
330 logger.debug(objectAsXmlString(loanin, LoansinCommon.class));