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