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 exitNumber = null;
70 private final static String CURRENT_DATE_UTC =
71 GregorianCalendarDateTimeUtils.currentDateUTC();
74 public String getServicePathComponent() {
75 return ObjectExitClient.SERVICE_PATH_COMPONENT;
79 protected String getServiceName() {
80 return ObjectExitClient.SERVICE_NAME;
84 protected CollectionSpaceClient getClientInstance() {
85 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
89 protected AbstractCommonList getCommonList(ClientResponse<AbstractCommonList> response) {
90 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
93 private PoxPayloadOut createObjectExitInstance(String depositorRefName, String exitNumber) {
94 this.exitNumber = exitNumber;
95 this.depositorRefName = depositorRefName;
96 ObjectexitCommon objectexit = new ObjectexitCommon();
97 objectexit.setDepositor(depositorRefName);
98 objectexit.setExitNumber(exitNumber);
100 PoxPayloadOut multipart = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
101 PayloadOutputPart commonPart = multipart.addPart(new ObjectExitClient().getCommonPartName(),
103 logger.debug("to be created, objectexit common: " + objectAsXmlString(objectexit, ObjectexitCommon.class));
107 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
108 public void createWithAuthRefs(String testName) throws Exception {
109 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
110 String identifier = createIdentifier(); // Submit the request to the service and store the response.
111 createPersonRefs();// Create all the person refs and entities
112 // Create a new Loans In resource. One or more fields in this resource will be PersonAuthority
113 // references, and will refer to Person resources by their refNames.
114 ObjectExitClient objectexitClient = new ObjectExitClient();
115 PoxPayloadOut multipart = createObjectExitInstance(depositorRefName,
116 "exitNumber-" + identifier);
117 // , CURRENT_DATE_UTC
118 ClientResponse<Response> res = objectexitClient.create(multipart);
120 assertStatusCode(res, testName);
121 if (knownResourceId == null) {// Store the ID returned from the first resource created for additional tests below.
122 knownResourceId = extractId(res);
126 res.releaseConnection();
129 objectexitIdsCreated.add(extractId(res));// Store the IDs from every resource created; delete on cleanup
132 protected void createPersonRefs() {
133 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
134 // Create a temporary PersonAuthority resource, and its corresponding refName by which it can be identified.
135 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
136 ClientResponse<Response> res = personAuthClient.create(multipart);
138 assertStatusCode(res, "createPersonRefs (not a surefire test)");
139 personAuthCSID = extractId(res);
142 res.releaseConnection();
145 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
146 // Create temporary Person resources, and their corresponding refNames by which they can be identified.
149 csid = createPerson("Owen the Cur", "Owner", "owenCurOwner", authRefName);
150 personIdsCreated.add(csid);
151 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
153 csid = createPerson("Davenport", "Depositor", "davenportDepositor", authRefName);
154 personIdsCreated.add(csid);
155 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
158 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
159 String result = null;
161 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
162 Map<String, String> personInfo = new HashMap<String, String>();
163 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
164 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
165 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
166 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, authRefName, personInfo, personAuthClient.getItemCommonPartName());
167 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
169 assertStatusCode(res, "createPerson (not a surefire test)");
170 result = extractId(res);
173 res.releaseConnection();
180 @Test(dataProvider = "testName",
181 dependsOnMethods = {"createWithAuthRefs"})
182 public void readAndCheckAuthRefs(String testName) throws Exception {
183 testSetup(STATUS_OK, ServiceRequestType.READ);
184 ObjectExitClient objectexitClient = new ObjectExitClient();
185 ClientResponse<String> res = objectexitClient.read(knownResourceId);
186 ObjectexitCommon objectexit = null;
188 assertStatusCode(res, testName);
189 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
190 objectexit = (ObjectexitCommon) extractPart(input, objectexitClient.getCommonPartName(), ObjectexitCommon.class);
191 Assert.assertNotNull(objectexit);
192 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));
195 res.releaseConnection();
199 // Check a couple of fields
200 Assert.assertEquals(objectexit.getDepositor(), depositorRefName);
201 Assert.assertEquals(objectexit.getExitNumber(), exitNumber);
203 // Get the auth refs and check them
204 ClientResponse<AuthorityRefList> res2 = objectexitClient.getAuthorityRefs(knownResourceId);
205 AuthorityRefList list = null;
207 assertStatusCode(res2, testName);
208 list = res2.getEntity();
211 res2.releaseConnection();
214 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
215 int numAuthRefsFound = items.size();
216 logger.debug("Authority references, found " + numAuthRefsFound);
217 //Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
218 // "Did not find all expected authority references! " +
219 // "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
220 if (logger.isDebugEnabled()) {
222 for (AuthorityRefList.AuthorityRefItem item : items) {
223 logger.debug(testName + ": list-item[" + i + "] Field:" + item.getSourceField() + "= " + item.getAuthDisplayName() + item.getItemDisplayName());
224 logger.debug(testName + ": list-item[" + i + "] refName=" + item.getRefName());
225 logger.debug(testName + ": list-item[" + i + "] URI=" + item.getUri());
232 * Deletes all resources created by tests, after all tests have been run.
234 * This cleanup method will always be run, even if one or more tests fail.
235 * For this reason, it attempts to remove all resources created
236 * at any point during testing, even if some of those resources
237 * may be expected to be deleted by certain tests.
239 @AfterClass(alwaysRun = true)
240 public void cleanUp() {
241 String noTest = System.getProperty("noTestCleanup");
242 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
243 logger.debug("Skipping Cleanup phase ...");
246 logger.debug("Cleaning up temporary resources created for testing ...");
247 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
248 // Delete Person resource(s) (before PersonAuthority resources).
249 for (String resourceId : personIdsCreated) {
250 // Note: Any non-success responses are ignored and not reported.
251 personAuthClient.deleteItem(personAuthCSID, resourceId);
253 // Delete PersonAuthority resource(s).
254 // Note: Any non-success response is ignored and not reported.
255 if (personAuthCSID != null) {
256 personAuthClient.delete(personAuthCSID);
257 // Delete Loans In resource(s).
258 ObjectExitClient objectexitClient = new ObjectExitClient();
259 for (String resourceId : objectexitIdsCreated) {
260 // Note: Any non-success responses are ignored and not reported.
261 objectexitClient.delete(resourceId);
267 protected Class<AbstractCommonList> getCommonListType() {
268 // TODO Auto-generated method stub