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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 //import java.util.ArrayList;
26 import java.math.BigInteger;
27 import java.util.Date;
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;
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;
45 import org.jboss.resteasy.client.ClientResponse;
46 import org.testng.Assert;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * ArticleServiceTest, carries out tests against a deployed and running Articles
55 * $LastChangedRevision$ $LastChangedDate$
57 public class ArticleServiceTest extends
58 AbstractPoxServiceTestImpl<AbstractCommonList, ArticlesCommon> {
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";
72 * org.collectionspace.services.client.test.BaseServiceTest#getClientInstance
76 protected ArticleClient getClientInstance() {
77 return new ArticleClient();
83 * @see org.collectionspace.services.client.test.BaseServiceTest#
84 * getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
87 protected AbstractCommonList getCommonList(
88 ClientResponse<AbstractCommonList> response) {
89 return response.getEntity(AbstractCommonList.class);
92 // ---------------------------------------------------------------
93 // CRUD tests : CREATE tests
94 // ---------------------------------------------------------------
102 * org.collectionspace.services.client.test.ServiceTest#create(java.lang
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).
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);
119 ClientResponse<Response> res = client.create(multipart);
121 int statusCode = res.getStatus();
123 // Check the status code of the response: does it match
124 // the expected response(s)?
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);
132 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
133 invalidStatusCodeMessage(testRequestType, statusCode));
134 Assert.assertEquals(statusCode, testExpectedStatusCode);
136 newID = extractId(res);
139 res.releaseConnection();
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);
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);
161 * org.collectionspace.services.client.test.AbstractServiceTestImpl#createList
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++) {
174 // ---------------------------------------------------------------
175 // CRUD tests : READ tests
176 // ---------------------------------------------------------------
182 * org.collectionspace.services.client.test.AbstractServiceTestImpl#read
186 // @Test(dataProvider = "testName", dataProviderClass =
187 // AbstractServiceTestImpl.class,
188 // dependsOnMethods = {"create"})
189 public void read(String testName) throws Exception {
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;
198 assertStatusCode(res, testName);
199 input = new PoxPayloadIn(res.getEntity());
202 res.releaseConnection();
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();
213 Assert.assertNotNull(articlesCommon);
222 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
223 * readNonExistent(java.lang.String)
226 // @Test(dataProvider = "testName", dataProviderClass =
227 // AbstractServiceTestImpl.class,
228 // dependsOnMethods = {"read"})
229 public void readNonExistent(String testName) throws Exception {
231 setupReadNonExistent();
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);
237 int statusCode = res.getStatus();
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);
244 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
245 invalidStatusCodeMessage(testRequestType, statusCode));
246 Assert.assertEquals(statusCode, testExpectedStatusCode);
249 res.releaseConnection();
254 // ---------------------------------------------------------------
255 // CRUD tests : READ_LIST tests
256 // ---------------------------------------------------------------
264 * org.collectionspace.services.client.test.AbstractServiceTestImpl#readList
268 // @Test(dataProvider = "testName", dataProviderClass =
269 // AbstractServiceTestImpl.class,
270 // dependsOnMethods = {"createList", "read"})
271 public void readList(String testName) throws Exception {
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);
281 int statusCode = res.getStatus();
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);
288 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
289 invalidStatusCodeMessage(testRequestType, statusCode));
290 Assert.assertEquals(statusCode, testExpectedStatusCode);
292 list = res.getEntity();
295 res.releaseConnection();
299 // Optionally output additional data about list members for debugging.
300 boolean iterateThroughList = true;
301 if (iterateThroughList && logger.isDebugEnabled()) {
302 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
311 // ---------------------------------------------------------------
312 // CRUD tests : UPDATE tests
313 // ---------------------------------------------------------------
321 * org.collectionspace.services.client.test.AbstractServiceTestImpl#update
325 // @Test(dataProvider = "testName", dataProviderClass =
326 // AbstractServiceTestImpl.class,
327 // dependsOnMethods = {"read"})
328 public void update(String testName) throws Exception {
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;
337 assertStatusCode(res, testName);
338 input = new PoxPayloadIn(res.getEntity());
339 if (logger.isDebugEnabled()) {
340 logger.debug("got object to update with ID: " + knownResourceId);
344 res.releaseConnection();
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();
355 Assert.assertNotNull(articlesCommon);
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));
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);
375 assertStatusCode(res, testName);
376 int statusCode = res.getStatus();
377 // Check the status code of the response: does it match the expected
379 if (logger.isDebugEnabled()) {
380 logger.debug(testName + ": status = " + statusCode);
382 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
383 invalidStatusCodeMessage(testRequestType, statusCode));
384 Assert.assertEquals(statusCode, testExpectedStatusCode);
385 input = new PoxPayloadIn(res.getEntity());
388 res.releaseConnection();
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();
398 Assert.assertNotNull(updatedArticleCommon);
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.");
405 if (logger.isDebugEnabled()) {
406 logger.debug("UTF-8 data sent=" + articlesCommon.getArticleJobId()
407 + "\n" + "UTF-8 data received="
408 + updatedArticleCommon.getArticleJobId());
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.");
421 // @Test(dataProvider = "testName", dataProviderClass =
422 // AbstractServiceTestImpl.class,
423 // dependsOnMethods = {"update", "testSubmitRequest"})
424 public void updateNonExistent(String testName) throws Exception {
426 setupUpdateNonExistent();
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);
435 int statusCode = res.getStatus();
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);
442 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
443 invalidStatusCodeMessage(testRequestType, statusCode));
444 Assert.assertEquals(statusCode, testExpectedStatusCode);
447 res.releaseConnection();
452 // ---------------------------------------------------------------
453 // CRUD tests : DELETE tests
454 // ---------------------------------------------------------------
462 * org.collectionspace.services.client.test.AbstractServiceTestImpl#delete
466 // @Test(dataProvider = "testName", dataProviderClass =
467 // AbstractServiceTestImpl.class,
468 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
469 public void delete(String testName) throws Exception {
473 // Submit the request to the service and store the response.
474 ArticleClient client = new ArticleClient();
475 ClientResponse<Response> res = client.delete(knownResourceId);
477 int statusCode = res.getStatus();
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);
484 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
485 invalidStatusCodeMessage(testRequestType, statusCode));
486 Assert.assertEquals(statusCode, testExpectedStatusCode);
489 res.releaseConnection();
499 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
500 * deleteNonExistent(java.lang.String)
503 // @Test(dataProvider = "testName", dataProviderClass =
504 // AbstractServiceTestImpl.class,
505 // dependsOnMethods = {"delete"})
506 public void deleteNonExistent(String testName) throws Exception {
508 setupDeleteNonExistent();
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);
514 int statusCode = res.getStatus();
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);
521 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
522 invalidStatusCodeMessage(testRequestType, statusCode));
523 Assert.assertEquals(statusCode, testExpectedStatusCode);
526 res.releaseConnection();
531 // ---------------------------------------------------------------
532 // Utility tests : tests of code used in tests above
533 // ---------------------------------------------------------------
536 * Tests the code for manually submitting data that is used by several of
539 // @Test(dependsOnMethods = {"create", "read"})
540 public void testSubmitRequest() {
542 // Expected status code: 200 OK
543 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
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);
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="
556 Assert.assertEquals(statusCode, EXPECTED_STATUS);
560 // ---------------------------------------------------------------
561 // Utility methods used by tests above
562 // ---------------------------------------------------------------
565 public String getServiceName() {
572 * @see org.collectionspace.services.client.test.BaseServiceTest#
573 * getServicePathComponent()
576 public String getServicePathComponent() {
577 return SERVICE_PATH_COMPONENT;
581 protected PoxPayloadOut createInstance(String identifier) {
582 return createArticleInstance(identifier);
586 * Creates the article instance.
590 * @return the multipart output
592 private PoxPayloadOut createArticleInstance(String identifier) {
593 return createArticleInstance("articleNumber-" + identifier,
594 "articleJobId-" + identifier);
598 * Creates the Article instance.
600 * @param articleNumber
602 * @param articleJobId
603 * the article asynch job ID
604 * @return the multipart output
606 private PoxPayloadOut createArticleInstance(String articleNumber,
607 String articleJobId) {
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());
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
624 articlesCommon.setAccessedCount(new BigInteger("3"));
625 articlesCommon.setAccessedCountLimit(new BigInteger("5"));
627 PoxPayloadOut multipart = new PoxPayloadOut(
628 this.getServicePathComponent());
629 PayloadOutputPart commonPart = multipart.addPart(
630 new ArticleClient().getCommonPartName(), articlesCommon);
632 if (logger.isDebugEnabled()) {
633 logger.debug("To be created, article common:");
634 logger.debug(objectAsXmlString(articlesCommon, ArticlesCommon.class));
641 public void CRUDTests(String testName) {
642 // TODO Auto-generated method stub
647 protected PoxPayloadOut createInstance(String commonPartName,
649 PoxPayloadOut result = createArticleInstance(identifier);
654 protected ArticlesCommon updateInstance(ArticlesCommon commonPartObject) {
655 // TODO Auto-generated method stub
660 protected void compareUpdatedInstances(ArticlesCommon original,
661 ArticlesCommon updated) throws Exception {
662 // TODO Auto-generated method stub