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.ObjectExitClient;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PersonAuthorityClient;
38 import org.collectionspace.services.client.PersonAuthorityClientUtils;
39 import org.collectionspace.services.client.PoxPayloadIn;
40 import org.collectionspace.services.client.PoxPayloadOut;
41 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
42 import org.collectionspace.services.common.authorityref.AuthorityRefList;
43 import org.collectionspace.services.jaxb.AbstractCommonList;
44 import org.collectionspace.services.objectexit.StructuredDateGroup;
45 import org.collectionspace.services.objectexit.ObjectexitCommon;
46 import org.collectionspace.services.person.PersonTermGroup;
48 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.Test;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * ObjectExitAuthRefsTest, carries out Authority References tests against a deployed and running ObjectExit (aka Loans Out) Service.
59 * $LastChangedRevision: $
62 public class ObjectExitAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
64 private final String CLASS_NAME = ObjectExitAuthRefsTest.class.getName();
65 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
66 final String PERSON_AUTHORITY_NAME = "ObjectexitPersonAuth";
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 StructuredDateGroup exitDateGroup = new StructuredDateGroup();
72 private String exitNumber = null;
73 private final static String CURRENT_DATE_UTC =
74 GregorianCalendarDateTimeUtils.currentDateUTC();
77 public String getServicePathComponent() {
78 return ObjectExitClient.SERVICE_PATH_COMPONENT;
82 protected String getServiceName() {
83 return ObjectExitClient.SERVICE_NAME;
87 protected CollectionSpaceClient getClientInstance() {
88 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
92 protected AbstractCommonList getCommonList(Response response) {
93 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
96 private PoxPayloadOut createObjectExitInstance(String depositorRefName, String exitNumber, String exitDateDisplayDate) {
97 this.exitNumber = exitNumber;
98 this.depositorRefName = depositorRefName;
99 this.exitDateGroup.setDateDisplayDate(exitDateDisplayDate);
100 this.exitDateGroup.setDateDisplayDate(exitDateDisplayDate);
102 ObjectexitCommon objectexit = new ObjectexitCommon();
103 StructuredDateGroup oeExitDateGroup = new StructuredDateGroup();
105 objectexit.setDepositor(depositorRefName);
106 objectexit.setExitNumber(exitNumber);
107 oeExitDateGroup.setDateDisplayDate(exitDateDisplayDate);
108 objectexit.setExitDateGroup(oeExitDateGroup);
110 PoxPayloadOut multipart = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
111 PayloadOutputPart commonPart = multipart.addPart(new ObjectExitClient().getCommonPartName(),
113 logger.debug("to be created, objectexit common: " + objectAsXmlString(objectexit, ObjectexitCommon.class));
117 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
118 public void createWithAuthRefs(String testName) throws Exception {
119 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
120 String identifier = createIdentifier(); // Submit the request to the service and store the response.
121 createPersonRefs();// Create all the person refs and entities
122 // Create a new Loans In resource. One or more fields in this resource will be PersonAuthority
123 // references, and will refer to Person resources by their refNames.
124 ObjectExitClient objectexitClient = new ObjectExitClient();
125 PoxPayloadOut multipart = createObjectExitInstance(depositorRefName,
126 "exitNumber-" + identifier, CURRENT_DATE_UTC);
127 Response res = objectexitClient.create(multipart);
129 assertStatusCode(res, testName);
130 if (knownResourceId == null) {// Store the ID returned from the first resource created for additional tests below.
131 knownResourceId = extractId(res);
138 objectexitIdsCreated.add(extractId(res));// Store the IDs from every resource created; delete on cleanup
141 protected void createPersonRefs() {
142 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
143 // Create a temporary PersonAuthority resource, and its corresponding refName by which it can be identified.
144 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
145 Response res = personAuthClient.create(multipart);
147 assertStatusCode(res, "createPersonRefs (not a surefire test)");
148 personAuthCSID = extractId(res);
154 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
155 // Create temporary Person resources, and their corresponding refNames by which they can be identified.
158 csid = createPerson("Owen the Cur", "Owner", "owenCurOwner", authRefName);
159 personIdsCreated.add(csid);
160 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
162 csid = createPerson("Davenport", "Depositor", "davenportDepositor", authRefName);
163 personIdsCreated.add(csid);
164 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
167 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
168 String result = null;
170 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
171 Map<String, String> personInfo = new HashMap<String, String>();
172 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
173 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
174 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
175 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
176 PersonTermGroup term = new PersonTermGroup();
177 String termName = firstName + " " + surName;
178 term.setTermDisplayName(termName);
179 term.setTermName(termName);
180 personTerms.add(term);
181 PoxPayloadOut multipart =
182 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
183 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
184 Response res = personAuthClient.createItem(personAuthCSID, multipart);
186 assertStatusCode(res, "createPerson (not a surefire test)");
187 result = extractId(res);
197 @Test(dataProvider = "testName",
198 dependsOnMethods = {"createWithAuthRefs"})
199 public void readAndCheckAuthRefs(String testName) throws Exception {
200 testSetup(STATUS_OK, ServiceRequestType.READ);
201 ObjectExitClient objectexitClient = new ObjectExitClient();
202 Response res = objectexitClient.read(knownResourceId);
203 ObjectexitCommon objectexit = null;
205 assertStatusCode(res, testName);
206 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
207 objectexit = (ObjectexitCommon) extractPart(input, objectexitClient.getCommonPartName(), ObjectexitCommon.class);
208 Assert.assertNotNull(objectexit);
209 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));
216 // Check a couple of fields
217 Assert.assertEquals(objectexit.getDepositor(), depositorRefName);
218 Assert.assertEquals(objectexit.getExitNumber(), exitNumber);
220 // Get the auth refs and check them
221 Response res2 = objectexitClient.getAuthorityRefs(knownResourceId);
222 AuthorityRefList list = null;
224 assertStatusCode(res2, testName);
225 list = (AuthorityRefList)res2.getEntity();
231 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
232 int numAuthRefsFound = items.size();
233 logger.debug("Authority references, found " + numAuthRefsFound);
234 //Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
235 // "Did not find all expected authority references! " +
236 // "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
237 if (logger.isDebugEnabled()) {
239 for (AuthorityRefList.AuthorityRefItem item : items) {
240 logger.debug(testName + ": list-item[" + i + "] Field:" + item.getSourceField() + "= " + item.getAuthDisplayName() + item.getItemDisplayName());
241 logger.debug(testName + ": list-item[" + i + "] refName=" + item.getRefName());
242 logger.debug(testName + ": list-item[" + i + "] URI=" + item.getUri());
249 * Deletes all resources created by tests, after all tests have been run.
251 * This cleanup method will always be run, even if one or more tests fail.
252 * For this reason, it attempts to remove all resources created
253 * at any point during testing, even if some of those resources
254 * may be expected to be deleted by certain tests.
256 @AfterClass(alwaysRun = true)
257 public void cleanUp() {
258 String noTest = System.getProperty("noTestCleanup");
259 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
260 logger.debug("Skipping Cleanup phase ...");
263 logger.debug("Cleaning up temporary resources created for testing ...");
264 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
265 // Delete Person resource(s) (before PersonAuthority resources).
266 for (String resourceId : personIdsCreated) {
267 // Note: Any non-success responses are ignored and not reported.
268 personAuthClient.deleteItem(personAuthCSID, resourceId);
270 // Delete PersonAuthority resource(s).
271 // Note: Any non-success response is ignored and not reported.
272 if (personAuthCSID != null) {
273 personAuthClient.delete(personAuthCSID);
274 // Delete Loans In resource(s).
275 ObjectExitClient objectexitClient = new ObjectExitClient();
276 for (String resourceId : objectexitIdsCreated) {
277 // Note: Any non-success responses are ignored and not reported.
278 objectexitClient.delete(resourceId);
284 protected Class<AbstractCommonList> getCommonListType() {
285 // TODO Auto-generated method stub