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 org.collectionspace.services.LocationJAXBSchema;
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.common.AbstractCommonListUtils;
37 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
38 import org.collectionspace.services.client.LocationAuthorityClient;
39 import org.collectionspace.services.client.LocationAuthorityClientUtils;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.location.LocationauthoritiesCommon;
42 import org.collectionspace.services.location.LocationsCommon;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46 import org.jboss.resteasy.client.ClientResponse;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import org.testng.Assert;
52 import org.testng.annotations.AfterClass;
53 import org.testng.annotations.Test;
56 * LocationAuthorityServiceTest, carries out tests against a
57 * deployed and running LocationAuthority Service.
59 * $LastChangedRevision: 753 $
60 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
62 public class LocationAuthorityServiceTest extends AbstractServiceTestImpl { //FIXME: Test classes for Vocab, Person, Org, and Location should have a base class!
65 private final String CLASS_NAME = LocationAuthorityServiceTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(LocationAuthorityServiceTest.class);
67 private final String REFNAME = "refName";
68 private final String DISPLAYNAME = "displayName";
69 private final static String CURRENT_DATE_UTC =
70 GregorianCalendarDateTimeUtils.currentDateUTC();
73 public String getServicePathComponent() {
74 return LocationAuthorityClient.SERVICE_PATH_COMPONENT;
78 protected String getServiceName() {
79 return LocationAuthorityClient.SERVICE_NAME;
82 public String getItemServicePathComponent() {
83 return AuthorityClient.ITEMS;
86 // Instance variables specific to this test.
88 // /** The SERVICE path component. */
89 // final String SERVICE_PATH_COMPONENT = "locationauthorities";
91 // /** The ITEM service path component. */
92 // final String ITEM_SERVICE_PATH_COMPONENT = "items";
94 // /** The CONTACT service path component. */
95 // final String CONTACT_SERVICE_PATH_COMPONENT = "contacts";
97 final String TEST_NAME = "Shelf 1";
98 final String TEST_SHORTID = "shelf1";
99 final String TEST_CONDITION_NOTE = "Basically clean";
100 final String TEST_CONDITION_NOTE_DATE = CURRENT_DATE_UTC;
101 final String TEST_SECURITY_NOTE = "Kind of safe";
102 final String TEST_ACCESS_NOTE = "Only right-thinkers may see";
103 final String TEST_ADDRESS = "123 Main Street, Anytown USA";
104 // TODO Make loc type be a controlled vocab term.
105 final String TEST_LOCATION_TYPE = "Shelf";
106 // TODO Make status type be a controlled vocab term.
107 final String TEST_STATUS = "Approved";
109 /** The known resource id. */
110 private String knownResourceId = null;
111 private String knownResourceShortIdentifer = null;
112 private String knownResourceRefName = null;
113 private String knownLocationTypeRefName = null;
114 private String knownItemResourceId = null;
115 private String knownItemResourceShortIdentifer = null;
116 private String knownContactResourceId = null;
118 /** The n items to create in list. */
119 private int nItemsToCreateInList = 3;
121 /** The all resource ids created. */
122 private List<String> allResourceIdsCreated = new ArrayList<String>();
124 /** The all item resource ids created. */
125 private Map<String, String> allItemResourceIdsCreated =
126 new HashMap<String, String>();
128 protected void setKnownResource( String id, String shortIdentifer,
130 knownResourceId = id;
131 knownResourceShortIdentifer = shortIdentifer;
132 knownResourceRefName = refName;
135 protected void setKnownItemResource( String id, String shortIdentifer ) {
136 knownItemResourceId = id;
137 knownItemResourceShortIdentifer = shortIdentifer;
141 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
144 protected CollectionSpaceClient getClientInstance() {
145 return new LocationAuthorityClient();
149 protected PoxPayloadOut createInstance(String identifier) {
150 LocationAuthorityClient client = new LocationAuthorityClient();
151 String shortId = identifier;
152 String displayName = "displayName-" + shortId;
153 // String baseRefName = LocationAuthorityClientUtils.createLocationAuthRefName(shortId, null);
154 PoxPayloadOut multipart =
155 LocationAuthorityClientUtils.createLocationAuthorityInstance(
156 displayName, shortId, client.getCommonPartName());
160 // ---------------------------------------------------------------
161 // CRUD tests : CREATE tests
162 // ---------------------------------------------------------------
165 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
168 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
170 public void create(String testName) throws Exception {
172 if (logger.isDebugEnabled()) {
173 logger.debug(testBanner(testName, CLASS_NAME));
175 // Perform setup, such as initializing the type of service request
176 // (e.g. CREATE, DELETE), its valid and expected status codes, and
177 // its associated HTTP method name (e.g. POST, DELETE).
180 // Submit the request to the service and store the response.
181 LocationAuthorityClient client = new LocationAuthorityClient();
182 String shortId = createIdentifier();
183 String displayName = "displayName-" + shortId;
184 // String baseRefName = LocationAuthorityClientUtils.createLocationAuthRefName(shortId, null);
186 PoxPayloadOut multipart =
187 LocationAuthorityClientUtils.createLocationAuthorityInstance(
188 displayName, shortId, client.getCommonPartName());
190 ClientResponse<Response> res = client.create(multipart);
192 int statusCode = res.getStatus();
194 // Check the status code of the response: does it match
195 // the expected response(s)?
198 // Does it fall within the set of valid status codes?
199 // Does it exactly match the expected status code?
200 if(logger.isDebugEnabled()){
201 logger.debug(testName + ": status = " + statusCode);
203 Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode),
204 invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode));
205 Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
207 newID = LocationAuthorityClientUtils.extractId(res);
209 res.releaseConnection();
211 // Store the ID returned from the first resource created
212 // for additional tests below.
213 final String EMPTY_REFNAME = "";
214 if (knownResourceId == null){
215 setKnownResource( newID, shortId, EMPTY_REFNAME );
216 if (logger.isDebugEnabled()) {
217 logger.debug(testName + ": knownResourceId=" + knownResourceId);
220 // Store the IDs from every resource created by tests,
221 // so they can be deleted after tests have been run.
222 allResourceIdsCreated.add(newID);
228 * @param testName the test name
230 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
231 groups = {"create"}, dependsOnMethods = {"create"})
232 public void createItem(String testName) {
233 if(logger.isDebugEnabled()){
234 logger.debug(testBanner(testName, CLASS_NAME));
237 createItemInAuthority(knownResourceId, knownResourceRefName);
241 * Creates the item in authority.
243 * @param vcsid the vcsid
244 * @param authRefName the auth ref name
247 private String createItemInAuthority(String vcsid, String authRefName) {
249 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
250 if(logger.isDebugEnabled()){
251 logger.debug(testBanner(testName, CLASS_NAME));
254 // Submit the request to the service and store the response.
255 LocationAuthorityClient client = new LocationAuthorityClient();
256 Map<String, String> shelf1Map = new HashMap<String,String>();
257 // TODO Make loc type and status be controlled vocabs.
258 shelf1Map.put(LocationJAXBSchema.NAME, TEST_NAME);
259 shelf1Map.put(LocationJAXBSchema.SHORT_IDENTIFIER, TEST_SHORTID);
260 shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE, TEST_CONDITION_NOTE);
261 shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE_DATE, TEST_CONDITION_NOTE_DATE);
262 shelf1Map.put(LocationJAXBSchema.SECURITY_NOTE, TEST_SECURITY_NOTE);
263 shelf1Map.put(LocationJAXBSchema.ACCESS_NOTE, TEST_ACCESS_NOTE);
264 shelf1Map.put(LocationJAXBSchema.ADDRESS, TEST_ADDRESS);
265 shelf1Map.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
266 shelf1Map.put(LocationJAXBSchema.TERM_STATUS, TEST_STATUS);
268 String newID = LocationAuthorityClientUtils.createItemInAuthority(vcsid,
269 authRefName, shelf1Map, client );
271 // Store the ID returned from the first item resource created
272 // for additional tests below.
273 if (knownItemResourceId == null){
274 setKnownItemResource(newID, TEST_SHORTID);
275 if (logger.isDebugEnabled()) {
276 logger.debug(testName + ": knownItemResourceId=" + newID);
280 // Store the IDs from any item resources created
281 // by tests, along with the IDs of their parents, so these items
282 // can be deleted after all tests have been run.
283 allItemResourceIdsCreated.put(newID, vcsid);
292 // Placeholders until the three tests below can be uncommented.
293 // See Issue CSPACE-401.
295 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
298 public void createWithEmptyEntityBody(String testName) throws Exception {
302 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
305 public void createWithMalformedXml(String testName) throws Exception {
309 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
312 public void createWithWrongXmlSchema(String testName) throws Exception {
316 // ---------------------------------------------------------------
317 // CRUD tests : CREATE LIST tests
318 // ---------------------------------------------------------------
321 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
324 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
325 groups = {"createList"}, dependsOnGroups = {"create"})
326 public void createList(String testName) throws Exception {
327 for (int i = 0; i < nItemsToCreateInList; i++) {
333 * Creates the item list.
335 * @param testName the test name
336 * @throws Exception the exception
338 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
339 groups = {"createList"}, dependsOnMethods = {"createList"})
340 public void createItemList(String testName) throws Exception {
341 // Add items to the initially-created, known parent record.
342 for (int j = 0; j < nItemsToCreateInList; j++) {
343 createItem(testName);
347 // ---------------------------------------------------------------
348 // CRUD tests : READ tests
349 // ---------------------------------------------------------------
352 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
355 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
356 groups = {"read"}, dependsOnGroups = {"create"})
357 public void read(String testName) throws Exception {
359 if (logger.isDebugEnabled()) {
360 logger.debug(testBanner(testName, CLASS_NAME));
365 // Submit the request to the service and store the response.
366 LocationAuthorityClient client = new LocationAuthorityClient();
368 ClientResponse<String> res = client.read(knownResourceId);
369 assertStatusCode(res, testName);
371 //FIXME: remove the following try catch once Aron fixes signatures
373 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
374 LocationauthoritiesCommon locationAuthority =
375 (LocationauthoritiesCommon) extractPart(input,
376 client.getCommonPartName(), LocationauthoritiesCommon.class);
377 Assert.assertNotNull(locationAuthority);
378 Assert.assertNotNull(locationAuthority.getDisplayName());
379 Assert.assertNotNull(locationAuthority.getShortIdentifier());
380 Assert.assertNotNull(locationAuthority.getRefName());
381 } catch (Exception e) {
382 throw new RuntimeException(e);
385 res.releaseConnection();
392 * @param testName the test name
393 * @throws Exception the exception
395 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
396 groups = {"read"}, dependsOnGroups = {"create"})
397 public void readByName(String testName) throws Exception {
399 if (logger.isDebugEnabled()) {
400 logger.debug(testBanner(testName+"("+knownResourceShortIdentifer+")", CLASS_NAME));
405 // Submit the request to the service and store the response.
406 LocationAuthorityClient client = new LocationAuthorityClient();
407 ClientResponse<String> res = client.readByName(knownResourceShortIdentifer);
408 assertStatusCode(res, testName);
410 //FIXME: remove the following try catch once Aron fixes signatures
412 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
413 LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
414 client.getCommonPartName(), LocationauthoritiesCommon.class);
415 Assert.assertNotNull(locationAuthority);
416 } catch (Exception e) {
417 throw new RuntimeException(e);
420 res.releaseConnection();
428 * @param testName the test name
429 * @throws Exception the exception
431 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
432 groups = {"read"}, dependsOnMethods = {"read"})
433 public void readItem(String testName) throws Exception {
435 if (logger.isDebugEnabled()) {
436 logger.debug(testBanner(testName, CLASS_NAME));
441 // Submit the request to the service and store the response.
442 LocationAuthorityClient client = new LocationAuthorityClient();
443 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
444 assertStatusCode(res, testName);
446 // Check whether we've received a location.
447 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
448 LocationsCommon location = (LocationsCommon) extractPart(input,
449 client.getItemCommonPartName(), LocationsCommon.class);
450 Assert.assertNotNull(location);
451 boolean showFull = true;
452 if(showFull && logger.isDebugEnabled()){
453 logger.debug(testName + ": returned payload:");
454 logger.debug(objectAsXmlString(location, LocationsCommon.class));
456 Assert.assertEquals(location.getInAuthority(), knownResourceId);
458 res.releaseConnection();
464 * Verify item display name.
466 * @param testName the test name
467 * @throws Exception the exception
469 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
470 dependsOnMethods = {"readItem", "updateItem"})
471 public void verifyItemDisplayName(String testName) throws Exception {
473 if (logger.isDebugEnabled()) {
474 logger.debug(testBanner(testName, CLASS_NAME));
479 // Submit the request to the service and store the response.
480 LocationAuthorityClient client = new LocationAuthorityClient();
481 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
482 assertStatusCode(res, testName);
484 // Check whether location has expected displayName.
485 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
486 LocationsCommon location = (LocationsCommon) extractPart(input,
487 client.getItemCommonPartName(), LocationsCommon.class);
488 Assert.assertNotNull(location);
489 String displayName = location.getDisplayName();
490 // Make sure displayName matches computed form
491 String expectedDisplayName =
492 LocationAuthorityClientUtils.prepareDefaultDisplayName(TEST_NAME);
493 Assert.assertNotNull(displayName, expectedDisplayName);
495 // Update the shortName and verify the computed name is updated.
496 location.setCsid(null);
497 location.setDisplayNameComputed(true);
498 location.setName("updated-" + TEST_NAME);
499 expectedDisplayName =
500 LocationAuthorityClientUtils.prepareDefaultDisplayName("updated-" + TEST_NAME);
502 // Submit the updated resource to the service and store the response.
503 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
504 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
505 commonPart.setLabel(client.getItemCommonPartName());
506 res.releaseConnection();
507 res = client.updateItem(knownResourceId, knownItemResourceId, output);
508 assertStatusCode(res, testName);
510 // Retrieve the updated resource and verify that its contents exist.
511 input = new PoxPayloadIn(res.getEntity());
512 LocationsCommon updatedLocation =
513 (LocationsCommon) extractPart(input,
514 client.getItemCommonPartName(), LocationsCommon.class);
515 Assert.assertNotNull(updatedLocation);
517 // Verify that the updated resource received the correct data.
518 Assert.assertEquals(updatedLocation.getName(), location.getName(),
519 "Updated ForeName in Location did not match submitted data.");
520 // Verify that the updated resource computes the right displayName.
521 Assert.assertEquals(updatedLocation.getDisplayName(), expectedDisplayName,
522 "Updated ForeName in Location not reflected in computed DisplayName.");
524 // Now Update the displayName, not computed and verify the computed name is overriden.
525 location.setDisplayNameComputed(false);
526 expectedDisplayName = "TestName";
527 location.setDisplayName(expectedDisplayName);
529 // Submit the updated resource to the service and store the response.
530 output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
531 commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
532 commonPart.setLabel(client.getItemCommonPartName());
533 res.releaseConnection();
534 res = client.updateItem(knownResourceId, knownItemResourceId, output);
535 assertStatusCode(res, testName);
537 // Retrieve the updated resource and verify that its contents exist.
538 input = new PoxPayloadIn(res.getEntity());
540 (LocationsCommon) extractPart(input,
541 client.getItemCommonPartName(), LocationsCommon.class);
542 Assert.assertNotNull(updatedLocation);
544 // Verify that the updated resource received the correct data.
545 Assert.assertEquals(updatedLocation.isDisplayNameComputed(), false,
546 "Updated displayNameComputed in Location did not match submitted data.");
547 // Verify that the updated resource computes the right displayName.
548 Assert.assertEquals(updatedLocation.getDisplayName(),
550 "Updated DisplayName (not computed) in Location not stored.");
552 res.releaseConnection();
557 * Verify illegal item display name.
559 * @param testName the test name
560 * @throws Exception the exception
562 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
563 dependsOnMethods = {"verifyItemDisplayName"})
564 public void verifyIllegalItemDisplayName(String testName) throws Exception {
566 if (logger.isDebugEnabled()) {
567 logger.debug(testBanner(testName, CLASS_NAME));
570 // Perform setup for read.
573 // Submit the request to the service and store the response.
574 LocationAuthorityClient client = new LocationAuthorityClient();
575 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
576 assertStatusCode(res, testName);
578 // Perform setup for update.
579 testSetup(STATUS_BAD_REQUEST, ServiceRequestType.UPDATE);
583 // Check whether Location has expected displayName.
584 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
585 LocationsCommon location = (LocationsCommon) extractPart(input,
586 client.getItemCommonPartName(), LocationsCommon.class);
587 Assert.assertNotNull(location);
588 // Try to Update with computed false and no displayName
589 location.setDisplayNameComputed(false);
590 location.setDisplayName(null);
592 // Submit the updated resource to the service and store the response.
593 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
594 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
595 commonPart.setLabel(client.getItemCommonPartName());
596 res.releaseConnection();
597 res = client.updateItem(knownResourceId, knownItemResourceId, output);
598 assertStatusCode(res, testName);
600 res.releaseConnection();
607 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
610 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
611 groups = {"read"}, dependsOnMethods = {"read"})
612 public void readNonExistent(String testName) {
614 if (logger.isDebugEnabled()) {
615 logger.debug(testBanner(testName, CLASS_NAME));
618 setupReadNonExistent();
620 // Submit the request to the service and store the response.
621 LocationAuthorityClient client = new LocationAuthorityClient();
622 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
624 int statusCode = res.getStatus();
626 // Check the status code of the response: does it match
627 // the expected response(s)?
628 if(logger.isDebugEnabled()){
629 logger.debug(testName + ": status = " + statusCode);
631 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
632 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
633 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
635 res.releaseConnection();
640 * Read item non existent.
642 * @param testName the test name
644 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
645 groups = {"read"}, dependsOnMethods = {"readItem"})
646 public void readItemNonExistent(String testName) {
648 if (logger.isDebugEnabled()) {
649 logger.debug(testBanner(testName, CLASS_NAME));
652 setupReadNonExistent();
654 // Submit the request to the service and store the response.
655 LocationAuthorityClient client = new LocationAuthorityClient();
656 ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
658 int statusCode = res.getStatus();
660 // Check the status code of the response: does it match
661 // the expected response(s)?
662 if(logger.isDebugEnabled()){
663 logger.debug(testName + ": status = " + statusCode);
665 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
666 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
667 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
669 res.releaseConnection();
674 // ---------------------------------------------------------------
675 // CRUD tests : READ_LIST tests
676 // ---------------------------------------------------------------
680 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
683 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
684 groups = {"readList"}, dependsOnGroups = {"createList", "read"})
685 public void readList(String testName) throws Exception {
687 if (logger.isDebugEnabled()) {
688 logger.debug(testBanner(testName, CLASS_NAME));
693 // Submit the request to the service and store the response.
694 LocationAuthorityClient client = new LocationAuthorityClient();
695 ClientResponse<AbstractCommonList> res = client.readList();
696 assertStatusCode(res, testName);
698 AbstractCommonList list = res.getEntity();
700 // Optionally output additional data about list members for debugging.
701 if(logger.isTraceEnabled()){
702 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
705 res.releaseConnection();
712 @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
713 public void readItemList() {
714 String testName = "readItemList";
715 if (logger.isDebugEnabled()) {
716 logger.debug(testBanner(testName, CLASS_NAME));
718 readItemList(knownResourceId, null);
722 * Read item list by authority name.
724 @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
725 public void readItemListByAuthorityName() {
726 String testName = "readItemListByAuthorityName";
727 if (logger.isDebugEnabled()) {
728 logger.debug(testBanner(testName, CLASS_NAME));
730 readItemList(null, knownResourceShortIdentifer);
736 * @param vcsid the vcsid
737 * @param name the name
739 private void readItemList(String vcsid, String shortId) {
741 String testName = "readItemList";
746 // Submit the request to the service and store the response.
747 LocationAuthorityClient client = new LocationAuthorityClient();
748 ClientResponse<AbstractCommonList> res = null;
750 res = client.readItemList(vcsid, null, null);
751 } else if(shortId!= null) {
752 res = client.readItemListForNamedAuthority(shortId, null, null);
754 Assert.fail("readItemList passed null csid and name!");
756 assertStatusCode(res, testName);
758 AbstractCommonList list = res.getEntity();
760 List<AbstractCommonList.ListItem> items =
762 int nItemsReturned = items.size();
763 // There will be one item created, associated with a
764 // known parent resource, by the createItem test.
766 // In addition, there will be 'nItemsToCreateInList'
767 // additional items created by the createItemList test,
768 // all associated with the same parent resource.
769 int nExpectedItems = nItemsToCreateInList + 1;
770 if(logger.isDebugEnabled()){
771 logger.debug(testName + ": Expected "
772 + nExpectedItems +" items; got: "+nItemsReturned);
774 Assert.assertEquals(nItemsReturned, nExpectedItems);
776 for (AbstractCommonList.ListItem item : items) {
778 AbstractCommonListUtils.ListItemGetElementValue(item, REFNAME);
779 Assert.assertTrue((null != value), "Item refName is null!");
781 AbstractCommonListUtils.ListItemGetElementValue(item, DISPLAYNAME);
782 Assert.assertTrue((null != value), "Item displayName is null!");
784 if(logger.isTraceEnabled()){
785 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
788 res.releaseConnection();
796 // ---------------------------------------------------------------
797 // CRUD tests : UPDATE tests
798 // ---------------------------------------------------------------
801 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
804 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
805 groups = {"update"}, dependsOnGroups = {"read", "readList"})
806 public void update(String testName) throws Exception {
808 if (logger.isDebugEnabled()) {
809 logger.debug(testBanner(testName, CLASS_NAME));
814 // Retrieve the contents of a resource to update.
815 LocationAuthorityClient client = new LocationAuthorityClient();
816 ClientResponse<String> res = client.read(knownResourceId);
817 assertStatusCode(res, testName);
820 if(logger.isDebugEnabled()){
821 logger.debug("got LocationAuthority to update with ID: " + knownResourceId);
823 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
824 LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
825 client.getCommonPartName(), LocationauthoritiesCommon.class);
826 Assert.assertNotNull(locationAuthority);
828 // Update the contents of this resource.
829 locationAuthority.setDisplayName("updated-" + locationAuthority.getDisplayName());
830 locationAuthority.setVocabType("updated-" + locationAuthority.getVocabType());
831 if(logger.isDebugEnabled()){
832 logger.debug("to be updated LocationAuthority");
833 logger.debug(objectAsXmlString(locationAuthority, LocationauthoritiesCommon.class));
836 // Submit the updated resource to the service and store the response.
837 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_PAYLOAD_NAME);
838 PayloadOutputPart commonPart = output.addPart(locationAuthority, MediaType.APPLICATION_XML_TYPE);
839 commonPart.setLabel(client.getCommonPartName());
840 res.releaseConnection();
841 res = client.update(knownResourceId, output);
842 assertStatusCode(res, testName);
844 // Retrieve the updated resource and verify that its contents exist.
845 input = new PoxPayloadIn(res.getEntity());
846 LocationauthoritiesCommon updatedLocationAuthority =
847 (LocationauthoritiesCommon) extractPart(input,
848 client.getCommonPartName(), LocationauthoritiesCommon.class);
849 Assert.assertNotNull(updatedLocationAuthority);
851 // Verify that the updated resource received the correct data.
852 Assert.assertEquals(updatedLocationAuthority.getDisplayName(),
853 locationAuthority.getDisplayName(),
854 "Data in updated object did not match submitted data.");
856 res.releaseConnection();
863 * @param testName the test name
864 * @throws Exception the exception
866 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
867 groups = {"update"}, dependsOnMethods = {"update"})
868 public void updateItem(String testName) throws Exception {
870 if (logger.isDebugEnabled()) {
871 logger.debug(testBanner(testName, CLASS_NAME));
876 // Retrieve the contents of a resource to update.
877 LocationAuthorityClient client = new LocationAuthorityClient();
878 ClientResponse<String> res =
879 client.readItem(knownResourceId, knownItemResourceId);
880 assertStatusCode(res, testName);
882 if(logger.isDebugEnabled()){
883 logger.debug("got Location to update with ID: " +
884 knownItemResourceId +
885 " in LocationAuthority: " + knownResourceId );
887 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
888 LocationsCommon location = (LocationsCommon) extractPart(input,
889 client.getItemCommonPartName(), LocationsCommon.class);
890 Assert.assertNotNull(location);
892 // Update the contents of this resource.
893 location.setCsid(null);
894 location.setName("updated-" + location.getName());
895 if(logger.isDebugEnabled()){
896 logger.debug("to be updated Location");
897 logger.debug(objectAsXmlString(location,
898 LocationsCommon.class));
901 // Submit the updated resource to the service and store the response.
902 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
903 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
904 commonPart.setLabel(client.getItemCommonPartName());
905 res.releaseConnection();
906 res = client.updateItem(knownResourceId, knownItemResourceId, output);
907 assertStatusCode(res, testName);
909 // Retrieve the updated resource and verify that its contents exist.
910 input = new PoxPayloadIn(res.getEntity());
911 LocationsCommon updatedLocation =
912 (LocationsCommon) extractPart(input,
913 client.getItemCommonPartName(), LocationsCommon.class);
914 Assert.assertNotNull(updatedLocation);
916 // Verify that the updated resource received the correct data.
917 Assert.assertEquals(updatedLocation.getName(), location.getName(),
918 "Data in updated Location did not match submitted data.");
920 res.releaseConnection();
925 // Placeholders until the three tests below can be uncommented.
926 // See Issue CSPACE-401.
928 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
931 public void updateWithEmptyEntityBody(String testName) throws Exception {
935 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
938 public void updateWithMalformedXml(String testName) throws Exception {
942 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
945 public void updateWithWrongXmlSchema(String testName) throws Exception {
950 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
953 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
954 groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
955 public void updateNonExistent(String testName) throws Exception {
957 if (logger.isDebugEnabled()) {
958 logger.debug(testBanner(testName, CLASS_NAME));
961 setupUpdateNonExistent();
963 // Submit the request to the service and store the response.
964 // Note: The ID(s) used when creating the request payload may be arbitrary.
965 // The only relevant ID may be the one used in update(), below.
966 LocationAuthorityClient client = new LocationAuthorityClient();
967 String displayName = "displayName-NON_EXISTENT_ID";
968 PoxPayloadOut multipart = LocationAuthorityClientUtils.createLocationAuthorityInstance(
969 displayName, "nonEx", client.getCommonPartName());
970 ClientResponse<String> res =
971 client.update(NON_EXISTENT_ID, multipart);
973 int statusCode = res.getStatus();
975 // Check the status code of the response: does it match
976 // the expected response(s)?
977 if(logger.isDebugEnabled()){
978 logger.debug(testName + ": status = " + statusCode);
980 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
981 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
982 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
984 res.releaseConnection();
989 * Update non existent item.
991 * @param testName the test name
992 * @throws Exception the exception
994 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
995 groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
996 public void updateNonExistentItem(String testName) throws Exception {
998 if (logger.isDebugEnabled()) {
999 logger.debug(testBanner(testName, CLASS_NAME));
1002 setupUpdateNonExistent();
1004 // Submit the request to the service and store the response.
1005 // Note: The ID used in this 'create' call may be arbitrary.
1006 // The only relevant ID may be the one used in update(), below.
1007 LocationAuthorityClient client = new LocationAuthorityClient();
1008 Map<String, String> nonexMap = new HashMap<String,String>();
1009 nonexMap.put(LocationJAXBSchema.NAME, TEST_NAME);
1010 nonexMap.put(LocationJAXBSchema.SHORT_IDENTIFIER, "nonEx");
1011 nonexMap.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
1012 nonexMap.put(LocationJAXBSchema.TERM_STATUS, TEST_STATUS);
1013 // PoxPayloadOut multipart =
1014 // LocationAuthorityClientUtils.createLocationInstance(
1015 // LocationAuthorityClientUtils.createLocationRefName(knownResourceRefName, "nonEx", "Non Existent"),
1016 // nonexMap, client.getItemCommonPartName() );
1017 final String EMPTY_REFNAME = "";
1018 PoxPayloadOut multipart =
1019 LocationAuthorityClientUtils.createLocationInstance(EMPTY_REFNAME,
1020 nonexMap, client.getItemCommonPartName() );
1021 ClientResponse<String> res =
1022 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1024 int statusCode = res.getStatus();
1026 // Check the status code of the response: does it match
1027 // the expected response(s)?
1028 if(logger.isDebugEnabled()){
1029 logger.debug(testName + ": status = " + statusCode);
1031 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1032 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1033 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1035 res.releaseConnection();
1039 // ---------------------------------------------------------------
1040 // CRUD tests : DELETE tests
1041 // ---------------------------------------------------------------
1044 // Note: delete sub-resources in ascending hierarchical order,
1045 // before deleting their parents.
1050 * @param testName the test name
1051 * @throws Exception the exception
1053 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1054 //groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "readListByPartialTerm", "update"})
1055 groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "update"})
1056 public void deleteItem(String testName) throws Exception {
1058 if (logger.isDebugEnabled()) {
1059 logger.debug(testBanner(testName, CLASS_NAME));
1064 if(logger.isDebugEnabled()){
1065 logger.debug("parentcsid =" + knownResourceId +
1066 " itemcsid = " + knownItemResourceId);
1069 // Submit the request to the service and store the response.
1070 LocationAuthorityClient client = new LocationAuthorityClient();
1071 ClientResponse<Response> res =
1072 client.deleteItem(knownResourceId, knownItemResourceId);
1074 int statusCode = res.getStatus();
1076 // Check the status code of the response: does it match
1077 // the expected response(s)?
1078 if(logger.isDebugEnabled()){
1079 logger.debug(testName + ": status = " + statusCode);
1081 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1082 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1083 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1085 res.releaseConnection();
1090 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
1093 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1094 groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1095 public void delete(String testName) throws Exception {
1097 if (logger.isDebugEnabled()) {
1098 logger.debug(testBanner(testName, CLASS_NAME));
1103 if(logger.isDebugEnabled()){
1104 logger.debug("parentcsid =" + knownResourceId);
1107 // Submit the request to the service and store the response.
1108 LocationAuthorityClient client = new LocationAuthorityClient();
1109 ClientResponse<Response> res = client.delete(knownResourceId);
1111 int statusCode = res.getStatus();
1113 // Check the status code of the response: does it match
1114 // the expected response(s)?
1115 if(logger.isDebugEnabled()){
1116 logger.debug(testName + ": status = " + statusCode);
1118 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1119 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1120 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1122 res.releaseConnection();
1128 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
1131 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1132 groups = {"delete"}, dependsOnMethods = {"delete"})
1133 public void deleteNonExistent(String testName) throws Exception {
1135 if (logger.isDebugEnabled()) {
1136 logger.debug(testBanner(testName, CLASS_NAME));
1139 setupDeleteNonExistent();
1141 // Submit the request to the service and store the response.
1142 LocationAuthorityClient client = new LocationAuthorityClient();
1143 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1145 int statusCode = res.getStatus();
1147 // Check the status code of the response: does it match
1148 // the expected response(s)?
1149 if(logger.isDebugEnabled()){
1150 logger.debug(testName + ": status = " + statusCode);
1152 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1153 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1154 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1156 res.releaseConnection();
1161 * Delete non existent item.
1163 * @param testName the test name
1165 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1166 groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1167 public void deleteNonExistentItem(String testName) {
1169 if (logger.isDebugEnabled()) {
1170 logger.debug(testBanner(testName, CLASS_NAME));
1173 setupDeleteNonExistent();
1175 // Submit the request to the service and store the response.
1176 LocationAuthorityClient client = new LocationAuthorityClient();
1177 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1179 int statusCode = res.getStatus();
1181 // Check the status code of the response: does it match
1182 // the expected response(s)?
1183 if(logger.isDebugEnabled()){
1184 logger.debug(testName + ": status = " + statusCode);
1186 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1187 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1188 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1190 res.releaseConnection();
1194 // ---------------------------------------------------------------
1195 // Utility tests : tests of code used in tests above
1196 // ---------------------------------------------------------------
1198 * Tests the code for manually submitting data that is used by several
1199 * of the methods above.
1201 @Test(dependsOnMethods = {"create", "read"})
1202 public void testSubmitRequest() {
1204 // Expected status code: 200 OK
1205 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1207 // Submit the request to the service and store the response.
1208 String method = ServiceRequestType.READ.httpMethodName();
1209 String url = getResourceURL(knownResourceId);
1210 int statusCode = submitRequest(method, url);
1212 // Check the status code of the response: does it match
1213 // the expected response(s)?
1214 if(logger.isDebugEnabled()){
1215 logger.debug("testSubmitRequest: url=" + url +
1216 " status=" + statusCode);
1218 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1223 * Test item submit request.
1225 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1226 public void testItemSubmitRequest() {
1228 // Expected status code: 200 OK
1229 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1231 // Submit the request to the service and store the response.
1232 String method = ServiceRequestType.READ.httpMethodName();
1233 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1234 int statusCode = submitRequest(method, url);
1236 // Check the status code of the response: does it match
1237 // the expected response(s)?
1238 if(logger.isDebugEnabled()){
1239 logger.debug("testItemSubmitRequest: url=" + url +
1240 " status=" + statusCode);
1242 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1246 // ---------------------------------------------------------------
1247 // Cleanup of resources created during testing
1248 // ---------------------------------------------------------------
1251 * Deletes all resources created by tests, after all tests have been run.
1253 * This cleanup method will always be run, even if one or more tests fail.
1254 * For this reason, it attempts to remove all resources created
1255 * at any point during testing, even if some of those resources
1256 * may be expected to be deleted by certain tests.
1259 @AfterClass(alwaysRun=true)
1260 public void cleanUp() {
1261 String noTest = System.getProperty("noTestCleanup");
1262 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1263 if (logger.isDebugEnabled()) {
1264 logger.debug("Skipping Cleanup phase ...");
1268 if (logger.isDebugEnabled()) {
1269 logger.debug("Cleaning up temporary resources created for testing ...");
1271 String parentResourceId;
1272 String itemResourceId;
1273 // Clean up contact resources.
1274 LocationAuthorityClient client = new LocationAuthorityClient();
1275 parentResourceId = knownResourceId;
1276 // Clean up item resources.
1277 for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
1278 itemResourceId = entry.getKey();
1279 parentResourceId = entry.getValue();
1280 // Note: Any non-success responses from the delete operation
1281 // below are ignored and not reported.
1282 ClientResponse<Response> res =
1283 client.deleteItem(parentResourceId, itemResourceId);
1284 res.releaseConnection();
1286 // Clean up parent resources.
1287 for (String resourceId : allResourceIdsCreated) {
1288 // Note: Any non-success responses from the delete operation
1289 // below are ignored and not reported.
1290 ClientResponse<Response> res = client.delete(resourceId);
1291 res.releaseConnection();
1295 // ---------------------------------------------------------------
1296 // Utility methods used by tests above
1297 // ---------------------------------------------------------------
1299 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
1303 * Returns the root URL for the item service.
1305 * This URL consists of a base URL for all services, followed by
1306 * a path component for the owning parent, followed by the
1307 * path component for the items.
1309 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1310 * parent authority resource of the relevant item resource.
1312 * @return The root URL for the item service.
1314 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1315 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1319 * Returns the URL of a specific item resource managed by a service, and
1320 * designated by an identifier (such as a universally unique ID, or UUID).
1322 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1323 * parent authority resource of the relevant item resource.
1325 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1328 * @return The URL of a specific item resource managed by a service.
1330 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1331 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;