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;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
53 * MaterialAuthorityServiceTest, carries out tests against a
54 * deployed and running MaterialAuthority Service.
57 public class MaterialAuthorityServiceTest extends AbstractAuthorityServiceTest<MaterialauthoritiesCommon, MaterialsCommon> {
60 private final Logger logger = LoggerFactory.getLogger(MaterialAuthorityServiceTest.class);
63 * Default constructor. Used to set the short ID for all tests authority items
65 public MaterialAuthorityServiceTest() {
67 TEST_SHORTID = "superglass";
71 public String getServicePathComponent() {
72 return MaterialAuthorityClient.SERVICE_PATH_COMPONENT;
76 protected String getServiceName() {
77 return MaterialAuthorityClient.SERVICE_NAME;
80 public String getItemServicePathComponent() {
81 return AuthorityClient.ITEMS;
84 final String TEST_MATERIAL_TERM_DISPLAY_NAME = "SuperGlass 2";
85 final String TEST_MATERIAL_TERM_NAME = "SuperGlass";
86 final String TEST_MATERIAL_TERM_STATUS = "accepted";
87 final String TEST_MATERIAL_TERM_SOURCE = "source";
88 final String TEST_MATERIAL_TERM_SOURCE_DETAIL = "internal";
89 final String TEST_MATERIAL_DESCRIPTION = "Really strong glass";
92 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
95 protected CollectionSpaceClient getClientInstance() throws Exception {
96 return new MaterialAuthorityClient();
100 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
101 return new MaterialAuthorityClient(clientPropertiesFilename);
105 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
106 return createItemInAuthority(client, authorityId, shortId, null /*refname*/);
110 * Creates the item in authority.
112 * @param vcsid the vcsid
113 * @param authRefName the auth ref name
116 private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
117 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
119 // Submit the request to the service and store the response.
120 Map<String, String> materialMap = new HashMap<String,String>();
121 // TODO Make material type and status be controlled vocabs.
122 materialMap.put(MaterialJAXBSchema.SHORT_IDENTIFIER, shortId);
123 materialMap.put(MaterialJAXBSchema.MATERIAL_DESCRIPTION, TEST_MATERIAL_DESCRIPTION);
125 List<MaterialTermGroup> terms = new ArrayList<MaterialTermGroup>();
126 MaterialTermGroup term = new MaterialTermGroup();
127 term.setTermDisplayName(TEST_MATERIAL_TERM_DISPLAY_NAME);
128 term.setTermName(TEST_MATERIAL_TERM_NAME);
129 term.setTermSource(TEST_MATERIAL_TERM_SOURCE);
130 term.setTermSourceDetail(TEST_MATERIAL_TERM_SOURCE_DETAIL);
131 term.setTermStatus(TEST_MATERIAL_TERM_STATUS);
134 String newID = MaterialAuthorityClientUtils.createItemInAuthority(vcsid,
135 authRefName, materialMap, terms, (MaterialAuthorityClient) client);
137 // Store the ID returned from the first item resource created
138 // for additional tests below.
139 if (knownItemResourceId == null){
140 setKnownItemResource(newID, shortId);
141 if (logger.isDebugEnabled()) {
142 logger.debug(testName + ": knownItemResourceId=" + newID);
146 // Store the IDs from any item resources created
147 // by tests, along with the IDs of their parents, so these items
148 // can be deleted after all tests have been run.
149 allResourceItemIdsCreated.put(newID, vcsid);
155 * Verify illegal item display name.
157 * @param testName the test name
158 * @throws Exception the exception
160 @Test(dataProvider="testName")
161 public void verifyIllegalItemDisplayName(String testName) throws Exception {
162 // Perform setup for read.
165 // Submit the request to the service and store the response.
166 MaterialAuthorityClient client = new MaterialAuthorityClient();
167 Response res = client.readItem(knownResourceId, knownItemResourceId);
168 MaterialsCommon material = null;
170 assertStatusCode(res, testName);
171 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
172 material = (MaterialsCommon) extractPart(input,
173 client.getItemCommonPartName(), MaterialsCommon.class);
174 Assert.assertNotNull(material);
182 // Make an invalid UPDATE request, without a display name
184 MaterialTermGroupList termList = material.getMaterialTermGroupList();
185 Assert.assertNotNull(termList);
186 List<MaterialTermGroup> terms = termList.getMaterialTermGroup();
187 Assert.assertNotNull(terms);
188 Assert.assertTrue(terms.size() > 0);
189 terms.get(0).setTermDisplayName(null);
190 terms.get(0).setTermName(null);
192 setupUpdateWithInvalidBody(); // we expect a failure
194 // Submit the updated resource to the service and store the response.
195 PoxPayloadOut output = new PoxPayloadOut(
196 MaterialAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
197 PayloadOutputPart commonPart = output.addPart(
198 client.getItemCommonPartName(), material);
199 setupUpdateWithInvalidBody(); // we expected a failure here.
200 res = client.updateItem(knownResourceId, knownItemResourceId, output);
202 assertStatusCode(res, testName);
211 public void delete(String testName) throws Exception {
212 // Do nothing. See localDelete(). This ensure proper test order.
215 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
216 public void localDelete(String testName) throws Exception {
217 super.delete(testName);
221 public void deleteItem(String testName) throws Exception {
222 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
223 // its dependencies will get run first and then we can call the base class' delete method.
226 @Test(dataProvider = "testName", groups = {"delete"},
227 dependsOnMethods = {"verifyIllegalItemDisplayName"})
228 public void localDeleteItem(String testName) throws Exception {
229 super.deleteItem(testName);
232 // ---------------------------------------------------------------
233 // Cleanup of resources created during testing
234 // ---------------------------------------------------------------
237 * Deletes all resources created by tests, after all tests have been run.
239 * This cleanup method will always be run, even if one or more tests fail.
240 * For this reason, it attempts to remove all resources created
241 * at any point during testing, even if some of those resources
242 * may be expected to be deleted by certain tests.
246 @AfterClass(alwaysRun=true)
247 public void cleanUp() throws Exception {
248 String noTest = System.getProperty("noTestCleanup");
249 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
250 if (logger.isDebugEnabled()) {
251 logger.debug("Skipping Cleanup phase ...");
255 if (logger.isDebugEnabled()) {
256 logger.debug("Cleaning up temporary resources created for testing ...");
258 String parentResourceId;
259 String itemResourceId;
260 // Clean up contact resources.
261 MaterialAuthorityClient client = new MaterialAuthorityClient();
262 parentResourceId = knownResourceId;
263 // Clean up item resources.
264 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
265 itemResourceId = entry.getKey();
266 parentResourceId = entry.getValue();
267 // Note: Any non-success responses from the delete operation
268 // below are ignored and not reported.
269 client.deleteItem(parentResourceId, itemResourceId).close();
271 // Clean up parent resources.
272 for (String resourceId : allResourceIdsCreated) {
273 // Note: Any non-success responses from the delete operation
274 // below are ignored and not reported.
275 client.delete(resourceId).close();
279 // ---------------------------------------------------------------
280 // Utility methods used by tests above
281 // ---------------------------------------------------------------
283 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
287 * Returns the root URL for the item service.
289 * This URL consists of a base URL for all services, followed by
290 * a path component for the owning parent, followed by the
291 * path component for the items.
293 * @param parentResourceIdentifier An identifier (such as a UUID) for the
294 * parent authority resource of the relevant item resource.
296 * @return The root URL for the item service.
298 protected String getItemServiceRootURL(String parentResourceIdentifier) {
299 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
303 * Returns the URL of a specific item resource managed by a service, and
304 * designated by an identifier (such as a universally unique ID, or UUID).
306 * @param parentResourceIdentifier An identifier (such as a UUID) for the
307 * parent authority resource of the relevant item resource.
309 * @param itemResourceIdentifier An identifier (such as a UUID) for an
312 * @return The URL of a specific item resource managed by a service.
314 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
315 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
319 public void authorityTests(String testName) {
320 // TODO Auto-generated method stub
325 // Material specific overrides
329 protected PoxPayloadOut createInstance(String commonPartName,
331 // Submit the request to the service and store the response.
332 String shortId = identifier;
333 String displayName = "displayName-" + shortId;
334 // String baseRefName = MaterialAuthorityClientUtils.createMaterialAuthRefName(shortId, null);
335 PoxPayloadOut result =
336 MaterialAuthorityClientUtils.createMaterialAuthorityInstance(
337 displayName, shortId, commonPartName);
342 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
343 String displayName = "displayName-NON_EXISTENT_ID";
344 PoxPayloadOut result = MaterialAuthorityClientUtils.createMaterialAuthorityInstance(
345 displayName, "nonEx", commonPartName);
350 protected MaterialauthoritiesCommon updateInstance(MaterialauthoritiesCommon materialauthoritiesCommon) {
351 MaterialauthoritiesCommon result = new MaterialauthoritiesCommon();
353 result.setDisplayName("updated-" + materialauthoritiesCommon.getDisplayName());
354 result.setVocabType("updated-" + materialauthoritiesCommon.getVocabType());
360 protected void compareUpdatedInstances(MaterialauthoritiesCommon original,
361 MaterialauthoritiesCommon updated) throws Exception {
362 Assert.assertEquals(updated.getDisplayName(),
363 original.getDisplayName(),
364 "Display name in updated object did not match submitted data.");
367 protected void compareReadInstances(MaterialauthoritiesCommon original,
368 MaterialauthoritiesCommon fromRead) throws Exception {
369 Assert.assertNotNull(fromRead.getDisplayName());
370 Assert.assertNotNull(fromRead.getShortIdentifier());
371 Assert.assertNotNull(fromRead.getRefName());
375 // Authority item specific overrides
379 protected MaterialsCommon updateItemInstance(MaterialsCommon materialsCommon) {
381 MaterialTermGroupList termList = materialsCommon.getMaterialTermGroupList();
382 Assert.assertNotNull(termList);
383 List<MaterialTermGroup> terms = termList.getMaterialTermGroup();
384 Assert.assertNotNull(terms);
385 Assert.assertTrue(terms.size() > 0);
386 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
387 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
388 materialsCommon.setMaterialTermGroupList(termList);
390 return materialsCommon;
394 protected void compareUpdatedItemInstances(MaterialsCommon original,
395 MaterialsCommon updated,
396 boolean compareRevNumbers) throws Exception {
398 MaterialTermGroupList originalTermList = original.getMaterialTermGroupList();
399 Assert.assertNotNull(originalTermList);
400 List<MaterialTermGroup> originalTerms = originalTermList.getMaterialTermGroup();
401 Assert.assertNotNull(originalTerms);
402 Assert.assertTrue(originalTerms.size() > 0);
404 MaterialTermGroupList updatedTermList = updated.getMaterialTermGroupList();
405 Assert.assertNotNull(updatedTermList);
406 List<MaterialTermGroup> updatedTerms = updatedTermList.getMaterialTermGroup();
407 Assert.assertNotNull(updatedTerms);
408 Assert.assertTrue(updatedTerms.size() > 0);
410 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
411 originalTerms.get(0).getTermDisplayName(),
412 "Value in updated record did not match submitted data.");
414 if (compareRevNumbers == true) {
415 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
420 protected void verifyReadItemInstance(MaterialsCommon item)
422 // TODO Auto-generated method stub
427 protected PoxPayloadOut createNonExistenceItemInstance(
428 String commonPartName, String identifier) {
429 Map<String, String> nonexMap = new HashMap<String,String>();
430 nonexMap.put(MaterialJAXBSchema.MATERIAL_TERM_DISPLAY_NAME, TEST_MATERIAL_TERM_DISPLAY_NAME);
431 nonexMap.put(MaterialJAXBSchema.SHORT_IDENTIFIER, "nonEx");
432 nonexMap.put(MaterialJAXBSchema.MATERIAL_TERM_STATUS, TEST_MATERIAL_TERM_STATUS);
433 final String EMPTY_REFNAME = "";
434 PoxPayloadOut result =
435 MaterialAuthorityClientUtils.createMaterialInstance(EMPTY_REFNAME, nonexMap,
436 MaterialAuthorityClientUtils.getTermGroupInstance(TEST_MATERIAL_TERM_DISPLAY_NAME), commonPartName);