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.CollectionObjectClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
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.collectionobject.CollectionobjectsCommon;
40 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.jboss.resteasy.client.ClientResponse;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * CollectionObjectAuthRefsTest, carries out tests against a
57 * deployed and running CollectionObject Service.
59 * $LastChangedRevision: 1327 $
60 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
62 public class CollectionObjectAuthRefsTest extends BaseServiceTest {
65 private final Logger logger =
66 LoggerFactory.getLogger(CollectionObjectAuthRefsTest.class);
68 // Instance variables specific to this test.
69 /** The SERVIC e_ pat h_ component. */
70 final String SERVICE_PATH_COMPONENT = "collectionobjects";
72 /** The PERSO n_ authorit y_ name. */
73 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
75 /** The known resource id. */
76 private String knownResourceId = null;
78 /** The collection object ids created. */
79 private List<String> collectionObjectIdsCreated = new ArrayList<String>();
81 /** The person ids created. */
82 private List<String> personIdsCreated = new ArrayList<String>();
84 /** The CREATE d_ status. */
85 private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
87 /** The O k_ status. */
88 private int OK_STATUS = Response.Status.OK.getStatusCode();
90 /** The person auth csid. */
91 private String personAuthCSID = null;
93 /** The content organization ref name. */
94 private String contentOrganizationRefName = null;
96 /** The content people ref name. */
97 private String contentPeopleRefName = null;
99 /** The content person ref name. */
100 private String contentPersonRefName = null;
102 /** The inscriber ref name. */
103 private String inscriberRefName = null;
105 /** The NU m_ aut h_ ref s_ expected. */
106 private final int NUM_AUTH_REFS_EXPECTED = 4;
109 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
112 protected CollectionSpaceClient getClientInstance() {
113 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
117 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
120 protected AbstractCommonList getAbstractCommonList(
121 ClientResponse<AbstractCommonList> response) {
122 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
125 // ---------------------------------------------------------------
126 // CRUD tests : CREATE tests
127 // ---------------------------------------------------------------
130 * Creates the with auth refs.
132 * @param testName the test name
133 * @throws Exception the exception
135 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
136 public void createWithAuthRefs(String testName) throws Exception {
138 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
140 // Submit the request to the service and store the response.
141 String identifier = createIdentifier();
143 // Create all the person refs and entities
146 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
147 MultipartOutput multipart = createCollectionObjectInstance(
150 contentOrganizationRefName,
151 contentPeopleRefName,
152 contentPersonRefName,
155 ClientResponse<Response> res = collectionObjectClient.create(multipart);
157 int statusCode = res.getStatus();
159 // Check the status code of the response: does it match
160 // the expected response(s)?
163 // Does it fall within the set of valid status codes?
164 // Does it exactly match the expected status code?
165 if(logger.isDebugEnabled()){
166 logger.debug(testName + ": status = " + statusCode);
168 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
169 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
170 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
172 // Store the ID returned from the first resource created
173 // for additional tests below.
174 if (knownResourceId == null){
175 knownResourceId = extractId(res);
176 if (logger.isDebugEnabled()) {
177 logger.debug(testName + ": knownResourceId=" + knownResourceId);
181 // Store the IDs from every resource created by tests,
182 // so they can be deleted after tests have been run.
183 collectionObjectIdsCreated.add(extractId(res));
187 * Creates the person refs.
189 protected void createPersonRefs(){
191 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
192 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
193 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
194 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
195 ClientResponse<Response> res = personAuthClient.create(multipart);
196 int statusCode = res.getStatus();
198 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200 Assert.assertEquals(statusCode, CREATED_STATUS);
201 personAuthCSID = extractId(res);
203 contentOrganizationRefName = PersonAuthorityClientUtils.createPersonRefName(
204 authRefName, "Omni Org", true);
205 personIdsCreated.add(createPerson("Omni", "Org", contentOrganizationRefName));
207 contentPeopleRefName = PersonAuthorityClientUtils.createPersonRefName(
208 authRefName, "Pushy People", true);
209 personIdsCreated.add(createPerson("Pushy", "People", contentPeopleRefName));
211 contentPersonRefName = PersonAuthorityClientUtils.createPersonRefName(
212 authRefName, "Connie ContactPerson", true);
213 personIdsCreated.add(createPerson("Connie", "ContactPerson", contentPersonRefName));
215 inscriberRefName = PersonAuthorityClientUtils.createPersonRefName(
216 authRefName, "Ingrid Inscriber", true);
217 personIdsCreated.add(createPerson("Ingrid", "Inscriber", inscriberRefName));
221 * Creates the person.
223 * @param firstName the first name
224 * @param surName the sur name
225 * @param refName the ref name
228 protected String createPerson(String firstName, String surName, String refName ) {
229 Map<String, String> personInfo = new HashMap<String,String>();
230 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
231 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
232 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
233 MultipartOutput multipart =
234 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
235 refName, personInfo, personAuthClient.getItemCommonPartName());
236 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
237 int statusCode = res.getStatus();
239 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241 Assert.assertEquals(statusCode, CREATED_STATUS);
242 return extractId(res);
247 * Read and check auth refs.
249 * @param testName the test name
250 * @throws Exception the exception
252 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
253 dependsOnMethods = {"createWithAuthRefs"})
254 public void readAndCheckAuthRefs(String testName) throws Exception {
257 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
259 // Submit the request to the service and store the response.
260 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
261 ClientResponse<MultipartInput> res = collectionObjectClient.read(knownResourceId);
262 int statusCode = res.getStatus();
264 // Check the status code of the response: does it match
265 // the expected response(s)?
266 if(logger.isDebugEnabled()){
267 logger.debug(testName + ".read: status = " + statusCode);
269 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
270 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
271 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
273 MultipartInput input = (MultipartInput) res.getEntity();
274 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
275 collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);
276 Assert.assertNotNull(collectionObject);
277 // Check a couple of fields
278 Assert.assertEquals(collectionObject.getContentOrganization(), contentOrganizationRefName);
279 Assert.assertEquals(collectionObject.getInscriber(), inscriberRefName);
281 // Get the auth refs and check them
282 ClientResponse<AuthorityRefList> res2 = collectionObjectClient.getAuthorityRefs(knownResourceId);
283 statusCode = res2.getStatus();
285 if(logger.isDebugEnabled()){
286 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
288 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291 AuthorityRefList list = res2.getEntity();
293 // Optionally output additional data about list members for debugging.
294 boolean iterateThroughList = true;
295 if(iterateThroughList && logger.isDebugEnabled()){
296 List<AuthorityRefList.AuthorityRefItem> items =
297 list.getAuthorityRefItem();
299 for(AuthorityRefList.AuthorityRefItem item : items){
300 logger.debug(testName + ": list-item[" + i + "] Field:" +
301 item.getSourceField() + "= " +
302 item.getAuthDisplayName() +
303 item.getItemDisplayName());
304 logger.debug(testName + ": list-item[" + i + "] refName=" +
306 logger.debug(testName + ": list-item[" + i + "] URI=" +
310 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
315 // ---------------------------------------------------------------
316 // Cleanup of resources created during testing
317 // ---------------------------------------------------------------
320 * Deletes all resources created by tests, after all tests have been run.
322 * This cleanup method will always be run, even if one or more tests fail.
323 * For this reason, it attempts to remove all resources created
324 * at any point during testing, even if some of those resources
325 * may be expected to be deleted by certain tests.
327 @AfterClass(alwaysRun=true)
328 public void cleanUp() {
329 String noTest = System.getProperty("noTestCleanup");
330 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
331 if (logger.isDebugEnabled()) {
332 logger.debug("Skipping Cleanup phase ...");
336 if (logger.isDebugEnabled()) {
337 logger.debug("Cleaning up temporary resources created for testing ...");
339 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
340 for (String resourceId : collectionObjectIdsCreated) {
341 // Note: Any non-success responses are ignored and not reported.
342 collectionObjectClient.delete(resourceId).releaseConnection();
344 // Note: Any non-success response is ignored and not reported.
345 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
346 // Delete persons before PersonAuth
347 for (String resourceId : personIdsCreated) {
348 // Note: Any non-success responses are ignored and not reported.
349 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
351 personAuthClient.delete(personAuthCSID).releaseConnection();
354 // ---------------------------------------------------------------
355 // Utility methods used by tests above
356 // ---------------------------------------------------------------
358 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
361 public String getServicePathComponent() {
362 return SERVICE_PATH_COMPONENT;
366 * Creates the collection object instance.
368 * @param title the title
369 * @param objNum the obj num
370 * @param contentOrganization the content organization
371 * @param contentPeople the content people
372 * @param contentPerson the content person
373 * @param inscriber the inscriber
374 * @return the multipart output
376 private MultipartOutput createCollectionObjectInstance(
379 String contentOrganization,
380 String contentPeople,
381 String contentPerson,
383 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
384 collectionObject.setTitle(title);
385 collectionObject.setObjectNumber(objNum);
386 collectionObject.setContentOrganization(contentOrganization);
387 collectionObject.setContentPeople(contentPeople);
388 collectionObject.setContentPerson(contentPerson);
389 collectionObject.setInscriber(inscriber);
390 MultipartOutput multipart = new MultipartOutput();
391 OutputPart commonPart =
392 multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
393 commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
395 if(logger.isDebugEnabled()){
396 logger.debug("to be created, collectionObject common");
397 logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));