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