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;
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 Logger logger = LoggerFactory.getLogger(WorkAuthorityServiceTest.class);
64 * Default constructor. Used to set the short ID for all tests authority items
66 WorkAuthorityServiceTest() {
67 TEST_SHORTID = "muppetstakemanhattan";
71 public String getServicePathComponent() {
72 return WorkAuthorityClient.SERVICE_PATH_COMPONENT;
76 protected String getServiceName() {
77 return WorkAuthorityClient.SERVICE_NAME;
80 public String getItemServicePathComponent() {
81 return AuthorityClient.ITEMS;
84 // Instance variables specific to this test.
86 final String TEST_WORK_TERM_DISPLAY_NAME = "Muppets Take Manhattan (1984)";
87 final String TEST_WORK_TERM_NAME = "Muppets Take Manhattan";
88 final String TEST_WORK_TERM_TYPE = "";
89 final String TEST_WORK_TERM_STATUS = "accepted";
90 final String TEST_WORK_TERM_QUALIFIER = "";
91 final String TEST_WORK_TERM_LANGUAGE = "english";
92 final Boolean TEST_WORK_TERM_PREFFORLANG = true;
93 final String TEST_WORK_TERM_SOURCE = "featurefilms";
94 final String TEST_WORK_TERM_SOURCE_DETAIL = "internal";
95 final String TEST_WORK_TERM_SOURCE_ID = "12345";
96 final String TEST_WORK_TERM_SOURCE_NOTE = "source note goes here";
97 final String TEST_WORK_HISTORY_NOTE = "history note goes here";
98 final String TEST_WORK_TYPE = "work type goes here";
99 final String TEST_WORK_CREATOR_GROUP_CREATOR = "Frank Oz";
100 final String TEST_WORK_CREATOR_GROUP_CREATOR_TYPE = "director";
101 final String TEST_WORK_PUBLISHER_GROUP_PUBLISHER = "TriStar Pictures";
102 final String TEST_WORK_PUBLISHER_GROUP_PUBLISHER_TYPE = "Distributor";
103 final String TEST_WORK_REFNAME = "refname";
106 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
109 protected CollectionSpaceClient getClientInstance() {
110 return new WorkAuthorityClient();
114 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
115 return new WorkAuthorityClient(clientPropertiesFilename);
119 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
120 return createItemInAuthority(client, authorityId, shortId, null /*refname*/);
124 * Creates the item in authority.
126 * @param vcsid the vcsid
127 * @param authRefName the auth ref name
130 private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
131 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
133 // Submit the request to the service and store the response.
134 Map<String, String> workMap = new HashMap<String,String>();
135 // TODO Make work type and status be controlled vocabs.
136 workMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, shortId);
137 workMap.put(WorkJAXBSchema.WORK_TYPE, TEST_WORK_TYPE);
138 workMap.put(WorkJAXBSchema.WORK_HISTORY_NOTE, TEST_WORK_HISTORY_NOTE);
140 List<WorkTermGroup> terms = new ArrayList<WorkTermGroup>();
141 WorkTermGroup term = new WorkTermGroup();
142 term.setTermDisplayName(TEST_WORK_TERM_DISPLAY_NAME);
143 term.setTermName(TEST_WORK_TERM_NAME);
144 term.setTermSource(TEST_WORK_TERM_SOURCE);
145 term.setTermSourceDetail(TEST_WORK_TERM_SOURCE_DETAIL);
146 term.setTermStatus(TEST_WORK_TERM_STATUS);
149 String newID = WorkAuthorityClientUtils.createItemInAuthority(vcsid,
150 authRefName, workMap, terms, (WorkAuthorityClient)client);
152 // Store the ID returned from the first item resource created
153 // for additional tests below.
154 if (knownItemResourceId == null){
155 setKnownItemResource(newID, shortId);
156 if (logger.isDebugEnabled()) {
157 logger.debug(testName + ": knownItemResourceId=" + newID);
161 // Store the IDs from any item resources created
162 // by tests, along with the IDs of their parents, so these items
163 // can be deleted after all tests have been run.
164 allResourceItemIdsCreated.put(newID, vcsid);
170 * Verify illegal item display name.
172 * @param testName the test name
173 * @throws Exception the exception
175 @Test(dataProvider="testName")
176 public void verifyIllegalItemDisplayName(String testName) throws Exception {
177 // Perform setup for read.
180 // Submit the request to the service and store the response.
181 WorkAuthorityClient client = new WorkAuthorityClient();
182 Response res = client.readItem(knownResourceId, knownItemResourceId);
183 WorksCommon work = null;
185 assertStatusCode(res, testName);
186 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
187 work = (WorksCommon) extractPart(input,
188 client.getItemCommonPartName(), WorksCommon.class);
189 Assert.assertNotNull(work);
197 // Make an invalid UPDATE request, without a display name
199 WorkTermGroupList termList = work.getWorkTermGroupList();
200 Assert.assertNotNull(termList);
201 List<WorkTermGroup> terms = termList.getWorkTermGroup();
202 Assert.assertNotNull(terms);
203 Assert.assertTrue(terms.size() > 0);
204 terms.get(0).setTermDisplayName(null);
205 terms.get(0).setTermName(null);
207 setupUpdateWithInvalidBody(); // we expect a failure
209 // Submit the updated resource to the service and store the response.
210 PoxPayloadOut output = new PoxPayloadOut(
211 WorkAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
212 output.addPart(client.getItemCommonPartName(), work);
213 setupUpdateWithInvalidBody(); // we expected a failure here.
214 res = client.updateItem(knownResourceId, knownItemResourceId, output);
216 assertStatusCode(res, testName);
227 @Test(dataProvider = "testName", groups = {"readList"},
228 dependsOnMethods = {"readList"})
229 public void readItemList(String testName) {
230 readItemList(knownAuthorityWithItems, null);
234 * Read item list by authority name.
236 @Test(dataProvider = "testName", groups = {"readList"},
237 dependsOnMethods = {"readItemList"})
238 public void readItemListByAuthorityName(String testName) {
239 readItemList(null, READITEMS_SHORT_IDENTIFIER);
250 private void readItemList(String vcsid, String shortId) {
251 String testName = "readItemList";
256 // Submit the request to the service and store the response.
257 WorkAuthorityClient client = new WorkAuthorityClient();
260 res = client.readItemList(vcsid, null, null);
261 } else if (shortId != null) {
262 res = client.readItemListForNamedAuthority(shortId, null, null);
264 Assert.fail("readItemList passed null csid and name!");
267 AbstractCommonList list = null;
269 assertStatusCode(res, testName);
270 list = res.readEntity(AbstractCommonList.class);
277 List<AbstractCommonList.ListItem> items = list.getListItem();
278 int nItemsReturned = items.size();
279 // There will be 'nItemsToCreateInList'
280 // items created by the createItemList test,
281 // all associated with the same parent resource.
282 int nExpectedItems = nItemsToCreateInList;
283 if (logger.isDebugEnabled()) {
284 logger.debug(testName + ": Expected " + nExpectedItems
285 + " items; got: " + nItemsReturned);
287 Assert.assertEquals(nItemsReturned, nExpectedItems);
289 for (AbstractCommonList.ListItem item : items) {
290 String value = AbstractCommonListUtils.ListItemGetElementValue(
291 item, WorkJAXBSchema.REF_NAME);
292 Assert.assertTrue((null != value), "Item refName is null!");
293 value = AbstractCommonListUtils.ListItemGetElementValue(item,
294 WorkJAXBSchema.WORK_TERM_DISPLAY_NAME);
295 Assert.assertTrue((null != value), "Item termDisplayName is null!");
297 if (logger.isTraceEnabled()) {
298 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
304 public void delete(String testName) throws Exception {
305 // Do nothing. See localDelete(). This ensure proper test order.
308 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
309 public void localDelete(String testName) throws Exception {
310 super.delete(testName);
314 public void deleteItem(String testName) throws Exception {
315 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
316 // its dependencies will get run first and then we can call the base class' delete method.
319 @Test(dataProvider = "testName", groups = {"delete"},
320 dependsOnMethods = {"verifyIllegalItemDisplayName"})
321 public void localDeleteItem(String testName) throws Exception {
322 super.deleteItem(testName);
325 // ---------------------------------------------------------------
326 // Cleanup of resources created during testing
327 // ---------------------------------------------------------------
330 * Deletes all resources created by tests, after all tests have been run.
332 * This cleanup method will always be run, even if one or more tests fail.
333 * For this reason, it attempts to remove all resources created
334 * at any point during testing, even if some of those resources
335 * may be expected to be deleted by certain tests.
338 @AfterClass(alwaysRun=true)
339 public void cleanUp() {
340 String noTest = System.getProperty("noTestCleanup");
341 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
342 if (logger.isDebugEnabled()) {
343 logger.debug("Skipping Cleanup phase ...");
347 if (logger.isDebugEnabled()) {
348 logger.debug("Cleaning up temporary resources created for testing ...");
350 String parentResourceId;
351 String itemResourceId;
352 // Clean up contact resources.
353 WorkAuthorityClient client = new WorkAuthorityClient();
354 parentResourceId = knownResourceId;
355 // Clean up item resources.
356 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
357 itemResourceId = entry.getKey();
358 parentResourceId = entry.getValue();
359 // Note: Any non-success responses from the delete operation
360 // below are ignored and not reported.
361 client.deleteItem(parentResourceId, itemResourceId).close();
363 // Clean up parent resources.
364 for (String resourceId : allResourceIdsCreated) {
365 // Note: Any non-success responses from the delete operation
366 // below are ignored and not reported.
367 client.delete(resourceId).close();
371 // ---------------------------------------------------------------
372 // Utility methods used by tests above
373 // ---------------------------------------------------------------
375 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
379 * Returns the root URL for the item service.
381 * This URL consists of a base URL for all services, followed by
382 * a path component for the owning parent, followed by the
383 * path component for the items.
385 * @param parentResourceIdentifier An identifier (such as a UUID) for the
386 * parent authority resource of the relevant item resource.
388 * @return The root URL for the item service.
390 protected String getItemServiceRootURL(String parentResourceIdentifier) {
391 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
395 * Returns the URL of a specific item resource managed by a service, and
396 * designated by an identifier (such as a universally unique ID, or UUID).
398 * @param parentResourceIdentifier An identifier (such as a UUID) for the
399 * parent authority resource of the relevant item resource.
401 * @param itemResourceIdentifier An identifier (such as a UUID) for an
404 * @return The URL of a specific item resource managed by a service.
406 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
407 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
411 public void authorityTests(String testName) {
412 // TODO Auto-generated method stub
417 // Work specific overrides
421 protected PoxPayloadOut createInstance(String commonPartName,
423 // Submit the request to the service and store the response.
424 String shortId = identifier;
425 String displayName = "displayName-" + shortId;
426 // String baseRefName = WorkAuthorityClientUtils.createWorkAuthRefName(shortId, null);
427 PoxPayloadOut result =
428 WorkAuthorityClientUtils.createWorkAuthorityInstance(
429 displayName, shortId, commonPartName);
434 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
435 String displayName = "displayName-NON_EXISTENT_ID";
436 PoxPayloadOut result = WorkAuthorityClientUtils.createWorkAuthorityInstance(
437 displayName, "nonEx", commonPartName);
442 protected WorkauthoritiesCommon updateInstance(WorkauthoritiesCommon workauthoritiesCommon) {
443 WorkauthoritiesCommon result = new WorkauthoritiesCommon();
445 result.setDisplayName("updated-" + workauthoritiesCommon.getDisplayName());
446 result.setVocabType("updated-" + workauthoritiesCommon.getVocabType());
452 protected void compareUpdatedInstances(WorkauthoritiesCommon original,
453 WorkauthoritiesCommon updated) throws Exception {
454 Assert.assertEquals(updated.getDisplayName(),
455 original.getDisplayName(),
456 "Display name in updated object did not match submitted data.");
459 protected void compareReadInstances(WorkauthoritiesCommon original,
460 WorkauthoritiesCommon fromRead) throws Exception {
461 Assert.assertNotNull(fromRead.getDisplayName());
462 Assert.assertNotNull(fromRead.getShortIdentifier());
463 Assert.assertNotNull(fromRead.getRefName());
467 // Authority item specific overrides
471 protected WorksCommon updateItemInstance(WorksCommon worksCommon) {
473 WorkTermGroupList termList = worksCommon.getWorkTermGroupList();
474 Assert.assertNotNull(termList);
475 List<WorkTermGroup> terms = termList.getWorkTermGroup();
476 Assert.assertNotNull(terms);
477 Assert.assertTrue(terms.size() > 0);
478 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
479 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
480 worksCommon.setWorkTermGroupList(termList);
486 protected void compareUpdatedItemInstances(WorksCommon original,
487 WorksCommon updated) throws Exception {
489 WorkTermGroupList originalTermList = original.getWorkTermGroupList();
490 Assert.assertNotNull(originalTermList);
491 List<WorkTermGroup> originalTerms = originalTermList.getWorkTermGroup();
492 Assert.assertNotNull(originalTerms);
493 Assert.assertTrue(originalTerms.size() > 0);
495 WorkTermGroupList updatedTermList = updated.getWorkTermGroupList();
496 Assert.assertNotNull(updatedTermList);
497 List<WorkTermGroup> updatedTerms = updatedTermList.getWorkTermGroup();
498 Assert.assertNotNull(updatedTerms);
499 Assert.assertTrue(updatedTerms.size() > 0);
501 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
502 originalTerms.get(0).getTermDisplayName(),
503 "Value in updated record did not match submitted data.");
507 protected void verifyReadItemInstance(WorksCommon item)
509 // TODO Auto-generated method stub
514 protected PoxPayloadOut createNonExistenceItemInstance(
515 String commonPartName, String identifier) {
516 Map<String, String> nonexMap = new HashMap<String,String>();
517 nonexMap.put(WorkJAXBSchema.WORK_TERM_DISPLAY_NAME, TEST_WORK_TERM_DISPLAY_NAME);
518 nonexMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, "nonEx");
519 nonexMap.put(WorkJAXBSchema.WORK_TERM_STATUS, TEST_WORK_TERM_STATUS);
520 final String EMPTY_REFNAME = "";
521 PoxPayloadOut result =
522 WorkAuthorityClientUtils.createWorkInstance(EMPTY_REFNAME, nonexMap,
523 WorkAuthorityClientUtils.getTermGroupInstance(TEST_WORK_TERM_DISPLAY_NAME), commonPartName);