]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d80f05b407584a7e44eaf3c61162f923b106b91e
[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  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.client.test;
24
25 import javax.ws.rs.core.Response;
26 import org.collectionspace.services.client.AbstractCommonListUtils;
27 import org.collectionspace.services.client.CollectionSpaceClient;
28 import org.collectionspace.services.client.RepatriationClaimClient;
29 import org.collectionspace.services.client.PayloadInputPart;
30 import org.collectionspace.services.client.PayloadOutputPart;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.jaxb.AbstractCommonList;
34 import org.collectionspace.services.repatriationclaim.RepatriationClaimsCommon;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.testng.Assert;
38
39 public class RepatriationClaimServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, RepatriationClaimsCommon> {
40
41     private final Logger logger = LoggerFactory.getLogger(RepatriationClaimServiceTest.class);
42
43     /** The service path component. */
44     final String SERVICE_NAME = "repatriationclaims";
45
46     final String SERVICE_PATH_COMPONENT = "repatriationclaims";
47
48     /* (non-Javadoc)
49      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
50      */
51     @Override
52     protected CollectionSpaceClient getClientInstance() throws Exception {
53         return new RepatriationClaimClient();
54     }
55
56     @Override
57     protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
58         return new RepatriationClaimClient(clientPropertiesFilename);
59     }
60
61     /* (non-Javadoc)
62      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
63      */
64     @Override
65     protected AbstractCommonList getCommonList(Response response) {
66         return response.readEntity(AbstractCommonList.class);
67     }
68
69     // ---------------------------------------------------------------
70     // CRUD tests : CREATE tests
71     // ---------------------------------------------------------------
72
73     // Success outcomes
74
75     /* (non-Javadoc)
76      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
77      */
78     @Override
79     public void create(String testName) throws Exception {
80         // Perform setup, such as initializing the type of service request
81         // (e.g. CREATE, DELETE), its valid and expected status codes, and
82         // its associated HTTP method name (e.g. POST, DELETE).
83         setupCreate();
84
85         // Submit the request to the service and store the response.
86         RepatriationClaimClient client = new RepatriationClaimClient();
87         String identifier = createIdentifier();
88         PoxPayloadOut multipart = createRepatriationClaimInstance(identifier);
89         String newID = null;
90         Response res = client.create(multipart);
91         try {
92             int statusCode = res.getStatus();
93
94             // Check the status code of the response: does it match
95             // the expected response(s)?
96             //
97             // Specifically:
98             // Does it fall within the set of valid status codes?
99             // Does it exactly match the expected status code?
100             logger.debug(testName + ": status = " + statusCode);
101             Assert.assertTrue(
102                     testRequestType.isValidStatusCode(statusCode),
103                     invalidStatusCodeMessage(testRequestType, statusCode));
104             Assert.assertEquals(statusCode, testExpectedStatusCode);
105
106             newID = extractId(res);
107         } finally {
108             if (res != null) {
109                 res.close();
110             }
111         }
112
113         // Store the ID returned from the first resource created
114         // for additional tests below.
115         if (knownResourceId == null) {
116             knownResourceId = newID;
117             logger.debug(testName + ": knownResourceId=" + knownResourceId);
118         }
119
120         // Store the IDs from every resource created by tests,
121         // so they can be deleted after tests have been run.
122         allResourceIdsCreated.add(newID);
123     }
124
125     /* (non-Javadoc)
126      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
127      */
128     @Override
129     public void createList(String testName) throws Exception {
130         for (int i = 0; i < 3; i++) {
131             create(testName);
132         }
133     }
134
135     // ---------------------------------------------------------------
136     // CRUD tests : READ tests
137     // ---------------------------------------------------------------
138
139     // Success outcomes
140
141     /* (non-Javadoc)
142      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
143      */
144     @Override
145     public void read(String testName) throws Exception {
146         // Perform setup.
147         setupRead();
148
149         // Submit the request to the service and store the response.
150         RepatriationClaimClient client = new RepatriationClaimClient();
151         Response res = client.read(knownResourceId);
152         PoxPayloadIn input;
153         try {
154             assertStatusCode(res, testName);
155             input = new PoxPayloadIn(res.readEntity(String.class));
156         } finally {
157             if (res != null) {
158                 res.close();
159             }
160         }
161
162         // Get the common part of the response and verify that it is not null.
163         PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
164         RepatriationClaimsCommon common = null;
165         if (payloadInputPart != null) {
166             common = (RepatriationClaimsCommon) payloadInputPart.getBody();
167         }
168         Assert.assertNotNull(common);
169     }
170
171     // Failure outcomes
172
173     /* (non-Javadoc)
174      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
175      */
176     @Override
177     public void readNonExistent(String testName) throws Exception {
178         // Perform setup.
179         setupReadNonExistent();
180
181         // Submit the request to the service and store the response.
182         RepatriationClaimClient client = new RepatriationClaimClient();
183         Response res = client.read(NON_EXISTENT_ID);
184         try {
185             int statusCode = res.getStatus();
186
187             // Check the status code of the response: does it match
188             // the expected response(s)?
189             logger.debug(testName + ": status = " + statusCode);
190             Assert.assertTrue(
191                     testRequestType.isValidStatusCode(statusCode),
192                     invalidStatusCodeMessage(testRequestType, statusCode));
193             Assert.assertEquals(statusCode, testExpectedStatusCode);
194         } finally {
195             if (res != null) {
196                 res.close();
197             }
198         }
199     }
200
201     // ---------------------------------------------------------------
202     // CRUD tests : READ_LIST tests
203     // ---------------------------------------------------------------
204
205     // Success outcomes
206
207     /* (non-Javadoc)
208      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
209      */
210     @Override
211     public void readList(String testName) throws Exception {
212         // Perform setup.
213         setupReadList();
214
215         // Submit the request to the service and store the response.
216         AbstractCommonList list;
217         RepatriationClaimClient client = new RepatriationClaimClient();
218         Response res = client.readList();
219         assertStatusCode(res, testName);
220         try {
221             int statusCode = res.getStatus();
222
223             // Check the status code of the response: does it match
224             // the expected response(s)?
225             logger.debug(testName + ": status = " + statusCode);
226             Assert.assertTrue(
227                     testRequestType.isValidStatusCode(statusCode),
228                     invalidStatusCodeMessage(testRequestType, statusCode));
229             Assert.assertEquals(statusCode, testExpectedStatusCode);
230
231             list = res.readEntity(getCommonListType());
232         } finally {
233             res.close();
234         }
235
236         // Optionally output additional data about list members for debugging.
237         AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
238     }
239
240     // Failure outcomes
241     // None at present.
242
243     // ---------------------------------------------------------------
244     // CRUD tests : UPDATE tests
245     // ---------------------------------------------------------------
246
247     // Success outcomes
248
249     /* (non-Javadoc)
250      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
251      */
252     @Override
253     public void update(String testName) throws Exception {
254         // Perform setup.
255         setupRead();
256
257         // Retrieve the contents of a resource to update.
258         RepatriationClaimClient client = new RepatriationClaimClient();
259         Response res = client.read(knownResourceId);
260         PoxPayloadIn input;
261         try {
262             assertStatusCode(res, testName);
263             input = new PoxPayloadIn(res.readEntity(String.class));
264             logger.debug("got object to update with ID: " + knownResourceId);
265         } finally {
266             if (res != null) {
267                 res.close();
268             }
269         }
270
271         // Extract the common part from the response.
272         PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
273         RepatriationClaimsCommon common = null;
274         if (payloadInputPart != null) {
275             common = (RepatriationClaimsCommon) payloadInputPart.getBody();
276         }
277         Assert.assertNotNull(common);
278
279         // Update the content of this resource.
280         common.setClaimNumber("updated-" + common.getClaimNumber());
281
282         logger.debug("to be updated object");
283         logger.debug(objectAsXmlString(common, RepatriationClaimsCommon.class));
284
285         setupUpdate();
286
287         // Submit the updated common part in an update request to the service
288         // and store the response.
289         PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
290         PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), common);
291         res = client.update(knownResourceId, output);
292         try {
293             assertStatusCode(res, testName);
294             int statusCode = res.getStatus();
295             // Check the status code of the response: does it match the expected response(s)?
296             logger.debug(testName + ": status = " + statusCode);
297             Assert.assertTrue(
298                     testRequestType.isValidStatusCode(statusCode),
299                     invalidStatusCodeMessage(testRequestType, statusCode));
300             Assert.assertEquals(statusCode, testExpectedStatusCode);
301             input = new PoxPayloadIn(res.readEntity(String.class));
302         } finally {
303             if (res != null) {
304                 res.close();
305             }
306         }
307
308         // Extract the updated common part from the response.
309         payloadInputPart = input.getPart(client.getCommonPartName());
310         RepatriationClaimsCommon updatedRepatriationClaimCommon = null;
311         if (payloadInputPart != null) {
312             updatedRepatriationClaimCommon = (RepatriationClaimsCommon) payloadInputPart.getBody();
313         }
314         Assert.assertNotNull(updatedRepatriationClaimCommon);
315
316         // Check selected fields in the updated common part.
317         Assert.assertEquals(
318                 updatedRepatriationClaimCommon.getClaimNumber(),
319                 common.getClaimNumber(),
320                 "Data in updated object did not match submitted data.");
321     }
322
323     @Override
324     public void updateNonExistent(String testName) throws Exception {
325         // Perform setup.
326         setupUpdateNonExistent();
327
328         // Submit the request to the service and store the response.
329         // Note: The ID used in this 'create' call may be arbitrary.
330         // The only relevant ID may be the one used in update(), below.
331         RepatriationClaimClient client = new RepatriationClaimClient();
332         PoxPayloadOut multipart = createRepatriationClaimInstance(NON_EXISTENT_ID);
333         Response res = client.update(NON_EXISTENT_ID, multipart);
334         try {
335             int statusCode = res.getStatus();
336
337             // Check the status code of the response: does it match
338             // the expected response(s)?
339             logger.debug(testName + ": status = " + statusCode);
340             Assert.assertTrue(
341                     testRequestType.isValidStatusCode(statusCode),
342                     invalidStatusCodeMessage(testRequestType, statusCode));
343             Assert.assertEquals(statusCode, testExpectedStatusCode);
344         } finally {
345             if (res != null) {
346                 res.close();
347             }
348         }
349     }
350
351     // ---------------------------------------------------------------
352     // CRUD tests : DELETE tests
353     // ---------------------------------------------------------------
354
355     // Success outcomes
356
357     /* (non-Javadoc)
358      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
359      */
360     @Override
361     public void delete(String testName) throws Exception {
362         // Perform setup.
363         setupDelete();
364
365         // Submit the request to the service and store the response.
366         RepatriationClaimClient client = new RepatriationClaimClient();
367         Response res = client.delete(knownResourceId);
368         try {
369             int statusCode = res.getStatus();
370
371             // Check the status code of the response: does it match
372             // the expected response(s)?
373             logger.debug(testName + ": status = " + statusCode);
374             Assert.assertTrue(
375                     testRequestType.isValidStatusCode(statusCode),
376                     invalidStatusCodeMessage(testRequestType, statusCode));
377             Assert.assertEquals(statusCode, testExpectedStatusCode);
378         } finally {
379             if (res != null) {
380                 res.close();
381             }
382         }
383     }
384
385     // Failure outcomes
386
387     /* (non-Javadoc)
388      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
389      */
390     @Override
391     public void deleteNonExistent(String testName) throws Exception {
392         // Perform setup.
393         setupDeleteNonExistent();
394
395         // Submit the request to the service and store the response.
396         RepatriationClaimClient client = new RepatriationClaimClient();
397         Response res = client.delete(NON_EXISTENT_ID);
398         try {
399             int statusCode = res.getStatus();
400
401             // Check the status code of the response: does it match
402             // the expected response(s)?
403             logger.debug(testName + ": status = " + statusCode);
404             Assert.assertTrue(
405                     testRequestType.isValidStatusCode(statusCode),
406                     invalidStatusCodeMessage(testRequestType, statusCode));
407             Assert.assertEquals(statusCode, testExpectedStatusCode);
408         } finally {
409             if (res != null) {
410                 res.close();
411             }
412         }
413     }
414
415     // ---------------------------------------------------------------
416     // Utility tests : tests of code used in tests above
417     // ---------------------------------------------------------------
418
419     /**
420      * Tests the code for manually submitting data that is used by several
421      * of the methods above.
422      */
423     public void testSubmitRequest() {
424
425         // Expected status code: 200 OK
426         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
427
428         // Submit the request to the service and store the response.
429         String method = ServiceRequestType.READ.httpMethodName();
430         String url = getResourceURL(knownResourceId);
431         int statusCode = submitRequest(method, url);
432
433         // Check the status code of the response: does it match
434         // the expected response(s)?
435         logger.debug("testSubmitRequest: url=" + url + " status=" + statusCode);
436         Assert.assertEquals(statusCode, EXPECTED_STATUS);
437     }
438
439     // ---------------------------------------------------------------
440     // Utility methods used by tests above
441     // ---------------------------------------------------------------
442
443     @Override
444     public String getServiceName() {
445         return SERVICE_NAME;
446     }
447
448     /* (non-Javadoc)
449      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
450      */
451     @Override
452     public String getServicePathComponent() {
453         return SERVICE_PATH_COMPONENT;
454     }
455
456     @Override
457     protected PoxPayloadOut createInstance(String identifier) throws Exception {
458         return createRepatriationClaimInstance(identifier);
459     }
460
461     /**
462      * Creates the repatriationClaim instance.
463      *
464      * @param claimNumber the exhibition number
465      * @return the multipart output
466      * @throws Exception
467      */
468     private PoxPayloadOut createRepatriationClaimInstance(String claimNumber) throws Exception {
469         RepatriationClaimsCommon common = new RepatriationClaimsCommon();
470         common.setClaimNumber(claimNumber);
471
472         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
473         PayloadOutputPart commonPart = multipart.addPart(new RepatriationClaimClient().getCommonPartName(), common);
474
475         logger.debug("to be created, repatriationclaim common");
476         logger.debug(objectAsXmlString(common, RepatriationClaimsCommon.class));
477
478         return multipart;
479     }
480
481     @Override
482     public void CRUDTests(String testName) {
483         // TODO Auto-generated method stub
484
485     }
486
487     @Override
488     protected PoxPayloadOut createInstance(String commonPartName, String identifier) throws Exception {
489         return createRepatriationClaimInstance(identifier);
490     }
491
492     @Override
493     protected RepatriationClaimsCommon updateInstance(RepatriationClaimsCommon commonPartObject) {
494         // TODO Auto-generated method stub
495         return null;
496     }
497
498     @Override
499     protected void compareUpdatedInstances(RepatriationClaimsCommon original, RepatriationClaimsCommon updated) {
500         // TODO Auto-generated method stub
501     }
502 }