]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e3433ecd33162dce8287849320546d2122527af5
[tmp/jakarta-migration.git] /
1 /**
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:
5  * <p>
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  * <p>
9  * Licensed under the Educational Community License (ECL), Version 2.0.
10  * You may not use this file except in compliance with this License.
11  * <p>
12  * You may obtain a copy of the ECL 2.0 License at
13  * https://source.collectionspace.org/collection-space/LICENSE.txt
14  */
15 package org.collectionspace.services.client;
16
17 import static org.testng.Assert.assertEquals;
18 import static org.testng.Assert.assertNotNull;
19 import static org.testng.Assert.assertTrue;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.ws.rs.core.Response;
27
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;
38
39 /**
40  * Test against a deployed ChronologyAuthority Service
41  */
42 public class ChronologyAuthorityServiceTest
43     extends AbstractAuthorityServiceTest<ChronologyauthoritiesCommon, ChronologiesCommon> {
44
45     private static final Logger logger = LoggerFactory.getLogger(ChronologyAuthorityServiceTest.class);
46
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";
53
54     public ChronologyAuthorityServiceTest() {
55         super();
56         TEST_SHORTID = "chronology1";
57     }
58
59     @Test(dataProvider="testName")
60     public void verifyIllegalItemDisplayName(String testName) throws Exception {
61         // Perform setup for read
62         setupRead();
63
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;
68         try {
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);
74         } finally {
75             if (res != null) {
76                 res.close();
77             }
78         }
79
80         //
81         // Make an invalid UPDATE request, without a display name
82         //
83         ChronologyTermGroupList termList = chronology.getChronologyTermGroupList();
84         assertNotNull(termList);
85         List<ChronologyTermGroup> terms = termList.getChronologyTermGroup();
86         assertNotNull(terms);
87         assertTrue(terms.size() > 0);
88         terms.get(0).setTermDisplayName(null);
89         terms.get(0).setTermName(null);
90
91         // we expect a failure
92         setupUpdateWithInvalidBody();
93
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);
97
98         // we expected a failure here.
99         setupUpdateWithInvalidBody();
100         final Response updateResponse = client.updateItem(knownResourceId, knownItemResourceId, output);
101         try {
102             assertStatusCode(updateResponse, testName);
103         } finally {
104             if (updateResponse != null) {
105                 updateResponse.close();
106             }
107         }
108     }
109
110     @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
111     public void localDelete(String testName) throws Exception {
112         super.delete(testName);
113     }
114
115     @Test(dataProvider = "testName", groups = {"delete"}, dependsOnMethods = {"verifyIllegalItemDisplayName"})
116     public void localDeleteItem(String testName) throws Exception {
117         super.deleteItem(testName);
118     }
119
120     @Override
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");
126             return;
127         }
128
129         logger.debug("Cleaning temporary resources for testing");
130
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();
136         }
137
138         for (String resourceId : allResourceIdsCreated) {
139             client.delete(resourceId).close();
140         }
141     }
142
143     @Override
144     public void authorityTests(String testName) {
145         // empty
146     }
147
148     @Override
149     public void delete(String testName) {
150         // Do nothing. Ensures proper test order.
151     }
152
153     @Override
154     public void deleteItem(String testName) {
155         // Do nothing. Ensures proper test order.
156     }
157
158     @Override
159     protected String createItemInAuthority(final AuthorityClient client, final String vcsid, final String shortId) {
160         return createItemInAuthority(client, vcsid, shortId, null);
161     }
162
163     private String createItemInAuthority(final AuthorityClient client,
164                                          final String vcsid,
165                                          final String shortId,
166                                          final String authRefName) {
167         final String testName = String.format("createItemInAuthority(%s, %s)", vcsid, shortId);
168
169         Map<String, String> materialMap = new HashMap<>();
170         materialMap.put(ChronologyJAXBSchema.SHORT_IDENTIFIER, shortId);
171         materialMap.put(ChronologyJAXBSchema.CHRONOLOGY_DESCRIPTION, TEST_CHRONOLOGY_DESCRIPTION);
172
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);
180         terms.add(term);
181
182         final String id = ChronologyAuthorityClientUtils.createItemInAuthority(vcsid, authRefName, materialMap, terms,
183                                                                                (ChronologyAuthorityClient) client);
184
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);
189         }
190
191         // Store item resource ids and parent ids so that they can be deleted after all tests run
192         allResourceItemIdsCreated.put(id, vcsid);
193
194         return id;
195     }
196
197     @Override
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;
208     }
209
210     @Override
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);
219
220         final ChronologyTermGroupList updatedTermList = updated.getChronologyTermGroupList();
221         assertNotNull(updatedTermList);
222         final List<ChronologyTermGroup> updatedTerms = updatedTermList.getChronologyTermGroup();
223         assertNotNull(updatedTerms);
224         assertTrue(updatedTerms.size() > 0);
225
226         assertEquals(updatedTerms.get(0).getTermDisplayName(), originalTerms.get(0).getTermDisplayName(),
227                      "Value in updated record did not match submitted data.");
228
229         if (compareRevNumbers) {
230             assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
231         }
232     }
233
234     @Override
235     protected void verifyReadItemInstance(final ChronologiesCommon item) {
236         // empty
237     }
238
239     @Override
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);
243     }
244
245     @Override
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);
253
254         return ChronologyAuthorityClientUtils.createChronologyInstance(nonexMap, termGroupInstance, commonPartName);
255     }
256
257     @Override
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,
262                                                                                 commonPartName);
263     }
264
265     @Override
266     protected ChronologyauthoritiesCommon updateInstance(final ChronologyauthoritiesCommon commonPartObject) {
267         ChronologyauthoritiesCommon result = new ChronologyauthoritiesCommon();
268
269         result.setDisplayName("updated-" + commonPartObject.getDisplayName());
270         result.setVocabType("updated-" + commonPartObject.getVocabType());
271
272         return result;
273     }
274
275     @Override
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.");
280     }
281
282     @Override
283     protected void compareReadInstances(final ChronologyauthoritiesCommon original,
284                                         final ChronologyauthoritiesCommon fromRead) {
285         assertNotNull(fromRead.getDisplayName());
286         assertNotNull(fromRead.getShortIdentifier());
287         assertNotNull(fromRead.getRefName());
288     }
289
290     @Override
291     protected CollectionSpaceClient getClientInstance() throws Exception {
292         return new ChronologyAuthorityClient();
293     }
294
295     @Override
296     protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
297         return new ChronologyAuthorityClient(clientPropertiesFilename);
298     }
299
300     @Override
301     protected String getServicePathComponent() {
302         return ChronologyAuthorityClient.SERVICE_PATH_COMPONENT;
303     }
304
305     @Override
306     protected String getServiceName() {
307         return ChronologyAuthorityClient.SERVICE_NAME;
308     }
309
310     @Override
311     protected String getItemServiceRootURL(String parentResourceIdentifier) {
312         return getResourceURL(parentResourceIdentifier) + "/" + getServicePathComponent();
313     }
314
315     @Override
316     protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
317         return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
318     }
319 }