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