]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8fba32b9c92c5fc05206e33e3c0207e7ca4cb44e
[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 java.util.ArrayList;
26 import java.math.BigInteger;
27
28 import javax.ws.rs.core.Response;
29 import javax.xml.datatype.DatatypeConfigurationException;
30 import javax.xml.datatype.DatatypeFactory;
31 import javax.xml.datatype.XMLGregorianCalendar;
32
33 import org.collectionspace.services.client.AbstractCommonListUtils;
34 import org.collectionspace.services.client.PublicItemClient;
35 import org.collectionspace.services.client.PayloadInputPart;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.publicitem.PublicitemsCommon;
41
42 import org.jboss.resteasy.client.ClientResponse;
43 import org.testng.Assert;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * PublicItemServiceTest, carries out tests against a deployed and running PublicItem
50  * Service.
51  * 
52  * $LastChangedRevision$ $LastChangedDate$
53  */
54 public class PublicItemServiceTest extends
55                 AbstractPoxServiceTestImpl<AbstractCommonList, PublicitemsCommon> {
56
57         /** The logger. */
58         private final String CLASS_NAME = PublicItemServiceTest.class.getName();
59         private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60         // Instance variables specific to this test.
61         /** The service path component. */
62         final String SERVICE_NAME = PublicItemClient.SERVICE_NAME;
63         final String SERVICE_PATH_COMPONENT = PublicItemClient.SERVICE_PATH_COMPONENT;
64
65         /*
66          * (non-Javadoc)
67          * 
68          * @see
69          * org.collectionspace.services.client.test.BaseServiceTest#getClientInstance
70          * ()
71          */
72         @Override
73         protected PublicItemClient getClientInstance() {
74                 return new PublicItemClient();
75         }
76
77         /*
78          * (non-Javadoc)
79          * 
80          * @see org.collectionspace.services.client.test.BaseServiceTest#
81          * getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
82          */
83         @Override
84         protected AbstractCommonList getCommonList(
85                         ClientResponse<AbstractCommonList> response) {
86                 return response.getEntity(AbstractCommonList.class);
87         }
88
89         // ---------------------------------------------------------------
90         // CRUD tests : CREATE tests
91         // ---------------------------------------------------------------
92
93         // Success outcomes
94
95         /*
96          * (non-Javadoc)
97          * 
98          * @see
99          * org.collectionspace.services.client.test.ServiceTest#create(java.lang
100          * .String)
101          */
102         @Override
103         // @Test(dataProvider = "testName", dataProviderClass =
104         // AbstractServiceTestImpl.class)
105         public void create(String testName) throws Exception {
106                 // Perform setup, such as initializing the type of service request
107                 // (e.g. CREATE, DELETE), its valid and expected status codes, and
108                 // its associated HTTP method name (e.g. POST, DELETE).
109                 setupCreate();
110
111                 // Submit the request to the service and store the response.
112                 PublicItemClient client = new PublicItemClient();
113                 String identifier = createIdentifier();
114                 PoxPayloadOut multipart = createPublicItemInstance(identifier);
115                 String newID = null;
116                 Response res = client.create(multipart);
117                 try {
118                         int statusCode = res.getStatus();
119
120                         // Check the status code of the response: does it match
121                         // the expected response(s)?
122                         //
123                         // Specifically:
124                         // Does it fall within the set of valid status codes?
125                         // Does it exactly match the expected status code?
126                         if (logger.isDebugEnabled()) {
127                                 logger.debug(testName + ": status = " + statusCode);
128                         }
129                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
130                                         invalidStatusCodeMessage(testRequestType, statusCode));
131                         Assert.assertEquals(statusCode, testExpectedStatusCode);
132
133                         newID = extractId(res);
134                 } finally {
135                         if (res != null) {
136                                 res.close();
137                         }
138                 }
139
140                 // Store the ID returned from the first resource created
141                 // for additional tests below.
142                 if (knownResourceId == null) {
143                         knownResourceId = newID;
144                         if (logger.isDebugEnabled()) {
145                                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
146                         }
147                 }
148
149                 // Store the IDs from every resource created by tests,
150                 // so they can be deleted after tests have been run.
151                 allResourceIdsCreated.add(newID);
152         }
153
154         /*
155          * (non-Javadoc)
156          * 
157          * @see
158          * org.collectionspace.services.client.test.AbstractServiceTestImpl#createList
159          * (java.lang.String)
160          */
161         @Override
162         // @Test(dataProvider = "testName", dataProviderClass =
163         // AbstractServiceTestImpl.class,
164         // dependsOnMethods = {"create"})
165         public void createList(String testName) throws Exception {
166                 for (int i = 0; i < 3; i++) {
167                         create(testName);
168                 }
169         }
170
171         // ---------------------------------------------------------------
172         // CRUD tests : READ tests
173         // ---------------------------------------------------------------
174
175         /*
176          * (non-Javadoc)
177          * 
178          * @see
179          * org.collectionspace.services.client.test.AbstractServiceTestImpl#read
180          * (java.lang.String)
181          */
182         @Override
183         // @Test(dataProvider = "testName", dataProviderClass =
184         // AbstractServiceTestImpl.class,
185         // dependsOnMethods = {"create"})
186         public void read(String testName) throws Exception {
187                 // Perform setup.
188                 setupRead();
189
190                 // Submit the request to the service and store the response.
191                 PublicItemClient client = new PublicItemClient();
192                 ClientResponse<String> res = client.read(knownResourceId);
193                 PoxPayloadIn input = null;
194                 try {
195                         assertStatusCode(res, testName);
196                         input = new PoxPayloadIn(res.getEntity());
197                 } finally {
198                         if (res != null) {
199                                 res.releaseConnection();
200                         }
201                 }
202
203                 // Get the common part of the response and verify that it is not null.
204                 PayloadInputPart payloadInputPart = input.getPart(client
205                                 .getCommonPartName());
206                 PublicitemsCommon publicItemsCommon = null;
207                 if (payloadInputPart != null) {
208                         publicItemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
209                 }
210                 Assert.assertNotNull(publicItemsCommon);
211
212         }
213
214         // Failure outcomes
215
216         /*
217          * (non-Javadoc)
218          * 
219          * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
220          * readNonExistent(java.lang.String)
221          */
222         @Override
223         // @Test(dataProvider = "testName", dataProviderClass =
224         // AbstractServiceTestImpl.class,
225         // dependsOnMethods = {"read"})
226         public void readNonExistent(String testName) throws Exception {
227                 // Perform setup.
228                 setupReadNonExistent();
229
230                 // Submit the request to the service and store the response.
231                 PublicItemClient client = new PublicItemClient();
232                 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
233                 try {
234                         int statusCode = res.getStatus();
235
236                         // Check the status code of the response: does it match
237                         // the expected response(s)?
238                         if (logger.isDebugEnabled()) {
239                                 logger.debug(testName + ": status = " + statusCode);
240                         }
241                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
242                                         invalidStatusCodeMessage(testRequestType, statusCode));
243                         Assert.assertEquals(statusCode, testExpectedStatusCode);
244                 } finally {
245                         if (res != null) {
246                                 res.releaseConnection();
247                         }
248                 }
249         }
250
251         // ---------------------------------------------------------------
252         // CRUD tests : READ_LIST tests
253         // ---------------------------------------------------------------
254
255         // Success outcomes
256
257         /*
258          * (non-Javadoc)
259          * 
260          * @see
261          * org.collectionspace.services.client.test.AbstractServiceTestImpl#readList
262          * (java.lang.String)
263          */
264         @Override
265         // @Test(dataProvider = "testName", dataProviderClass =
266         // AbstractServiceTestImpl.class,
267         // dependsOnMethods = {"createList", "read"})
268         public void readList(String testName) throws Exception {
269                 // Perform setup.
270                 setupReadList();
271
272                 // Submit the request to the service and store the response.
273                 AbstractCommonList list = null;
274                 PublicItemClient client = new PublicItemClient();
275                 ClientResponse<AbstractCommonList> res = client.readList();
276                 assertStatusCode(res, testName);
277                 try {
278                         int statusCode = res.getStatus();
279
280                         // Check the status code of the response: does it match
281                         // the expected response(s)?
282                         if (logger.isDebugEnabled()) {
283                                 logger.debug(testName + ": status = " + statusCode);
284                         }
285                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
286                                         invalidStatusCodeMessage(testRequestType, statusCode));
287                         Assert.assertEquals(statusCode, testExpectedStatusCode);
288
289                         list = res.getEntity();
290                 } finally {
291                         if (res != null) {
292                                 res.releaseConnection();
293                         }
294                 }
295
296                 // Optionally output additional data about list members for debugging.
297                 boolean iterateThroughList = true;
298                 if (iterateThroughList && logger.isDebugEnabled()) {
299                         AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
300                                         testName);
301                 }
302
303         }
304
305         // Failure outcomes
306         // None at present.
307
308         // ---------------------------------------------------------------
309         // CRUD tests : UPDATE tests
310         // ---------------------------------------------------------------
311
312         // Success outcomes
313
314         /*
315          * (non-Javadoc)
316          * 
317          * @see
318          * org.collectionspace.services.client.test.AbstractServiceTestImpl#update
319          * (java.lang.String)
320          */
321         @Override
322         // @Test(dataProvider = "testName", dataProviderClass =
323         // AbstractServiceTestImpl.class,
324         // dependsOnMethods = {"read"})
325         public void update(String testName) throws Exception {
326                 // Perform setup.
327                 setupRead();
328
329                 // Retrieve the contents of a resource to update.
330                 PublicItemClient client = new PublicItemClient();
331                 ClientResponse<String> res = client.read(knownResourceId);
332                 PoxPayloadIn input = null;
333                 try {
334                         assertStatusCode(res, testName);
335                         input = new PoxPayloadIn(res.getEntity());
336                         if (logger.isDebugEnabled()) {
337                                 logger.debug("got object to update with ID: " + knownResourceId);
338                         }
339                 } finally {
340                         if (res != null) {
341                                 res.releaseConnection();
342                         }
343                 }
344
345                 // Extract the common part from the response.
346                 PayloadInputPart payloadInputPart = input.getPart(client
347                                 .getCommonPartName());
348                 PublicitemsCommon publicItemsCommon = null;
349                 if (payloadInputPart != null) {
350                         publicItemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
351                 }
352                 Assert.assertNotNull(publicItemsCommon);
353
354                 // Update the content of this resource.
355                 publicItemsCommon.setItemNumber("updated-"
356                                 + publicItemsCommon.getItemNumber());
357                 publicItemsCommon.setContentCreationJobId("updated-" + publicItemsCommon.getContentCreationJobId());
358                 if (logger.isDebugEnabled()) {
359                         logger.debug("to be updated object");
360                         logger.debug(objectAsXmlString(publicItemsCommon, PublicitemsCommon.class));
361                 }
362
363                 setupUpdate();
364
365                 // Submit the updated common part in an update request to the service
366                 // and store the response.
367                 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
368                 PayloadOutputPart commonPart = output.addPart(
369                                 client.getCommonPartName(), publicItemsCommon);
370                 res = client.update(knownResourceId, output);
371                 try {
372                         assertStatusCode(res, testName);
373                         int statusCode = res.getStatus();
374                         // Check the status code of the response: does it match the expected
375                         // response(s)?
376                         if (logger.isDebugEnabled()) {
377                                 logger.debug(testName + ": status = " + statusCode);
378                         }
379                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
380                                         invalidStatusCodeMessage(testRequestType, statusCode));
381                         Assert.assertEquals(statusCode, testExpectedStatusCode);
382                         input = new PoxPayloadIn(res.getEntity());
383                 } finally {
384                         if (res != null) {
385                                 res.releaseConnection();
386                         }
387                 }
388
389                 // Extract the updated common part from the response.
390                 payloadInputPart = input.getPart(client.getCommonPartName());
391                 PublicitemsCommon updatedPublicitemsCommon = null;
392                 if (payloadInputPart != null) {
393                         updatedPublicitemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
394                 }
395                 Assert.assertNotNull(updatedPublicitemsCommon);
396
397                 // Check selected fields in the updated common part.
398                 Assert.assertEquals(updatedPublicitemsCommon.getItemNumber(),
399                                 publicItemsCommon.getItemNumber(),
400                                 "Data in updated object did not match submitted data.");
401
402                 if (logger.isDebugEnabled()) {
403                         logger.debug("UTF-8 data sent=" + publicItemsCommon.getContentCreationJobId()
404                                         + "\n" + "UTF-8 data received="
405                                         + updatedPublicitemsCommon.getContentCreationJobId());
406                 }
407                 Assert.assertTrue(updatedPublicitemsCommon.getContentSource().contains(
408                                                 getUTF8DataFragment()), "UTF-8 data retrieved '"
409                                                 + updatedPublicitemsCommon.getContentSource()
410                                                 + "' does not contain expected data '"
411                                                 + getUTF8DataFragment());
412                 Assert.assertEquals(updatedPublicitemsCommon.getContentCreationJobId(),
413                                 publicItemsCommon.getContentCreationJobId(),
414                                 "Data in updated object did not match submitted data.");
415         }
416
417         @Override
418         // @Test(dataProvider = "testName", dataProviderClass =
419         // AbstractServiceTestImpl.class,
420         // dependsOnMethods = {"update", "testSubmitRequest"})
421         public void updateNonExistent(String testName) throws Exception {
422                 // Perform setup.
423                 setupUpdateNonExistent();
424
425                 // Submit the request to the service and store the response.
426                 // Note: The ID used in this 'create' call may be arbitrary.
427                 // The only relevant ID may be the one used in update(), below.
428                 PublicItemClient client = new PublicItemClient();
429                 PoxPayloadOut multipart = createPublicItemInstance(NON_EXISTENT_ID);
430                 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
431                 try {
432                         int statusCode = res.getStatus();
433
434                         // Check the status code of the response: does it match
435                         // the expected response(s)?
436                         if (logger.isDebugEnabled()) {
437                                 logger.debug(testName + ": status = " + statusCode);
438                         }
439                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
440                                         invalidStatusCodeMessage(testRequestType, statusCode));
441                         Assert.assertEquals(statusCode, testExpectedStatusCode);
442                 } finally {
443                         if (res != null) {
444                                 res.releaseConnection();
445                         }
446                 }
447         }
448
449         // ---------------------------------------------------------------
450         // CRUD tests : DELETE tests
451         // ---------------------------------------------------------------
452
453         // Success outcomes
454
455         /*
456          * (non-Javadoc)
457          * 
458          * @see
459          * org.collectionspace.services.client.test.AbstractServiceTestImpl#delete
460          * (java.lang.String)
461          */
462         @Override
463         // @Test(dataProvider = "testName", dataProviderClass =
464         // AbstractServiceTestImpl.class,
465         // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
466         public void delete(String testName) throws Exception {
467                 // Perform setup.
468                 setupDelete();
469
470                 // Submit the request to the service and store the response.
471                 PublicItemClient client = new PublicItemClient();
472                 Response res = client.delete(knownResourceId);
473                 try {
474                         int statusCode = res.getStatus();
475
476                         // Check the status code of the response: does it match
477                         // the expected response(s)?
478                         if (logger.isDebugEnabled()) {
479                                 logger.debug(testName + ": status = " + statusCode);
480                         }
481                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
482                                         invalidStatusCodeMessage(testRequestType, statusCode));
483                         Assert.assertEquals(statusCode, testExpectedStatusCode);
484                 } finally {
485                         if (res != null) {
486                                 res.close();
487                         }
488                 }
489         }
490
491         // Failure outcomes
492
493         /*
494          * (non-Javadoc)
495          * 
496          * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
497          * deleteNonExistent(java.lang.String)
498          */
499         @Override
500         // @Test(dataProvider = "testName", dataProviderClass =
501         // AbstractServiceTestImpl.class,
502         // dependsOnMethods = {"delete"})
503         public void deleteNonExistent(String testName) throws Exception {
504                 // Perform setup.
505                 setupDeleteNonExistent();
506
507                 // Submit the request to the service and store the response.
508                 PublicItemClient client = new PublicItemClient();
509                 Response res = client.delete(NON_EXISTENT_ID);
510                 try {
511                         int statusCode = res.getStatus();
512
513                         // Check the status code of the response: does it match
514                         // the expected response(s)?
515                         if (logger.isDebugEnabled()) {
516                                 logger.debug(testName + ": status = " + statusCode);
517                         }
518                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
519                                         invalidStatusCodeMessage(testRequestType, statusCode));
520                         Assert.assertEquals(statusCode, testExpectedStatusCode);
521                 } finally {
522                         if (res != null) {
523                                 res.close();
524                         }
525                 }
526         }
527
528         // ---------------------------------------------------------------
529         // Utility tests : tests of code used in tests above
530         // ---------------------------------------------------------------
531
532         /**
533          * Tests the code for manually submitting data that is used by several of
534          * the methods above.
535          */
536         // @Test(dependsOnMethods = {"create", "read"})
537         public void testSubmitRequest() {
538
539                 // Expected status code: 200 OK
540                 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
541
542                 // Submit the request to the service and store the response.
543                 String method = ServiceRequestType.READ.httpMethodName();
544                 String url = getResourceURL(knownResourceId);
545                 int statusCode = submitRequest(method, url);
546
547                 // Check the status code of the response: does it match
548                 // the expected response(s)?
549                 if (logger.isDebugEnabled()) {
550                         logger.debug("testSubmitRequest: url=" + url + " status="
551                                         + statusCode);
552                 }
553                 Assert.assertEquals(statusCode, EXPECTED_STATUS);
554
555         }
556
557         // ---------------------------------------------------------------
558         // Utility methods used by tests above
559         // ---------------------------------------------------------------
560
561         @Override
562         public String getServiceName() {
563                 return SERVICE_NAME;
564         }
565
566         /*
567          * (non-Javadoc)
568          * 
569          * @see org.collectionspace.services.client.test.BaseServiceTest#
570          * getServicePathComponent()
571          */
572         @Override
573         public String getServicePathComponent() {
574                 return SERVICE_PATH_COMPONENT;
575         }
576
577         @Override
578         protected PoxPayloadOut createInstance(String identifier) {
579                 return createPublicItemInstance(identifier);
580         }
581
582         /**
583          * Creates the publicitem instance.
584          * 
585          * @param identifier
586          *            the identifier
587          * @return the multipart output
588          */
589         private PoxPayloadOut createPublicItemInstance(String identifier) {
590                 return createPublicItemInstance("itemNumber-" + identifier,
591                                 "contentCreationJobId-" + identifier);
592         }
593
594         /**
595          * Creates the PublicItem instance.
596          * 
597          * @param itemNumber
598          *            the publicitem number
599          * @param contentCreationJobId
600          *            the publicitem asynch job ID
601          * @return the multipart output
602          */
603         private PoxPayloadOut createPublicItemInstance(String itemNumber,
604                         String itemJobId) {
605
606                 PublicitemsCommon publicItemsCommon = new PublicitemsCommon();
607                 publicItemsCommon.setItemNumber(itemNumber);
608                 publicItemsCommon.setContentName("Inventory report-" + itemNumber);
609                 publicItemsCommon.setContentId("42640780-82eb-4650-8a70");
610                 publicItemsCommon.setContentUri("publicitems/1/7eaf0780-9eeb-af50-9d76/content");
611                 publicItemsCommon.setContentCreationJobId(itemJobId);
612                 publicItemsCommon.setContentSource(getUTF8DataFragment());
613                 try {
614                         XMLGregorianCalendar expirationDate = 
615                                         DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2013, 12, 31, 0);
616                         publicItemsCommon.setContentExpirationDate(expirationDate);
617                 } catch (DatatypeConfigurationException e) {
618                         // TODO Auto-generated catch block
619                         e.printStackTrace();
620                 }
621                 publicItemsCommon.setContentAccessedCount(new BigInteger("3"));
622                 publicItemsCommon.setContentAccessCountLimit(new BigInteger("5"));
623
624                 PoxPayloadOut multipart = new PoxPayloadOut(
625                                 this.getServicePathComponent());
626                 PayloadOutputPart commonPart = multipart.addPart(
627                                 new PublicItemClient().getCommonPartName(), publicItemsCommon);
628
629                 if (logger.isDebugEnabled()) {
630                         logger.debug("To be created, publicitem common:");
631                         logger.debug(objectAsXmlString(publicItemsCommon, PublicitemsCommon.class));
632                 }
633
634                 return multipart;
635         }
636
637         @Override
638         public void CRUDTests(String testName) {
639                 // TODO Auto-generated method stub
640
641         }
642
643         @Override
644         protected PoxPayloadOut createInstance(String commonPartName,
645                         String identifier) {
646                 PoxPayloadOut result = createPublicItemInstance(identifier);
647                 return result;
648         }
649
650         @Override
651         protected PublicitemsCommon updateInstance(PublicitemsCommon commonPartObject) {
652                 // TODO Auto-generated method stub
653                 return null;
654         }
655
656         @Override
657         protected void compareUpdatedInstances(PublicitemsCommon original,
658                         PublicitemsCommon updated) throws Exception {
659                 // TODO Auto-generated method stub
660
661         }
662 }