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 (c)) 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.WorkJAXBSchema;
33 import org.collectionspace.services.client.AbstractCommonListUtils;
34 import org.collectionspace.services.client.AuthorityClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.client.WorkAuthorityClient;
40 import org.collectionspace.services.client.WorkAuthorityClientUtils;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.work.WorkTermGroup;
43 import org.collectionspace.services.work.WorkTermGroupList;
44 import org.collectionspace.services.work.WorkauthoritiesCommon;
45 import org.collectionspace.services.work.WorksCommon;
46 import org.jboss.resteasy.client.ClientResponse;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
54 * WorkAuthorityServiceTest, carries out tests against a
55 * deployed and running WorkAuthority Service.
58 public class WorkAuthorityServiceTest extends AbstractAuthorityServiceTest<WorkauthoritiesCommon, WorksCommon> {
61 private final String CLASS_NAME = WorkAuthorityServiceTest.class.getName();
62 private final Logger logger = LoggerFactory.getLogger(WorkAuthorityServiceTest.class);
65 public String getServicePathComponent() {
66 return WorkAuthorityClient.SERVICE_PATH_COMPONENT;
70 protected String getServiceName() {
71 return WorkAuthorityClient.SERVICE_NAME;
74 public String getItemServicePathComponent() {
75 return AuthorityClient.ITEMS;
78 // Instance variables specific to this test.
80 // /** The SERVICE path component. */
81 // final String SERVICE_PATH_COMPONENT = "workauthorities";
83 // /** The ITEM service path component. */
84 // final String ITEM_SERVICE_PATH_COMPONENT = "items";
87 final String TEST_WORK_TERM_DISPLAY_NAME = "Muppets Take Manhattan (1984)";
88 final String TEST_WORK_TERM_NAME = "Muppets Take Manhattan";
89 final String TEST_WORK_TERM_TYPE = "";
90 final String TEST_WORK_TERM_STATUS = "accepted";
91 final String TEST_WORK_TERM_QUALIFIER = "";
92 final String TEST_WORK_TERM_LANGUAGE = "english";
93 final Boolean TEST_WORK_TERM_PREFFORLANG = true;
94 final String TEST_WORK_TERM_SOURCE = "featurefilms";
95 final String TEST_WORK_TERM_SOURCE_DETAIL = "internal";
96 final String TEST_WORK_TERM_SOURCE_ID = "12345";
97 final String TEST_WORK_TERM_SOURCE_NOTE = "source note goes here";
98 final String TEST_WORK_HISTORY_NOTE = "history note goes here";
99 final String TEST_WORK_TYPE = "work type goes here";
100 final String TEST_WORK_CREATOR_GROUP_CREATOR = "Frank Oz";
101 final String TEST_WORK_CREATOR_GROUP_CREATOR_TYPE = "director";
102 final String TEST_WORK_PUBLISHER_GROUP_PUBLISHER = "TriStar Pictures";
103 final String TEST_WORK_PUBLISHER_GROUP_PUBLISHER_TYPE = "Distributor";
104 final String TEST_WORK_SHORT_IDENTIFIER = "muppetstakemanhattan";
105 final String TEST_WORK_REFNAME = "refname";
107 /** The known resource id. */
108 private String knownResourceShortIdentifer = null;
109 private String knownResourceRefName = null;
110 private String knownWorkTypeRefName = null;
113 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
116 protected CollectionSpaceClient getClientInstance() {
117 return new WorkAuthorityClient();
121 * Creates the item in authority.
123 * @param vcsid the vcsid
124 * @param authRefName the auth ref name
127 private String createItemInAuthority(String vcsid, String authRefName) {
128 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
130 // Submit the request to the service and store the response.
131 WorkAuthorityClient client = new WorkAuthorityClient();
132 Map<String, String> workMap = new HashMap<String,String>();
133 // TODO Make work type and status be controlled vocabs.
134 workMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, TEST_WORK_SHORT_IDENTIFIER);
135 workMap.put(WorkJAXBSchema.WORK_TYPE, TEST_WORK_TYPE);
136 workMap.put(WorkJAXBSchema.WORK_HISTORY_NOTE, TEST_WORK_HISTORY_NOTE);
138 List<WorkTermGroup> terms = new ArrayList<WorkTermGroup>();
139 WorkTermGroup term = new WorkTermGroup();
140 term.setTermDisplayName(TEST_WORK_TERM_DISPLAY_NAME);
141 term.setTermName(TEST_WORK_TERM_NAME);
142 term.setTermSource(TEST_WORK_TERM_SOURCE);
143 term.setTermSourceDetail(TEST_WORK_TERM_SOURCE_DETAIL);
144 term.setTermStatus(TEST_WORK_TERM_STATUS);
147 String newID = WorkAuthorityClientUtils.createItemInAuthority(vcsid,
148 authRefName, workMap, terms, client );
150 // Store the ID returned from the first item resource created
151 // for additional tests below.
152 if (knownItemResourceId == null){
153 setKnownItemResource(newID, TEST_WORK_SHORT_IDENTIFIER);
154 if (logger.isDebugEnabled()) {
155 logger.debug(testName + ": knownItemResourceId=" + newID);
159 // Store the IDs from any item resources created
160 // by tests, along with the IDs of their parents, so these items
161 // can be deleted after all tests have been run.
162 allResourceItemIdsCreated.put(newID, vcsid);
168 * Verify illegal item display name.
170 * @param testName the test name
171 * @throws Exception the exception
173 @Test(dataProvider="testName")
174 public void verifyIllegalItemDisplayName(String testName) throws Exception {
175 // Perform setup for read.
178 // Submit the request to the service and store the response.
179 WorkAuthorityClient client = new WorkAuthorityClient();
180 Response res = client.readItem(knownResourceId, knownItemResourceId);
181 WorksCommon work = null;
183 assertStatusCode(res, testName);
184 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
185 work = (WorksCommon) extractPart(input,
186 client.getItemCommonPartName(), WorksCommon.class);
187 Assert.assertNotNull(work);
195 // Make an invalid UPDATE request, without a display name
197 WorkTermGroupList termList = work.getWorkTermGroupList();
198 Assert.assertNotNull(termList);
199 List<WorkTermGroup> terms = termList.getWorkTermGroup();
200 Assert.assertNotNull(terms);
201 Assert.assertTrue(terms.size() > 0);
202 terms.get(0).setTermDisplayName(null);
203 terms.get(0).setTermName(null);
205 setupUpdateWithInvalidBody(); // we expect a failure
207 // Submit the updated resource to the service and store the response.
208 PoxPayloadOut output = new PoxPayloadOut(
209 WorkAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
210 PayloadOutputPart commonPart = output.addPart(
211 client.getItemCommonPartName(), work);
212 setupUpdateWithInvalidBody(); // we expected a failure here.
213 res = client.updateItem(knownResourceId, knownItemResourceId, output);
215 assertStatusCode(res, testName);
226 @Test(dataProvider = "testName", groups = {"readList"},
227 dependsOnMethods = {"readList"})
228 public void readItemList(String testName) {
229 readItemList(knownAuthorityWithItems, null);
233 * Read item list by authority name.
235 @Test(dataProvider = "testName", groups = {"readList"},
236 dependsOnMethods = {"readItemList"})
237 public void readItemListByAuthorityName(String testName) {
238 readItemList(null, READITEMS_SHORT_IDENTIFIER);
249 private void readItemList(String vcsid, String shortId) {
250 String testName = "readItemList";
255 // Submit the request to the service and store the response.
256 WorkAuthorityClient client = new WorkAuthorityClient();
257 ClientResponse<AbstractCommonList> res = null;
259 res = client.readItemList(vcsid, null, null);
260 } else if (shortId != null) {
261 res = client.readItemListForNamedAuthority(shortId, null, null);
263 Assert.fail("readItemList passed null csid and name!");
266 AbstractCommonList list = null;
268 assertStatusCode(res, testName);
269 list = res.getEntity();
276 List<AbstractCommonList.ListItem> items = list.getListItem();
277 int nItemsReturned = items.size();
278 // There will be 'nItemsToCreateInList'
279 // items created by the createItemList test,
280 // all associated with the same parent resource.
281 int nExpectedItems = nItemsToCreateInList;
282 if (logger.isDebugEnabled()) {
283 logger.debug(testName + ": Expected " + nExpectedItems
284 + " items; got: " + nItemsReturned);
286 Assert.assertEquals(nItemsReturned, nExpectedItems);
288 for (AbstractCommonList.ListItem item : items) {
289 String value = AbstractCommonListUtils.ListItemGetElementValue(
290 item, WorkJAXBSchema.REF_NAME);
291 Assert.assertTrue((null != value), "Item refName is null!");
292 value = AbstractCommonListUtils.ListItemGetElementValue(item,
293 WorkJAXBSchema.WORK_TERM_DISPLAY_NAME);
294 Assert.assertTrue((null != value), "Item termDisplayName is null!");
296 if (logger.isTraceEnabled()) {
297 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
303 public void delete(String testName) throws Exception {
304 // Do nothing. See localDelete(). This ensure proper test order.
307 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
308 public void localDelete(String testName) throws Exception {
309 super.delete(testName);
313 public void deleteItem(String testName) throws Exception {
314 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
315 // its dependencies will get run first and then we can call the base class' delete method.
318 @Test(dataProvider = "testName", groups = {"delete"},
319 dependsOnMethods = {"verifyIllegalItemDisplayName"})
320 public void localDeleteItem(String testName) throws Exception {
321 super.deleteItem(testName);
324 // ---------------------------------------------------------------
325 // Cleanup of resources created during testing
326 // ---------------------------------------------------------------
329 * Deletes all resources created by tests, after all tests have been run.
331 * This cleanup method will always be run, even if one or more tests fail.
332 * For this reason, it attempts to remove all resources created
333 * at any point during testing, even if some of those resources
334 * may be expected to be deleted by certain tests.
337 @AfterClass(alwaysRun=true)
338 public void cleanUp() {
339 String noTest = System.getProperty("noTestCleanup");
340 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
341 if (logger.isDebugEnabled()) {
342 logger.debug("Skipping Cleanup phase ...");
346 if (logger.isDebugEnabled()) {
347 logger.debug("Cleaning up temporary resources created for testing ...");
349 String parentResourceId;
350 String itemResourceId;
351 // Clean up contact resources.
352 WorkAuthorityClient client = new WorkAuthorityClient();
353 parentResourceId = knownResourceId;
354 // Clean up item resources.
355 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
356 itemResourceId = entry.getKey();
357 parentResourceId = entry.getValue();
358 // Note: Any non-success responses from the delete operation
359 // below are ignored and not reported.
360 client.deleteItem(parentResourceId, itemResourceId).close();
362 // Clean up parent resources.
363 for (String resourceId : allResourceIdsCreated) {
364 // Note: Any non-success responses from the delete operation
365 // below are ignored and not reported.
366 client.delete(resourceId).close();
370 // ---------------------------------------------------------------
371 // Utility methods used by tests above
372 // ---------------------------------------------------------------
374 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
378 * Returns the root URL for the item service.
380 * This URL consists of a base URL for all services, followed by
381 * a path component for the owning parent, followed by the
382 * path component for the items.
384 * @param parentResourceIdentifier An identifier (such as a UUID) for the
385 * parent authority resource of the relevant item resource.
387 * @return The root URL for the item service.
389 protected String getItemServiceRootURL(String parentResourceIdentifier) {
390 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
394 * Returns the URL of a specific item resource managed by a service, and
395 * designated by an identifier (such as a universally unique ID, or UUID).
397 * @param parentResourceIdentifier An identifier (such as a UUID) for the
398 * parent authority resource of the relevant item resource.
400 * @param itemResourceIdentifier An identifier (such as a UUID) for an
403 * @return The URL of a specific item resource managed by a service.
405 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
406 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
410 public void authorityTests(String testName) {
411 // TODO Auto-generated method stub
416 // Work specific overrides
420 protected PoxPayloadOut createInstance(String commonPartName,
422 // Submit the request to the service and store the response.
423 String shortId = identifier;
424 String displayName = "displayName-" + shortId;
425 // String baseRefName = WorkAuthorityClientUtils.createWorkAuthRefName(shortId, null);
426 PoxPayloadOut result =
427 WorkAuthorityClientUtils.createWorkAuthorityInstance(
428 displayName, shortId, commonPartName);
433 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
434 String displayName = "displayName-NON_EXISTENT_ID";
435 PoxPayloadOut result = WorkAuthorityClientUtils.createWorkAuthorityInstance(
436 displayName, "nonEx", commonPartName);
441 protected WorkauthoritiesCommon updateInstance(WorkauthoritiesCommon workauthoritiesCommon) {
442 WorkauthoritiesCommon result = new WorkauthoritiesCommon();
444 result.setDisplayName("updated-" + workauthoritiesCommon.getDisplayName());
445 result.setVocabType("updated-" + workauthoritiesCommon.getVocabType());
451 protected void compareUpdatedInstances(WorkauthoritiesCommon original,
452 WorkauthoritiesCommon updated) throws Exception {
453 Assert.assertEquals(updated.getDisplayName(),
454 original.getDisplayName(),
455 "Display name in updated object did not match submitted data.");
458 protected void compareReadInstances(WorkauthoritiesCommon original,
459 WorkauthoritiesCommon fromRead) throws Exception {
460 Assert.assertNotNull(fromRead.getDisplayName());
461 Assert.assertNotNull(fromRead.getShortIdentifier());
462 Assert.assertNotNull(fromRead.getRefName());
466 // Authority item specific overrides
470 protected String createItemInAuthority(String authorityId) {
471 return createItemInAuthority(authorityId, null /*refname*/);
475 protected WorksCommon updateItemInstance(WorksCommon worksCommon) {
477 WorkTermGroupList termList = worksCommon.getWorkTermGroupList();
478 Assert.assertNotNull(termList);
479 List<WorkTermGroup> terms = termList.getWorkTermGroup();
480 Assert.assertNotNull(terms);
481 Assert.assertTrue(terms.size() > 0);
482 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
483 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
484 worksCommon.setWorkTermGroupList(termList);
490 protected void compareUpdatedItemInstances(WorksCommon original,
491 WorksCommon updated) throws Exception {
493 WorkTermGroupList originalTermList = original.getWorkTermGroupList();
494 Assert.assertNotNull(originalTermList);
495 List<WorkTermGroup> originalTerms = originalTermList.getWorkTermGroup();
496 Assert.assertNotNull(originalTerms);
497 Assert.assertTrue(originalTerms.size() > 0);
499 WorkTermGroupList updatedTermList = updated.getWorkTermGroupList();
500 Assert.assertNotNull(updatedTermList);
501 List<WorkTermGroup> updatedTerms = updatedTermList.getWorkTermGroup();
502 Assert.assertNotNull(updatedTerms);
503 Assert.assertTrue(updatedTerms.size() > 0);
505 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
506 originalTerms.get(0).getTermDisplayName(),
507 "Value in updated record did not match submitted data.");
511 protected void verifyReadItemInstance(WorksCommon item)
513 // TODO Auto-generated method stub
518 protected PoxPayloadOut createNonExistenceItemInstance(
519 String commonPartName, String identifier) {
520 Map<String, String> nonexMap = new HashMap<String,String>();
521 nonexMap.put(WorkJAXBSchema.WORK_TERM_DISPLAY_NAME, TEST_WORK_TERM_DISPLAY_NAME);
522 nonexMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, "nonEx");
523 nonexMap.put(WorkJAXBSchema.WORK_TERM_STATUS, TEST_WORK_TERM_STATUS);
524 final String EMPTY_REFNAME = "";
525 PoxPayloadOut result =
526 WorkAuthorityClientUtils.createWorkInstance(EMPTY_REFNAME, nonexMap,
527 WorkAuthorityClientUtils.getTermGroupInstance(TEST_WORK_TERM_DISPLAY_NAME), commonPartName);