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.MaterialJAXBSchema;
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.MaterialAuthorityClient;
40 import org.collectionspace.services.client.MaterialAuthorityClientUtils;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.material.MaterialTermGroup;
43 import org.collectionspace.services.material.MaterialTermGroupList;
44 import org.collectionspace.services.material.MaterialauthoritiesCommon;
45 import org.collectionspace.services.material.MaterialsCommon;
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 * MaterialAuthorityServiceTest, carries out tests against a
55 * deployed and running MaterialAuthority Service.
58 public class MaterialAuthorityServiceTest extends AbstractAuthorityServiceTest<MaterialauthoritiesCommon, MaterialsCommon> {
61 private final String CLASS_NAME = MaterialAuthorityServiceTest.class.getName();
62 private final Logger logger = LoggerFactory.getLogger(MaterialAuthorityServiceTest.class);
65 public String getServicePathComponent() {
66 return MaterialAuthorityClient.SERVICE_PATH_COMPONENT;
70 protected String getServiceName() {
71 return MaterialAuthorityClient.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 = "materialauthorities";
83 // /** The ITEM service path component. */
84 // final String ITEM_SERVICE_PATH_COMPONENT = "items";
87 final String TEST_MATERIAL_TERM_DISPLAY_NAME = "SuperGlass 2";
88 final String TEST_MATERIAL_TERM_NAME = "SuperGlass";
89 final String TEST_MATERIAL_TERM_STATUS = "accepted";
90 final String TEST_MATERIAL_TERM_SOURCE = "source";
91 final String TEST_MATERIAL_TERM_SOURCE_DETAIL = "internal";
92 final String TEST_MATERIAL_DESCRIPTION = "Really strong glass";
93 final String TEST_MATERIAL_SHORT_IDENTIFIER = "superglass";
96 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
99 protected CollectionSpaceClient getClientInstance() {
100 return new MaterialAuthorityClient();
104 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
105 return new MaterialAuthorityClient(clientPropertiesFilename);
109 * Creates the item in authority.
111 * @param vcsid the vcsid
112 * @param authRefName the auth ref name
115 private String createItemInAuthority(AuthorityClient client, String vcsid, String authRefName) {
116 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
118 // Submit the request to the service and store the response.
119 Map<String, String> materialMap = new HashMap<String,String>();
120 // TODO Make material type and status be controlled vocabs.
121 materialMap.put(MaterialJAXBSchema.SHORT_IDENTIFIER, TEST_MATERIAL_SHORT_IDENTIFIER);
122 materialMap.put(MaterialJAXBSchema.MATERIAL_DESCRIPTION, TEST_MATERIAL_DESCRIPTION);
124 List<MaterialTermGroup> terms = new ArrayList<MaterialTermGroup>();
125 MaterialTermGroup term = new MaterialTermGroup();
126 term.setTermDisplayName(TEST_MATERIAL_TERM_DISPLAY_NAME);
127 term.setTermName(TEST_MATERIAL_TERM_NAME);
128 term.setTermSource(TEST_MATERIAL_TERM_SOURCE);
129 term.setTermSourceDetail(TEST_MATERIAL_TERM_SOURCE_DETAIL);
130 term.setTermStatus(TEST_MATERIAL_TERM_STATUS);
133 String newID = MaterialAuthorityClientUtils.createItemInAuthority(vcsid,
134 authRefName, materialMap, terms, (MaterialAuthorityClient) client);
136 // Store the ID returned from the first item resource created
137 // for additional tests below.
138 if (knownItemResourceId == null){
139 setKnownItemResource(newID, TEST_MATERIAL_SHORT_IDENTIFIER);
140 if (logger.isDebugEnabled()) {
141 logger.debug(testName + ": knownItemResourceId=" + newID);
145 // Store the IDs from any item resources created
146 // by tests, along with the IDs of their parents, so these items
147 // can be deleted after all tests have been run.
148 allResourceItemIdsCreated.put(newID, vcsid);
154 * Verify illegal item display name.
156 * @param testName the test name
157 * @throws Exception the exception
159 @Test(dataProvider="testName")
160 public void verifyIllegalItemDisplayName(String testName) throws Exception {
161 // Perform setup for read.
164 // Submit the request to the service and store the response.
165 MaterialAuthorityClient client = new MaterialAuthorityClient();
166 Response res = client.readItem(knownResourceId, knownItemResourceId);
167 MaterialsCommon material = null;
169 assertStatusCode(res, testName);
170 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
171 material = (MaterialsCommon) extractPart(input,
172 client.getItemCommonPartName(), MaterialsCommon.class);
173 Assert.assertNotNull(material);
181 // Make an invalid UPDATE request, without a display name
183 MaterialTermGroupList termList = material.getMaterialTermGroupList();
184 Assert.assertNotNull(termList);
185 List<MaterialTermGroup> terms = termList.getMaterialTermGroup();
186 Assert.assertNotNull(terms);
187 Assert.assertTrue(terms.size() > 0);
188 terms.get(0).setTermDisplayName(null);
189 terms.get(0).setTermName(null);
191 setupUpdateWithInvalidBody(); // we expect a failure
193 // Submit the updated resource to the service and store the response.
194 PoxPayloadOut output = new PoxPayloadOut(
195 MaterialAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
196 PayloadOutputPart commonPart = output.addPart(
197 client.getItemCommonPartName(), material);
198 setupUpdateWithInvalidBody(); // we expected a failure here.
199 res = client.updateItem(knownResourceId, knownItemResourceId, output);
201 assertStatusCode(res, testName);
212 @Test(dataProvider = "testName", groups = {"readList"},
213 dependsOnMethods = {"readList"})
214 public void readItemList(String testName) {
215 readItemList(knownAuthorityWithItems, null);
219 * Read item list by authority name.
221 @Test(dataProvider = "testName", groups = {"readList"},
222 dependsOnMethods = {"readItemList"})
223 public void readItemListByAuthorityName(String testName) {
224 readItemList(null, READITEMS_SHORT_IDENTIFIER);
235 private void readItemList(String vcsid, String shortId) {
236 String testName = "readItemList";
241 // Submit the request to the service and store the response.
242 MaterialAuthorityClient client = new MaterialAuthorityClient();
245 res = client.readItemList(vcsid, null, null);
246 } else if (shortId != null) {
247 res = client.readItemListForNamedAuthority(shortId, null, null);
249 Assert.fail("readItemList passed null csid and name!");
252 AbstractCommonList list = null;
254 assertStatusCode(res, testName);
255 list = res.readEntity(AbstractCommonList.class);
262 List<AbstractCommonList.ListItem> items = list.getListItem();
263 int nItemsReturned = items.size();
264 // There will be 'nItemsToCreateInList'
265 // items created by the createItemList test,
266 // all associated with the same parent resource.
267 int nExpectedItems = nItemsToCreateInList;
268 if (logger.isDebugEnabled()) {
269 logger.debug(testName + ": Expected " + nExpectedItems
270 + " items; got: " + nItemsReturned);
272 Assert.assertEquals(nItemsReturned, nExpectedItems);
274 for (AbstractCommonList.ListItem item : items) {
275 String value = AbstractCommonListUtils.ListItemGetElementValue(
276 item, MaterialJAXBSchema.REF_NAME);
277 Assert.assertTrue((null != value), "Item refName is null!");
278 value = AbstractCommonListUtils.ListItemGetElementValue(item,
279 MaterialJAXBSchema.MATERIAL_TERM_DISPLAY_NAME);
280 Assert.assertTrue((null != value), "Item termDisplayName is null!");
282 if (logger.isTraceEnabled()) {
283 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
289 public void delete(String testName) throws Exception {
290 // Do nothing. See localDelete(). This ensure proper test order.
293 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
294 public void localDelete(String testName) throws Exception {
295 super.delete(testName);
299 public void deleteItem(String testName) throws Exception {
300 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
301 // its dependencies will get run first and then we can call the base class' delete method.
304 @Test(dataProvider = "testName", groups = {"delete"},
305 dependsOnMethods = {"verifyIllegalItemDisplayName"})
306 public void localDeleteItem(String testName) throws Exception {
307 super.deleteItem(testName);
310 // ---------------------------------------------------------------
311 // Cleanup of resources created during testing
312 // ---------------------------------------------------------------
315 * Deletes all resources created by tests, after all tests have been run.
317 * This cleanup method will always be run, even if one or more tests fail.
318 * For this reason, it attempts to remove all resources created
319 * at any point during testing, even if some of those resources
320 * may be expected to be deleted by certain tests.
323 @AfterClass(alwaysRun=true)
324 public void cleanUp() {
325 String noTest = System.getProperty("noTestCleanup");
326 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
327 if (logger.isDebugEnabled()) {
328 logger.debug("Skipping Cleanup phase ...");
332 if (logger.isDebugEnabled()) {
333 logger.debug("Cleaning up temporary resources created for testing ...");
335 String parentResourceId;
336 String itemResourceId;
337 // Clean up contact resources.
338 MaterialAuthorityClient client = new MaterialAuthorityClient();
339 parentResourceId = knownResourceId;
340 // Clean up item resources.
341 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
342 itemResourceId = entry.getKey();
343 parentResourceId = entry.getValue();
344 // Note: Any non-success responses from the delete operation
345 // below are ignored and not reported.
346 client.deleteItem(parentResourceId, itemResourceId).close();
348 // Clean up parent resources.
349 for (String resourceId : allResourceIdsCreated) {
350 // Note: Any non-success responses from the delete operation
351 // below are ignored and not reported.
352 client.delete(resourceId).close();
356 // ---------------------------------------------------------------
357 // Utility methods used by tests above
358 // ---------------------------------------------------------------
360 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
364 * Returns the root URL for the item service.
366 * This URL consists of a base URL for all services, followed by
367 * a path component for the owning parent, followed by the
368 * path component for the items.
370 * @param parentResourceIdentifier An identifier (such as a UUID) for the
371 * parent authority resource of the relevant item resource.
373 * @return The root URL for the item service.
375 protected String getItemServiceRootURL(String parentResourceIdentifier) {
376 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
380 * Returns the URL of a specific item resource managed by a service, and
381 * designated by an identifier (such as a universally unique ID, or UUID).
383 * @param parentResourceIdentifier An identifier (such as a UUID) for the
384 * parent authority resource of the relevant item resource.
386 * @param itemResourceIdentifier An identifier (such as a UUID) for an
389 * @return The URL of a specific item resource managed by a service.
391 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
392 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
396 public void authorityTests(String testName) {
397 // TODO Auto-generated method stub
402 // Material specific overrides
406 protected PoxPayloadOut createInstance(String commonPartName,
408 // Submit the request to the service and store the response.
409 String shortId = identifier;
410 String displayName = "displayName-" + shortId;
411 // String baseRefName = MaterialAuthorityClientUtils.createMaterialAuthRefName(shortId, null);
412 PoxPayloadOut result =
413 MaterialAuthorityClientUtils.createMaterialAuthorityInstance(
414 displayName, shortId, commonPartName);
419 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
420 String displayName = "displayName-NON_EXISTENT_ID";
421 PoxPayloadOut result = MaterialAuthorityClientUtils.createMaterialAuthorityInstance(
422 displayName, "nonEx", commonPartName);
427 protected MaterialauthoritiesCommon updateInstance(MaterialauthoritiesCommon materialauthoritiesCommon) {
428 MaterialauthoritiesCommon result = new MaterialauthoritiesCommon();
430 result.setDisplayName("updated-" + materialauthoritiesCommon.getDisplayName());
431 result.setVocabType("updated-" + materialauthoritiesCommon.getVocabType());
437 protected void compareUpdatedInstances(MaterialauthoritiesCommon original,
438 MaterialauthoritiesCommon updated) throws Exception {
439 Assert.assertEquals(updated.getDisplayName(),
440 original.getDisplayName(),
441 "Display name in updated object did not match submitted data.");
444 protected void compareReadInstances(MaterialauthoritiesCommon original,
445 MaterialauthoritiesCommon fromRead) throws Exception {
446 Assert.assertNotNull(fromRead.getDisplayName());
447 Assert.assertNotNull(fromRead.getShortIdentifier());
448 Assert.assertNotNull(fromRead.getRefName());
452 // Authority item specific overrides
456 protected String createItemInAuthority(AuthorityClient client, String authorityId) {
457 return createItemInAuthority(client, authorityId, null /*refname*/);
461 protected MaterialsCommon updateItemInstance(MaterialsCommon materialsCommon) {
463 MaterialTermGroupList termList = materialsCommon.getMaterialTermGroupList();
464 Assert.assertNotNull(termList);
465 List<MaterialTermGroup> terms = termList.getMaterialTermGroup();
466 Assert.assertNotNull(terms);
467 Assert.assertTrue(terms.size() > 0);
468 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
469 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
470 materialsCommon.setMaterialTermGroupList(termList);
472 return materialsCommon;
476 protected void compareUpdatedItemInstances(MaterialsCommon original,
477 MaterialsCommon updated) throws Exception {
479 MaterialTermGroupList originalTermList = original.getMaterialTermGroupList();
480 Assert.assertNotNull(originalTermList);
481 List<MaterialTermGroup> originalTerms = originalTermList.getMaterialTermGroup();
482 Assert.assertNotNull(originalTerms);
483 Assert.assertTrue(originalTerms.size() > 0);
485 MaterialTermGroupList updatedTermList = updated.getMaterialTermGroupList();
486 Assert.assertNotNull(updatedTermList);
487 List<MaterialTermGroup> updatedTerms = updatedTermList.getMaterialTermGroup();
488 Assert.assertNotNull(updatedTerms);
489 Assert.assertTrue(updatedTerms.size() > 0);
491 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
492 originalTerms.get(0).getTermDisplayName(),
493 "Value in updated record did not match submitted data.");
497 protected void verifyReadItemInstance(MaterialsCommon item)
499 // TODO Auto-generated method stub
504 protected PoxPayloadOut createNonExistenceItemInstance(
505 String commonPartName, String identifier) {
506 Map<String, String> nonexMap = new HashMap<String,String>();
507 nonexMap.put(MaterialJAXBSchema.MATERIAL_TERM_DISPLAY_NAME, TEST_MATERIAL_TERM_DISPLAY_NAME);
508 nonexMap.put(MaterialJAXBSchema.SHORT_IDENTIFIER, "nonEx");
509 nonexMap.put(MaterialJAXBSchema.MATERIAL_TERM_STATUS, TEST_MATERIAL_TERM_STATUS);
510 final String EMPTY_REFNAME = "";
511 PoxPayloadOut result =
512 MaterialAuthorityClientUtils.createMaterialInstance(EMPTY_REFNAME, nonexMap,
513 MaterialAuthorityClientUtils.getTermGroupInstance(TEST_MATERIAL_TERM_DISPLAY_NAME), commonPartName);