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;
29 import org.collectionspace.services.WorkJAXBSchema;
30 import org.collectionspace.services.client.AbstractCommonListUtils;
31 import org.collectionspace.services.client.AuthorityClient;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.client.PayloadOutputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.client.WorkAuthorityClient;
37 import org.collectionspace.services.client.WorkAuthorityClientUtils;
38 import org.collectionspace.services.jaxb.AbstractCommonList;
39 import org.collectionspace.services.work.WorkTermGroup;
40 import org.collectionspace.services.work.WorkTermGroupList;
41 import org.collectionspace.services.work.WorkauthoritiesCommon;
42 import org.collectionspace.services.work.WorksCommon;
43 import org.jboss.resteasy.client.ClientResponse;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
51 * WorkAuthorityServiceTest, carries out tests against a
52 * deployed and running WorkAuthority Service.
55 public class WorkAuthorityServiceTest extends AbstractAuthorityServiceTest<WorkauthoritiesCommon, WorksCommon> {
58 private final String CLASS_NAME = WorkAuthorityServiceTest.class.getName();
59 private final Logger logger = LoggerFactory.getLogger(WorkAuthorityServiceTest.class);
62 public String getServicePathComponent() {
63 return WorkAuthorityClient.SERVICE_PATH_COMPONENT;
67 protected String getServiceName() {
68 return WorkAuthorityClient.SERVICE_NAME;
71 public String getItemServicePathComponent() {
72 return AuthorityClient.ITEMS;
75 // Instance variables specific to this test.
77 // /** The SERVICE path component. */
78 // final String SERVICE_PATH_COMPONENT = "workauthorities";
80 // /** The ITEM service path component. */
81 // final String ITEM_SERVICE_PATH_COMPONENT = "items";
84 final String TEST_WORK_TERM_DISPLAY_NAME = "Muppets Take Manhattan (1984)";
85 final String TEST_WORK_TERM_NAME = "Muppets Take Manhattan";
86 final String TEST_WORK_TERM_TYPE = "";
87 final String TEST_WORK_TERM_STATUS = "accepted";
88 final String TEST_WORK_TERM_QUALIFIER = "";
89 final String TEST_WORK_TERM_LANGUAGE = "english";
90 final Boolean TEST_WORK_TERM_PREFFORLANG = true;
91 final String TEST_WORK_TERM_SOURCE = "featurefilms";
92 final String TEST_WORK_TERM_SOURCE_DETAIL = "internal";
93 final String TEST_WORK_TERM_SOURCE_ID = "12345";
94 final String TEST_WORK_TERM_SOURCE_NOTE = "source note goes here";
95 final String TEST_WORK_HISTORY_NOTE = "history note goes here";
96 final String TEST_WORK_TYPE = "work type goes here";
97 final String TEST_WORK_CREATOR_GROUP_CREATOR = "Frank Oz";
98 final String TEST_WORK_CREATOR_GROUP_CREATOR_TYPE = "director";
99 final String TEST_WORK_PUBLISHER_GROUP_PUBLISHER = "TriStar Pictures";
100 final String TEST_WORK_PUBLISHER_GROUP_PUBLISHER_TYPE = "Distributor";
101 final String TEST_WORK_SHORT_IDENTIFIER = "muppetstakemanhattan";
102 final String TEST_WORK_REFNAME = "refname";
104 /** The known resource id. */
105 private String knownResourceShortIdentifer = null;
106 private String knownResourceRefName = null;
107 private String knownWorkTypeRefName = null;
110 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
113 protected CollectionSpaceClient getClientInstance() {
114 return new WorkAuthorityClient();
118 * Creates the item in authority.
120 * @param vcsid the vcsid
121 * @param authRefName the auth ref name
124 private String createItemInAuthority(String vcsid, String authRefName) {
125 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
127 // Submit the request to the service and store the response.
128 WorkAuthorityClient client = new WorkAuthorityClient();
129 Map<String, String> workMap = new HashMap<String,String>();
130 // TODO Make work type and status be controlled vocabs.
131 workMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, TEST_WORK_SHORT_IDENTIFIER);
132 workMap.put(WorkJAXBSchema.WORK_TYPE, TEST_WORK_TYPE);
133 workMap.put(WorkJAXBSchema.WORK_HISTORY_NOTE, TEST_WORK_HISTORY_NOTE);
135 List<WorkTermGroup> terms = new ArrayList<WorkTermGroup>();
136 WorkTermGroup term = new WorkTermGroup();
137 term.setTermDisplayName(TEST_WORK_TERM_DISPLAY_NAME);
138 term.setTermName(TEST_WORK_TERM_NAME);
139 term.setTermSource(TEST_WORK_TERM_SOURCE);
140 term.setTermSourceDetail(TEST_WORK_TERM_SOURCE_DETAIL);
141 term.setTermStatus(TEST_WORK_TERM_STATUS);
144 String newID = WorkAuthorityClientUtils.createItemInAuthority(vcsid,
145 authRefName, workMap, terms, client );
147 // Store the ID returned from the first item resource created
148 // for additional tests below.
149 if (knownItemResourceId == null){
150 setKnownItemResource(newID, TEST_WORK_SHORT_IDENTIFIER);
151 if (logger.isDebugEnabled()) {
152 logger.debug(testName + ": knownItemResourceId=" + newID);
156 // Store the IDs from any item resources created
157 // by tests, along with the IDs of their parents, so these items
158 // can be deleted after all tests have been run.
159 allResourceItemIdsCreated.put(newID, vcsid);
165 * Verify illegal item display name.
167 * @param testName the test name
168 * @throws Exception the exception
170 @Test(dataProvider="testName")
171 public void verifyIllegalItemDisplayName(String testName) throws Exception {
172 // Perform setup for read.
175 // Submit the request to the service and store the response.
176 WorkAuthorityClient client = new WorkAuthorityClient();
177 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
178 WorksCommon work = null;
180 assertStatusCode(res, testName);
181 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
182 work = (WorksCommon) extractPart(input,
183 client.getItemCommonPartName(), WorksCommon.class);
184 Assert.assertNotNull(work);
187 res.releaseConnection();
192 // Make an invalid UPDATE request, without a display name
194 WorkTermGroupList termList = work.getWorkTermGroupList();
195 Assert.assertNotNull(termList);
196 List<WorkTermGroup> terms = termList.getWorkTermGroup();
197 Assert.assertNotNull(terms);
198 Assert.assertTrue(terms.size() > 0);
199 terms.get(0).setTermDisplayName(null);
200 terms.get(0).setTermName(null);
202 setupUpdateWithInvalidBody(); // we expect a failure
204 // Submit the updated resource to the service and store the response.
205 PoxPayloadOut output = new PoxPayloadOut(WorkAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
206 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), work);
207 setupUpdateWithInvalidBody(); // we expected a failure here.
208 res = client.updateItem(knownResourceId, knownItemResourceId, output);
210 assertStatusCode(res, testName);
213 res.releaseConnection();
221 @Test(dataProvider = "testName", groups = {"readList"},
222 dependsOnMethods = {"readList"})
223 public void readItemList(String testName) {
224 readItemList(knownAuthorityWithItems, null);
228 * Read item list by authority name.
230 @Test(dataProvider = "testName", groups = {"readList"},
231 dependsOnMethods = {"readItemList"})
232 public void readItemListByAuthorityName(String testName) {
233 readItemList(null, READITEMS_SHORT_IDENTIFIER);
244 private void readItemList(String vcsid, String shortId) {
245 String testName = "readItemList";
250 // Submit the request to the service and store the response.
251 WorkAuthorityClient client = new WorkAuthorityClient();
252 ClientResponse<AbstractCommonList> res = null;
254 res = client.readItemList(vcsid, null, null);
255 } else if (shortId != null) {
256 res = client.readItemListForNamedAuthority(shortId, null, null);
258 Assert.fail("readItemList passed null csid and name!");
261 AbstractCommonList list = null;
263 assertStatusCode(res, testName);
264 list = res.getEntity();
267 res.releaseConnection();
271 List<AbstractCommonList.ListItem> items = list.getListItem();
272 int nItemsReturned = items.size();
273 // There will be 'nItemsToCreateInList'
274 // items created by the createItemList test,
275 // all associated with the same parent resource.
276 int nExpectedItems = nItemsToCreateInList;
277 if (logger.isDebugEnabled()) {
278 logger.debug(testName + ": Expected " + nExpectedItems
279 + " items; got: " + nItemsReturned);
281 Assert.assertEquals(nItemsReturned, nExpectedItems);
283 for (AbstractCommonList.ListItem item : items) {
284 String value = AbstractCommonListUtils.ListItemGetElementValue(
285 item, WorkJAXBSchema.REF_NAME);
286 Assert.assertTrue((null != value), "Item refName is null!");
287 value = AbstractCommonListUtils.ListItemGetElementValue(item,
288 WorkJAXBSchema.WORK_TERM_DISPLAY_NAME);
289 Assert.assertTrue((null != value), "Item termDisplayName is null!");
291 if (logger.isTraceEnabled()) {
292 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
298 public void delete(String testName) throws Exception {
299 // Do nothing. See localDelete(). This ensure proper test order.
302 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
303 public void localDelete(String testName) throws Exception {
304 super.delete(testName);
308 public void deleteItem(String testName) throws Exception {
309 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
310 // its dependencies will get run first and then we can call the base class' delete method.
313 @Test(dataProvider = "testName", groups = {"delete"},
314 dependsOnMethods = {"verifyIllegalItemDisplayName"})
315 public void localDeleteItem(String testName) throws Exception {
316 super.deleteItem(testName);
319 // ---------------------------------------------------------------
320 // Cleanup of resources created during testing
321 // ---------------------------------------------------------------
324 * Deletes all resources created by tests, after all tests have been run.
326 * This cleanup method will always be run, even if one or more tests fail.
327 * For this reason, it attempts to remove all resources created
328 * at any point during testing, even if some of those resources
329 * may be expected to be deleted by certain tests.
332 @AfterClass(alwaysRun=true)
333 public void cleanUp() {
334 String noTest = System.getProperty("noTestCleanup");
335 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
336 if (logger.isDebugEnabled()) {
337 logger.debug("Skipping Cleanup phase ...");
341 if (logger.isDebugEnabled()) {
342 logger.debug("Cleaning up temporary resources created for testing ...");
344 String parentResourceId;
345 String itemResourceId;
346 // Clean up contact resources.
347 WorkAuthorityClient client = new WorkAuthorityClient();
348 parentResourceId = knownResourceId;
349 // Clean up item resources.
350 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
351 itemResourceId = entry.getKey();
352 parentResourceId = entry.getValue();
353 // Note: Any non-success responses from the delete operation
354 // below are ignored and not reported.
355 client.deleteItem(parentResourceId, itemResourceId).close();
357 // Clean up parent resources.
358 for (String resourceId : allResourceIdsCreated) {
359 // Note: Any non-success responses from the delete operation
360 // below are ignored and not reported.
361 client.delete(resourceId).close();
365 // ---------------------------------------------------------------
366 // Utility methods used by tests above
367 // ---------------------------------------------------------------
369 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
373 * Returns the root URL for the item service.
375 * This URL consists of a base URL for all services, followed by
376 * a path component for the owning parent, followed by the
377 * path component for the items.
379 * @param parentResourceIdentifier An identifier (such as a UUID) for the
380 * parent authority resource of the relevant item resource.
382 * @return The root URL for the item service.
384 protected String getItemServiceRootURL(String parentResourceIdentifier) {
385 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
389 * Returns the URL of a specific item resource managed by a service, and
390 * designated by an identifier (such as a universally unique ID, or UUID).
392 * @param parentResourceIdentifier An identifier (such as a UUID) for the
393 * parent authority resource of the relevant item resource.
395 * @param itemResourceIdentifier An identifier (such as a UUID) for an
398 * @return The URL of a specific item resource managed by a service.
400 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
401 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
405 public void authorityTests(String testName) {
406 // TODO Auto-generated method stub
411 // Work specific overrides
415 protected PoxPayloadOut createInstance(String commonPartName,
417 // Submit the request to the service and store the response.
418 String shortId = identifier;
419 String displayName = "displayName-" + shortId;
420 // String baseRefName = WorkAuthorityClientUtils.createWorkAuthRefName(shortId, null);
421 PoxPayloadOut result =
422 WorkAuthorityClientUtils.createWorkAuthorityInstance(
423 displayName, shortId, commonPartName);
428 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
429 String displayName = "displayName-NON_EXISTENT_ID";
430 PoxPayloadOut result = WorkAuthorityClientUtils.createWorkAuthorityInstance(
431 displayName, "nonEx", commonPartName);
436 protected WorkauthoritiesCommon updateInstance(WorkauthoritiesCommon workauthoritiesCommon) {
437 WorkauthoritiesCommon result = new WorkauthoritiesCommon();
439 result.setDisplayName("updated-" + workauthoritiesCommon.getDisplayName());
440 result.setVocabType("updated-" + workauthoritiesCommon.getVocabType());
446 protected void compareUpdatedInstances(WorkauthoritiesCommon original,
447 WorkauthoritiesCommon updated) throws Exception {
448 Assert.assertEquals(updated.getDisplayName(),
449 original.getDisplayName(),
450 "Display name in updated object did not match submitted data.");
453 protected void compareReadInstances(WorkauthoritiesCommon original,
454 WorkauthoritiesCommon fromRead) throws Exception {
455 Assert.assertNotNull(fromRead.getDisplayName());
456 Assert.assertNotNull(fromRead.getShortIdentifier());
457 Assert.assertNotNull(fromRead.getRefName());
461 // Authority item specific overrides
465 protected String createItemInAuthority(String authorityId) {
466 return createItemInAuthority(authorityId, null /*refname*/);
470 protected WorksCommon updateItemInstance(WorksCommon worksCommon) {
472 WorkTermGroupList termList = worksCommon.getWorkTermGroupList();
473 Assert.assertNotNull(termList);
474 List<WorkTermGroup> terms = termList.getWorkTermGroup();
475 Assert.assertNotNull(terms);
476 Assert.assertTrue(terms.size() > 0);
477 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
478 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
479 worksCommon.setWorkTermGroupList(termList);
485 protected void compareUpdatedItemInstances(WorksCommon original,
486 WorksCommon updated) throws Exception {
488 WorkTermGroupList originalTermList = original.getWorkTermGroupList();
489 Assert.assertNotNull(originalTermList);
490 List<WorkTermGroup> originalTerms = originalTermList.getWorkTermGroup();
491 Assert.assertNotNull(originalTerms);
492 Assert.assertTrue(originalTerms.size() > 0);
494 WorkTermGroupList updatedTermList = updated.getWorkTermGroupList();
495 Assert.assertNotNull(updatedTermList);
496 List<WorkTermGroup> updatedTerms = updatedTermList.getWorkTermGroup();
497 Assert.assertNotNull(updatedTerms);
498 Assert.assertTrue(updatedTerms.size() > 0);
500 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
501 originalTerms.get(0).getTermDisplayName(),
502 "Value in updated record did not match submitted data.");
506 protected void verifyReadItemInstance(WorksCommon item)
508 // TODO Auto-generated method stub
513 protected PoxPayloadOut createNonExistenceItemInstance(
514 String commonPartName, String identifier) {
515 Map<String, String> nonexMap = new HashMap<String,String>();
516 nonexMap.put(WorkJAXBSchema.WORK_TERM_DISPLAY_NAME, TEST_WORK_TERM_DISPLAY_NAME);
517 nonexMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, "nonEx");
518 nonexMap.put(WorkJAXBSchema.WORK_TERM_STATUS, TEST_WORK_TERM_STATUS);
519 final String EMPTY_REFNAME = "";
520 PoxPayloadOut result =
521 WorkAuthorityClientUtils.createWorkInstance(EMPTY_REFNAME, nonexMap,
522 WorkAuthorityClientUtils.getTermGroupInstance(TEST_WORK_TERM_DISPLAY_NAME), commonPartName);