]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e7861f0c41003755f11570ae9784faab49bfbde9
[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(
405                                 updatedArticleCommon.getArticleJobId().contains(
406                                                 getUTF8DataFragment()), "UTF-8 data retrieved '"
407                                                 + updatedArticleCommon.getArticleJobId()
408                                                 + "' does not contain expected data '"
409                                                 + getUTF8DataFragment());
410                 Assert.assertEquals(updatedArticleCommon.getArticleJobId(),
411                                 articlesCommon.getArticleJobId(),
412                                 "Data in updated object did not match submitted data.");
413         }
414
415         @Override
416         // @Test(dataProvider = "testName", dataProviderClass =
417         // AbstractServiceTestImpl.class,
418         // dependsOnMethods = {"update", "testSubmitRequest"})
419         public void updateNonExistent(String testName) throws Exception {
420                 // Perform setup.
421                 setupUpdateNonExistent();
422
423                 // Submit the request to the service and store the response.
424                 // Note: The ID used in this 'create' call may be arbitrary.
425                 // The only relevant ID may be the one used in update(), below.
426                 ArticleClient client = new ArticleClient();
427                 PoxPayloadOut multipart = createArticleInstance(NON_EXISTENT_ID);
428                 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
429                 try {
430                         int statusCode = res.getStatus();
431
432                         // Check the status code of the response: does it match
433                         // the expected response(s)?
434                         if (logger.isDebugEnabled()) {
435                                 logger.debug(testName + ": status = " + statusCode);
436                         }
437                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
438                                         invalidStatusCodeMessage(testRequestType, statusCode));
439                         Assert.assertEquals(statusCode, testExpectedStatusCode);
440                 } finally {
441                         if (res != null) {
442                                 res.releaseConnection();
443                         }
444                 }
445         }
446
447         // ---------------------------------------------------------------
448         // CRUD tests : DELETE tests
449         // ---------------------------------------------------------------
450
451         // Success outcomes
452
453         /*
454          * (non-Javadoc)
455          * 
456          * @see
457          * org.collectionspace.services.client.test.AbstractServiceTestImpl#delete
458          * (java.lang.String)
459          */
460         @Override
461         // @Test(dataProvider = "testName", dataProviderClass =
462         // AbstractServiceTestImpl.class,
463         // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
464         public void delete(String testName) throws Exception {
465                 // Perform setup.
466                 setupDelete();
467
468                 // Submit the request to the service and store the response.
469                 ArticleClient client = new ArticleClient();
470                 ClientResponse<Response> res = client.delete(knownResourceId);
471                 try {
472                         int statusCode = res.getStatus();
473
474                         // Check the status code of the response: does it match
475                         // the expected response(s)?
476                         if (logger.isDebugEnabled()) {
477                                 logger.debug(testName + ": status = " + statusCode);
478                         }
479                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
480                                         invalidStatusCodeMessage(testRequestType, statusCode));
481                         Assert.assertEquals(statusCode, testExpectedStatusCode);
482                 } finally {
483                         if (res != null) {
484                                 res.releaseConnection();
485                         }
486                 }
487         }
488
489         // Failure outcomes
490
491         /*
492          * (non-Javadoc)
493          * 
494          * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
495          * deleteNonExistent(java.lang.String)
496          */
497         @Override
498         // @Test(dataProvider = "testName", dataProviderClass =
499         // AbstractServiceTestImpl.class,
500         // dependsOnMethods = {"delete"})
501         public void deleteNonExistent(String testName) throws Exception {
502                 // Perform setup.
503                 setupDeleteNonExistent();
504
505                 // Submit the request to the service and store the response.
506                 ArticleClient client = new ArticleClient();
507                 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
508                 try {
509                         int statusCode = res.getStatus();
510
511                         // Check the status code of the response: does it match
512                         // the expected response(s)?
513                         if (logger.isDebugEnabled()) {
514                                 logger.debug(testName + ": status = " + statusCode);
515                         }
516                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
517                                         invalidStatusCodeMessage(testRequestType, statusCode));
518                         Assert.assertEquals(statusCode, testExpectedStatusCode);
519                 } finally {
520                         if (res != null) {
521                                 res.releaseConnection();
522                         }
523                 }
524         }
525
526         // ---------------------------------------------------------------
527         // Utility tests : tests of code used in tests above
528         // ---------------------------------------------------------------
529
530         /**
531          * Tests the code for manually submitting data that is used by several of
532          * the methods above.
533          */
534         // @Test(dependsOnMethods = {"create", "read"})
535         public void testSubmitRequest() {
536
537                 // Expected status code: 200 OK
538                 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
539
540                 // Submit the request to the service and store the response.
541                 String method = ServiceRequestType.READ.httpMethodName();
542                 String url = getResourceURL(knownResourceId);
543                 int statusCode = submitRequest(method, url);
544
545                 // Check the status code of the response: does it match
546                 // the expected response(s)?
547                 if (logger.isDebugEnabled()) {
548                         logger.debug("testSubmitRequest: url=" + url + " status="
549                                         + statusCode);
550                 }
551                 Assert.assertEquals(statusCode, EXPECTED_STATUS);
552
553         }
554
555         // ---------------------------------------------------------------
556         // Utility methods used by tests above
557         // ---------------------------------------------------------------
558
559         @Override
560         public String getServiceName() {
561                 return SERVICE_NAME;
562         }
563
564         /*
565          * (non-Javadoc)
566          * 
567          * @see org.collectionspace.services.client.test.BaseServiceTest#
568          * getServicePathComponent()
569          */
570         @Override
571         public String getServicePathComponent() {
572                 return SERVICE_PATH_COMPONENT;
573         }
574
575         @Override
576         protected PoxPayloadOut createInstance(String identifier) {
577                 return createArticleInstance(identifier);
578         }
579
580         /**
581          * Creates the article instance.
582          * 
583          * @param identifier
584          *            the identifier
585          * @return the multipart output
586          */
587         private PoxPayloadOut createArticleInstance(String identifier) {
588                 return createArticleInstance("articleNumber-" + identifier,
589                                 "articleJobId-" + identifier);
590         }
591
592         /**
593          * Creates the Article instance.
594          * 
595          * @param articleNumber
596          *            the article number
597          * @param articleJobId
598          *            the article asynch job ID
599          * @return the multipart output
600          */
601         private PoxPayloadOut createArticleInstance(String articleNumber,
602                         String articleJobId) {
603
604                 ArticlesCommon articlesCommon = new ArticlesCommon();
605                 articlesCommon.setArticleNumber(articleNumber);
606                 articlesCommon.setArticleJobId(articleJobId);
607
608                 PoxPayloadOut multipart = new PoxPayloadOut(
609                                 this.getServicePathComponent());
610                 PayloadOutputPart commonPart = multipart.addPart(
611                                 new ArticleClient().getCommonPartName(), articlesCommon);
612
613                 if (logger.isDebugEnabled()) {
614                         logger.debug("To be created, article common:");
615                         logger.debug(objectAsXmlString(articlesCommon, ArticlesCommon.class));
616                 }
617
618                 return multipart;
619         }
620
621         @Override
622         public void CRUDTests(String testName) {
623                 // TODO Auto-generated method stub
624
625         }
626
627         @Override
628         protected PoxPayloadOut createInstance(String commonPartName,
629                         String identifier) {
630                 PoxPayloadOut result = createArticleInstance(identifier);
631                 return result;
632         }
633
634         @Override
635         protected ArticlesCommon updateInstance(ArticlesCommon commonPartObject) {
636                 // TODO Auto-generated method stub
637                 return null;
638         }
639
640         @Override
641         protected void compareUpdatedInstances(ArticlesCommon original,
642                         ArticlesCommon updated) throws Exception {
643                 // TODO Auto-generated method stub
644
645         }
646 }