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