]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4a5fbbfc1e66f05e210178e5cc292d5c7d94ddbc
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.dom4j.Element;
30
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.client.IntakeClient;
33 import org.collectionspace.services.client.PayloadInputPart;
34 import org.collectionspace.services.client.PayloadOutputPart;
35 import org.collectionspace.services.client.PoxPayloadIn;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.common.AbstractCommonListUtils;
38 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
39 import org.collectionspace.services.intake.EntryMethodList;
40 import org.collectionspace.services.intake.FieldCollectionEventNameList;
41 import org.collectionspace.services.intake.CurrentLocationGroup;
42 import org.collectionspace.services.intake.CurrentLocationGroupList;
43 import org.collectionspace.services.intake.IntakesCommon;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45
46 import org.jboss.resteasy.client.ClientResponse;
47 import org.testng.Assert;
48 import org.testng.annotations.Test;
49
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 // FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
54 /**
55  * IntakeServiceTest, carries out tests against a
56  * deployed and running Intake Service.
57  *
58  * $LastChangedRevision$
59  * $LastChangedDate$
60  */
61 public class IntakeServiceTest extends AbstractServiceTestImpl {
62
63     /** The logger. */
64     private final String CLASS_NAME = IntakeServiceTest.class.getName();
65     private final Logger logger = LoggerFactory.getLogger(IntakeServiceTest.class);
66     /** The known resource id. */
67     private String knownResourceId = null;
68
69     @Override
70     protected CollectionSpaceClient getClientInstance() {
71         return new IntakeClient();
72     }
73
74     @Override
75     protected String getServiceName() {
76         return IntakeClient.SERVICE_NAME;
77     }
78
79     /* (non-Javadoc)
80      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
81      */
82     @Override
83     protected AbstractCommonList getAbstractCommonList(
84             ClientResponse<AbstractCommonList> response) {
85         return response.getEntity(AbstractCommonList.class);
86     }
87
88     // ---------------------------------------------------------------
89     // CRUD tests : CREATE tests
90     // ---------------------------------------------------------------
91     
92     // Success outcomes
93     
94     /* (non-Javadoc)
95      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
96      */
97     @Override
98     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
99     public void create(String testName) throws Exception {
100
101         if (logger.isDebugEnabled()) {
102             logger.debug(testBanner(testName, CLASS_NAME));
103         }
104         // Perform setup, such as initializing the type of service request
105         // (e.g. CREATE, DELETE), its valid and expected status codes, and
106         // its associated HTTP method name (e.g. POST, DELETE).
107         setupCreate();
108
109         // Submit the request to the service and store the response.
110         IntakeClient client = new IntakeClient();
111         String identifier = createIdentifier();
112         PoxPayloadOut multipart = createInstance(identifier);
113         ClientResponse<Response> res = client.create(multipart);
114
115         int statusCode = res.getStatus();
116
117         // Check the status code of the response: does it match
118         // the expected response(s)?
119         //
120         // Specifically:
121         // Does it fall within the set of valid status codes?
122         // Does it exactly match the expected status code?
123         if (logger.isDebugEnabled()) {
124             logger.debug(testName + ": status = " + statusCode);
125         }
126         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
129
130         // Store the ID returned from the first resource created
131         // for additional tests below.
132         if (knownResourceId == null) {
133             knownResourceId = extractId(res);
134             if (logger.isDebugEnabled()) {
135                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
136             }
137         }
138
139         // Store the IDs from every resource created by tests,
140         // so they can be deleted after tests have been run.
141         allResourceIdsCreated.add(extractId(res));
142     }
143
144     /* (non-Javadoc)
145      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
146      */
147     @Override
148     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
149     dependsOnMethods = {"create"})
150     public void createList(String testName) throws Exception {
151         for (int i = 0; i < 3; i++) {
152             create(testName);
153         }
154     }
155
156     // Failure outcomes
157     // Placeholders until the three tests below can be uncommented.
158     
159     // See Issue CSPACE-401.
160     /* (non-Javadoc)
161      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
162      */
163     @Override
164     public void createWithEmptyEntityBody(String testName) throws Exception {
165         //Should this really be empty?
166     }
167
168     /* (non-Javadoc)
169      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
170      */
171     @Override
172     public void createWithMalformedXml(String testName) throws Exception {
173         //Should this really be empty?
174     }
175
176     /* (non-Javadoc)
177      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
178      */
179     @Override
180     public void createWithWrongXmlSchema(String testName) throws Exception {
181         //Should this really be empty?
182     }
183
184     /*
185     @Override
186     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
187     dependsOnMethods = {"create", "testSubmitRequest"})
188     public void createWithEmptyEntityBody(String testName) throws Exception {
189     
190     if (logger.isDebugEnabled()) {
191     logger.debug(testBanner(testName, CLASS_NAME));
192     }
193     // Perform setup.
194     setupCreateWithEmptyEntityBody();
195     
196     // Submit the request to the service and store the response.
197     String method = REQUEST_TYPE.httpMethodName();
198     String url = getServiceRootURL();
199     String mediaType = MediaType.APPLICATION_XML;
200     final String entity = "";
201     int statusCode = submitRequest(method, url, mediaType, entity);
202     
203     // Check the status code of the response: does it match
204     // the expected response(s)?
205     if(logger.isDebugEnabled()){
206     logger.debug("createWithEmptyEntityBody url=" + url +
207     " status=" + statusCode);
208     }
209     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
210     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
211     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
212     }
213     
214     @Override
215     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
216     dependsOnMethods = {"create", "testSubmitRequest"})
217     public void createWithMalformedXml(String testName) throws Exception {
218     
219     if (logger.isDebugEnabled()) {
220     logger.debug(testBanner(testName, CLASS_NAME));
221     }
222     // Perform setup.
223     setupCreateWithMalformedXml();
224     
225     // Submit the request to the service and store the response.
226     String method = REQUEST_TYPE.httpMethodName();
227     String url = getServiceRootURL();
228     String mediaType = MediaType.APPLICATION_XML;
229     final String entity = MALFORMED_XML_DATA; // Constant from base class.
230     int statusCode = submitRequest(method, url, mediaType, entity);
231     
232     // Check the status code of the response: does it match
233     // the expected response(s)?
234     if(logger.isDebugEnabled()){
235     logger.debug(testName + ": url=" + url +
236     " status=" + statusCode);
237     }
238     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
239     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
240     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241     }
242     
243     @Override
244     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
245     dependsOnMethods = {"create", "testSubmitRequest"})
246     public void createWithWrongXmlSchema(String testName) throws Exception {
247     
248     if (logger.isDebugEnabled()) {
249     logger.debug(testBanner(testName, CLASS_NAME));
250     }
251     // Perform setup.
252     setupCreateWithWrongXmlSchema(testName, logger);
253     
254     // Submit the request to the service and store the response.
255     String method = REQUEST_TYPE.httpMethodName();
256     String url = getServiceRootURL();
257     String mediaType = MediaType.APPLICATION_XML;
258     final String entity = WRONG_XML_SCHEMA_DATA;
259     int statusCode = submitRequest(method, url, mediaType, entity);
260     
261     // Check the status code of the response: does it match
262     // the expected response(s)?
263     if(logger.isDebugEnabled()){
264     logger.debug(testName + ": url=" + url +
265     " status=" + statusCode);
266     }
267     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
268     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
269     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
270     }
271      */
272     
273     // ---------------------------------------------------------------
274     // CRUD tests : READ tests
275     // ---------------------------------------------------------------
276     
277     // Success outcomes
278     
279     /* (non-Javadoc)
280      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
281      */
282     @Override
283     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
284     dependsOnMethods = {"create"})
285     public void read(String testName) throws Exception {
286
287         if (logger.isDebugEnabled()) {
288             logger.debug(testBanner(testName, CLASS_NAME));
289         }
290         // Perform setup.
291         setupRead();
292
293         // Submit the request to the service and store the response.
294         IntakeClient client = new IntakeClient();
295         ClientResponse<String> res = client.read(knownResourceId);
296         int statusCode = res.getStatus();
297
298         // Check the status code of the response: does it match
299         // the expected response(s)?
300         if (logger.isDebugEnabled()) {
301             logger.debug(testName + ": status = " + statusCode);
302         }
303         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
304                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
305         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
306
307         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
308         PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
309         IntakesCommon intakeCommons = null;
310         if (payloadInputPart != null) {
311             intakeCommons = (IntakesCommon) payloadInputPart.getBody();
312         }
313 //        IntakesCommon intake = (IntakesCommon) extractPart(input,
314 //                client.getCommonPartName(), IntakesCommon.class);
315         Assert.assertNotNull(intakeCommons);
316
317         // Verify the number and contents of values in repeatable fields,
318         // as created in the instance record used for testing.
319         List<String> entryMethods =
320                 intakeCommons.getEntryMethods().getEntryMethod();
321         Assert.assertTrue(entryMethods.size() > 0);
322         Assert.assertNotNull(entryMethods.get(0));
323
324         List<String> fieldCollectionEventNames =
325                 intakeCommons.getFieldCollectionEventNames().getFieldCollectionEventName();
326         Assert.assertTrue(fieldCollectionEventNames.size() > 0);
327         Assert.assertNotNull(fieldCollectionEventNames.get(0));
328
329         CurrentLocationGroupList currentLocationGroupList = intakeCommons.getCurrentLocationGroupList();
330         Assert.assertNotNull(currentLocationGroupList);
331         List<CurrentLocationGroup> currentLocationGroups = currentLocationGroupList.getCurrentLocationGroup();
332         Assert.assertNotNull(currentLocationGroups);
333         Assert.assertTrue(currentLocationGroups.size() > 0);
334         CurrentLocationGroup currentLocationGroup = currentLocationGroups.get(0);
335         Assert.assertNotNull(currentLocationGroup);
336         Assert.assertNotNull(currentLocationGroup.getCurrentLocationNote());
337
338         // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
339         if (logger.isDebugEnabled()) {
340             logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
341                     + "UTF-8 data received=" + intakeCommons.getEntryNote());
342         }
343         Assert.assertEquals(intakeCommons.getEntryNote(), getUTF8DataFragment(),
344                 "UTF-8 data retrieved '" + intakeCommons.getEntryNote()
345                 + "' does not match expected data '" + getUTF8DataFragment());
346     }
347
348     // Failure outcomes
349     
350     /* (non-Javadoc)
351      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
352      */
353     @Override
354     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
355     dependsOnMethods = {"read"})
356     public void readNonExistent(String testName) throws Exception {
357
358         if (logger.isDebugEnabled()) {
359             logger.debug(testBanner(testName, CLASS_NAME));
360         }
361         // Perform setup.
362         setupReadNonExistent();
363
364         // Submit the request to the service and store the response.
365         IntakeClient client = new IntakeClient();
366         ClientResponse<String> res = client.read(NON_EXISTENT_ID);
367         int statusCode = res.getStatus();
368
369         // Check the status code of the response: does it match
370         // the expected response(s)?
371         if (logger.isDebugEnabled()) {
372             logger.debug(testName + ": status = " + statusCode);
373         }
374         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
375                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
376         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
377     }
378
379     // ---------------------------------------------------------------
380     // CRUD tests : READ_LIST tests
381     // ---------------------------------------------------------------
382     
383     // Success outcomes
384     
385     /* (non-Javadoc)
386      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
387      */
388     @Override
389     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
390     dependsOnMethods = {"createList", "read"})
391     public void readList(String testName) throws Exception {
392
393         if (logger.isDebugEnabled()) {
394             logger.debug(testBanner(testName, CLASS_NAME));
395         }
396         // Perform setup.
397         setupReadList();
398
399         // Submit the request to the service and store the response.
400         IntakeClient client = new IntakeClient();
401         ClientResponse<AbstractCommonList> res = client.readList();
402         AbstractCommonList list = res.getEntity();
403         int statusCode = res.getStatus();
404
405         // Check the status code of the response: does it match
406         // the expected response(s)?
407         if (logger.isDebugEnabled()) {
408             logger.debug(testName + ": status = " + statusCode);
409         }
410         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
411                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
412         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
413
414         // Optionally output additional data about list members for debugging.
415         boolean iterateThroughList = true;
416         if (iterateThroughList && logger.isDebugEnabled()) {
417             AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
418         }
419
420     }
421
422     // Failure outcomes
423     // None at present.
424     
425     // ---------------------------------------------------------------
426     // CRUD tests : UPDATE tests
427     // ---------------------------------------------------------------
428     
429     // Success outcomes
430     
431     /* (non-Javadoc)
432      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
433      */
434     @Override
435     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
436     dependsOnMethods = {"read"})
437     public void update(String testName) throws Exception {
438
439         if (logger.isDebugEnabled()) {
440             logger.debug(testBanner(testName, CLASS_NAME));
441         }
442         // Perform setup.
443         setupUpdate();
444
445         // Retrieve the contents of a resource to update.
446         IntakeClient client = new IntakeClient();
447         ClientResponse<String> res = client.read(knownResourceId);
448         if (logger.isDebugEnabled()) {
449             logger.debug(testName + ": read status = " + res.getStatus());
450         }
451         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
452
453         if (logger.isDebugEnabled()) {
454             logger.debug("got object to update with ID: " + knownResourceId);
455         }
456         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
457         PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
458         IntakesCommon intakeCommons = null;
459         if (payloadInputPart != null) {
460             intakeCommons = (IntakesCommon) payloadInputPart.getBody();
461         }
462 //        IntakesCommon intake = (IntakesCommon) extractPart(input,
463 //                client.getCommonPartName(), IntakesCommon.class);
464         Assert.assertNotNull(intakeCommons);
465
466         // Update the content of this resource.
467         intakeCommons.setEntryNumber("updated-" + intakeCommons.getEntryNumber());
468         intakeCommons.setEntryDate("updated-" + intakeCommons.getEntryDate());
469         if (logger.isDebugEnabled()) {
470             logger.debug("to be updated object");
471             logger.debug(objectAsXmlString(intakeCommons, IntakesCommon.class));
472         }
473
474         CurrentLocationGroupList currentLocationGroupList = intakeCommons.getCurrentLocationGroupList();
475         Assert.assertNotNull(currentLocationGroupList);
476         List<CurrentLocationGroup> currentLocationGroups = currentLocationGroupList.getCurrentLocationGroup();
477         Assert.assertNotNull(currentLocationGroups);
478         Assert.assertTrue(currentLocationGroups.size() > 0);
479         CurrentLocationGroup currentLocationGroup = currentLocationGroups.get(0);
480         Assert.assertNotNull(currentLocationGroup);
481         String currentLocationNote = currentLocationGroup.getCurrentLocationNote();
482         Assert.assertNotNull(currentLocationNote);
483         String updatedCurrentLocationNote = "updated-" + currentLocationNote;
484         currentLocationGroups.get(0).setCurrentLocationNote(updatedCurrentLocationNote);
485         intakeCommons.setCurrentLocationGroupList(currentLocationGroupList);
486
487         // Create an output payload to send to the service, and add teh common part
488         PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
489         PayloadOutputPart commonPart = output.addPart(intakeCommons, MediaType.APPLICATION_XML_TYPE);
490         commonPart.setLabel(client.getCommonPartName());
491         
492         // Submit the request to the service and store the response.
493         res = client.update(knownResourceId, output);
494         int statusCode = res.getStatus();
495         // Check the status code of the response: does it match the expected response(s)?
496         if (logger.isDebugEnabled()) {
497             logger.debug(testName + ": status = " + statusCode);
498         }
499         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
500                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
501         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
502
503         input = new PoxPayloadIn(res.getEntity());
504         IntakesCommon updatedIntake =
505                 (IntakesCommon) extractPart(input,
506                 client.getCommonPartName(), IntakesCommon.class);
507
508         Assert.assertNotNull(updatedIntake);
509
510         Assert.assertEquals(updatedIntake.getEntryDate(),
511                 intakeCommons.getEntryDate(),
512                 "Data in updated object did not match submitted data.");
513
514         currentLocationGroupList = updatedIntake.getCurrentLocationGroupList();
515         Assert.assertNotNull(currentLocationGroupList);
516         currentLocationGroups = currentLocationGroupList.getCurrentLocationGroup();
517         Assert.assertNotNull(currentLocationGroups);
518         Assert.assertTrue(currentLocationGroups.size() > 0);
519         Assert.assertNotNull(currentLocationGroups.get(0));
520         Assert.assertEquals(updatedCurrentLocationNote,
521                 currentLocationGroups.get(0).getCurrentLocationNote(),
522                 "Data in updated object did not match submitted data.");
523
524         if (logger.isDebugEnabled()) {
525             logger.debug("UTF-8 data sent=" + intakeCommons.getEntryNote() + "\n"
526                     + "UTF-8 data received=" + updatedIntake.getEntryNote());
527         }
528         Assert.assertTrue(updatedIntake.getEntryNote().contains(getUTF8DataFragment()),
529                 "UTF-8 data retrieved '" + updatedIntake.getEntryNote()
530                 + "' does not contain expected data '" + getUTF8DataFragment());
531         Assert.assertEquals(updatedIntake.getEntryNote(),
532                 intakeCommons.getEntryNote(),
533                 "Data in updated object did not match submitted data.");
534
535     }
536
537     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
538     dependsOnMethods = {"update"})
539     public void verifyReadOnlyCoreFields(String testName) throws Exception {
540         // TODO These should be in some core client utils
541         final String COLLECTIONSPACE_CORE_SCHEMA = "collectionspace_core";
542         final String COLLECTIONSPACE_CORE_TENANTID = "tenantId";
543         final String COLLECTIONSPACE_CORE_URI = "uri";
544         final String COLLECTIONSPACE_CORE_CREATED_AT = "createdAt";
545         final String COLLECTIONSPACE_CORE_UPDATED_AT = "updatedAt";
546         final String COLLECTIONSPACE_CORE_CREATED_BY = "createdBy";
547         final String COLLECTIONSPACE_CORE_UPDATED_BY = "updatedBy";
548
549         if (logger.isDebugEnabled()) {
550             logger.debug(testBanner(testName, CLASS_NAME));
551         }
552         // Perform setup.
553         setupUpdate();
554
555         // Retrieve the contents of a resource to update.
556         IntakeClient client = new IntakeClient();
557         ClientResponse<String> res = client.read(knownResourceId);
558         if (logger.isDebugEnabled()) {
559             logger.debug(testName + ": read status = " + res.getStatus());
560         }
561         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
562
563         if (logger.isDebugEnabled()) {
564             logger.debug("got object to update with ID: " + knownResourceId);
565         }
566         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
567         PayloadInputPart payloadInputPart = input.getPart(COLLECTIONSPACE_CORE_SCHEMA);
568         Element coreAsElement = null;
569         if (payloadInputPart != null) {
570                 coreAsElement = payloadInputPart.getElementBody();
571         }
572         Assert.assertNotNull(coreAsElement);
573         if (logger.isDebugEnabled()) {
574             logger.debug("Core part before update:");
575             logger.debug(coreAsElement.asXML());
576         }
577
578         // Update the read-only elements
579         Element tenantId = coreAsElement.element(COLLECTIONSPACE_CORE_TENANTID);
580         String originalTenantId = tenantId.getText();
581         tenantId.setText("foo");
582         Element uri = coreAsElement.element(COLLECTIONSPACE_CORE_URI);
583         String originalUri = uri.getText();
584         uri.setText("foo");
585         Element createdAt = coreAsElement.element(COLLECTIONSPACE_CORE_CREATED_AT);
586         String originalCreatedAt = createdAt.getText();
587         String now = GregorianCalendarDateTimeUtils.timestampUTC();
588         if(originalCreatedAt.equalsIgnoreCase(now) && logger.isWarnEnabled()) {
589                         logger.warn("Cannot check createdAt read-only; too fast!");
590         }
591         createdAt.setText(now);
592         Element createdBy = coreAsElement.element(COLLECTIONSPACE_CORE_CREATED_BY);
593         String originalCreatedBy = createdBy.getText();
594         createdBy.setText("foo");
595         
596         if (logger.isDebugEnabled()) {
597             logger.debug("Core part to be updated:");
598             logger.debug(coreAsElement.asXML());
599         }
600
601         // Create an output payload to send to the service, and add the common part
602         PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
603         PayloadOutputPart corePart = output.addPart(
604                                                 COLLECTIONSPACE_CORE_SCHEMA, coreAsElement);
605         
606         // Submit the request to the service and store the response.
607         res = client.update(knownResourceId, output);
608         int statusCode = res.getStatus();
609         // Check the status code of the response: does it match the expected response(s)?
610         if (logger.isDebugEnabled()) {
611             logger.debug(testName + ": status = " + statusCode);
612         }
613         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
614                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
615         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
616
617         input = new PoxPayloadIn(res.getEntity());
618         PayloadInputPart updatedCorePart = input.getPart(COLLECTIONSPACE_CORE_SCHEMA);
619         Element updatedCoreAsElement = null;
620         if (updatedCorePart != null) {
621                 updatedCoreAsElement = updatedCorePart.getElementBody();
622         }
623         Assert.assertNotNull(updatedCoreAsElement);
624         
625         tenantId = updatedCoreAsElement.element(COLLECTIONSPACE_CORE_TENANTID);
626         String updatedTenantId = tenantId.getText();
627         Assert.assertEquals(updatedTenantId, originalTenantId,
628                                 "CORE part TenantID was able to update!");
629         uri = updatedCoreAsElement.element(COLLECTIONSPACE_CORE_URI);
630         String updatedUri = uri.getText();
631         Assert.assertEquals(updatedUri, originalUri,
632                                 "CORE part URI was able to update!");
633         createdAt = updatedCoreAsElement.element(COLLECTIONSPACE_CORE_CREATED_AT);
634         String updatedCreatedAt = createdAt.getText();
635         Assert.assertEquals(updatedCreatedAt, originalCreatedAt,
636                                 "CORE part CreatedAt was able to update!");
637         createdBy = updatedCoreAsElement.element(COLLECTIONSPACE_CORE_CREATED_BY);
638         String updatedCreatedBy = createdBy.getText();
639         Assert.assertEquals(updatedCreatedBy, originalCreatedBy,
640                                 "CORE part CreatedBy was able to update!");
641
642     }
643
644     // Failure outcomes
645     // Placeholders until the three tests below can be uncommented.
646     
647     // See Issue CSPACE-401.
648     /* (non-Javadoc)
649      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
650      */
651     @Override
652     public void updateWithEmptyEntityBody(String testName) throws Exception {
653         //Should this really be empty?
654     }
655
656     /* (non-Javadoc)
657      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
658      */
659     @Override
660     public void updateWithMalformedXml(String testName) throws Exception {
661         //Should this really be empty?
662     }
663
664     /* (non-Javadoc)
665      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
666      */
667     @Override
668     public void updateWithWrongXmlSchema(String testName) throws Exception {
669         //Should this really be empty?
670     }
671
672     /*
673     @Override
674     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
675     dependsOnMethods = {"create", "update", "testSubmitRequest"})
676     public void updateWithEmptyEntityBody(String testName) throws Exception {
677     
678     if (logger.isDebugEnabled()) {
679     logger.debug(testBanner(testName, CLASS_NAME));
680     }
681     // Perform setup.
682     setupUpdateWithEmptyEntityBody();
683     
684     // Submit the request to the service and store the response.
685     String method = REQUEST_TYPE.httpMethodName();
686     String url = getResourceURL(knownResourceId);
687     String mediaType = MediaType.APPLICATION_XML;
688     final String entity = "";
689     int statusCode = submitRequest(method, url, mediaType, entity);
690     
691     // Check the status code of the response: does it match
692     // the expected response(s)?
693     if(logger.isDebugEnabled()){
694     logger.debug(testName + ": url=" + url +
695     " status=" + statusCode);
696     }
697     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
698     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
699     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
700     }
701     
702     @Override
703     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
704     dependsOnMethods = {"create", "update", "testSubmitRequest"})
705     public void updateWithMalformedXml(String testName) throws Exception {
706     
707     if (logger.isDebugEnabled()) {
708     logger.debug(testBanner(testName, CLASS_NAME));
709     }
710     // Perform setup.
711     setupUpdateWithMalformedXml();
712     
713     // Submit the request to the service and store the response.
714     String method = REQUEST_TYPE.httpMethodName();
715     String url = getResourceURL(knownResourceId);
716     String mediaType = MediaType.APPLICATION_XML;
717     final String entity = MALFORMED_XML_DATA;
718     int statusCode = submitRequest(method, url, mediaType, entity);
719     
720     // Check the status code of the response: does it match
721     // the expected response(s)?
722     if(logger.isDebugEnabled()){
723     logger.debug(testName + ": url=" + url +
724     " status=" + statusCode);
725     }
726     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
727     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
728     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
729     }
730     
731     @Override
732     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
733     dependsOnMethods = {"create", "update", "testSubmitRequest"})
734     public void updateWithWrongXmlSchema(String testName) throws Exception {
735     
736     if (logger.isDebugEnabled()) {
737     logger.debug(testBanner(testName, CLASS_NAME));
738     }
739     // Perform setup.
740     setupUpdateWithWrongXmlSchema();
741     
742     // Submit the request to the service and store the response.
743     String method = REQUEST_TYPE.httpMethodName();
744     String url = getResourceURL(knownResourceId);
745     String mediaType = MediaType.APPLICATION_XML;
746     final String entity = WRONG_XML_SCHEMA_DATA;
747     int statusCode = submitRequest(method, url, mediaType, entity);
748     
749     // Check the status code of the response: does it match
750     // the expected response(s)?
751     if(logger.isDebugEnabled()){
752     logger.debug(testName + ": url=" + url +
753     " status=" + statusCode);
754     }
755     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
756     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
757     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
758     }
759      */
760
761     /* (non-Javadoc)
762      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
763      */
764     @Override
765     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
766     dependsOnMethods = {"update", "testSubmitRequest"})
767     public void updateNonExistent(String testName) throws Exception {
768
769         if (logger.isDebugEnabled()) {
770             logger.debug(testBanner(testName, CLASS_NAME));
771         }
772         // Perform setup.
773         setupUpdateNonExistent();
774
775         // Submit the request to the service and store the response.
776         // Note: The ID used in this 'create' call may be arbitrary.
777         // The only relevant ID may be the one used in update(), below.
778         IntakeClient client = new IntakeClient();
779         PoxPayloadOut multipart = createInstance(NON_EXISTENT_ID);
780         ClientResponse<String> res =
781                 client.update(NON_EXISTENT_ID, multipart);
782         int statusCode = res.getStatus();
783
784         // Check the status code of the response: does it match
785         // the expected response(s)?
786         if (logger.isDebugEnabled()) {
787             logger.debug(testName + ": status = " + statusCode);
788         }
789         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
790                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
791         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
792     }
793
794     // ---------------------------------------------------------------
795     // CRUD tests : DELETE tests
796     // ---------------------------------------------------------------
797     
798     // Success outcomes
799     
800     /* (non-Javadoc)
801      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
802      */
803     @Override
804     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
805     dependsOnMethods = {"create", "readList", "testSubmitRequest", "update", "verifyReadOnlyCoreFields"})
806     public void delete(String testName) throws Exception {
807
808         if (logger.isDebugEnabled()) {
809             logger.debug(testBanner(testName, CLASS_NAME));
810         }
811         // Perform setup.
812         setupDelete();
813
814         // Submit the request to the service and store the response.
815         IntakeClient client = new IntakeClient();
816         ClientResponse<Response> res = client.delete(knownResourceId);
817         int statusCode = res.getStatus();
818
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);
823         }
824         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
825                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
826         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
827     }
828
829     // Failure outcomes
830     
831     /* (non-Javadoc)
832      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
833      */
834     @Override
835     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
836     dependsOnMethods = {"delete"})
837     public void deleteNonExistent(String testName) throws Exception {
838
839         if (logger.isDebugEnabled()) {
840             logger.debug(testBanner(testName, CLASS_NAME));
841         }
842         // Perform setup.
843         setupDeleteNonExistent();
844
845         // Submit the request to the service and store the response.
846         IntakeClient client = new IntakeClient();
847         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
848         int statusCode = res.getStatus();
849
850         // Check the status code of the response: does it match
851         // the expected response(s)?
852         if (logger.isDebugEnabled()) {
853             logger.debug(testName + ": status = " + statusCode);
854         }
855         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
856                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
857         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
858     }
859
860     // ---------------------------------------------------------------
861     // Utility tests : tests of code used in tests above
862     // ---------------------------------------------------------------
863     
864     /**
865      * Tests the code for manually submitting data that is used by several
866      * of the methods above.
867      */
868     @Test(dependsOnMethods = {"create", "read"})
869     public void testSubmitRequest() {
870
871         // Expected status code: 200 OK
872         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
873
874         // Submit the request to the service and store the response.
875         String method = ServiceRequestType.READ.httpMethodName();
876         String url = getResourceURL(knownResourceId);
877         int statusCode = submitRequest(method, url);
878
879         // Check the status code of the response: does it match
880         // the expected response(s)?
881         if (logger.isDebugEnabled()) {
882             logger.debug("testSubmitRequest: url=" + url
883                     + " status=" + statusCode);
884         }
885         Assert.assertEquals(statusCode, EXPECTED_STATUS);
886
887     }
888
889     // ---------------------------------------------------------------
890     // Utility methods used by tests above
891     // ---------------------------------------------------------------
892     
893     /* (non-Javadoc)
894      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
895      */
896     @Override
897     public String getServicePathComponent() {
898         return IntakeClient.SERVICE_PATH_COMPONENT;
899     }
900
901     /**
902      * Creates the intake instance.
903      *
904      * @param identifier the identifier
905      * @return the multipart output
906      */
907     @Override
908     protected PoxPayloadOut createInstance(String identifier) {
909         return createIntakeInstance(
910                 "entryNumber-" + identifier,
911                 "entryDate-" + identifier,
912                 "depositor-" + identifier);
913     }
914
915     /**
916      * Creates the intake instance.
917      *
918      * @param entryNumber the entry number
919      * @param entryDate the entry date
920      * @param depositor the depositor
921      * @return the multipart output
922      */
923     private PoxPayloadOut createIntakeInstance(String entryNumber,
924             String entryDate,
925             String depositor) {
926         IntakesCommon intake = new IntakesCommon();
927         intake.setEntryNumber(entryNumber);
928         intake.setEntryDate(entryDate);
929         intake.setDepositor(depositor);
930
931         EntryMethodList entryMethodsList = new EntryMethodList();
932         List<String> entryMethods = entryMethodsList.getEntryMethod();
933         entryMethods.add("Left at doorstep");
934         entryMethods.add("Received via post");
935         intake.setEntryMethods(entryMethodsList);
936
937         FieldCollectionEventNameList eventNamesList = new FieldCollectionEventNameList();
938         List<String> eventNames = eventNamesList.getFieldCollectionEventName();
939         // FIXME Use properly formatted refNames for representative event names
940         // in this example test record. The following are mere placeholders.
941         eventNames.add("Field Collection Event Name-1");
942         eventNames.add("Field Collection Event Name-2");
943         intake.setFieldCollectionEventNames(eventNamesList);
944
945         CurrentLocationGroupList currentLocationGroupList = new CurrentLocationGroupList();
946         List<CurrentLocationGroup> currentLocationGroups = currentLocationGroupList.getCurrentLocationGroup();
947         CurrentLocationGroup currentLocationGroup = new CurrentLocationGroup();
948         currentLocationGroup.setCurrentLocation("upstairs");
949         currentLocationGroup.setCurrentLocationFitness("suitable");
950         currentLocationGroup.setCurrentLocationNote("A most suitable location.");
951         currentLocationGroups.add(currentLocationGroup);
952         intake.setCurrentLocationGroupList(currentLocationGroupList);
953
954         intake.setEntryNote(getUTF8DataFragment());
955
956         PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
957         PayloadOutputPart commonPart =
958                 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
959         commonPart.setLabel(new IntakeClient().getCommonPartName());
960
961         if (logger.isDebugEnabled()) {
962             logger.debug("to be created, intake common");
963             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
964         }
965
966         return multipart;
967     }
968 }