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 * Licensed under the Educational Community License (ECL), Version 2.0.
10 * You may not use this file except in compliance with this License.
12 * You may obtain a copy of the ECL 2.0 License at
13 * https://source.collectionspace.org/collection-space/LICENSE.txt
15 package org.collectionspace.services.client;
17 import static org.testng.Assert.assertEquals;
18 import static org.testng.Assert.assertNotNull;
19 import static org.testng.Assert.assertTrue;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
26 import javax.ws.rs.core.Response;
28 import org.collectionspace.services.ChronologyJAXBSchema;
29 import org.collectionspace.services.chronology.ChronologiesCommon;
30 import org.collectionspace.services.chronology.ChronologyTermGroup;
31 import org.collectionspace.services.chronology.ChronologyTermGroupList;
32 import org.collectionspace.services.chronology.ChronologyauthoritiesCommon;
33 import org.collectionspace.services.client.test.AbstractAuthorityServiceTest;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.testng.annotations.AfterClass;
37 import org.testng.annotations.Test;
40 * Test against a deployed ChronologyAuthority Service
42 public class ChronologyAuthorityServiceTest
43 extends AbstractAuthorityServiceTest<ChronologyauthoritiesCommon, ChronologiesCommon> {
45 private static final Logger logger = LoggerFactory.getLogger(ChronologyAuthorityServiceTest.class);
47 private static final String TEST_CHRONOLOGY_DESCRIPTION = "A Chronology description";
48 private static final String TEST_CHRONOLOGY_TERM_NAME = "ChronoTerm";
49 private static final String TEST_CHRONOLOGY_TERM_DISPLAY_NAME = "Chronology 1";
50 private static final String TEST_CHRONOLOGY_TERM_STATUS = "accepted";
51 private static final String TEST_CHRONOLOGY_TERM_SOURCE = "source";
52 private static final String TEST_CHRONOLOGY_TERM_SOURCE_DETAIL = "detail";
54 public ChronologyAuthorityServiceTest() {
56 TEST_SHORTID = "chronology1";
59 @Test(dataProvider="testName")
60 public void verifyIllegalItemDisplayName(String testName) throws Exception {
61 // Perform setup for read
64 // Submit the request to the service and store the response.
65 final ChronologyAuthorityClient client = new ChronologyAuthorityClient();
66 final Response res = client.readItem(knownResourceId, knownItemResourceId);
67 final ChronologiesCommon chronology;
69 assertStatusCode(res, testName);
70 final String commonPartName = client.getItemCommonPartName();
71 final PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
72 chronology = (ChronologiesCommon) extractPart(input, commonPartName, ChronologiesCommon.class);
73 assertNotNull(chronology);
81 // Make an invalid UPDATE request, without a display name
83 ChronologyTermGroupList termList = chronology.getChronologyTermGroupList();
84 assertNotNull(termList);
85 List<ChronologyTermGroup> terms = termList.getChronologyTermGroup();
87 assertTrue(terms.size() > 0);
88 terms.get(0).setTermDisplayName(null);
89 terms.get(0).setTermName(null);
91 // we expect a failure
92 setupUpdateWithInvalidBody();
94 // Submit the updated resource to the service and store the response.
95 PoxPayloadOut output = new PoxPayloadOut(ChronologyAuthorityClient.SERVICE_ITEM_NAME);
96 output.addPart(client.getItemCommonPartName(), chronology);
98 // we expected a failure here.
99 setupUpdateWithInvalidBody();
100 final Response updateResponse = client.updateItem(knownResourceId, knownItemResourceId, output);
102 assertStatusCode(updateResponse, testName);
104 if (updateResponse != null) {
105 updateResponse.close();
110 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
111 public void localDelete(String testName) throws Exception {
112 super.delete(testName);
115 @Test(dataProvider = "testName", groups = {"delete"}, dependsOnMethods = {"verifyIllegalItemDisplayName"})
116 public void localDeleteItem(String testName) throws Exception {
117 super.deleteItem(testName);
121 @AfterClass(alwaysRun = true)
122 public void cleanUp() throws Exception {
123 final String noTestCleanup = System.getProperty("noTestCleanup");
124 if (Boolean.parseBoolean(noTestCleanup)) {
125 logger.debug("Skipping cleanup");
129 logger.debug("Cleaning temporary resources for testing");
131 ChronologyAuthorityClient client = new ChronologyAuthorityClient();
132 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
133 final String itemResourceId = entry.getKey();
134 final String parentResourceId = entry.getValue();
135 client.deleteItem(parentResourceId, itemResourceId).close();
138 for (String resourceId : allResourceIdsCreated) {
139 client.delete(resourceId).close();
144 public void authorityTests(String testName) {
149 public void delete(String testName) {
150 // Do nothing. Ensures proper test order.
154 public void deleteItem(String testName) {
155 // Do nothing. Ensures proper test order.
159 protected String createItemInAuthority(final AuthorityClient client, final String vcsid, final String shortId) {
160 return createItemInAuthority(client, vcsid, shortId, null);
163 private String createItemInAuthority(final AuthorityClient client,
165 final String shortId,
166 final String authRefName) {
167 final String testName = String.format("createItemInAuthority(%s, %s)", vcsid, shortId);
169 Map<String, String> materialMap = new HashMap<>();
170 materialMap.put(ChronologyJAXBSchema.SHORT_IDENTIFIER, shortId);
171 materialMap.put(ChronologyJAXBSchema.CHRONOLOGY_DESCRIPTION, TEST_CHRONOLOGY_DESCRIPTION);
173 List<ChronologyTermGroup> terms = new ArrayList<>();
174 ChronologyTermGroup term = new ChronologyTermGroup();
175 term.setTermName(TEST_CHRONOLOGY_TERM_NAME);
176 term.setTermDisplayName(TEST_CHRONOLOGY_TERM_DISPLAY_NAME);
177 term.setTermSource(TEST_CHRONOLOGY_TERM_SOURCE);
178 term.setTermSourceDetail(TEST_CHRONOLOGY_TERM_SOURCE_DETAIL);
179 term.setTermStatus(TEST_CHRONOLOGY_TERM_STATUS);
182 final String id = ChronologyAuthorityClientUtils.createItemInAuthority(vcsid, authRefName, materialMap, terms,
183 (ChronologyAuthorityClient) client);
185 // Store the id returned from the first item resource created for additional tests
186 if (knownItemResourceId == null) {
187 setKnownItemResource(id, shortId);
188 logger.debug("{}: knownResourceId={}", testName, id);
191 // Store item resource ids and parent ids so that they can be deleted after all tests run
192 allResourceItemIdsCreated.put(id, vcsid);
198 protected ChronologiesCommon updateItemInstance(final ChronologiesCommon authorityItem) {
199 final ChronologyTermGroupList termList = authorityItem.getChronologyTermGroupList();
200 assertNotNull(termList);
201 final List<ChronologyTermGroup> terms = termList.getChronologyTermGroup();
202 assertNotNull(terms);
203 assertTrue(terms.size() > 0);
204 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
205 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
206 authorityItem.setChronologyTermGroupList(termList);
207 return authorityItem;
211 protected void compareUpdatedItemInstances(final ChronologiesCommon original,
212 final ChronologiesCommon updated,
213 final boolean compareRevNumbers) {
214 final ChronologyTermGroupList originalTermList = original.getChronologyTermGroupList();
215 assertNotNull(originalTermList);
216 final List<ChronologyTermGroup> originalTerms = originalTermList.getChronologyTermGroup();
217 assertNotNull(originalTerms);
218 assertTrue(originalTerms.size() > 0);
220 final ChronologyTermGroupList updatedTermList = updated.getChronologyTermGroupList();
221 assertNotNull(updatedTermList);
222 final List<ChronologyTermGroup> updatedTerms = updatedTermList.getChronologyTermGroup();
223 assertNotNull(updatedTerms);
224 assertTrue(updatedTerms.size() > 0);
226 assertEquals(updatedTerms.get(0).getTermDisplayName(), originalTerms.get(0).getTermDisplayName(),
227 "Value in updated record did not match submitted data.");
229 if (compareRevNumbers) {
230 assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
235 protected void verifyReadItemInstance(final ChronologiesCommon item) {
240 protected PoxPayloadOut createNonExistenceInstance(final String commonPartName, final String identifier) {
241 final String displayName = "displayName-NON_EXISTENT_ID";
242 return ChronologyAuthorityClientUtils.createChronologyAuthorityInstance(displayName, "nonEx", commonPartName);
246 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier) {
247 Map<String, String> nonexMap = new HashMap<>();
248 nonexMap.put(ChronologyJAXBSchema.SHORT_IDENTIFIER, "nonEx");
249 nonexMap.put(ChronologyJAXBSchema.TERM_STATUS, TEST_CHRONOLOGY_TERM_STATUS);
250 nonexMap.put(ChronologyJAXBSchema.TERM_DISPLAY_NAME, TEST_CHRONOLOGY_TERM_DISPLAY_NAME);
251 final List<ChronologyTermGroup> termGroupInstance =
252 ChronologyAuthorityClientUtils.getTermGroupInstance(TEST_CHRONOLOGY_DESCRIPTION);
254 return ChronologyAuthorityClientUtils.createChronologyInstance(nonexMap, termGroupInstance, commonPartName);
258 protected PoxPayloadOut createInstance(final String commonPartName, final String identifier) {
259 // Submit the request to the service and store the response.
260 final String displayName = "displayName-" + identifier;
261 return ChronologyAuthorityClientUtils.createChronologyAuthorityInstance(displayName, identifier,
266 protected ChronologyauthoritiesCommon updateInstance(final ChronologyauthoritiesCommon commonPartObject) {
267 ChronologyauthoritiesCommon result = new ChronologyauthoritiesCommon();
269 result.setDisplayName("updated-" + commonPartObject.getDisplayName());
270 result.setVocabType("updated-" + commonPartObject.getVocabType());
276 protected void compareUpdatedInstances(final ChronologyauthoritiesCommon original,
277 final ChronologyauthoritiesCommon updated) {
278 assertEquals(updated.getDisplayName(), original.getDisplayName(),
279 "Display name in updated object did not match submitted data.");
283 protected void compareReadInstances(final ChronologyauthoritiesCommon original,
284 final ChronologyauthoritiesCommon fromRead) {
285 assertNotNull(fromRead.getDisplayName());
286 assertNotNull(fromRead.getShortIdentifier());
287 assertNotNull(fromRead.getRefName());
291 protected CollectionSpaceClient getClientInstance() throws Exception {
292 return new ChronologyAuthorityClient();
296 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
297 return new ChronologyAuthorityClient(clientPropertiesFilename);
301 protected String getServicePathComponent() {
302 return ChronologyAuthorityClient.SERVICE_PATH_COMPONENT;
306 protected String getServiceName() {
307 return ChronologyAuthorityClient.SERVICE_NAME;
311 protected String getItemServiceRootURL(String parentResourceIdentifier) {
312 return getResourceURL(parentResourceIdentifier) + "/" + getServicePathComponent();
316 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
317 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;