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.authorityref.AuthorityRefList;
42 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
43 import org.collectionspace.services.jaxb.AbstractCommonList;
44 import org.collectionspace.services.objectexit.ObjectexitCommon;
46 import org.jboss.resteasy.client.ClientResponse;
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 * ObjectExitAuthRefsTest, carries out Authority References tests against a deployed and running ObjectExit (aka Loans Out) Service.
57 * $LastChangedRevision: $
60 public class ObjectExitAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
62 private final String CLASS_NAME = ObjectExitAuthRefsTest.class.getName();
63 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
64 final String PERSON_AUTHORITY_NAME = "ObjectexitPersonAuth";
65 private List<String> objectexitIdsCreated = new ArrayList<String>();
66 private List<String> personIdsCreated = new ArrayList<String>();
67 private String personAuthCSID = null;
68 private String depositorRefName = null;
69 private String exitDate = null;
70 private String exitNumber = null;
71 private final static String CURRENT_DATE_UTC =
72 GregorianCalendarDateTimeUtils.currentDateUTC();
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 getCommonList(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(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 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
113 String identifier = createIdentifier(); // Submit the request to the service and store the response.
114 createPersonRefs();// Create all the person refs and entities
115 // Create a new Loans In resource. One or more fields in this resource will be PersonAuthority
116 // references, and will refer to Person resources by their refNames.
117 ObjectExitClient objectexitClient = new ObjectExitClient();
118 PoxPayloadOut multipart = createObjectExitInstance(depositorRefName,
119 "exitNumber-" + identifier, CURRENT_DATE_UTC);
120 ClientResponse<Response> res = objectexitClient.create(multipart);
122 assertStatusCode(res, testName);
123 if (knownResourceId == null) {// Store the ID returned from the first resource created for additional tests below.
124 knownResourceId = extractId(res);
128 res.releaseConnection();
131 objectexitIdsCreated.add(extractId(res));// Store the IDs from every resource created; delete on cleanup
134 protected void createPersonRefs() {
135 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
136 // Create a temporary PersonAuthority resource, and its corresponding refName by which it can be identified.
137 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
138 ClientResponse<Response> res = personAuthClient.create(multipart);
140 assertStatusCode(res, "createPersonRefs (not a surefire test)");
141 personAuthCSID = extractId(res);
144 res.releaseConnection();
147 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
148 // Create temporary Person resources, and their corresponding refNames by which they can be identified.
151 csid = createPerson("Owen the Cur", "Owner", "owenCurOwner", authRefName);
152 personIdsCreated.add(csid);
153 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
155 csid = createPerson("Davenport", "Depositor", "davenportDepositor", authRefName);
156 personIdsCreated.add(csid);
157 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
160 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
161 String result = null;
163 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
164 Map<String, String> personInfo = new HashMap<String, String>();
165 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
166 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
167 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
168 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, authRefName, personInfo, personAuthClient.getItemCommonPartName());
169 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
171 assertStatusCode(res, "createPerson (not a surefire test)");
172 result = extractId(res);
175 res.releaseConnection();
182 @Test(dataProvider = "testName",
183 dependsOnMethods = {"createWithAuthRefs"})
184 public void readAndCheckAuthRefs(String testName) throws Exception {
185 testSetup(STATUS_OK, ServiceRequestType.READ);
186 ObjectExitClient objectexitClient = new ObjectExitClient();
187 ClientResponse<String> res = objectexitClient.read(knownResourceId);
188 ObjectexitCommon objectexit = null;
190 assertStatusCode(res, testName);
191 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
192 objectexit = (ObjectexitCommon) extractPart(input, objectexitClient.getCommonPartName(), ObjectexitCommon.class);
193 Assert.assertNotNull(objectexit);
194 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));
197 res.releaseConnection();
201 // Check a couple of fields
202 Assert.assertEquals(objectexit.getDepositor(), depositorRefName);
203 Assert.assertEquals(objectexit.getExitNumber(), exitNumber);
205 // Get the auth refs and check them
206 ClientResponse<AuthorityRefList> res2 = objectexitClient.getAuthorityRefs(knownResourceId);
207 AuthorityRefList list = null;
209 assertStatusCode(res2, testName);
210 list = res2.getEntity();
213 res2.releaseConnection();
216 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
217 int numAuthRefsFound = items.size();
218 logger.debug("Authority references, found " + numAuthRefsFound);
219 //Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
220 // "Did not find all expected authority references! " +
221 // "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
222 if (logger.isDebugEnabled()) {
224 for (AuthorityRefList.AuthorityRefItem item : items) {
225 logger.debug(testName + ": list-item[" + i + "] Field:" + item.getSourceField() + "= " + item.getAuthDisplayName() + item.getItemDisplayName());
226 logger.debug(testName + ": list-item[" + i + "] refName=" + item.getRefName());
227 logger.debug(testName + ": list-item[" + i + "] URI=" + item.getUri());
234 * Deletes all resources created by tests, after all tests have been run.
236 * This cleanup method will always be run, even if one or more tests fail.
237 * For this reason, it attempts to remove all resources created
238 * at any point during testing, even if some of those resources
239 * may be expected to be deleted by certain tests.
241 @AfterClass(alwaysRun = true)
242 public void cleanUp() {
243 String noTest = System.getProperty("noTestCleanup");
244 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
245 logger.debug("Skipping Cleanup phase ...");
248 logger.debug("Cleaning up temporary resources created for testing ...");
249 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
250 // Delete Person resource(s) (before PersonAuthority resources).
251 for (String resourceId : personIdsCreated) {
252 // Note: Any non-success responses are ignored and not reported.
253 personAuthClient.deleteItem(personAuthCSID, resourceId);
255 // Delete PersonAuthority resource(s).
256 // Note: Any non-success response is ignored and not reported.
257 if (personAuthCSID != null) {
258 personAuthClient.delete(personAuthCSID);
259 // Delete Loans In resource(s).
260 ObjectExitClient objectexitClient = new ObjectExitClient();
261 for (String resourceId : objectexitIdsCreated) {
262 // Note: Any non-success responses are ignored and not reported.
263 objectexitClient.delete(resourceId);
269 protected Class<AbstractCommonList> getCommonListType() {
270 // TODO Auto-generated method stub