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 public WorkAuthorityServiceTest() {
68 TEST_SHORTID = "muppetstakemanhattan";
72 public String getServicePathComponent() {
73 return WorkAuthorityClient.SERVICE_PATH_COMPONENT;
77 protected String getServiceName() {
78 return WorkAuthorityClient.SERVICE_NAME;
81 public String getItemServicePathComponent() {
82 return AuthorityClient.ITEMS;
85 // Instance variables specific to this test.
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_REFNAME = "refname";
107 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
110 protected CollectionSpaceClient getClientInstance() {
111 return new WorkAuthorityClient();
115 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
116 return new WorkAuthorityClient(clientPropertiesFilename);
120 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
121 return createItemInAuthority(client, authorityId, shortId, null /*refname*/);
125 * Creates the item in authority.
127 * @param vcsid the vcsid
128 * @param authRefName the auth ref name
131 private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
132 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
134 // Submit the request to the service and store the response.
135 Map<String, String> workMap = new HashMap<String,String>();
136 // TODO Make work type and status be controlled vocabs.
137 workMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, shortId);
138 workMap.put(WorkJAXBSchema.WORK_TYPE, TEST_WORK_TYPE);
139 workMap.put(WorkJAXBSchema.WORK_HISTORY_NOTE, TEST_WORK_HISTORY_NOTE);
141 List<WorkTermGroup> terms = new ArrayList<WorkTermGroup>();
142 WorkTermGroup term = new WorkTermGroup();
143 term.setTermDisplayName(TEST_WORK_TERM_DISPLAY_NAME);
144 term.setTermName(TEST_WORK_TERM_NAME);
145 term.setTermSource(TEST_WORK_TERM_SOURCE);
146 term.setTermSourceDetail(TEST_WORK_TERM_SOURCE_DETAIL);
147 term.setTermStatus(TEST_WORK_TERM_STATUS);
150 String newID = WorkAuthorityClientUtils.createItemInAuthority(vcsid,
151 authRefName, workMap, terms, (WorkAuthorityClient)client);
153 // Store the ID returned from the first item resource created
154 // for additional tests below.
155 if (knownItemResourceId == null){
156 setKnownItemResource(newID, shortId);
157 if (logger.isDebugEnabled()) {
158 logger.debug(testName + ": knownItemResourceId=" + newID);
162 // Store the IDs from any item resources created
163 // by tests, along with the IDs of their parents, so these items
164 // can be deleted after all tests have been run.
165 allResourceItemIdsCreated.put(newID, vcsid);
171 * Verify illegal item display name.
173 * @param testName the test name
174 * @throws Exception the exception
176 @Test(dataProvider="testName")
177 public void verifyIllegalItemDisplayName(String testName) throws Exception {
178 // Perform setup for read.
181 // Submit the request to the service and store the response.
182 WorkAuthorityClient client = new WorkAuthorityClient();
183 Response res = client.readItem(knownResourceId, knownItemResourceId);
184 WorksCommon work = null;
186 assertStatusCode(res, testName);
187 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
188 work = (WorksCommon) extractPart(input,
189 client.getItemCommonPartName(), WorksCommon.class);
190 Assert.assertNotNull(work);
198 // Make an invalid UPDATE request, without a display name
200 WorkTermGroupList termList = work.getWorkTermGroupList();
201 Assert.assertNotNull(termList);
202 List<WorkTermGroup> terms = termList.getWorkTermGroup();
203 Assert.assertNotNull(terms);
204 Assert.assertTrue(terms.size() > 0);
205 terms.get(0).setTermDisplayName(null);
206 terms.get(0).setTermName(null);
208 setupUpdateWithInvalidBody(); // we expect a failure
210 // Submit the updated resource to the service and store the response.
211 PoxPayloadOut output = new PoxPayloadOut(
212 WorkAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
213 output.addPart(client.getItemCommonPartName(), work);
214 setupUpdateWithInvalidBody(); // we expected a failure here.
215 res = client.updateItem(knownResourceId, knownItemResourceId, output);
217 assertStatusCode(res, testName);
228 @Test(dataProvider = "testName", groups = {"readList"},
229 dependsOnMethods = {"readList"})
230 public void readItemList(String testName) {
231 readItemList(knownAuthorityWithItems, null);
235 * Read item list by authority name.
237 @Test(dataProvider = "testName", groups = {"readList"},
238 dependsOnMethods = {"readItemList"})
239 public void readItemListByAuthorityName(String testName) {
240 readItemList(null, READITEMS_SHORT_IDENTIFIER);
251 private void readItemList(String vcsid, String shortId) {
252 String testName = "readItemList";
257 // Submit the request to the service and store the response.
258 WorkAuthorityClient client = new WorkAuthorityClient();
261 res = client.readItemList(vcsid, null, null);
262 } else if (shortId != null) {
263 res = client.readItemListForNamedAuthority(shortId, null, null);
265 Assert.fail("readItemList passed null csid and name!");
268 AbstractCommonList list = null;
270 assertStatusCode(res, testName);
271 list = res.readEntity(AbstractCommonList.class);
278 List<AbstractCommonList.ListItem> items = list.getListItem();
279 int nItemsReturned = items.size();
280 // There will be 'nItemsToCreateInList'
281 // items created by the createItemList test,
282 // all associated with the same parent resource.
283 int nExpectedItems = nItemsToCreateInList;
284 if (logger.isDebugEnabled()) {
285 logger.debug(testName + ": Expected " + nExpectedItems
286 + " items; got: " + nItemsReturned);
288 Assert.assertEquals(nItemsReturned, nExpectedItems);
290 for (AbstractCommonList.ListItem item : items) {
291 String value = AbstractCommonListUtils.ListItemGetElementValue(
292 item, WorkJAXBSchema.REF_NAME);
293 Assert.assertTrue((null != value), "Item refName is null!");
294 value = AbstractCommonListUtils.ListItemGetElementValue(item,
295 WorkJAXBSchema.WORK_TERM_DISPLAY_NAME);
296 Assert.assertTrue((null != value), "Item termDisplayName is null!");
298 if (logger.isTraceEnabled()) {
299 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
305 public void delete(String testName) throws Exception {
306 // Do nothing. See localDelete(). This ensure proper test order.
309 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
310 public void localDelete(String testName) throws Exception {
311 super.delete(testName);
315 public void deleteItem(String testName) throws Exception {
316 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
317 // its dependencies will get run first and then we can call the base class' delete method.
320 @Test(dataProvider = "testName", groups = {"delete"},
321 dependsOnMethods = {"verifyIllegalItemDisplayName"})
322 public void localDeleteItem(String testName) throws Exception {
323 super.deleteItem(testName);
326 // ---------------------------------------------------------------
327 // Cleanup of resources created during testing
328 // ---------------------------------------------------------------
331 * Deletes all resources created by tests, after all tests have been run.
333 * This cleanup method will always be run, even if one or more tests fail.
334 * For this reason, it attempts to remove all resources created
335 * at any point during testing, even if some of those resources
336 * may be expected to be deleted by certain tests.
339 @AfterClass(alwaysRun=true)
340 public void cleanUp() {
341 String noTest = System.getProperty("noTestCleanup");
342 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
343 if (logger.isDebugEnabled()) {
344 logger.debug("Skipping Cleanup phase ...");
348 if (logger.isDebugEnabled()) {
349 logger.debug("Cleaning up temporary resources created for testing ...");
351 String parentResourceId;
352 String itemResourceId;
353 // Clean up contact resources.
354 WorkAuthorityClient client = new WorkAuthorityClient();
355 parentResourceId = knownResourceId;
356 // Clean up item resources.
357 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
358 itemResourceId = entry.getKey();
359 parentResourceId = entry.getValue();
360 // Note: Any non-success responses from the delete operation
361 // below are ignored and not reported.
362 client.deleteItem(parentResourceId, itemResourceId).close();
364 // Clean up parent resources.
365 for (String resourceId : allResourceIdsCreated) {
366 // Note: Any non-success responses from the delete operation
367 // below are ignored and not reported.
368 client.delete(resourceId).close();
372 // ---------------------------------------------------------------
373 // Utility methods used by tests above
374 // ---------------------------------------------------------------
376 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
380 * Returns the root URL for the item service.
382 * This URL consists of a base URL for all services, followed by
383 * a path component for the owning parent, followed by the
384 * path component for the items.
386 * @param parentResourceIdentifier An identifier (such as a UUID) for the
387 * parent authority resource of the relevant item resource.
389 * @return The root URL for the item service.
391 protected String getItemServiceRootURL(String parentResourceIdentifier) {
392 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
396 * Returns the URL of a specific item resource managed by a service, and
397 * designated by an identifier (such as a universally unique ID, or UUID).
399 * @param parentResourceIdentifier An identifier (such as a UUID) for the
400 * parent authority resource of the relevant item resource.
402 * @param itemResourceIdentifier An identifier (such as a UUID) for an
405 * @return The URL of a specific item resource managed by a service.
407 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
408 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
412 public void authorityTests(String testName) {
413 // TODO Auto-generated method stub
418 // Work specific overrides
422 protected PoxPayloadOut createInstance(String commonPartName,
424 // Submit the request to the service and store the response.
425 String shortId = identifier;
426 String displayName = "displayName-" + shortId;
427 // String baseRefName = WorkAuthorityClientUtils.createWorkAuthRefName(shortId, null);
428 PoxPayloadOut result =
429 WorkAuthorityClientUtils.createWorkAuthorityInstance(
430 displayName, shortId, commonPartName);
435 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
436 String displayName = "displayName-NON_EXISTENT_ID";
437 PoxPayloadOut result = WorkAuthorityClientUtils.createWorkAuthorityInstance(
438 displayName, "nonEx", commonPartName);
443 protected WorkauthoritiesCommon updateInstance(WorkauthoritiesCommon workauthoritiesCommon) {
444 WorkauthoritiesCommon result = new WorkauthoritiesCommon();
446 result.setDisplayName("updated-" + workauthoritiesCommon.getDisplayName());
447 result.setVocabType("updated-" + workauthoritiesCommon.getVocabType());
453 protected void compareUpdatedInstances(WorkauthoritiesCommon original,
454 WorkauthoritiesCommon updated) throws Exception {
455 Assert.assertEquals(updated.getDisplayName(),
456 original.getDisplayName(),
457 "Display name in updated object did not match submitted data.");
460 protected void compareReadInstances(WorkauthoritiesCommon original,
461 WorkauthoritiesCommon fromRead) throws Exception {
462 Assert.assertNotNull(fromRead.getDisplayName());
463 Assert.assertNotNull(fromRead.getShortIdentifier());
464 Assert.assertNotNull(fromRead.getRefName());
468 // Authority item specific overrides
472 protected WorksCommon updateItemInstance(WorksCommon worksCommon) {
474 WorkTermGroupList termList = worksCommon.getWorkTermGroupList();
475 Assert.assertNotNull(termList);
476 List<WorkTermGroup> terms = termList.getWorkTermGroup();
477 Assert.assertNotNull(terms);
478 Assert.assertTrue(terms.size() > 0);
479 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
480 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
481 worksCommon.setWorkTermGroupList(termList);
487 protected void compareUpdatedItemInstances(WorksCommon original,
488 WorksCommon updated) throws Exception {
490 WorkTermGroupList originalTermList = original.getWorkTermGroupList();
491 Assert.assertNotNull(originalTermList);
492 List<WorkTermGroup> originalTerms = originalTermList.getWorkTermGroup();
493 Assert.assertNotNull(originalTerms);
494 Assert.assertTrue(originalTerms.size() > 0);
496 WorkTermGroupList updatedTermList = updated.getWorkTermGroupList();
497 Assert.assertNotNull(updatedTermList);
498 List<WorkTermGroup> updatedTerms = updatedTermList.getWorkTermGroup();
499 Assert.assertNotNull(updatedTerms);
500 Assert.assertTrue(updatedTerms.size() > 0);
502 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
503 originalTerms.get(0).getTermDisplayName(),
504 "Value in updated record did not match submitted data.");
508 protected void verifyReadItemInstance(WorksCommon item)
510 // TODO Auto-generated method stub
515 protected PoxPayloadOut createNonExistenceItemInstance(
516 String commonPartName, String identifier) {
517 Map<String, String> nonexMap = new HashMap<String,String>();
518 nonexMap.put(WorkJAXBSchema.WORK_TERM_DISPLAY_NAME, TEST_WORK_TERM_DISPLAY_NAME);
519 nonexMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, "nonEx");
520 nonexMap.put(WorkJAXBSchema.WORK_TERM_STATUS, TEST_WORK_TERM_STATUS);
521 final String EMPTY_REFNAME = "";
522 PoxPayloadOut result =
523 WorkAuthorityClientUtils.createWorkInstance(EMPTY_REFNAME, nonexMap,
524 WorkAuthorityClientUtils.getTermGroupInstance(TEST_WORK_TERM_DISPLAY_NAME), commonPartName);