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.OrganizationJAXBSchema;
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.client.OrgAuthorityClient;
35 import org.collectionspace.services.client.OrgAuthorityClientUtils;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;
39 import org.collectionspace.services.common.authorityref.AuthorityRefList.AuthorityRefItem;
40 import org.collectionspace.services.organization.OrgauthoritiesCommon;
41 import org.collectionspace.services.organization.OrganizationsCommon;
43 import org.jboss.resteasy.client.ClientResponse;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * LoaninAuthRefsTest, carries out Authority References tests against a
56 * deployed and running Loanin (aka Loans In) Service.
58 * $LastChangedRevision: 1327 $
59 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
61 public class OrgAuthorityAuthRefsTest extends BaseServiceTest {
63 private final Logger logger =
64 LoggerFactory.getLogger(OrgAuthorityAuthRefsTest.class);
66 // Instance variables specific to this test.
67 final String SERVICE_PATH_COMPONENT = "orgauthorities";
68 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
69 private String knownResourceRefName = null;
70 private String knownAuthResourceId = null;
71 private String knownItemId = null;
72 private List<String> allResourceIdsCreated = new ArrayList<String>();
73 private Map<String, String> allItemResourceIdsCreated =
74 new HashMap<String, String>();
75 private List<String> personIdsCreated = new ArrayList();
76 private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
77 private int OK_STATUS = Response.Status.OK.getStatusCode();
78 private String personAuthCSID = null;
79 private String organizationContactPersonRefName = null;
80 private final int NUM_AUTH_REFS_EXPECTED = 1;
82 // ---------------------------------------------------------------
83 // CRUD tests : CREATE tests
84 // ---------------------------------------------------------------
86 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
87 public void createWithAuthRefs(String testName) throws Exception {
89 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
91 // Create a new Organization Authority resource.
92 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
93 String identifier = createIdentifier();
94 String displayName = "TestOrgAuth-" + identifier;
95 boolean createWithDisplayName = false;
96 knownResourceRefName =
97 OrgAuthorityClientUtils.createOrgAuthRefName(displayName, createWithDisplayName);
98 MultipartOutput multipart =
99 OrgAuthorityClientUtils.createOrgAuthorityInstance(
100 displayName, knownResourceRefName, new OrgAuthorityClient().getCommonPartName());
102 // Submit the request to the service and store the response.
103 ClientResponse<Response> res = orgAuthClient.create(multipart);
104 int statusCode = res.getStatus();
106 // Check the status code of the response: does it match
107 // the expected response(s)?
110 // Does it fall within the set of valid status codes?
111 // Does it exactly match the expected status code?
112 if(logger.isDebugEnabled()){
113 logger.debug(testName + ": status = " + statusCode);
115 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
116 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
117 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
119 // Store the IDs from every resource created by tests,
120 // so they can be deleted after tests have been run.
121 knownAuthResourceId = extractId(res);
122 allResourceIdsCreated.add(knownAuthResourceId);
124 // Create all the person refs and entities
127 // Initialize values for a new Organization item, to be created within
128 // the newly-created Organization Authority resource.
130 // One or more fields in the Organization item record will
131 // contain references to Persons, via their refNames, as
132 // per the initialization(s) below.
133 Map<String, String> testOrgMap = new HashMap<String,String>();
134 testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME,
135 "Test Organization-" + identifier);
136 testOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Test Organization Name");
137 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Anytown, USA");
138 testOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, organizationContactPersonRefName);
140 // Finishing creating the new Organization item, then
141 // submit the request to the service and store the response.
142 knownItemId = OrgAuthorityClientUtils.createItemInAuthority(
143 knownAuthResourceId, knownResourceRefName, testOrgMap, orgAuthClient);
145 // Check the status code of the response: does it match
146 // the expected response(s)?
147 if(logger.isDebugEnabled()){
148 logger.debug(testName + ": status = " + statusCode);
150 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
151 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
152 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
154 // Store the IDs from every item created by tests,
155 // so they can be deleted after tests have been run.
156 allItemResourceIdsCreated.put(knownItemId, knownAuthResourceId);
160 protected void createPersonRefs(){
162 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
163 // Create a temporary PersonAuthority resource, and its corresponding
164 // refName by which it can be identified.
166 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
167 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
168 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
169 ClientResponse<Response> res = personAuthClient.create(multipart);
170 int statusCode = res.getStatus();
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, CREATED_STATUS);
175 personAuthCSID = extractId(res);
177 // Create a temporary Person resource, and its corresponding refName
178 // by which it can be identified.
179 organizationContactPersonRefName =
180 PersonAuthorityClientUtils.createPersonRefName(authRefName, "Charlie Orgcontact", true);
181 personIdsCreated.add(createPerson("Charlie", "Orgcontact", organizationContactPersonRefName));
185 protected String createPerson(String firstName, String surName, String refName ) {
186 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
187 Map<String, String> personInfo = new HashMap<String,String>();
188 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
189 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
190 MultipartOutput multipart =
191 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
192 refName, personInfo, personAuthClient.getItemCommonPartName());
193 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
194 int statusCode = res.getStatus();
196 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
197 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
198 Assert.assertEquals(statusCode, CREATED_STATUS);
199 return extractId(res);
203 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
204 dependsOnMethods = {"createWithAuthRefs"})
205 public void readAndCheckAuthRefs(String testName) throws Exception {
208 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
210 // Submit the request to the service and store the response.
211 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
212 ClientResponse<MultipartInput> res =
213 orgAuthClient.readItem(knownAuthResourceId, knownItemId);
214 int statusCode = res.getStatus();
216 // Check the status code of the response: does it match
217 // the expected response(s)?
218 if(logger.isDebugEnabled()){
219 logger.debug(testName + ".read: status = " + statusCode);
221 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
222 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
223 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
225 MultipartInput input = (MultipartInput) res.getEntity();
226 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
227 orgAuthClient.getItemCommonPartName(), OrganizationsCommon.class);
228 Assert.assertNotNull(organization);
229 if(logger.isDebugEnabled()){
230 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
232 // Check one or more of the authority fields in the Organization item
233 Assert.assertEquals(organization.getContactName(), organizationContactPersonRefName);
235 // Get the auth refs and check them
236 // FIXME - need to create this method in the client
237 // and get the ID for the organization item
238 ClientResponse<AuthorityRefList> res2 =
239 orgAuthClient.getItemAuthorityRefs(knownAuthResourceId, knownItemId);
240 statusCode = res2.getStatus();
242 if(logger.isDebugEnabled()){
243 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
248 AuthorityRefList list = res2.getEntity();
250 // Optionally output additional data about list members for debugging.
251 boolean iterateThroughList = true;
252 if(iterateThroughList && logger.isDebugEnabled()){
253 List<AuthorityRefList.AuthorityRefItem> items =
254 list.getAuthorityRefItem();
256 for(AuthorityRefList.AuthorityRefItem item : items){
257 logger.debug(testName + ": list-item[" + i + "] Field:" +
258 item.getSourceField() + "=" +
259 item.getItemDisplayName());
260 logger.debug(testName + ": list-item[" + i + "] refName=" +
262 logger.debug(testName + ": list-item[" + i + "] URI=" +
266 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
270 // ---------------------------------------------------------------
271 // Cleanup of resources created during testing
272 // ---------------------------------------------------------------
275 * Deletes all resources created by tests, after all tests have been run.
277 * This cleanup method will always be run, even if one or more tests fail.
278 * For this reason, it attempts to remove all resources created
279 * at any point during testing, even if some of those resources
280 * may be expected to be deleted by certain tests.
282 @AfterClass(alwaysRun=true)
283 public void cleanUp() {
284 String noTest = System.getProperty("noTestCleanup");
285 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
286 if (logger.isDebugEnabled()) {
287 logger.debug("Skipping Cleanup phase ...");
291 if (logger.isDebugEnabled()) {
292 logger.debug("Cleaning up temporary resources created for testing ...");
294 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
295 // Delete Person resource(s) (before PersonAuthority resources).
296 ClientResponse<Response> res;
297 for (String resourceId : personIdsCreated) {
298 // Note: Any non-success responses are ignored and not reported.
299 res = personAuthClient.deleteItem(personAuthCSID, resourceId);
301 // Delete PersonAuthority resource(s).
302 // Note: Any non-success response is ignored and not reported.
303 res = personAuthClient.delete(personAuthCSID);
304 String parentResourceId;
305 String itemResourceId;
306 OrgAuthorityClient client = new OrgAuthorityClient();
307 // Clean up item resources.
308 for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
309 itemResourceId = entry.getKey();
310 parentResourceId = entry.getValue();
311 // Note: Any non-success responses from the delete operation
312 // below are ignored and not reported.
313 res = client.deleteItem(parentResourceId, itemResourceId);
315 // Clean up parent resources.
316 for (String resourceId : allResourceIdsCreated) {
317 // Note: Any non-success responses from the delete operation
318 // below are ignored and not reported.
319 res = client.delete(resourceId);
323 // ---------------------------------------------------------------
324 // Utility methods used by tests above
325 // ---------------------------------------------------------------
327 public String getServicePathComponent() {
328 return SERVICE_PATH_COMPONENT;
331 private MultipartOutput createOrgAuthorityInstance(String identifier) {
332 String displayName = "displayName-" + identifier;
333 String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
334 return OrgAuthorityClientUtils.createOrgAuthorityInstance(
335 displayName, refName,
336 new OrgAuthorityClient().getCommonPartName());