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.ContactClient;
36 import org.collectionspace.services.client.ObjectExitClient;
37 import org.collectionspace.services.client.PayloadOutputPart;
38 import org.collectionspace.services.client.PersonAuthorityClient;
39 import org.collectionspace.services.client.PersonAuthorityClientUtils;
40 import org.collectionspace.services.client.PoxPayloadIn;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.common.authorityref.AuthorityRefList;
43 import org.collectionspace.services.jaxb.AbstractCommonList;
44 import org.collectionspace.services.objectexit.ObjectexitCommon;
46 import org.jboss.resteasy.client.ClientResponse;
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 * ObjectExitAuthRefsTest, carries out Authority References tests against a deployed and running ObjectExit (aka Loans Out) Service.
58 * $LastChangedRevision: $
61 public class ObjectExitAuthRefsTest extends BaseServiceTest {
63 private final String CLASS_NAME = ObjectExitAuthRefsTest.class.getName();
64 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
65 final String PERSON_AUTHORITY_NAME = "ObjectexitPersonAuth";
66 private String knownResourceId = null;
67 private List<String> objectexitIdsCreated = new ArrayList<String>();
68 private List<String> personIdsCreated = new ArrayList<String>();
69 private String personAuthCSID = null;
70 private String depositorRefName = null;
71 private String exitDate = null;
72 private String exitNumber = null;
75 public String getServicePathComponent() {
76 return ObjectExitClient.SERVICE_PATH_COMPONENT;
80 protected String getServiceName() {
81 return ObjectExitClient.SERVICE_NAME;
85 protected CollectionSpaceClient getClientInstance() {
86 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
90 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
91 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
94 private PoxPayloadOut createObjectExitInstance(String depositorRefName, String exitNumber, String exitDate) {
95 this.exitDate = exitDate;
96 this.exitNumber = exitNumber;
97 this.depositorRefName = depositorRefName;
98 ObjectexitCommon objectexit = new ObjectexitCommon();
99 objectexit.setDepositor(depositorRefName);
100 objectexit.setExitNumber(exitNumber);
101 objectexit.setExitDate(exitDate);
103 PoxPayloadOut multipart = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
104 PayloadOutputPart commonPart = multipart.addPart(objectexit, MediaType.APPLICATION_XML_TYPE);
105 commonPart.setLabel(new ObjectExitClient().getCommonPartName());
106 logger.debug("to be created, objectexit common: " + objectAsXmlString(objectexit, ObjectexitCommon.class));
110 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
111 public void createWithAuthRefs(String testName) throws Exception {
112 logger.debug(testBanner(testName, CLASS_NAME));
113 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
114 String identifier = createIdentifier(); // Submit the request to the service and store the response.
115 createPersonRefs();// Create all the person refs and entities
116 // Create a new Loans In resource. One or more fields in this resource will be PersonAuthority
117 // references, and will refer to Person resources by their refNames.
118 ObjectExitClient objectexitClient = new ObjectExitClient();
119 PoxPayloadOut multipart = createObjectExitInstance(depositorRefName, "exitNumber-" + identifier, "exitDate-" + identifier);
120 ClientResponse<Response> res = objectexitClient.create(multipart);
121 assertStatusCode(res, testName);
122 if (knownResourceId == null) {// Store the ID returned from the first resource created for additional tests below.
123 knownResourceId = extractId(res);
125 objectexitIdsCreated.add(extractId(res));// Store the IDs from every resource created; delete on cleanup
128 protected void createPersonRefs() {
129 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
130 // Create a temporary PersonAuthority resource, and its corresponding refName by which it can be identified.
131 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
132 ClientResponse<Response> res = personAuthClient.create(multipart);
133 assertStatusCode(res, "createPersonRefs (not a surefire test)");
134 personAuthCSID = extractId(res);
135 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
136 // Create temporary Person resources, and their corresponding refNames by which they can be identified.
139 csid = createPerson("Owen the Cur", "Owner", "owenCurOwner", authRefName);
140 personIdsCreated.add(csid);
141 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
143 csid = createPerson("Davenport", "Depositor", "davenportDepositor", authRefName);
144 personIdsCreated.add(csid);
145 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
148 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
149 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
150 Map<String, String> personInfo = new HashMap<String, String>();
151 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
152 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
153 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
154 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, authRefName, personInfo, personAuthClient.getItemCommonPartName());
155 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
156 assertStatusCode(res, "createPerson (not a surefire test)");
157 return extractId(res);
160 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"createWithAuthRefs"})
161 public void readAndCheckAuthRefs(String testName) throws Exception {
162 logger.debug(testBanner(testName, CLASS_NAME));
163 testSetup(STATUS_OK, ServiceRequestType.READ);
164 ObjectExitClient objectexitClient = new ObjectExitClient();
165 ClientResponse<String> res = objectexitClient.read(knownResourceId);
166 assertStatusCode(res, testName);
167 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
168 ObjectexitCommon objectexit = (ObjectexitCommon) extractPart(input, objectexitClient.getCommonPartName(), ObjectexitCommon.class);
169 Assert.assertNotNull(objectexit);
170 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));
172 // Check a couple of fields
173 Assert.assertEquals(objectexit.getDepositor(), depositorRefName);
174 Assert.assertEquals(objectexit.getExitDate(), exitDate);
175 Assert.assertEquals(objectexit.getExitNumber(), exitNumber);
177 // Get the auth refs and check them
178 ClientResponse<AuthorityRefList> res2 = objectexitClient.getAuthorityRefs(knownResourceId);
179 assertStatusCode(res2, testName);
180 AuthorityRefList list = res2.getEntity();
181 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
182 int numAuthRefsFound = items.size();
183 logger.debug("Authority references, found " + numAuthRefsFound);
184 //Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
185 // "Did not find all expected authority references! " +
186 // "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
187 if (logger.isDebugEnabled()) {
189 for (AuthorityRefList.AuthorityRefItem item : items) {
190 logger.debug(testName + ": list-item[" + i + "] Field:" + item.getSourceField() + "= " + item.getAuthDisplayName() + item.getItemDisplayName());
191 logger.debug(testName + ": list-item[" + i + "] refName=" + item.getRefName());
192 logger.debug(testName + ": list-item[" + i + "] URI=" + item.getUri());
199 * Deletes all resources created by tests, after all tests have been run.
201 * This cleanup method will always be run, even if one or more tests fail.
202 * For this reason, it attempts to remove all resources created
203 * at any point during testing, even if some of those resources
204 * may be expected to be deleted by certain tests.
206 @AfterClass(alwaysRun = true)
207 public void cleanUp() {
208 String noTest = System.getProperty("noTestCleanup");
209 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
210 logger.debug("Skipping Cleanup phase ...");
213 logger.debug("Cleaning up temporary resources created for testing ...");
214 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
215 // Delete Person resource(s) (before PersonAuthority resources).
216 for (String resourceId : personIdsCreated) {
217 // Note: Any non-success responses are ignored and not reported.
218 personAuthClient.deleteItem(personAuthCSID, resourceId);
220 // Delete PersonAuthority resource(s).
221 // Note: Any non-success response is ignored and not reported.
222 if (personAuthCSID != null) {
223 personAuthClient.delete(personAuthCSID);
224 // Delete Loans In resource(s).
225 ObjectExitClient objectexitClient = new ObjectExitClient();
226 for (String resourceId : objectexitIdsCreated) {
227 // Note: Any non-success responses are ignored and not reported.
228 objectexitClient.delete(resourceId);