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 // ---------------------------------------------------------------
76 // CRUD tests : CREATE tests
77 // ---------------------------------------------------------------
80 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
81 public void create(String testName) throws Exception {
83 // Perform setup, such as initializing the type of service request
84 // (e.g. CREATE, DELETE), its valid and expected status codes, and
85 // its associated HTTP method name (e.g. POST, DELETE).
86 setupCreate(testName);
88 // Submit the request to the service and store the response.
89 String identifier = createIdentifier();
90 String displayName = "displayName-" + identifier;
91 String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
92 MultipartOutput multipart =
93 OrgAuthorityClientUtils.createOrgAuthorityInstance(
95 client.getCommonPartName());
96 ClientResponse<Response> res = client.create(multipart);
97 int statusCode = res.getStatus();
99 // Check the status code of the response: does it match
100 // the expected response(s)?
103 // Does it fall within the set of valid status codes?
104 // Does it exactly match the expected status code?
105 if(logger.isDebugEnabled()){
106 logger.debug(testName + ": status = " + statusCode);
108 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
109 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
110 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
112 // Store the refname from the first resource created
113 // for additional tests below.
114 knownResourceRefName = refName;
116 lastOrgAuthId = extractId(res);
117 // Store the ID returned from the first resource created
118 // for additional tests below.
119 if (knownResourceId == null){
120 knownResourceId = lastOrgAuthId;
121 if (logger.isDebugEnabled()) {
122 logger.debug(testName + ": knownResourceId=" + knownResourceId);
125 // Store the IDs from every resource created by tests,
126 // so they can be deleted after tests have been run.
127 allResourceIdsCreated.add(extractId(res));
131 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
132 dependsOnMethods = {"create"})
133 public void createItem(String testName) {
134 setupCreate(testName);
136 knownItemResourceId = createItemInAuthority(lastOrgAuthId);
137 if(logger.isDebugEnabled()){
138 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
142 private String createItemInAuthority(String vcsid) {
144 final String testName = "createItemInAuthority";
145 if(logger.isDebugEnabled()){
146 logger.debug(testName + ":...");
149 // Submit the request to the service and store the response.
150 String identifier = createIdentifier();
151 String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, identifier, true);
152 Map<String, String> testOrgMap = new HashMap<String,String>();
153 testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Test Org");
154 testOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "The real official test organization");
155 testOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "joe@test.org");
156 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "May 26, 1907");
157 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Anytown, USA");
158 testOrgMap.put(OrganizationJAXBSchema.FUNCTION, "For testing");
159 MultipartOutput multipart =
160 OrgAuthorityClientUtils.createOrganizationInstance(vcsid,
161 refName, testOrgMap, client.getItemCommonPartName() );
162 ClientResponse<Response> res = client.createItem(vcsid, multipart);
163 int statusCode = res.getStatus();
165 // Check the status code of the response: does it match
166 // the expected response(s)?
167 if(logger.isDebugEnabled()){
168 logger.debug(testName + ": status = " + statusCode);
170 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
174 // Store the ID returned from the first item resource created
175 // for additional tests below.
176 if (knownItemResourceId == null){
177 knownItemResourceId = extractId(res);
178 if (logger.isDebugEnabled()) {
179 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
183 // Store the IDs from any item resources created
184 // by tests, along with the IDs of their parents, so these items
185 // can be deleted after all tests have been run.
187 // Item resource IDs are unique, so these are used as keys;
188 // the non-unique IDs of their parents are stored as associated values.
189 allResourceItemIdsCreated.put(extractId(res), vcsid);
191 return extractId(res);
195 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
196 dependsOnMethods = {"create", "createItem"})
197 public void createList(String testName) throws Exception {
198 for (int i = 0; i < 3; i++) {
200 knownResourceId = lastOrgAuthId;
201 if (logger.isDebugEnabled()) {
202 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
204 // Add nItemsToCreateInList items to each orgauthority
205 for (int j = 0; j < nItemsToCreateInList; j++) {
206 createItem(testName);
212 // Placeholders until the three tests below can be uncommented.
213 // See Issue CSPACE-401.
215 public void createWithEmptyEntityBody(String testName) throws Exception {
219 public void createWithMalformedXml(String testName) throws Exception {
223 public void createWithWrongXmlSchema(String testName) throws Exception {
228 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
229 dependsOnMethods = {"create", "testSubmitRequest"})
230 public void createWithEmptyEntityBody(String testName) throws Exception {
233 setupCreateWithEmptyEntityBody(testName);
235 // Submit the request to the service and store the response.
236 String method = REQUEST_TYPE.httpMethodName();
237 String url = getServiceRootURL();
238 String mediaType = MediaType.APPLICATION_XML;
239 final String entity = "";
240 int statusCode = submitRequest(method, url, mediaType, entity);
242 // Check the status code of the response: does it match
243 // the expected response(s)?
244 if(logger.isDebugEnabled()) {
245 logger.debug(testName + ": url=" + url +
246 " status=" + statusCode);
248 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
249 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
250 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
254 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
255 dependsOnMethods = {"create", "testSubmitRequest"})
256 public void createWithMalformedXml(String testName) throws Exception {
259 setupCreateWithMalformedXml(testName);
261 // Submit the request to the service and store the response.
262 String method = REQUEST_TYPE.httpMethodName();
263 String url = getServiceRootURL();
264 String mediaType = MediaType.APPLICATION_XML;
265 final String entity = MALFORMED_XML_DATA; // Constant from base class.
266 int statusCode = submitRequest(method, url, mediaType, entity);
268 // Check the status code of the response: does it match
269 // the expected response(s)?
270 if(logger.isDebugEnabled()){
271 logger.debug(testName + ": url=" + url +
272 " status=" + statusCode);
274 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
275 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
276 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
280 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
281 dependsOnMethods = {"create", "testSubmitRequest"})
282 public void createWithWrongXmlSchema(String testName) throws Exception {
285 setupCreateWithWrongXmlSchema(testName);
287 // Submit the request to the service and store the response.
288 String method = REQUEST_TYPE.httpMethodName();
289 String url = getServiceRootURL();
290 String mediaType = MediaType.APPLICATION_XML;
291 final String entity = WRONG_XML_SCHEMA_DATA;
292 int statusCode = submitRequest(method, url, mediaType, entity);
294 // Check the status code of the response: does it match
295 // the expected response(s)?
296 if(logger.isDebugEnabled()){
297 logger.debug(testName + ": url=" + url +
298 " status=" + statusCode);
300 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
301 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
302 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
306 // ---------------------------------------------------------------
307 // CRUD tests : READ tests
308 // ---------------------------------------------------------------
311 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
312 dependsOnMethods = {"create"})
313 public void read(String testName) throws Exception {
318 // Submit the request to the service and store the response.
319 ClientResponse<MultipartInput> res = client.read(knownResourceId);
320 int statusCode = res.getStatus();
322 // Check the status code of the response: does it match
323 // the expected response(s)?
324 if(logger.isDebugEnabled()){
325 logger.debug(testName + ": status = " + statusCode);
327 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
328 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
329 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
330 //FIXME: remove the following try catch once Aron fixes signatures
332 MultipartInput input = (MultipartInput) res.getEntity();
333 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
334 client.getCommonPartName(), OrgauthoritiesCommon.class);
335 Assert.assertNotNull(orgAuthority);
336 } catch (Exception e) {
337 throw new RuntimeException(e);
342 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
343 dependsOnMethods = {"read"})
344 public void readByName(String testName) throws Exception {
349 // Submit the request to the service and store the response.
350 ClientResponse<MultipartInput> res = client.read(knownResourceId);
351 int statusCode = res.getStatus();
353 // Check the status code of the response: does it match
354 // the expected response(s)?
355 if(logger.isDebugEnabled()){
356 logger.debug(testName + ": status = " + statusCode);
358 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
359 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
360 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
361 //FIXME: remove the following try catch once Aron fixes signatures
363 MultipartInput input = (MultipartInput) res.getEntity();
364 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
365 client.getCommonPartName(), OrgauthoritiesCommon.class);
366 Assert.assertNotNull(orgAuthority);
367 } catch (Exception e) {
368 throw new RuntimeException(e);
373 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
374 dependsOnMethods = {"createItem", "read"})
375 public void readItem(String testName) throws Exception {
380 // Submit the request to the service and store the response.
381 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
382 int statusCode = res.getStatus();
384 // Check the status code of the response: does it match
385 // the expected response(s)?
386 if(logger.isDebugEnabled()){
387 logger.debug(testName + ": status = " + statusCode);
389 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
390 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
391 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
393 // Check whether we've received a organization.
394 MultipartInput input = (MultipartInput) res.getEntity();
395 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
396 client.getItemCommonPartName(), OrganizationsCommon.class);
397 Assert.assertNotNull(organization);
398 boolean showFull = true;
399 if(showFull && logger.isDebugEnabled()){
400 logger.debug(testName + ": returned payload:");
401 logger.debug(objectAsXmlString(organization,
402 OrganizationsCommon.class));
404 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
409 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
410 dependsOnMethods = {"read"})
411 public void readNonExistent(String testName) {
414 setupReadNonExistent(testName);
416 // Submit the request to the service and store the response.
417 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
418 int statusCode = res.getStatus();
420 // Check the status code of the response: does it match
421 // the expected response(s)?
422 if(logger.isDebugEnabled()){
423 logger.debug(testName + ": status = " + statusCode);
425 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
426 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
427 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
430 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
431 dependsOnMethods = {"readItem", "readNonExistent"})
432 public void readItemNonExistent(String testName) {
435 setupReadNonExistent(testName);
437 // Submit the request to the service and store the response.
438 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
439 int statusCode = res.getStatus();
441 // Check the status code of the response: does it match
442 // the expected response(s)?
443 if(logger.isDebugEnabled()){
444 logger.debug(testName + ": status = " + statusCode);
446 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
447 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
448 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
450 // ---------------------------------------------------------------
451 // CRUD tests : READ_LIST tests
452 // ---------------------------------------------------------------
456 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
457 dependsOnMethods = {"createList", "read"})
458 public void readList(String testName) throws Exception {
461 setupReadList(testName);
463 // Submit the request to the service and store the response.
464 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
465 OrgauthoritiesCommonList list = res.getEntity();
466 int statusCode = res.getStatus();
468 // Check the status code of the response: does it match
469 // the expected response(s)?
470 if(logger.isDebugEnabled()){
471 logger.debug(testName + ": status = " + statusCode);
473 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
474 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
475 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
477 // Optionally output additional data about list members for debugging.
478 boolean iterateThroughList = false;
479 if (iterateThroughList && logger.isDebugEnabled()) {
480 List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
481 list.getOrgauthorityListItem();
483 for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
484 String csid = item.getCsid();
485 logger.debug(testName + ": list-item[" + i + "] csid=" +
487 logger.debug(testName + ": list-item[" + i + "] displayName=" +
488 item.getDisplayName());
489 logger.debug(testName + ": list-item[" + i + "] URI=" +
497 @Test(dependsOnMethods = {"createList", "readItem"})
498 public void readItemList() {
499 readItemList(knownResourceId);
502 private void readItemList(String vcsid) {
504 final String testName = "readItemList";
507 setupReadList(testName);
509 // Submit the request to the service and store the response.
510 ClientResponse<OrganizationsCommonList> res =
511 client.readItemList(vcsid);
512 OrganizationsCommonList list = res.getEntity();
513 int statusCode = res.getStatus();
515 // Check the status code of the response: does it match
516 // the expected response(s)?
517 if(logger.isDebugEnabled()){
518 logger.debug(" " + testName + ": status = " + statusCode);
520 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
521 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
522 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
524 List<OrganizationsCommonList.OrganizationListItem> items =
525 list.getOrganizationListItem();
526 int nItemsReturned = items.size();
527 if(logger.isDebugEnabled()){
528 logger.debug(" " + testName + ": Expected "
529 + nItemsToCreateInList+" items; got: "+nItemsReturned);
531 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
534 for (OrganizationsCommonList.OrganizationListItem item : items) {
535 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
536 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
537 // Optionally output additional data about list members for debugging.
538 boolean showDetails = true;
539 if (showDetails && logger.isDebugEnabled()) {
540 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
542 logger.debug(" " + testName + ": list-item[" + i + "] refName=" +
544 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
545 item.getDisplayName());
546 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
555 // ---------------------------------------------------------------
556 // CRUD tests : UPDATE tests
557 // ---------------------------------------------------------------
560 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
561 dependsOnMethods = {"read"})
562 public void update(String testName) throws Exception {
565 setupUpdate(testName);
567 // Retrieve the contents of a resource to update.
568 ClientResponse<MultipartInput> res =
569 client.read(knownResourceId);
570 if(logger.isDebugEnabled()){
571 logger.debug(testName + ": read status = " + res.getStatus());
573 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
575 if(logger.isDebugEnabled()){
576 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
578 MultipartInput input = (MultipartInput) res.getEntity();
579 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
580 client.getCommonPartName(), OrgauthoritiesCommon.class);
581 Assert.assertNotNull(orgAuthority);
583 // Update the contents of this resource.
584 orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
585 orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
586 if(logger.isDebugEnabled()){
587 logger.debug("to be updated OrgAuthority");
588 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
591 // Submit the updated resource to the service and store the response.
592 MultipartOutput output = new MultipartOutput();
593 OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
594 commonPart.getHeaders().add("label", client.getCommonPartName());
595 res = client.update(knownResourceId, output);
596 int statusCode = res.getStatus();
598 // Check the status code of the response: does it match the expected response(s)?
599 if(logger.isDebugEnabled()){
600 logger.debug("update: status = " + statusCode);
602 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
603 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
604 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
606 // Retrieve the updated resource and verify that its contents exist.
607 input = (MultipartInput) res.getEntity();
608 OrgauthoritiesCommon updatedOrgAuthority =
609 (OrgauthoritiesCommon) extractPart(input,
610 client.getCommonPartName(), OrgauthoritiesCommon.class);
611 Assert.assertNotNull(updatedOrgAuthority);
613 // Verify that the updated resource received the correct data.
614 Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
615 orgAuthority.getDisplayName(),
616 "Data in updated object did not match submitted data.");
619 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
620 dependsOnMethods = {"readItem", "update"})
621 public void updateItem(String testName) throws Exception {
624 setupUpdate(testName);
626 ClientResponse<MultipartInput> res =
627 client.readItem(knownResourceId, knownItemResourceId);
628 if(logger.isDebugEnabled()){
629 logger.debug(testName + ": read status = " + res.getStatus());
631 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
633 if(logger.isDebugEnabled()){
634 logger.debug("got Organization to update with ID: " +
635 knownItemResourceId +
636 " in OrgAuthority: " + knownResourceId );
638 MultipartInput input = (MultipartInput) res.getEntity();
639 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
640 client.getItemCommonPartName(), OrganizationsCommon.class);
641 Assert.assertNotNull(organization);
643 // Update the contents of this resource.
644 organization.setShortName("updated-" + organization.getShortName());
645 if(logger.isDebugEnabled()){
646 logger.debug("to be updated Organization");
647 logger.debug(objectAsXmlString(organization,
648 OrganizationsCommon.class));
651 // Submit the updated resource to the service and store the response.
652 MultipartOutput output = new MultipartOutput();
653 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
654 commonPart.getHeaders().add("label", client.getItemCommonPartName());
655 res = client.updateItem(knownResourceId, knownItemResourceId, output);
656 int statusCode = res.getStatus();
658 // Check the status code of the response: does it match the expected response(s)?
659 if(logger.isDebugEnabled()){
660 logger.debug("updateItem: status = " + statusCode);
662 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
663 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
664 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
666 // Retrieve the updated resource and verify that its contents exist.
667 input = (MultipartInput) res.getEntity();
668 OrganizationsCommon updatedOrganization =
669 (OrganizationsCommon) extractPart(input,
670 client.getItemCommonPartName(), OrganizationsCommon.class);
671 Assert.assertNotNull(updatedOrganization);
673 // Verify that the updated resource received the correct data.
674 Assert.assertEquals(updatedOrganization.getShortName(),
675 organization.getShortName(),
676 "Data in updated Organization did not match submitted data.");
680 // Placeholders until the three tests below can be uncommented.
681 // See Issue CSPACE-401.
683 public void updateWithEmptyEntityBody(String testName) throws Exception {
687 public void updateWithMalformedXml(String testName) throws Exception {
691 public void updateWithWrongXmlSchema(String testName) throws Exception {
696 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
697 dependsOnMethods = {"create", "update", "testSubmitRequest"})
698 public void updateWithEmptyEntityBody(String testName) throws Exception {
701 setupUpdateWithEmptyEntityBody(testName);
703 // Submit the request to the service and store the response.
704 String method = REQUEST_TYPE.httpMethodName();
705 String url = getResourceURL(knownResourceId);
706 String mediaType = MediaType.APPLICATION_XML;
707 final String entity = "";
708 int statusCode = submitRequest(method, url, mediaType, entity);
710 // Check the status code of the response: does it match
711 // the expected response(s)?
712 if(logger.isDebugEnabled()){
713 logger.debug(testName + ": url=" + url +
714 " status=" + statusCode);
716 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
717 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
718 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
722 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
723 dependsOnMethods = {"create", "update", "testSubmitRequest"})
724 public void updateWithMalformedXml(String testName) throws Exception {
727 setupUpdateWithMalformedXml(testName);
729 // Submit the request to the service and store the response.
730 String method = REQUEST_TYPE.httpMethodName();
731 String url = getResourceURL(knownResourceId);
732 String mediaType = MediaType.APPLICATION_XML;
733 final String entity = MALFORMED_XML_DATA;
734 int statusCode = submitRequest(method, url, mediaType, entity);
736 // Check the status code of the response: does it match
737 // the expected response(s)?
738 if(logger.isDebugEnabled()){
739 logger.debug(testName + ": url=" + url +
740 " status=" + statusCode);
742 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
743 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
744 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
748 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
749 dependsOnMethods = {"create", "update", "testSubmitRequest"})
750 public void updateWithWrongXmlSchema(String testName) throws Exception {
753 setupUpdateWithWrongXmlSchema(testName);
755 // Submit the request to the service and store the response.
756 String method = REQUEST_TYPE.httpMethodName();
757 String url = getResourceURL(knownResourceId);
758 String mediaType = MediaType.APPLICATION_XML;
759 final String entity = WRONG_XML_SCHEMA_DATA;
760 int statusCode = submitRequest(method, url, mediaType, entity);
762 // Check the status code of the response: does it match
763 // the expected response(s)?
764 if(logger.isDebugEnabled()){
765 logger.debug("updateWithWrongXmlSchema: url=" + url +
766 " status=" + statusCode);
768 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
769 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
770 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
776 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
777 dependsOnMethods = {"update", "testSubmitRequest"})
778 public void updateNonExistent(String testName) throws Exception {
781 setupUpdateNonExistent(testName);
783 // Submit the request to the service and store the response.
784 // Note: The ID used in this 'create' call may be arbitrary.
785 // The only relevant ID may be the one used in update(), below.
787 // The only relevant ID may be the one used in update(), below.
788 MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
789 ClientResponse<MultipartInput> res =
790 client.update(NON_EXISTENT_ID, multipart);
791 int statusCode = res.getStatus();
793 // Check the status code of the response: does it match
794 // the expected response(s)?
795 if(logger.isDebugEnabled()){
796 logger.debug(testName + ": status = " + statusCode);
798 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
799 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
800 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
803 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
804 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
805 public void updateNonExistentItem(String testName) throws Exception {
808 setupUpdateNonExistent(testName);
810 // Submit the request to the service and store the response.
811 // Note: The ID used in this 'create' call may be arbitrary.
812 // The only relevant ID may be the one used in update(), below.
814 // The only relevant ID may be the one used in update(), below.
815 Map<String, String> nonexOrgMap = new HashMap<String,String>();
816 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
817 String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, NON_EXISTENT_ID, true);
818 MultipartOutput multipart =
819 OrgAuthorityClientUtils.createOrganizationInstance(
820 knownResourceId, refName,
821 nonexOrgMap, client.getItemCommonPartName() );
822 ClientResponse<MultipartInput> res =
823 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
824 int statusCode = res.getStatus();
826 // Check the status code of the response: does it match
827 // the expected response(s)?
828 if(logger.isDebugEnabled()){
829 logger.debug(testName + ": status = " + statusCode);
831 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
832 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
833 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
836 // ---------------------------------------------------------------
837 // CRUD tests : DELETE tests
838 // ---------------------------------------------------------------
841 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
842 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
843 public void delete(String testName) throws Exception {
846 setupDelete(testName);
848 // Submit the request to the service and store the response.
849 ClientResponse<Response> res = client.delete(knownResourceId);
850 int statusCode = res.getStatus();
852 // Check the status code of the response: does it match
853 // the expected response(s)?
854 if(logger.isDebugEnabled()){
855 logger.debug(testName + ": status = " + statusCode);
857 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
858 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
859 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
862 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
863 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
865 public void deleteItem(String testName) throws Exception {
868 setupDelete(testName);
870 // Submit the request to the service and store the response.
871 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
872 int statusCode = res.getStatus();
874 // Check the status code of the response: does it match
875 // the expected response(s)?
876 if(logger.isDebugEnabled()){
877 logger.debug("delete: status = " + statusCode);
879 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
880 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
881 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
886 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
887 dependsOnMethods = {"delete"})
888 public void deleteNonExistent(String testName) throws Exception {
891 setupDeleteNonExistent(testName);
893 // Submit the request to the service and store the response.
894 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
895 int statusCode = res.getStatus();
897 // Check the status code of the response: does it match
898 // the expected response(s)?
899 if(logger.isDebugEnabled()){
900 logger.debug(testName + ": status = " + statusCode);
902 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
903 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
904 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
907 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
908 dependsOnMethods = {"deleteItem"})
909 public void deleteNonExistentItem(String testName) {
912 setupDeleteNonExistent(testName);
914 // Submit the request to the service and store the response.
915 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
916 int statusCode = res.getStatus();
918 // Check the status code of the response: does it match
919 // the expected response(s)?
920 if(logger.isDebugEnabled()){
921 logger.debug(testName + ": status = " + statusCode);
923 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
924 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
925 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
928 // ---------------------------------------------------------------
929 // Utility tests : tests of code used in tests above
930 // ---------------------------------------------------------------
932 * Tests the code for manually submitting data that is used by several
933 * of the methods above.
935 @Test(dependsOnMethods = {"create", "read"})
936 public void testSubmitRequest() {
938 // Expected status code: 200 OK
939 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
941 // Submit the request to the service and store the response.
942 String method = ServiceRequestType.READ.httpMethodName();
943 String url = getResourceURL(knownResourceId);
944 int statusCode = submitRequest(method, url);
946 // Check the status code of the response: does it match
947 // the expected response(s)?
948 if(logger.isDebugEnabled()){
949 logger.debug("testSubmitRequest: url=" + url +
950 " status=" + statusCode);
952 Assert.assertEquals(statusCode, EXPECTED_STATUS);
956 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
957 public void testItemSubmitRequest() {
959 // Expected status code: 200 OK
960 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
962 // Submit the request to the service and store the response.
963 String method = ServiceRequestType.READ.httpMethodName();
964 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
965 int statusCode = submitRequest(method, url);
967 // Check the status code of the response: does it match
968 // the expected response(s)?
969 if(logger.isDebugEnabled()){
970 logger.debug("testItemSubmitRequest: url=" + url +
971 " status=" + statusCode);
973 Assert.assertEquals(statusCode, EXPECTED_STATUS);
977 // ---------------------------------------------------------------
978 // Cleanup of resources created during testing
979 // ---------------------------------------------------------------
982 * Deletes all resources created by tests, after all tests have been run.
984 * This cleanup method will always be run, even if one or more tests fail.
985 * For this reason, it attempts to remove all resources created
986 * at any point during testing, even if some of those resources
987 * may be expected to be deleted by certain tests.
989 @AfterClass(alwaysRun=true)
990 public void cleanUp() {
991 if (logger.isDebugEnabled()) {
992 logger.debug("Cleaning up temporary resources created for testing ...");
994 // Clean up organization resources.
995 String orgAuthorityResourceId;
996 String organizationResourceId;
997 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
998 organizationResourceId = entry.getKey();
999 orgAuthorityResourceId = entry.getValue();
1000 // Note: Any non-success responses are ignored and not reported.
1001 ClientResponse<Response> res =
1002 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
1004 // Clean up orgAuthority resources.
1005 for (String resourceId : allResourceIdsCreated) {
1006 // Note: Any non-success responses are ignored and not reported.
1007 ClientResponse<Response> res = client.delete(resourceId);
1011 // ---------------------------------------------------------------
1012 // Utility methods used by tests above
1013 // ---------------------------------------------------------------
1015 public String getServicePathComponent() {
1016 return SERVICE_PATH_COMPONENT;
1019 public String getItemServicePathComponent() {
1020 return ITEM_SERVICE_PATH_COMPONENT;
1024 * Returns the root URL for a service.
1026 * This URL consists of a base URL for all services, followed by
1027 * a path component for the owning orgAuthority, followed by the
1028 * path component for the items.
1030 * @return The root URL for a service.
1032 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1033 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1037 * Returns the URL of a specific resource managed by a service, and
1038 * designated by an identifier (such as a universally unique ID, or UUID).
1040 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1042 * @return The URL of a specific resource managed by a service.
1044 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1045 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1048 private MultipartOutput createOrgAuthorityInstance(String identifier) {
1049 String displayName = "displayName-" + identifier;
1050 String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
1051 return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1052 displayName, refName,
1053 client.getCommonPartName());