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.common.datetime.GregorianCalendarDateTimeUtils;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.collectionspace.services.objectexit.ObjectexitCommon;
47 import org.jboss.resteasy.client.ClientResponse;
49 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
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 {
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 String knownResourceId = null;
68 private List<String> objectexitIdsCreated = new ArrayList<String>();
69 private List<String> personIdsCreated = new ArrayList<String>();
70 private String personAuthCSID = null;
71 private String depositorRefName = null;
72 private String exitDate = null;
73 private String exitNumber = null;
74 private final static String CURRENT_DATE_UTC =
75 GregorianCalendarDateTimeUtils.currentDateUTC();
78 public String getServicePathComponent() {
79 return ObjectExitClient.SERVICE_PATH_COMPONENT;
83 protected String getServiceName() {
84 return ObjectExitClient.SERVICE_NAME;
88 protected CollectionSpaceClient getClientInstance() {
89 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
93 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
94 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
97 private PoxPayloadOut createObjectExitInstance(String depositorRefName, String exitNumber, String exitDate) {
98 this.exitDate = exitDate;
99 this.exitNumber = exitNumber;
100 this.depositorRefName = depositorRefName;
101 ObjectexitCommon objectexit = new ObjectexitCommon();
102 objectexit.setDepositor(depositorRefName);
103 objectexit.setExitNumber(exitNumber);
104 objectexit.setExitDate(exitDate);
106 PoxPayloadOut multipart = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
107 PayloadOutputPart commonPart = multipart.addPart(objectexit, MediaType.APPLICATION_XML_TYPE);
108 commonPart.setLabel(new ObjectExitClient().getCommonPartName());
109 logger.debug("to be created, objectexit common: " + objectAsXmlString(objectexit, ObjectexitCommon.class));
113 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
114 public void createWithAuthRefs(String testName) throws Exception {
115 logger.debug(testBanner(testName, CLASS_NAME));
116 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
117 String identifier = createIdentifier(); // Submit the request to the service and store the response.
118 createPersonRefs();// Create all the person refs and entities
119 // Create a new Loans In resource. One or more fields in this resource will be PersonAuthority
120 // references, and will refer to Person resources by their refNames.
121 ObjectExitClient objectexitClient = new ObjectExitClient();
122 PoxPayloadOut multipart = createObjectExitInstance(depositorRefName,
123 "exitNumber-" + identifier, CURRENT_DATE_UTC);
124 ClientResponse<Response> res = objectexitClient.create(multipart);
125 assertStatusCode(res, testName);
126 if (knownResourceId == null) {// Store the ID returned from the first resource created for additional tests below.
127 knownResourceId = extractId(res);
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);
137 assertStatusCode(res, "createPersonRefs (not a surefire test)");
138 personAuthCSID = extractId(res);
139 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
140 // Create temporary Person resources, and their corresponding refNames by which they can be identified.
143 csid = createPerson("Owen the Cur", "Owner", "owenCurOwner", authRefName);
144 personIdsCreated.add(csid);
145 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
147 csid = createPerson("Davenport", "Depositor", "davenportDepositor", authRefName);
148 personIdsCreated.add(csid);
149 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
152 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
153 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
154 Map<String, String> personInfo = new HashMap<String, String>();
155 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
156 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
157 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
158 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, authRefName, personInfo, personAuthClient.getItemCommonPartName());
159 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
160 assertStatusCode(res, "createPerson (not a surefire test)");
161 return extractId(res);
164 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"createWithAuthRefs"})
165 public void readAndCheckAuthRefs(String testName) throws Exception {
166 logger.debug(testBanner(testName, CLASS_NAME));
167 testSetup(STATUS_OK, ServiceRequestType.READ);
168 ObjectExitClient objectexitClient = new ObjectExitClient();
169 ClientResponse<String> res = objectexitClient.read(knownResourceId);
170 assertStatusCode(res, testName);
171 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
172 ObjectexitCommon objectexit = (ObjectexitCommon) extractPart(input, objectexitClient.getCommonPartName(), ObjectexitCommon.class);
173 Assert.assertNotNull(objectexit);
174 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));
176 // Check a couple of fields
177 Assert.assertEquals(objectexit.getDepositor(), depositorRefName);
178 Assert.assertEquals(objectexit.getExitNumber(), exitNumber);
180 // Get the auth refs and check them
181 ClientResponse<AuthorityRefList> res2 = objectexitClient.getAuthorityRefs(knownResourceId);
182 assertStatusCode(res2, testName);
183 AuthorityRefList list = res2.getEntity();
184 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
185 int numAuthRefsFound = items.size();
186 logger.debug("Authority references, found " + numAuthRefsFound);
187 //Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
188 // "Did not find all expected authority references! " +
189 // "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
190 if (logger.isDebugEnabled()) {
192 for (AuthorityRefList.AuthorityRefItem item : items) {
193 logger.debug(testName + ": list-item[" + i + "] Field:" + item.getSourceField() + "= " + item.getAuthDisplayName() + item.getItemDisplayName());
194 logger.debug(testName + ": list-item[" + i + "] refName=" + item.getRefName());
195 logger.debug(testName + ": list-item[" + i + "] URI=" + item.getUri());
202 * Deletes all resources created by tests, after all tests have been run.
204 * This cleanup method will always be run, even if one or more tests fail.
205 * For this reason, it attempts to remove all resources created
206 * at any point during testing, even if some of those resources
207 * may be expected to be deleted by certain tests.
209 @AfterClass(alwaysRun = true)
210 public void cleanUp() {
211 String noTest = System.getProperty("noTestCleanup");
212 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
213 logger.debug("Skipping Cleanup phase ...");
216 logger.debug("Cleaning up temporary resources created for testing ...");
217 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
218 // Delete Person resource(s) (before PersonAuthority resources).
219 for (String resourceId : personIdsCreated) {
220 // Note: Any non-success responses are ignored and not reported.
221 personAuthClient.deleteItem(personAuthCSID, resourceId);
223 // Delete PersonAuthority resource(s).
224 // Note: Any non-success response is ignored and not reported.
225 if (personAuthCSID != null) {
226 personAuthClient.delete(personAuthCSID);
227 // Delete Loans In resource(s).
228 ObjectExitClient objectexitClient = new ObjectExitClient();
229 for (String resourceId : objectexitIdsCreated) {
230 // Note: Any non-success responses are ignored and not reported.
231 objectexitClient.delete(resourceId);