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);
402 boolean showFull = true;
403 if(showFull && logger.isDebugEnabled()){
404 logger.debug(testName + ": returned payload:");
405 logger.debug(objectAsXmlString(organization,
406 OrganizationsCommon.class));
408 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
413 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
414 dependsOnMethods = {"read"})
415 public void readNonExistent(String testName) {
418 setupReadNonExistent(testName);
420 // Submit the request to the service and store the response.
421 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
422 int statusCode = res.getStatus();
424 // Check the status code of the response: does it match
425 // the expected response(s)?
426 if(logger.isDebugEnabled()){
427 logger.debug(testName + ": status = " + statusCode);
429 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
430 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
431 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
434 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
435 dependsOnMethods = {"readItem", "readNonExistent"})
436 public void readItemNonExistent(String testName) {
439 setupReadNonExistent(testName);
441 // Submit the request to the service and store the response.
442 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
443 int statusCode = res.getStatus();
445 // Check the status code of the response: does it match
446 // the expected response(s)?
447 if(logger.isDebugEnabled()){
448 logger.debug(testName + ": status = " + statusCode);
450 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
451 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
452 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
454 // ---------------------------------------------------------------
455 // CRUD tests : READ_LIST tests
456 // ---------------------------------------------------------------
460 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
461 dependsOnMethods = {"createList", "read"})
462 public void readList(String testName) throws Exception {
465 setupReadList(testName);
467 // Submit the request to the service and store the response.
468 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
469 OrgauthoritiesCommonList list = res.getEntity();
470 int statusCode = res.getStatus();
472 // Check the status code of the response: does it match
473 // the expected response(s)?
474 if(logger.isDebugEnabled()){
475 logger.debug(testName + ": status = " + statusCode);
477 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
481 // Optionally output additional data about list members for debugging.
482 boolean iterateThroughList = false;
483 if (iterateThroughList && logger.isDebugEnabled()) {
484 List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
485 list.getOrgauthorityListItem();
487 for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
488 String csid = item.getCsid();
489 logger.debug(testName + ": list-item[" + i + "] csid=" +
491 logger.debug(testName + ": list-item[" + i + "] displayName=" +
492 item.getDisplayName());
493 logger.debug(testName + ": list-item[" + i + "] URI=" +
501 @Test(dependsOnMethods = {"createList", "readItem"})
502 public void readItemList() {
503 readItemList(knownResourceId);
506 private void readItemList(String vcsid) {
508 final String testName = "readItemList";
511 setupReadList(testName);
513 // Submit the request to the service and store the response.
514 ClientResponse<OrganizationsCommonList> res =
515 client.readItemList(vcsid);
516 OrganizationsCommonList list = res.getEntity();
517 int statusCode = res.getStatus();
519 // Check the status code of the response: does it match
520 // the expected response(s)?
521 if(logger.isDebugEnabled()){
522 logger.debug(" " + testName + ": status = " + statusCode);
524 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
525 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
526 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
528 List<OrganizationsCommonList.OrganizationListItem> items =
529 list.getOrganizationListItem();
530 int nItemsReturned = items.size();
531 if(logger.isDebugEnabled()){
532 logger.debug(" " + testName + ": Expected "
533 + nItemsToCreateInList+" items; got: "+nItemsReturned);
535 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
538 for (OrganizationsCommonList.OrganizationListItem item : items) {
539 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
540 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
541 // Optionally output additional data about list members for debugging.
542 boolean showDetails = true;
543 if (showDetails && logger.isDebugEnabled()) {
544 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
546 logger.debug(" " + testName + ": list-item[" + i + "] refName=" +
548 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
549 item.getDisplayName());
550 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
559 // ---------------------------------------------------------------
560 // CRUD tests : UPDATE tests
561 // ---------------------------------------------------------------
564 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
565 dependsOnMethods = {"read"})
566 public void update(String testName) throws Exception {
569 setupUpdate(testName);
571 // Retrieve the contents of a resource to update.
572 ClientResponse<MultipartInput> res =
573 client.read(knownResourceId);
574 if(logger.isDebugEnabled()){
575 logger.debug(testName + ": read status = " + res.getStatus());
577 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
579 if(logger.isDebugEnabled()){
580 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
582 MultipartInput input = (MultipartInput) res.getEntity();
583 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
584 client.getCommonPartName(), OrgauthoritiesCommon.class);
585 Assert.assertNotNull(orgAuthority);
587 // Update the contents of this resource.
588 orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
589 orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
590 if(logger.isDebugEnabled()){
591 logger.debug("to be updated OrgAuthority");
592 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
595 // Submit the updated resource to the service and store the response.
596 MultipartOutput output = new MultipartOutput();
597 OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
598 commonPart.getHeaders().add("label", client.getCommonPartName());
599 res = client.update(knownResourceId, output);
600 int statusCode = res.getStatus();
602 // Check the status code of the response: does it match the expected response(s)?
603 if(logger.isDebugEnabled()){
604 logger.debug("update: status = " + statusCode);
606 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
607 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
608 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
610 // Retrieve the updated resource and verify that its contents exist.
611 input = (MultipartInput) res.getEntity();
612 OrgauthoritiesCommon updatedOrgAuthority =
613 (OrgauthoritiesCommon) extractPart(input,
614 client.getCommonPartName(), OrgauthoritiesCommon.class);
615 Assert.assertNotNull(updatedOrgAuthority);
617 // Verify that the updated resource received the correct data.
618 Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
619 orgAuthority.getDisplayName(),
620 "Data in updated object did not match submitted data.");
623 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
624 dependsOnMethods = {"readItem", "update"})
625 public void updateItem(String testName) throws Exception {
628 setupUpdate(testName);
630 ClientResponse<MultipartInput> res =
631 client.readItem(knownResourceId, knownItemResourceId);
632 if(logger.isDebugEnabled()){
633 logger.debug(testName + ": read status = " + res.getStatus());
635 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
637 if(logger.isDebugEnabled()){
638 logger.debug("got Organization to update with ID: " +
639 knownItemResourceId +
640 " in OrgAuthority: " + knownResourceId );
642 MultipartInput input = (MultipartInput) res.getEntity();
643 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
644 client.getItemCommonPartName(), OrganizationsCommon.class);
645 Assert.assertNotNull(organization);
647 // Update the contents of this resource.
648 organization.setShortName("updated-" + organization.getShortName());
649 if(logger.isDebugEnabled()){
650 logger.debug("to be updated Organization");
651 logger.debug(objectAsXmlString(organization,
652 OrganizationsCommon.class));
655 // Submit the updated resource to the service and store the response.
656 MultipartOutput output = new MultipartOutput();
657 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
658 commonPart.getHeaders().add("label", client.getItemCommonPartName());
659 res = client.updateItem(knownResourceId, knownItemResourceId, output);
660 int statusCode = res.getStatus();
662 // Check the status code of the response: does it match the expected response(s)?
663 if(logger.isDebugEnabled()){
664 logger.debug("updateItem: status = " + statusCode);
666 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
667 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
668 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
670 // Retrieve the updated resource and verify that its contents exist.
671 input = (MultipartInput) res.getEntity();
672 OrganizationsCommon updatedOrganization =
673 (OrganizationsCommon) extractPart(input,
674 client.getItemCommonPartName(), OrganizationsCommon.class);
675 Assert.assertNotNull(updatedOrganization);
677 // Verify that the updated resource received the correct data.
678 Assert.assertEquals(updatedOrganization.getShortName(),
679 organization.getShortName(),
680 "Data in updated Organization did not match submitted data.");
684 // Placeholders until the three tests below can be uncommented.
685 // See Issue CSPACE-401.
687 public void updateWithEmptyEntityBody(String testName) throws Exception {
691 public void updateWithMalformedXml(String testName) throws Exception {
695 public void updateWithWrongXmlSchema(String testName) throws Exception {
700 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
701 dependsOnMethods = {"create", "update", "testSubmitRequest"})
702 public void updateWithEmptyEntityBody(String testName) throws Exception {
705 setupUpdateWithEmptyEntityBody(testName);
707 // Submit the request to the service and store the response.
708 String method = REQUEST_TYPE.httpMethodName();
709 String url = getResourceURL(knownResourceId);
710 String mediaType = MediaType.APPLICATION_XML;
711 final String entity = "";
712 int statusCode = submitRequest(method, url, mediaType, entity);
714 // Check the status code of the response: does it match
715 // the expected response(s)?
716 if(logger.isDebugEnabled()){
717 logger.debug(testName + ": url=" + url +
718 " status=" + statusCode);
720 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
721 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
722 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
726 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
727 dependsOnMethods = {"create", "update", "testSubmitRequest"})
728 public void updateWithMalformedXml(String testName) throws Exception {
731 setupUpdateWithMalformedXml(testName);
733 // Submit the request to the service and store the response.
734 String method = REQUEST_TYPE.httpMethodName();
735 String url = getResourceURL(knownResourceId);
736 String mediaType = MediaType.APPLICATION_XML;
737 final String entity = MALFORMED_XML_DATA;
738 int statusCode = submitRequest(method, url, mediaType, entity);
740 // Check the status code of the response: does it match
741 // the expected response(s)?
742 if(logger.isDebugEnabled()){
743 logger.debug(testName + ": url=" + url +
744 " status=" + statusCode);
746 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
747 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
748 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
752 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
753 dependsOnMethods = {"create", "update", "testSubmitRequest"})
754 public void updateWithWrongXmlSchema(String testName) throws Exception {
757 setupUpdateWithWrongXmlSchema(testName);
759 // Submit the request to the service and store the response.
760 String method = REQUEST_TYPE.httpMethodName();
761 String url = getResourceURL(knownResourceId);
762 String mediaType = MediaType.APPLICATION_XML;
763 final String entity = WRONG_XML_SCHEMA_DATA;
764 int statusCode = submitRequest(method, url, mediaType, entity);
766 // Check the status code of the response: does it match
767 // the expected response(s)?
768 if(logger.isDebugEnabled()){
769 logger.debug("updateWithWrongXmlSchema: url=" + url +
770 " status=" + statusCode);
772 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
773 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
774 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
780 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
781 dependsOnMethods = {"update", "testSubmitRequest"})
782 public void updateNonExistent(String testName) throws Exception {
785 setupUpdateNonExistent(testName);
787 // Submit the request to the service and store the response.
788 // Note: The ID used in this 'create' call may be arbitrary.
789 // The only relevant ID may be the one used in update(), below.
791 // The only relevant ID may be the one used in update(), below.
792 MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
793 ClientResponse<MultipartInput> res =
794 client.update(NON_EXISTENT_ID, multipart);
795 int statusCode = res.getStatus();
797 // Check the status code of the response: does it match
798 // the expected response(s)?
799 if(logger.isDebugEnabled()){
800 logger.debug(testName + ": status = " + statusCode);
802 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
803 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
804 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
807 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
808 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
809 public void updateNonExistentItem(String testName) throws Exception {
812 setupUpdateNonExistent(testName);
814 // Submit the request to the service and store the response.
815 // Note: The ID used in this 'create' call may be arbitrary.
816 // The only relevant ID may be the one used in update(), below.
818 // The only relevant ID may be the one used in update(), below.
819 Map<String, String> nonexOrgMap = new HashMap<String,String>();
820 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
821 MultipartOutput multipart =
822 OrgAuthorityClientUtils.createOrganizationInstance(
823 knownResourceId, createRefName(NON_EXISTENT_ID),
824 nonexOrgMap, client.getItemCommonPartName() );
825 ClientResponse<MultipartInput> res =
826 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
827 int statusCode = res.getStatus();
829 // Check the status code of the response: does it match
830 // the expected response(s)?
831 if(logger.isDebugEnabled()){
832 logger.debug(testName + ": status = " + statusCode);
834 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
835 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
836 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
839 // ---------------------------------------------------------------
840 // CRUD tests : DELETE tests
841 // ---------------------------------------------------------------
844 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
845 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
846 public void delete(String testName) throws Exception {
849 setupDelete(testName);
851 // Submit the request to the service and store the response.
852 ClientResponse<Response> res = client.delete(knownResourceId);
853 int statusCode = res.getStatus();
855 // Check the status code of the response: does it match
856 // the expected response(s)?
857 if(logger.isDebugEnabled()){
858 logger.debug(testName + ": status = " + statusCode);
860 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
861 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
862 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
865 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
866 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
868 public void deleteItem(String testName) throws Exception {
871 setupDelete(testName);
873 // Submit the request to the service and store the response.
874 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
875 int statusCode = res.getStatus();
877 // Check the status code of the response: does it match
878 // the expected response(s)?
879 if(logger.isDebugEnabled()){
880 logger.debug("delete: status = " + statusCode);
882 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
883 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
884 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
889 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
890 dependsOnMethods = {"delete"})
891 public void deleteNonExistent(String testName) throws Exception {
894 setupDeleteNonExistent(testName);
896 // Submit the request to the service and store the response.
897 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
898 int statusCode = res.getStatus();
900 // Check the status code of the response: does it match
901 // the expected response(s)?
902 if(logger.isDebugEnabled()){
903 logger.debug(testName + ": status = " + statusCode);
905 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
906 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
907 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
910 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
911 dependsOnMethods = {"deleteItem"})
912 public void deleteNonExistentItem(String testName) {
915 setupDeleteNonExistent(testName);
917 // Submit the request to the service and store the response.
918 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
919 int statusCode = res.getStatus();
921 // Check the status code of the response: does it match
922 // the expected response(s)?
923 if(logger.isDebugEnabled()){
924 logger.debug(testName + ": status = " + statusCode);
926 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
927 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
928 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
931 // ---------------------------------------------------------------
932 // Utility tests : tests of code used in tests above
933 // ---------------------------------------------------------------
935 * Tests the code for manually submitting data that is used by several
936 * of the methods above.
938 @Test(dependsOnMethods = {"create", "read"})
939 public void testSubmitRequest() {
941 // Expected status code: 200 OK
942 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
944 // Submit the request to the service and store the response.
945 String method = ServiceRequestType.READ.httpMethodName();
946 String url = getResourceURL(knownResourceId);
947 int statusCode = submitRequest(method, url);
949 // Check the status code of the response: does it match
950 // the expected response(s)?
951 if(logger.isDebugEnabled()){
952 logger.debug("testSubmitRequest: url=" + url +
953 " status=" + statusCode);
955 Assert.assertEquals(statusCode, EXPECTED_STATUS);
959 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
960 public void testItemSubmitRequest() {
962 // Expected status code: 200 OK
963 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
965 // Submit the request to the service and store the response.
966 String method = ServiceRequestType.READ.httpMethodName();
967 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
968 int statusCode = submitRequest(method, url);
970 // Check the status code of the response: does it match
971 // the expected response(s)?
972 if(logger.isDebugEnabled()){
973 logger.debug("testItemSubmitRequest: url=" + url +
974 " status=" + statusCode);
976 Assert.assertEquals(statusCode, EXPECTED_STATUS);
980 // ---------------------------------------------------------------
981 // Cleanup of resources created during testing
982 // ---------------------------------------------------------------
985 * Deletes all resources created by tests, after all tests have been run.
987 * This cleanup method will always be run, even if one or more tests fail.
988 * For this reason, it attempts to remove all resources created
989 * at any point during testing, even if some of those resources
990 * may be expected to be deleted by certain tests.
992 @AfterClass(alwaysRun=true)
993 public void cleanUp() {
994 if (logger.isDebugEnabled()) {
995 logger.debug("Cleaning up temporary resources created for testing ...");
997 // Clean up organization resources.
998 String orgAuthorityResourceId;
999 String organizationResourceId;
1000 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1001 organizationResourceId = entry.getKey();
1002 orgAuthorityResourceId = entry.getValue();
1003 // Note: Any non-success responses are ignored and not reported.
1004 ClientResponse<Response> res =
1005 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
1007 // Clean up orgAuthority resources.
1008 for (String resourceId : allResourceIdsCreated) {
1009 // Note: Any non-success responses are ignored and not reported.
1010 ClientResponse<Response> res = client.delete(resourceId);
1014 // ---------------------------------------------------------------
1015 // Utility methods used by tests above
1016 // ---------------------------------------------------------------
1018 public String getServicePathComponent() {
1019 return SERVICE_PATH_COMPONENT;
1022 public String getItemServicePathComponent() {
1023 return ITEM_SERVICE_PATH_COMPONENT;
1027 * Returns the root URL for a service.
1029 * This URL consists of a base URL for all services, followed by
1030 * a path component for the owning orgAuthority, followed by the
1031 * path component for the items.
1033 * @return The root URL for a service.
1035 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1036 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1040 * Returns the URL of a specific resource managed by a service, and
1041 * designated by an identifier (such as a universally unique ID, or UUID).
1043 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1045 * @return The URL of a specific resource managed by a service.
1047 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1048 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1051 private MultipartOutput createOrgAuthorityInstance(String identifier) {
1052 String displayName = "displayName-" + identifier;
1053 String refName = createRefName(displayName);
1054 return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1055 displayName, refName,
1056 client.getCommonPartName());