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.Response;
32 import org.collectionspace.services.PersonJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.ConditioncheckClient;
35 import org.collectionspace.services.client.PersonAuthorityClient;
36 import org.collectionspace.services.client.PersonAuthorityClientUtils;
37 import org.collectionspace.services.client.PayloadOutputPart;
38 import org.collectionspace.services.client.PoxPayloadIn;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
41 import org.collectionspace.services.common.authorityref.AuthorityRefList;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.collectionspace.services.conditioncheck.ConditionchecksCommon;
44 import org.collectionspace.services.person.PersonTermGroup;
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 * ConditioncheckAuthRefsTest, carries out Authority References tests against a
57 * deployed and running Conditioncheck (aka Condition Checks) Service.
59 public class ConditioncheckAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
61 private final String CLASS_NAME = ConditioncheckAuthRefsTest.class.getName();
62 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
64 // Instance variables specific to this test.
65 final String SERVICE_NAME = "conditionchecks";
66 final String SERVICE_PATH_COMPONENT = "conditionchecks";
67 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68 private String knownResourceId = null;
69 private List<String> conditioncheckIdsCreated = new ArrayList<String>();
70 private List<String> personIdsCreated = new ArrayList<String>();
71 private String personAuthCSID = null;
72 private String conditionCheckerRefName = null;
74 private final int NUM_AUTH_REFS_EXPECTED = 1;
78 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
81 protected CollectionSpaceClient getClientInstance() {
82 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
86 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
89 protected AbstractCommonList getCommonList(
90 ClientResponse<AbstractCommonList> response) {
91 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
94 // ---------------------------------------------------------------
95 // CRUD tests : CREATE tests
96 // ---------------------------------------------------------------
98 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
99 public void createWithAuthRefs(String testName) throws Exception {
100 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
102 // Submit the request to the service and store the response.
103 String identifier = createIdentifier();
105 // Create all the person refs and entities
108 // Create a new Condition Check resource.
110 // One or more fields in this resource will be PersonAuthority
111 // references, and will refer to Person resources by their refNames.
112 ConditioncheckClient conditioncheckClient = new ConditioncheckClient();
113 PoxPayloadOut multipart = createConditioncheckInstance(
114 "conditionCheckRefNumber-" + identifier,
115 conditionCheckerRefName);
116 Response response = conditioncheckClient.create(multipart);
117 int statusCode = response.getStatus();
119 // Check the status code of the response: does it match
120 // the expected response(s)?
123 // Does it fall within the set of valid status codes?
124 // Does it exactly match the expected status code?
125 if(logger.isDebugEnabled()){
126 logger.debug(testName + ": status = " + statusCode);
128 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
129 invalidStatusCodeMessage(testRequestType, statusCode));
130 Assert.assertEquals(statusCode, testExpectedStatusCode);
132 // Store the ID returned from the first resource created
133 // for additional tests below.
134 if (knownResourceId == null){
135 knownResourceId = extractId(response);
136 if (logger.isDebugEnabled()) {
137 logger.debug(testName + ": knownResourceId=" + knownResourceId);
141 // Store the IDs from every resource created by tests,
142 // so they can be deleted after tests have been run.
143 conditioncheckIdsCreated.add(extractId(response));
149 protected void createPersonRefs(){
150 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
151 // Create a temporary PersonAuthority resource, and its corresponding
152 // refName by which it can be identified.
153 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
154 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
155 Response res = personAuthClient.create(multipart);
157 int statusCode = res.getStatus();
158 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
159 invalidStatusCodeMessage(testRequestType, statusCode));
160 Assert.assertEquals(statusCode, STATUS_CREATED);
161 personAuthCSID = extractId(res);
166 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
167 // Create temporary Person resources, and their corresponding refNames
168 // by which they can be identified.
169 String csid = createPerson("Carrie", "ConditionChecker1", "carrieConditionChecker", authRefName);
170 personIdsCreated.add(csid);
171 conditionCheckerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
174 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
175 String result = null;
177 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
178 Map<String, String> personInfo = new HashMap<String,String>();
179 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
180 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
181 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
182 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
183 PersonTermGroup term = new PersonTermGroup();
184 String termName = firstName + " " + surName;
185 term.setTermDisplayName(termName);
186 term.setTermName(termName);
187 personTerms.add(term);
188 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
189 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
191 Response res = personAuthClient.createItem(personAuthCSID, multipart);
193 int statusCode = res.getStatus();
194 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
195 invalidStatusCodeMessage(testRequestType, statusCode));
196 Assert.assertEquals(statusCode, STATUS_CREATED);
197 result = extractId(res);
206 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
207 dependsOnMethods = {"createWithAuthRefs"})
208 public void readAndCheckAuthRefs(String testName) throws Exception {
210 testSetup(STATUS_OK, ServiceRequestType.READ);
212 // Submit the request to the service and store the response.
213 ConditioncheckClient conditioncheckClient = new ConditioncheckClient();
214 Response res = conditioncheckClient.read(knownResourceId);
215 ConditionchecksCommon conditioncheckCommon = null;
217 assertStatusCode(res, testName);
218 // Extract the common part from the response.
219 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
220 conditioncheckCommon = (ConditionchecksCommon) extractPart(input,
221 conditioncheckClient.getCommonPartName(), ConditionchecksCommon.class);
222 Assert.assertNotNull(conditioncheckCommon);
223 if(logger.isDebugEnabled()){
224 logger.debug(objectAsXmlString(conditioncheckCommon, ConditionchecksCommon.class));
232 // Check a couple of fields
233 Assert.assertEquals(conditioncheckCommon.getConditionChecker(), conditionCheckerRefName);
235 // Get the auth refs and check them
236 Response res2 = conditioncheckClient.getAuthorityRefs(knownResourceId);
237 AuthorityRefList list = null;
239 assertStatusCode(res2, testName);
240 list = (AuthorityRefList)res2.getEntity();
241 Assert.assertNotNull(list);
248 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
249 int numAuthRefsFound = items.size();
250 if(logger.isDebugEnabled()){
251 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
252 " authority references, found " + numAuthRefsFound);
255 // Optionally output additional data about list members for debugging.
256 boolean iterateThroughList = true;
257 if(iterateThroughList && logger.isDebugEnabled()){
259 for(AuthorityRefList.AuthorityRefItem item : items){
260 logger.debug(testName + ": list-item[" + i + "] Field:" +
261 item.getSourceField() + "= " +
262 item.getAuthDisplayName() +
263 item.getItemDisplayName());
264 logger.debug(testName + ": list-item[" + i + "] refName=" +
266 logger.debug(testName + ": list-item[" + i + "] URI=" +
272 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
273 "Did not find all expected authority references! " +
274 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
279 // ---------------------------------------------------------------
280 // Cleanup of resources created during testing
281 // ---------------------------------------------------------------
284 * Deletes all resources created by tests, after all tests have been run.
286 * This cleanup method will always be run, even if one or more tests fail.
287 * For this reason, it attempts to remove all resources created
288 * at any point during testing, even if some of those resources
289 * may be expected to be deleted by certain tests.
291 @AfterClass(alwaysRun=true)
292 public void cleanUp() {
293 String noTest = System.getProperty("noTestCleanup");
294 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
295 if (logger.isDebugEnabled()) {
296 logger.debug("Skipping Cleanup phase ...");
300 if (logger.isDebugEnabled()) {
301 logger.debug("Cleaning up temporary resources created for testing ...");
303 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
304 // Delete Person resource(s) (before PersonAuthority resources).
306 for (String resourceId : personIdsCreated) {
307 // Note: Any non-success responses are ignored and not reported.
308 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
311 // Delete PersonAuthority resource(s).
312 // Note: Any non-success response is ignored and not reported.
313 if (personAuthCSID != null) {
314 personAuthClient.delete(personAuthCSID);
315 // Delete Condition Checks resource(s).
316 ConditioncheckClient conditioncheckClient = new ConditioncheckClient();
317 for (String resourceId : conditioncheckIdsCreated) {
318 // Note: Any non-success responses are ignored and not reported.
319 conditioncheckClient.delete(resourceId).close();
324 // ---------------------------------------------------------------
325 // Utility methods used by tests above
326 // ---------------------------------------------------------------
327 public String getServiceName() {
332 public String getServicePathComponent() {
333 return SERVICE_PATH_COMPONENT;
336 private PoxPayloadOut createConditioncheckInstance(String conditionCheckRefNumber,
337 String conditionChecker) {
338 ConditionchecksCommon conditioncheckCommon = new ConditionchecksCommon();
340 conditioncheckCommon.setConditionCheckRefNumber(conditionCheckRefNumber);
341 conditioncheckCommon.setConditionChecker(conditionChecker);
343 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
344 PayloadOutputPart commonPart =
345 multipart.addPart(new ConditioncheckClient().getCommonPartName(), conditioncheckCommon);
347 if(logger.isDebugEnabled()){
348 logger.debug("to be created, conditioncheck common");
349 logger.debug(objectAsXmlString(conditioncheckCommon, ConditionchecksCommon.class));
356 protected Class<AbstractCommonList> getCommonListType() {
357 return AbstractCommonList.class;