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