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 (c)) 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.util.HashMap;
27 import java.util.List;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.OrganizationJAXBSchema;
33 import org.collectionspace.services.client.OrgAuthorityClient;
34 import org.collectionspace.services.client.OrgAuthorityClientUtils;
35 import org.collectionspace.services.organization.OrgauthoritiesCommon;
36 import org.collectionspace.services.organization.OrgauthoritiesCommonList;
37 import org.collectionspace.services.organization.OrganizationsCommon;
38 import org.collectionspace.services.organization.OrganizationsCommonList;
40 import org.jboss.resteasy.client.ClientResponse;
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
43 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
51 * OrgAuthorityServiceTest, carries out tests against a
52 * deployed and running OrgAuthority Service.
54 * $LastChangedRevision: 753 $
55 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
57 public class OrgAuthorityServiceTest extends AbstractServiceTest {
59 private final Logger logger =
60 LoggerFactory.getLogger(OrgAuthorityServiceTest.class);
62 // Instance variables specific to this test.
63 private OrgAuthorityClient client = new OrgAuthorityClient();
64 final String SERVICE_PATH_COMPONENT = "orgauthorities";
65 final String ITEM_SERVICE_PATH_COMPONENT = "items";
66 private String knownResourceId = null;
67 private String lastOrgAuthId = null;
68 private String knownResourceRefName = null;
69 private String knownItemResourceId = null;
70 private int nItemsToCreateInList = 3;
71 private List<String> allResourceIdsCreated = new ArrayList<String>();
72 private Map<String, String> allResourceItemIdsCreated =
73 new HashMap<String, String>();
75 protected String createRefName(String displayName) {
76 return displayName.replaceAll("\\W", "");
79 // ---------------------------------------------------------------
80 // CRUD tests : CREATE tests
81 // ---------------------------------------------------------------
84 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
85 public void create(String testName) throws Exception {
87 // Perform setup, such as initializing the type of service request
88 // (e.g. CREATE, DELETE), its valid and expected status codes, and
89 // its associated HTTP method name (e.g. POST, DELETE).
90 setupCreate(testName);
92 // Submit the request to the service and store the response.
93 String identifier = createIdentifier();
94 String displayName = "displayName-" + identifier;
95 String refName = createRefName(displayName);
96 MultipartOutput multipart =
97 OrgAuthorityClientUtils.createOrgAuthorityInstance(
99 client.getCommonPartName());
100 ClientResponse<Response> res = client.create(multipart);
101 int statusCode = res.getStatus();
103 // Check the status code of the response: does it match
104 // the expected response(s)?
107 // Does it fall within the set of valid status codes?
108 // Does it exactly match the expected status code?
109 if(logger.isDebugEnabled()){
110 logger.debug(testName + ": status = " + statusCode);
112 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
113 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
114 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
116 // Store the refname from the first resource created
117 // for additional tests below.
118 knownResourceRefName = refName;
120 lastOrgAuthId = extractId(res);
121 // Store the ID returned from the first resource created
122 // for additional tests below.
123 if (knownResourceId == null){
124 knownResourceId = lastOrgAuthId;
125 if (logger.isDebugEnabled()) {
126 logger.debug(testName + ": knownResourceId=" + knownResourceId);
129 // Store the IDs from every resource created by tests,
130 // so they can be deleted after tests have been run.
131 allResourceIdsCreated.add(extractId(res));
135 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
136 dependsOnMethods = {"create"})
137 public void createItem(String testName) {
138 setupCreate(testName);
140 knownItemResourceId = createItemInAuthority(lastOrgAuthId);
141 if(logger.isDebugEnabled()){
142 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
146 private String createItemInAuthority(String vcsid) {
148 final String testName = "createItemInAuthority";
149 if(logger.isDebugEnabled()){
150 logger.debug(testName + ":...");
153 // Submit the request to the service and store the response.
154 String identifier = createIdentifier();
155 String refName = createRefName(identifier);
156 Map<String, String> testOrgMap = new HashMap<String,String>();
157 testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Test Org");
158 testOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "The real official test organization");
159 testOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "joe@test.org");
160 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "May 26, 1907");
161 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Anytown, USA");
162 testOrgMap.put(OrganizationJAXBSchema.FUNCTION, "For testing");
163 MultipartOutput multipart =
164 OrgAuthorityClientUtils.createOrganizationInstance(vcsid,
165 refName, testOrgMap, client.getItemCommonPartName() );
166 ClientResponse<Response> res = client.createItem(vcsid, multipart);
167 int statusCode = res.getStatus();
169 // Check the status code of the response: does it match
170 // the expected response(s)?
171 if(logger.isDebugEnabled()){
172 logger.debug(testName + ": status = " + statusCode);
174 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 // Store the ID returned from the first item resource created
179 // for additional tests below.
180 if (knownItemResourceId == null){
181 knownItemResourceId = extractId(res);
182 if (logger.isDebugEnabled()) {
183 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
187 // Store the IDs from any item resources created
188 // by tests, along with the IDs of their parents, so these items
189 // can be deleted after all tests have been run.
191 // Item resource IDs are unique, so these are used as keys;
192 // the non-unique IDs of their parents are stored as associated values.
193 allResourceItemIdsCreated.put(extractId(res), vcsid);
195 return extractId(res);
199 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
200 dependsOnMethods = {"create", "createItem"})
201 public void createList(String testName) throws Exception {
202 for (int i = 0; i < 3; i++) {
204 knownResourceId = lastOrgAuthId;
205 if (logger.isDebugEnabled()) {
206 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
208 // Add nItemsToCreateInList items to each orgauthority
209 for (int j = 0; j < nItemsToCreateInList; j++) {
210 createItem(testName);
216 // Placeholders until the three tests below can be uncommented.
217 // See Issue CSPACE-401.
219 public void createWithEmptyEntityBody(String testName) throws Exception {
223 public void createWithMalformedXml(String testName) throws Exception {
227 public void createWithWrongXmlSchema(String testName) throws Exception {
232 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
233 dependsOnMethods = {"create", "testSubmitRequest"})
234 public void createWithEmptyEntityBody(String testName) throws Exception {
237 setupCreateWithEmptyEntityBody(testName);
239 // Submit the request to the service and store the response.
240 String method = REQUEST_TYPE.httpMethodName();
241 String url = getServiceRootURL();
242 String mediaType = MediaType.APPLICATION_XML;
243 final String entity = "";
244 int statusCode = submitRequest(method, url, mediaType, entity);
246 // Check the status code of the response: does it match
247 // the expected response(s)?
248 if(logger.isDebugEnabled()) {
249 logger.debug(testName + ": url=" + url +
250 " status=" + statusCode);
252 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
253 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
254 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
258 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
259 dependsOnMethods = {"create", "testSubmitRequest"})
260 public void createWithMalformedXml(String testName) throws Exception {
263 setupCreateWithMalformedXml(testName);
265 // Submit the request to the service and store the response.
266 String method = REQUEST_TYPE.httpMethodName();
267 String url = getServiceRootURL();
268 String mediaType = MediaType.APPLICATION_XML;
269 final String entity = MALFORMED_XML_DATA; // Constant from base class.
270 int statusCode = submitRequest(method, url, mediaType, entity);
272 // Check the status code of the response: does it match
273 // the expected response(s)?
274 if(logger.isDebugEnabled()){
275 logger.debug(testName + ": url=" + url +
276 " status=" + statusCode);
278 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
279 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
280 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
284 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
285 dependsOnMethods = {"create", "testSubmitRequest"})
286 public void createWithWrongXmlSchema(String testName) throws Exception {
289 setupCreateWithWrongXmlSchema(testName);
291 // Submit the request to the service and store the response.
292 String method = REQUEST_TYPE.httpMethodName();
293 String url = getServiceRootURL();
294 String mediaType = MediaType.APPLICATION_XML;
295 final String entity = WRONG_XML_SCHEMA_DATA;
296 int statusCode = submitRequest(method, url, mediaType, entity);
298 // Check the status code of the response: does it match
299 // the expected response(s)?
300 if(logger.isDebugEnabled()){
301 logger.debug(testName + ": url=" + url +
302 " status=" + statusCode);
304 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
305 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
306 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
310 // ---------------------------------------------------------------
311 // CRUD tests : READ tests
312 // ---------------------------------------------------------------
315 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
316 dependsOnMethods = {"create"})
317 public void read(String testName) throws Exception {
322 // Submit the request to the service and store the response.
323 ClientResponse<MultipartInput> res = client.read(knownResourceId);
324 int statusCode = res.getStatus();
326 // Check the status code of the response: does it match
327 // the expected response(s)?
328 if(logger.isDebugEnabled()){
329 logger.debug(testName + ": status = " + statusCode);
331 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
332 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
333 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
334 //FIXME: remove the following try catch once Aron fixes signatures
336 MultipartInput input = (MultipartInput) res.getEntity();
337 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
338 client.getCommonPartName(), OrgauthoritiesCommon.class);
339 Assert.assertNotNull(orgAuthority);
340 } catch (Exception e) {
341 throw new RuntimeException(e);
346 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
347 dependsOnMethods = {"read"})
348 public void readByName(String testName) throws Exception {
353 // Submit the request to the service and store the response.
354 ClientResponse<MultipartInput> res = client.read(knownResourceId);
355 int statusCode = res.getStatus();
357 // Check the status code of the response: does it match
358 // the expected response(s)?
359 if(logger.isDebugEnabled()){
360 logger.debug(testName + ": status = " + statusCode);
362 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
363 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
364 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
365 //FIXME: remove the following try catch once Aron fixes signatures
367 MultipartInput input = (MultipartInput) res.getEntity();
368 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
369 client.getCommonPartName(), OrgauthoritiesCommon.class);
370 Assert.assertNotNull(orgAuthority);
371 } catch (Exception e) {
372 throw new RuntimeException(e);
377 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
378 dependsOnMethods = {"createItem", "read"})
379 public void readItem(String testName) throws Exception {
384 // Submit the request to the service and store the response.
385 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
386 int statusCode = res.getStatus();
388 // Check the status code of the response: does it match
389 // the expected response(s)?
390 if(logger.isDebugEnabled()){
391 logger.debug(testName + ": status = " + statusCode);
393 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
394 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
395 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
397 // Check whether we've received a organization.
398 MultipartInput input = (MultipartInput) res.getEntity();
399 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
400 client.getItemCommonPartName(), OrganizationsCommon.class);
401 Assert.assertNotNull(organization);
407 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
408 dependsOnMethods = {"read"})
409 public void readNonExistent(String testName) {
412 setupReadNonExistent(testName);
414 // Submit the request to the service and store the response.
415 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
416 int statusCode = res.getStatus();
418 // Check the status code of the response: does it match
419 // the expected response(s)?
420 if(logger.isDebugEnabled()){
421 logger.debug(testName + ": status = " + statusCode);
423 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
428 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
429 dependsOnMethods = {"readItem", "readNonExistent"})
430 public void readItemNonExistent(String testName) {
433 setupReadNonExistent(testName);
435 // Submit the request to the service and store the response.
436 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
437 int statusCode = res.getStatus();
439 // Check the status code of the response: does it match
440 // the expected response(s)?
441 if(logger.isDebugEnabled()){
442 logger.debug(testName + ": status = " + statusCode);
444 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
445 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
446 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
448 // ---------------------------------------------------------------
449 // CRUD tests : READ_LIST tests
450 // ---------------------------------------------------------------
454 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
455 dependsOnMethods = {"createList", "read"})
456 public void readList(String testName) throws Exception {
459 setupReadList(testName);
461 // Submit the request to the service and store the response.
462 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
463 OrgauthoritiesCommonList list = res.getEntity();
464 int statusCode = res.getStatus();
466 // Check the status code of the response: does it match
467 // the expected response(s)?
468 if(logger.isDebugEnabled()){
469 logger.debug(testName + ": status = " + statusCode);
471 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
472 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
473 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
475 // Optionally output additional data about list members for debugging.
476 boolean iterateThroughList = false;
477 if (iterateThroughList && logger.isDebugEnabled()) {
478 List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
479 list.getOrgauthorityListItem();
481 for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
482 String csid = item.getCsid();
483 logger.debug(testName + ": list-item[" + i + "] csid=" +
485 logger.debug(testName + ": list-item[" + i + "] displayName=" +
486 item.getDisplayName());
487 logger.debug(testName + ": list-item[" + i + "] URI=" +
495 @Test(dependsOnMethods = {"createList", "readItem"})
496 public void readItemList() {
497 readItemList(knownResourceId);
500 private void readItemList(String vcsid) {
502 final String testName = "readItemList";
505 setupReadList(testName);
507 // Submit the request to the service and store the response.
508 ClientResponse<OrganizationsCommonList> res =
509 client.readItemList(vcsid);
510 OrganizationsCommonList list = res.getEntity();
511 int statusCode = res.getStatus();
513 // Check the status code of the response: does it match
514 // the expected response(s)?
515 if(logger.isDebugEnabled()){
516 logger.debug(" " + testName + ": status = " + statusCode);
518 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
519 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
520 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
522 List<OrganizationsCommonList.OrganizationListItem> items =
523 list.getOrganizationListItem();
524 int nItemsReturned = items.size();
525 if(logger.isDebugEnabled()){
526 logger.debug(" " + testName + ": Expected "
527 + nItemsToCreateInList+" items; got: "+nItemsReturned);
529 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
531 // Optionally output additional data about list members for debugging.
532 boolean iterateThroughList = false;
533 if (iterateThroughList && logger.isDebugEnabled()) {
535 for (OrganizationsCommonList.OrganizationListItem item : items) {
536 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
538 logger.debug(" " + testName + ": list-item[" + i + "] shortName=" +
539 item.getDisplayName());
540 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
549 // ---------------------------------------------------------------
550 // CRUD tests : UPDATE tests
551 // ---------------------------------------------------------------
554 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
555 dependsOnMethods = {"read"})
556 public void update(String testName) throws Exception {
559 setupUpdate(testName);
561 // Retrieve the contents of a resource to update.
562 ClientResponse<MultipartInput> res =
563 client.read(knownResourceId);
564 if(logger.isDebugEnabled()){
565 logger.debug(testName + ": read status = " + res.getStatus());
567 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
569 if(logger.isDebugEnabled()){
570 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
572 MultipartInput input = (MultipartInput) res.getEntity();
573 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
574 client.getCommonPartName(), OrgauthoritiesCommon.class);
575 Assert.assertNotNull(orgAuthority);
577 // Update the contents of this resource.
578 orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
579 orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
580 if(logger.isDebugEnabled()){
581 logger.debug("to be updated OrgAuthority");
582 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
585 // Submit the updated resource to the service and store the response.
586 MultipartOutput output = new MultipartOutput();
587 OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
588 commonPart.getHeaders().add("label", client.getCommonPartName());
589 res = client.update(knownResourceId, output);
590 int statusCode = res.getStatus();
592 // Check the status code of the response: does it match the expected response(s)?
593 if(logger.isDebugEnabled()){
594 logger.debug("update: status = " + statusCode);
596 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
597 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
598 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
600 // Retrieve the updated resource and verify that its contents exist.
601 input = (MultipartInput) res.getEntity();
602 OrgauthoritiesCommon updatedOrgAuthority =
603 (OrgauthoritiesCommon) extractPart(input,
604 client.getCommonPartName(), OrgauthoritiesCommon.class);
605 Assert.assertNotNull(updatedOrgAuthority);
607 // Verify that the updated resource received the correct data.
608 Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
609 orgAuthority.getDisplayName(),
610 "Data in updated object did not match submitted data.");
613 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
614 dependsOnMethods = {"readItem", "update"})
615 public void updateItem(String testName) throws Exception {
618 setupUpdate(testName);
620 ClientResponse<MultipartInput> res =
621 client.readItem(knownResourceId, knownItemResourceId);
622 if(logger.isDebugEnabled()){
623 logger.debug(testName + ": read status = " + res.getStatus());
625 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
627 if(logger.isDebugEnabled()){
628 logger.debug("got Organization to update with ID: " +
629 knownItemResourceId +
630 " in OrgAuthority: " + knownResourceId );
632 MultipartInput input = (MultipartInput) res.getEntity();
633 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
634 client.getItemCommonPartName(), OrganizationsCommon.class);
635 Assert.assertNotNull(organization);
637 // Update the contents of this resource.
638 organization.setShortName("updated-" + organization.getShortName());
639 if(logger.isDebugEnabled()){
640 logger.debug("to be updated Organization");
641 logger.debug(objectAsXmlString(organization,
642 OrganizationsCommon.class));
645 // Submit the updated resource to the service and store the response.
646 MultipartOutput output = new MultipartOutput();
647 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
648 commonPart.getHeaders().add("label", client.getItemCommonPartName());
649 res = client.updateItem(knownResourceId, knownItemResourceId, output);
650 int statusCode = res.getStatus();
652 // Check the status code of the response: does it match the expected response(s)?
653 if(logger.isDebugEnabled()){
654 logger.debug("updateItem: status = " + statusCode);
656 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
657 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
658 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
660 // Retrieve the updated resource and verify that its contents exist.
661 input = (MultipartInput) res.getEntity();
662 OrganizationsCommon updatedOrganization =
663 (OrganizationsCommon) extractPart(input,
664 client.getItemCommonPartName(), OrganizationsCommon.class);
665 Assert.assertNotNull(updatedOrganization);
667 // Verify that the updated resource received the correct data.
668 Assert.assertEquals(updatedOrganization.getShortName(),
669 organization.getShortName(),
670 "Data in updated Organization did not match submitted data.");
674 // Placeholders until the three tests below can be uncommented.
675 // See Issue CSPACE-401.
677 public void updateWithEmptyEntityBody(String testName) throws Exception {
681 public void updateWithMalformedXml(String testName) throws Exception {
685 public void updateWithWrongXmlSchema(String testName) throws Exception {
690 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
691 dependsOnMethods = {"create", "update", "testSubmitRequest"})
692 public void updateWithEmptyEntityBody(String testName) throws Exception {
695 setupUpdateWithEmptyEntityBody(testName);
697 // Submit the request to the service and store the response.
698 String method = REQUEST_TYPE.httpMethodName();
699 String url = getResourceURL(knownResourceId);
700 String mediaType = MediaType.APPLICATION_XML;
701 final String entity = "";
702 int statusCode = submitRequest(method, url, mediaType, entity);
704 // Check the status code of the response: does it match
705 // the expected response(s)?
706 if(logger.isDebugEnabled()){
707 logger.debug(testName + ": url=" + url +
708 " status=" + statusCode);
710 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
711 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
712 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
716 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
717 dependsOnMethods = {"create", "update", "testSubmitRequest"})
718 public void updateWithMalformedXml(String testName) throws Exception {
721 setupUpdateWithMalformedXml(testName);
723 // Submit the request to the service and store the response.
724 String method = REQUEST_TYPE.httpMethodName();
725 String url = getResourceURL(knownResourceId);
726 String mediaType = MediaType.APPLICATION_XML;
727 final String entity = MALFORMED_XML_DATA;
728 int statusCode = submitRequest(method, url, mediaType, entity);
730 // Check the status code of the response: does it match
731 // the expected response(s)?
732 if(logger.isDebugEnabled()){
733 logger.debug(testName + ": url=" + url +
734 " status=" + statusCode);
736 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
737 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
738 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
742 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
743 dependsOnMethods = {"create", "update", "testSubmitRequest"})
744 public void updateWithWrongXmlSchema(String testName) throws Exception {
747 setupUpdateWithWrongXmlSchema(testName);
749 // Submit the request to the service and store the response.
750 String method = REQUEST_TYPE.httpMethodName();
751 String url = getResourceURL(knownResourceId);
752 String mediaType = MediaType.APPLICATION_XML;
753 final String entity = WRONG_XML_SCHEMA_DATA;
754 int statusCode = submitRequest(method, url, mediaType, entity);
756 // Check the status code of the response: does it match
757 // the expected response(s)?
758 if(logger.isDebugEnabled()){
759 logger.debug("updateWithWrongXmlSchema: url=" + url +
760 " status=" + statusCode);
762 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
763 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
764 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
770 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
771 dependsOnMethods = {"update", "testSubmitRequest"})
772 public void updateNonExistent(String testName) throws Exception {
775 setupUpdateNonExistent(testName);
777 // Submit the request to the service and store the response.
778 // Note: The ID used in this 'create' call may be arbitrary.
779 // The only relevant ID may be the one used in update(), below.
781 // The only relevant ID may be the one used in update(), below.
782 MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
783 ClientResponse<MultipartInput> res =
784 client.update(NON_EXISTENT_ID, multipart);
785 int statusCode = res.getStatus();
787 // Check the status code of the response: does it match
788 // the expected response(s)?
789 if(logger.isDebugEnabled()){
790 logger.debug(testName + ": status = " + statusCode);
792 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
793 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
794 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
797 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
798 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
799 public void updateNonExistentItem(String testName) throws Exception {
802 setupUpdateNonExistent(testName);
804 // Submit the request to the service and store the response.
805 // Note: The ID used in this 'create' call may be arbitrary.
806 // The only relevant ID may be the one used in update(), below.
808 // The only relevant ID may be the one used in update(), below.
809 Map<String, String> nonexOrgMap = new HashMap<String,String>();
810 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
811 MultipartOutput multipart =
812 OrgAuthorityClientUtils.createOrganizationInstance(
813 knownResourceId, createRefName(NON_EXISTENT_ID),
814 nonexOrgMap, client.getCommonPartName() );
815 ClientResponse<MultipartInput> res =
816 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
817 int statusCode = res.getStatus();
819 // Check the status code of the response: does it match
820 // the expected response(s)?
821 if(logger.isDebugEnabled()){
822 logger.debug(testName + ": status = " + statusCode);
824 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
825 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
826 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
829 // ---------------------------------------------------------------
830 // CRUD tests : DELETE tests
831 // ---------------------------------------------------------------
834 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
835 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
836 public void delete(String testName) throws Exception {
839 setupDelete(testName);
841 // Submit the request to the service and store the response.
842 ClientResponse<Response> res = client.delete(knownResourceId);
843 int statusCode = res.getStatus();
845 // Check the status code of the response: does it match
846 // the expected response(s)?
847 if(logger.isDebugEnabled()){
848 logger.debug(testName + ": status = " + statusCode);
850 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
851 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
852 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
855 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
856 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
858 public void deleteItem(String testName) throws Exception {
861 setupDelete(testName);
863 // Submit the request to the service and store the response.
864 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
865 int statusCode = res.getStatus();
867 // Check the status code of the response: does it match
868 // the expected response(s)?
869 if(logger.isDebugEnabled()){
870 logger.debug("delete: status = " + statusCode);
872 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
873 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
874 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
879 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
880 dependsOnMethods = {"delete"})
881 public void deleteNonExistent(String testName) throws Exception {
884 setupDeleteNonExistent(testName);
886 // Submit the request to the service and store the response.
887 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
888 int statusCode = res.getStatus();
890 // Check the status code of the response: does it match
891 // the expected response(s)?
892 if(logger.isDebugEnabled()){
893 logger.debug(testName + ": status = " + statusCode);
895 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
896 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
897 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
900 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
901 dependsOnMethods = {"deleteItem"})
902 public void deleteNonExistentItem(String testName) {
905 setupDeleteNonExistent(testName);
907 // Submit the request to the service and store the response.
908 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
909 int statusCode = res.getStatus();
911 // Check the status code of the response: does it match
912 // the expected response(s)?
913 if(logger.isDebugEnabled()){
914 logger.debug(testName + ": status = " + statusCode);
916 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
917 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
918 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
921 // ---------------------------------------------------------------
922 // Utility tests : tests of code used in tests above
923 // ---------------------------------------------------------------
925 * Tests the code for manually submitting data that is used by several
926 * of the methods above.
928 @Test(dependsOnMethods = {"create", "read"})
929 public void testSubmitRequest() {
931 // Expected status code: 200 OK
932 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
934 // Submit the request to the service and store the response.
935 String method = ServiceRequestType.READ.httpMethodName();
936 String url = getResourceURL(knownResourceId);
937 int statusCode = submitRequest(method, url);
939 // Check the status code of the response: does it match
940 // the expected response(s)?
941 if(logger.isDebugEnabled()){
942 logger.debug("testSubmitRequest: url=" + url +
943 " status=" + statusCode);
945 Assert.assertEquals(statusCode, EXPECTED_STATUS);
949 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
950 public void testItemSubmitRequest() {
952 // Expected status code: 200 OK
953 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
955 // Submit the request to the service and store the response.
956 String method = ServiceRequestType.READ.httpMethodName();
957 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
958 int statusCode = submitRequest(method, url);
960 // Check the status code of the response: does it match
961 // the expected response(s)?
962 if(logger.isDebugEnabled()){
963 logger.debug("testItemSubmitRequest: url=" + url +
964 " status=" + statusCode);
966 Assert.assertEquals(statusCode, EXPECTED_STATUS);
970 // ---------------------------------------------------------------
971 // Cleanup of resources created during testing
972 // ---------------------------------------------------------------
975 * Deletes all resources created by tests, after all tests have been run.
977 * This cleanup method will always be run, even if one or more tests fail.
978 * For this reason, it attempts to remove all resources created
979 * at any point during testing, even if some of those resources
980 * may be expected to be deleted by certain tests.
982 @AfterClass(alwaysRun=true)
983 public void cleanUp() {
984 if (logger.isDebugEnabled()) {
985 logger.debug("Cleaning up temporary resources created for testing ...");
987 // Clean up organization resources.
988 String orgAuthorityResourceId;
989 String organizationResourceId;
990 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
991 organizationResourceId = entry.getKey();
992 orgAuthorityResourceId = entry.getValue();
993 // Note: Any non-success responses are ignored and not reported.
994 ClientResponse<Response> res =
995 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
997 // Clean up orgAuthority resources.
998 for (String resourceId : allResourceIdsCreated) {
999 // Note: Any non-success responses are ignored and not reported.
1000 ClientResponse<Response> res = client.delete(resourceId);
1004 // ---------------------------------------------------------------
1005 // Utility methods used by tests above
1006 // ---------------------------------------------------------------
1008 public String getServicePathComponent() {
1009 return SERVICE_PATH_COMPONENT;
1012 public String getItemServicePathComponent() {
1013 return ITEM_SERVICE_PATH_COMPONENT;
1017 * Returns the root URL for a service.
1019 * This URL consists of a base URL for all services, followed by
1020 * a path component for the owning orgAuthority, followed by the
1021 * path component for the items.
1023 * @return The root URL for a service.
1025 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1026 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1030 * Returns the URL of a specific resource managed by a service, and
1031 * designated by an identifier (such as a universally unique ID, or UUID).
1033 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1035 * @return The URL of a specific resource managed by a service.
1037 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1038 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1041 private MultipartOutput createOrgAuthorityInstance(String identifier) {
1042 String displayName = "displayName-" + identifier;
1043 String refName = createRefName(displayName);
1044 return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1045 displayName, refName,
1046 client.getCommonPartName());