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 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 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
122 return new WorkAuthorityClient(clientPropertiesFilename);
126 * Creates the item in authority.
128 * @param vcsid the vcsid
129 * @param authRefName the auth ref name
132 private String createItemInAuthority(AuthorityClient client, String vcsid, String authRefName) {
133 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
135 // Submit the request to the service and store the response.
136 Map<String, String> workMap = new HashMap<String,String>();
137 // TODO Make work type and status be controlled vocabs.
138 workMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, TEST_WORK_SHORT_IDENTIFIER);
139 workMap.put(WorkJAXBSchema.WORK_TYPE, TEST_WORK_TYPE);
140 workMap.put(WorkJAXBSchema.WORK_HISTORY_NOTE, TEST_WORK_HISTORY_NOTE);
142 List<WorkTermGroup> terms = new ArrayList<WorkTermGroup>();
143 WorkTermGroup term = new WorkTermGroup();
144 term.setTermDisplayName(TEST_WORK_TERM_DISPLAY_NAME);
145 term.setTermName(TEST_WORK_TERM_NAME);
146 term.setTermSource(TEST_WORK_TERM_SOURCE);
147 term.setTermSourceDetail(TEST_WORK_TERM_SOURCE_DETAIL);
148 term.setTermStatus(TEST_WORK_TERM_STATUS);
151 String newID = WorkAuthorityClientUtils.createItemInAuthority(vcsid,
152 authRefName, workMap, terms, (WorkAuthorityClient)client);
154 // Store the ID returned from the first item resource created
155 // for additional tests below.
156 if (knownItemResourceId == null){
157 setKnownItemResource(newID, TEST_WORK_SHORT_IDENTIFIER);
158 if (logger.isDebugEnabled()) {
159 logger.debug(testName + ": knownItemResourceId=" + newID);
163 // Store the IDs from any item resources created
164 // by tests, along with the IDs of their parents, so these items
165 // can be deleted after all tests have been run.
166 allResourceItemIdsCreated.put(newID, vcsid);
172 * Verify illegal item display name.
174 * @param testName the test name
175 * @throws Exception the exception
177 @Test(dataProvider="testName")
178 public void verifyIllegalItemDisplayName(String testName) throws Exception {
179 // Perform setup for read.
182 // Submit the request to the service and store the response.
183 WorkAuthorityClient client = new WorkAuthorityClient();
184 Response res = client.readItem(knownResourceId, knownItemResourceId);
185 WorksCommon work = null;
187 assertStatusCode(res, testName);
188 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
189 work = (WorksCommon) extractPart(input,
190 client.getItemCommonPartName(), WorksCommon.class);
191 Assert.assertNotNull(work);
199 // Make an invalid UPDATE request, without a display name
201 WorkTermGroupList termList = work.getWorkTermGroupList();
202 Assert.assertNotNull(termList);
203 List<WorkTermGroup> terms = termList.getWorkTermGroup();
204 Assert.assertNotNull(terms);
205 Assert.assertTrue(terms.size() > 0);
206 terms.get(0).setTermDisplayName(null);
207 terms.get(0).setTermName(null);
209 setupUpdateWithInvalidBody(); // we expect a failure
211 // Submit the updated resource to the service and store the response.
212 PoxPayloadOut output = new PoxPayloadOut(
213 WorkAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
214 PayloadOutputPart commonPart = output.addPart(
215 client.getItemCommonPartName(), work);
216 setupUpdateWithInvalidBody(); // we expected a failure here.
217 res = client.updateItem(knownResourceId, knownItemResourceId, output);
219 assertStatusCode(res, testName);
230 @Test(dataProvider = "testName", groups = {"readList"},
231 dependsOnMethods = {"readList"})
232 public void readItemList(String testName) {
233 readItemList(knownAuthorityWithItems, null);
237 * Read item list by authority name.
239 @Test(dataProvider = "testName", groups = {"readList"},
240 dependsOnMethods = {"readItemList"})
241 public void readItemListByAuthorityName(String testName) {
242 readItemList(null, READITEMS_SHORT_IDENTIFIER);
253 private void readItemList(String vcsid, String shortId) {
254 String testName = "readItemList";
259 // Submit the request to the service and store the response.
260 WorkAuthorityClient client = new WorkAuthorityClient();
263 res = client.readItemList(vcsid, null, null);
264 } else if (shortId != null) {
265 res = client.readItemListForNamedAuthority(shortId, null, null);
267 Assert.fail("readItemList passed null csid and name!");
270 AbstractCommonList list = null;
272 assertStatusCode(res, testName);
273 list = res.readEntity(AbstractCommonList.class);
280 List<AbstractCommonList.ListItem> items = list.getListItem();
281 int nItemsReturned = items.size();
282 // There will be 'nItemsToCreateInList'
283 // items created by the createItemList test,
284 // all associated with the same parent resource.
285 int nExpectedItems = nItemsToCreateInList;
286 if (logger.isDebugEnabled()) {
287 logger.debug(testName + ": Expected " + nExpectedItems
288 + " items; got: " + nItemsReturned);
290 Assert.assertEquals(nItemsReturned, nExpectedItems);
292 for (AbstractCommonList.ListItem item : items) {
293 String value = AbstractCommonListUtils.ListItemGetElementValue(
294 item, WorkJAXBSchema.REF_NAME);
295 Assert.assertTrue((null != value), "Item refName is null!");
296 value = AbstractCommonListUtils.ListItemGetElementValue(item,
297 WorkJAXBSchema.WORK_TERM_DISPLAY_NAME);
298 Assert.assertTrue((null != value), "Item termDisplayName is null!");
300 if (logger.isTraceEnabled()) {
301 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
307 public void delete(String testName) throws Exception {
308 // Do nothing. See localDelete(). This ensure proper test order.
311 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
312 public void localDelete(String testName) throws Exception {
313 super.delete(testName);
317 public void deleteItem(String testName) throws Exception {
318 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
319 // its dependencies will get run first and then we can call the base class' delete method.
322 @Test(dataProvider = "testName", groups = {"delete"},
323 dependsOnMethods = {"verifyIllegalItemDisplayName"})
324 public void localDeleteItem(String testName) throws Exception {
325 super.deleteItem(testName);
328 // ---------------------------------------------------------------
329 // Cleanup of resources created during testing
330 // ---------------------------------------------------------------
333 * Deletes all resources created by tests, after all tests have been run.
335 * This cleanup method will always be run, even if one or more tests fail.
336 * For this reason, it attempts to remove all resources created
337 * at any point during testing, even if some of those resources
338 * may be expected to be deleted by certain tests.
341 @AfterClass(alwaysRun=true)
342 public void cleanUp() {
343 String noTest = System.getProperty("noTestCleanup");
344 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
345 if (logger.isDebugEnabled()) {
346 logger.debug("Skipping Cleanup phase ...");
350 if (logger.isDebugEnabled()) {
351 logger.debug("Cleaning up temporary resources created for testing ...");
353 String parentResourceId;
354 String itemResourceId;
355 // Clean up contact resources.
356 WorkAuthorityClient client = new WorkAuthorityClient();
357 parentResourceId = knownResourceId;
358 // Clean up item resources.
359 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
360 itemResourceId = entry.getKey();
361 parentResourceId = entry.getValue();
362 // Note: Any non-success responses from the delete operation
363 // below are ignored and not reported.
364 client.deleteItem(parentResourceId, itemResourceId).close();
366 // Clean up parent resources.
367 for (String resourceId : allResourceIdsCreated) {
368 // Note: Any non-success responses from the delete operation
369 // below are ignored and not reported.
370 client.delete(resourceId).close();
374 // ---------------------------------------------------------------
375 // Utility methods used by tests above
376 // ---------------------------------------------------------------
378 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
382 * Returns the root URL for the item service.
384 * This URL consists of a base URL for all services, followed by
385 * a path component for the owning parent, followed by the
386 * path component for the items.
388 * @param parentResourceIdentifier An identifier (such as a UUID) for the
389 * parent authority resource of the relevant item resource.
391 * @return The root URL for the item service.
393 protected String getItemServiceRootURL(String parentResourceIdentifier) {
394 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
398 * Returns the URL of a specific item resource managed by a service, and
399 * designated by an identifier (such as a universally unique ID, or UUID).
401 * @param parentResourceIdentifier An identifier (such as a UUID) for the
402 * parent authority resource of the relevant item resource.
404 * @param itemResourceIdentifier An identifier (such as a UUID) for an
407 * @return The URL of a specific item resource managed by a service.
409 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
410 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
414 public void authorityTests(String testName) {
415 // TODO Auto-generated method stub
420 // Work specific overrides
424 protected PoxPayloadOut createInstance(String commonPartName,
426 // Submit the request to the service and store the response.
427 String shortId = identifier;
428 String displayName = "displayName-" + shortId;
429 // String baseRefName = WorkAuthorityClientUtils.createWorkAuthRefName(shortId, null);
430 PoxPayloadOut result =
431 WorkAuthorityClientUtils.createWorkAuthorityInstance(
432 displayName, shortId, commonPartName);
437 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
438 String displayName = "displayName-NON_EXISTENT_ID";
439 PoxPayloadOut result = WorkAuthorityClientUtils.createWorkAuthorityInstance(
440 displayName, "nonEx", commonPartName);
445 protected WorkauthoritiesCommon updateInstance(WorkauthoritiesCommon workauthoritiesCommon) {
446 WorkauthoritiesCommon result = new WorkauthoritiesCommon();
448 result.setDisplayName("updated-" + workauthoritiesCommon.getDisplayName());
449 result.setVocabType("updated-" + workauthoritiesCommon.getVocabType());
455 protected void compareUpdatedInstances(WorkauthoritiesCommon original,
456 WorkauthoritiesCommon updated) throws Exception {
457 Assert.assertEquals(updated.getDisplayName(),
458 original.getDisplayName(),
459 "Display name in updated object did not match submitted data.");
462 protected void compareReadInstances(WorkauthoritiesCommon original,
463 WorkauthoritiesCommon fromRead) throws Exception {
464 Assert.assertNotNull(fromRead.getDisplayName());
465 Assert.assertNotNull(fromRead.getShortIdentifier());
466 Assert.assertNotNull(fromRead.getRefName());
470 // Authority item specific overrides
474 protected String createItemInAuthority(AuthorityClient client, String authorityId) {
475 return createItemInAuthority(client, authorityId, null /*refname*/);
479 protected WorksCommon updateItemInstance(WorksCommon worksCommon) {
481 WorkTermGroupList termList = worksCommon.getWorkTermGroupList();
482 Assert.assertNotNull(termList);
483 List<WorkTermGroup> terms = termList.getWorkTermGroup();
484 Assert.assertNotNull(terms);
485 Assert.assertTrue(terms.size() > 0);
486 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
487 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
488 worksCommon.setWorkTermGroupList(termList);
494 protected void compareUpdatedItemInstances(WorksCommon original,
495 WorksCommon updated) throws Exception {
497 WorkTermGroupList originalTermList = original.getWorkTermGroupList();
498 Assert.assertNotNull(originalTermList);
499 List<WorkTermGroup> originalTerms = originalTermList.getWorkTermGroup();
500 Assert.assertNotNull(originalTerms);
501 Assert.assertTrue(originalTerms.size() > 0);
503 WorkTermGroupList updatedTermList = updated.getWorkTermGroupList();
504 Assert.assertNotNull(updatedTermList);
505 List<WorkTermGroup> updatedTerms = updatedTermList.getWorkTermGroup();
506 Assert.assertNotNull(updatedTerms);
507 Assert.assertTrue(updatedTerms.size() > 0);
509 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
510 originalTerms.get(0).getTermDisplayName(),
511 "Value in updated record did not match submitted data.");
515 protected void verifyReadItemInstance(WorksCommon item)
517 // TODO Auto-generated method stub
522 protected PoxPayloadOut createNonExistenceItemInstance(
523 String commonPartName, String identifier) {
524 Map<String, String> nonexMap = new HashMap<String,String>();
525 nonexMap.put(WorkJAXBSchema.WORK_TERM_DISPLAY_NAME, TEST_WORK_TERM_DISPLAY_NAME);
526 nonexMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, "nonEx");
527 nonexMap.put(WorkJAXBSchema.WORK_TERM_STATUS, TEST_WORK_TERM_STATUS);
528 final String EMPTY_REFNAME = "";
529 PoxPayloadOut result =
530 WorkAuthorityClientUtils.createWorkInstance(EMPTY_REFNAME, nonexMap,
531 WorkAuthorityClientUtils.getTermGroupInstance(TEST_WORK_TERM_DISPLAY_NAME), commonPartName);