]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8d8a993f875514c673b112fbe914beace8d6bdfb
[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.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.LoanoutClient;
31 import org.collectionspace.services.client.PayloadInputPart;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.loanout.LoanedObjectStatusGroup;
37 import org.collectionspace.services.loanout.LoanedObjectStatusGroupList;
38 import org.collectionspace.services.loanout.LoansoutCommon;
39
40 import org.jboss.resteasy.client.ClientResponse;
41
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * LoanoutServiceTest, carries out tests against a
50  * deployed and running Loanout (aka Loans Out) Service.
51  *
52  * $LastChangedRevision$
53  * $LastChangedDate$
54  */
55 public class LoanoutServiceTest extends AbstractServiceTestImpl {
56
57     /** The logger. */
58     private final String CLASS_NAME = LoanoutServiceTest.class.getName();
59     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60     /** The known resource id. */
61     private String knownResourceId = null;
62
63     /* (non-Javadoc)
64      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
65      */
66     @Override
67     protected CollectionSpaceClient getClientInstance() {
68         return new LoanoutClient();
69     }
70
71     /* (non-Javadoc)
72      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
73      */
74     @Override
75     protected AbstractCommonList getAbstractCommonList(
76             ClientResponse<AbstractCommonList> response) {
77         return response.getEntity(AbstractCommonList.class);
78     }
79
80     // ---------------------------------------------------------------
81     // CRUD tests : CREATE tests
82     // ---------------------------------------------------------------
83     // Success outcomes
84     /* (non-Javadoc)
85      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
86      */
87     @Override
88     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
89     public void create(String testName) throws Exception {
90
91         if (logger.isDebugEnabled()) {
92             logger.debug(testBanner(testName, CLASS_NAME));
93         }
94         // Perform setup, such as initializing the type of service request
95         // (e.g. CREATE, DELETE), its valid and expected status codes, and
96         // its associated HTTP method name (e.g. POST, DELETE).
97         setupCreate();
98
99         // Submit the request to the service and store the response.
100         LoanoutClient client = new LoanoutClient();
101         String identifier = createIdentifier();
102         PoxPayloadOut multipart = createLoanoutInstance(identifier);
103         ClientResponse<Response> res = client.create(multipart);
104
105         int statusCode = res.getStatus();
106
107         // Check the status code of the response: does it match
108         // the expected response(s)?
109         //
110         // Specifically:
111         // Does it fall within the set of valid status codes?
112         // Does it exactly match the expected status code?
113         if (logger.isDebugEnabled()) {
114             logger.debug(testName + ": status = " + statusCode);
115         }
116         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
117                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
118         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
119
120         // Store the ID returned from the first resource created
121         // for additional tests below.
122         if (knownResourceId == null) {
123             knownResourceId = extractId(res);
124             if (logger.isDebugEnabled()) {
125                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
126             }
127         }
128
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));
132     }
133
134     /* (non-Javadoc)
135      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
136      */
137     @Override
138     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
139     dependsOnMethods = {"create"})
140     public void createList(String testName) throws Exception {
141         for (int i = 0; i < 3; i++) {
142             create(testName);
143         }
144     }
145
146     // Failure outcomes
147     // Placeholders until the three tests below can be uncommented.
148     // See Issue CSPACE-401.
149     /* (non-Javadoc)
150      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
151      */
152     @Override
153     public void createWithEmptyEntityBody(String testName) throws Exception {
154         //Should this really be empty?
155     }
156
157     /* (non-Javadoc)
158      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
159      */
160     @Override
161     public void createWithMalformedXml(String testName) throws Exception {
162         //Should this really be empty?
163     }
164
165     /* (non-Javadoc)
166      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
167      */
168     @Override
169     public void createWithWrongXmlSchema(String testName) throws Exception {
170         //Should this really be empty?
171     }
172
173     /*
174     @Override
175     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
176     dependsOnMethods = {"create", "testSubmitRequest"})
177     public void createWithEmptyEntityBody(String testName) throws Exception {
178
179     if (logger.isDebugEnabled()) {
180     logger.debug(testBanner(testName, CLASS_NAME));
181     }
182     // Perform setup.
183     setupCreateWithEmptyEntityBody();
184
185     // Submit the request to the service and store the response.
186     String method = REQUEST_TYPE.httpMethodName();
187     String url = getServiceRootURL();
188     String mediaType = MediaType.APPLICATION_XML;
189     final String entity = "";
190     int statusCode = submitRequest(method, url, mediaType, entity);
191
192     // Check the status code of the response: does it match
193     // the expected response(s)?
194     if(logger.isDebugEnabled()){
195     logger.debug("createWithEmptyEntityBody url=" + url +
196     " status=" + statusCode);
197     }
198     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201     }
202
203     @Override
204     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
205     dependsOnMethods = {"create", "testSubmitRequest"})
206     public void createWithMalformedXml(String testName) throws Exception {
207
208     if (logger.isDebugEnabled()) {
209     logger.debug(testBanner(testName, CLASS_NAME));
210     }
211     // Perform setup.
212     setupCreateWithMalformedXml();
213
214     // Submit the request to the service and store the response.
215     String method = REQUEST_TYPE.httpMethodName();
216     String url = getServiceRootURL();
217     String mediaType = MediaType.APPLICATION_XML;
218     final String entity = MALFORMED_XML_DATA; // Constant from base class.
219     int statusCode = submitRequest(method, url, mediaType, entity);
220
221     // Check the status code of the response: does it match
222     // the expected response(s)?
223     if(logger.isDebugEnabled()){
224     logger.debug(testName + ": url=" + url +
225     " status=" + statusCode);
226     }
227     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
230     }
231
232     @Override
233     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
234     dependsOnMethods = {"create", "testSubmitRequest"})
235     public void createWithWrongXmlSchema(String testName) throws Exception {
236
237     if (logger.isDebugEnabled()) {
238     logger.debug(testBanner(testName, CLASS_NAME));
239     }
240     // Perform setup.
241     setupCreateWithWrongXmlSchema();
242
243     // Submit the request to the service and store the response.
244     String method = REQUEST_TYPE.httpMethodName();
245     String url = getServiceRootURL();
246     String mediaType = MediaType.APPLICATION_XML;
247     final String entity = WRONG_XML_SCHEMA_DATA;
248     int statusCode = submitRequest(method, url, mediaType, entity);
249
250     // Check the status code of the response: does it match
251     // the expected response(s)?
252     if(logger.isDebugEnabled()){
253     logger.debug(testName + ": url=" + url +
254     " status=" + statusCode);
255     }
256     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
257     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
258     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
259     }
260      */
261     // ---------------------------------------------------------------
262     // CRUD tests : READ tests
263     // ---------------------------------------------------------------
264     // Success outcomes
265     /* (non-Javadoc)
266      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
267      */
268     @Override
269     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
270     dependsOnMethods = {"create"})
271     public void read(String testName) throws Exception {
272
273         if (logger.isDebugEnabled()) {
274             logger.debug(testBanner(testName, CLASS_NAME));
275         }
276         // Perform setup.
277         setupRead();
278
279         // Submit the request to the service and store the response.
280         LoanoutClient client = new LoanoutClient();
281         ClientResponse<String> res = client.read(knownResourceId);
282         int statusCode = res.getStatus();
283
284         // Check the status code of the response: does it match
285         // the expected response(s)?
286         if (logger.isDebugEnabled()) {
287             logger.debug(testName + ": status = " + statusCode);
288         }
289         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
290                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
291         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
292
293         // Get the common part of the response and verify that it is not null.
294         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
295         PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
296         LoansoutCommon loanoutCommon = null;
297         if (payloadInputPart != null) {
298             loanoutCommon = (LoansoutCommon) payloadInputPart.getBody();
299         }
300         Assert.assertNotNull(loanoutCommon);
301
302         // Check selected fields in the common part.
303         Assert.assertNotNull(loanoutCommon.getLoanOutNumber());
304
305         LoanedObjectStatusGroupList statusGroupList = loanoutCommon.getLoanedObjectStatusGroupList();
306         Assert.assertNotNull(statusGroupList);
307         List<LoanedObjectStatusGroup> statusGroups = statusGroupList.getLoanedObjectStatusGroup();
308         Assert.assertNotNull(statusGroups);
309         Assert.assertTrue(statusGroups.size() > 0);
310         LoanedObjectStatusGroup statusGroup = statusGroups.get(0);
311         Assert.assertNotNull(statusGroup);
312         Assert.assertNotNull(statusGroup.getLoanedObjectStatus());
313
314         // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
315         if (logger.isDebugEnabled()) {
316             logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
317                     + "UTF-8 data received=" + loanoutCommon.getLoanOutNote());
318         }
319         Assert.assertEquals(loanoutCommon.getLoanOutNote(), getUTF8DataFragment(),
320                 "UTF-8 data retrieved '" + loanoutCommon.getLoanOutNote()
321                 + "' does not match expected data '" + getUTF8DataFragment());
322     }
323
324     // Failure outcomes
325     /* (non-Javadoc)
326      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
327      */
328     @Override
329     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
330     dependsOnMethods = {"read"})
331     public void readNonExistent(String testName) throws Exception {
332
333         if (logger.isDebugEnabled()) {
334             logger.debug(testBanner(testName, CLASS_NAME));
335         }
336         // Perform setup.
337         setupReadNonExistent();
338
339         // Submit the request to the service and store the response.
340         LoanoutClient client = new LoanoutClient();
341         ClientResponse<String> res = client.read(NON_EXISTENT_ID);
342         int statusCode = res.getStatus();
343
344         // Check the status code of the response: does it match
345         // the expected response(s)?
346         if (logger.isDebugEnabled()) {
347             logger.debug(testName + ": status = " + statusCode);
348         }
349         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
350                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
351         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
352     }
353
354     // ---------------------------------------------------------------
355     // CRUD tests : READ_LIST tests
356     // ---------------------------------------------------------------
357     // Success outcomes
358     /* (non-Javadoc)
359      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
360      */
361     @Override
362     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
363     dependsOnMethods = {"createList", "read"})
364     public void readList(String testName) throws Exception {
365
366         if (logger.isDebugEnabled()) {
367             logger.debug(testBanner(testName, CLASS_NAME));
368         }
369         // Perform setup.
370         setupReadList();
371
372         // Submit the request to the service and store the response.
373         LoanoutClient client = new LoanoutClient();
374         ClientResponse<AbstractCommonList> res = client.readList();
375         AbstractCommonList list = res.getEntity();
376         int statusCode = res.getStatus();
377
378         // Check the status code of the response: does it match
379         // the expected response(s)?
380         if (logger.isDebugEnabled()) {
381             logger.debug(testName + ": status = " + statusCode);
382         }
383         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
384                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
385         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
386
387         // Optionally output additional data about list members for debugging.
388         boolean iterateThroughList = true;
389         if(iterateThroughList && logger.isDebugEnabled()){
390                 ListItemsInAbstractCommonList(list, logger, testName);
391         }
392
393     }
394
395     // Failure outcomes
396     // None at present.
397     // ---------------------------------------------------------------
398     // CRUD tests : UPDATE tests
399     // ---------------------------------------------------------------
400     // Success outcomes
401     /* (non-Javadoc)
402      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
403      */
404     @Override
405     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
406     dependsOnMethods = {"read"})
407     public void update(String testName) throws Exception {
408
409         if (logger.isDebugEnabled()) {
410             logger.debug(testBanner(testName, CLASS_NAME));
411         }
412         // Perform setup.
413         setupUpdate();
414
415         // Retrieve the contents of a resource to update.
416         LoanoutClient client = new LoanoutClient();
417         ClientResponse<String> res = client.read(knownResourceId);
418         if (logger.isDebugEnabled()) {
419             logger.debug(testName + ": read status = " + res.getStatus());
420         }
421         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
422
423         if (logger.isDebugEnabled()) {
424             logger.debug("got object to update with ID: " + knownResourceId);
425         }
426
427         // Extract the common part from the response.
428         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
429         PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
430         LoansoutCommon loanoutCommon = null;
431         if (payloadInputPart != null) {
432             loanoutCommon = (LoansoutCommon) payloadInputPart.getBody();
433         }
434         Assert.assertNotNull(loanoutCommon);
435
436         // Update the content of this resource.
437         loanoutCommon.setLoanOutNumber("updated-" + loanoutCommon.getLoanOutNumber());
438         loanoutCommon.setLoanReturnDate("updated-" + loanoutCommon.getLoanReturnDate());
439         LoanedObjectStatusGroupList statusGroupList = loanoutCommon.getLoanedObjectStatusGroupList();
440         Assert.assertNotNull(statusGroupList);
441         List<LoanedObjectStatusGroup> statusGroups = statusGroupList.getLoanedObjectStatusGroup();
442         Assert.assertNotNull(statusGroups);
443         Assert.assertTrue(statusGroups.size() > 0);
444         LoanedObjectStatusGroup statusGroup = statusGroups.get(0);
445         Assert.assertNotNull(statusGroup);
446         String loanedObjectStatus = statusGroup.getLoanedObjectStatus();
447         Assert.assertNotNull(loanedObjectStatus);
448         String updatedLoanedObjectStatus = "updated-" + loanedObjectStatus;
449         statusGroups.get(0).setLoanedObjectStatus(updatedLoanedObjectStatus);
450         loanoutCommon.setLoanedObjectStatusGroupList(statusGroupList);
451         if (logger.isDebugEnabled()) {
452             logger.debug("to be updated object");
453             logger.debug(objectAsXmlString(loanoutCommon, LoansoutCommon.class));
454         }
455         loanoutCommon.setLoanOutNote("updated-" + loanoutCommon.getLoanOutNote());
456
457         // Submit the updated resource in an update request to the service and store the response.
458         PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
459         PayloadOutputPart commonPart = output.addPart(loanoutCommon, MediaType.APPLICATION_XML_TYPE);
460         commonPart.setLabel(client.getCommonPartName());
461         res = client.update(knownResourceId, output);
462         int statusCode = res.getStatus();
463
464         // Check the status code of the response: does it match the expected response(s)?
465         if (logger.isDebugEnabled()) {
466             logger.debug(testName + ": status = " + statusCode);
467         }
468         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
469                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
470         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
471
472         // Extract the updated common part from the response.
473         input = new PoxPayloadIn(res.getEntity());
474         payloadInputPart = input.getPart(client.getCommonPartName());
475         LoansoutCommon updatedLoanoutCommon = null;
476         if (payloadInputPart != null) {
477             updatedLoanoutCommon = (LoansoutCommon) payloadInputPart.getBody();
478         }
479         Assert.assertNotNull(updatedLoanoutCommon);
480
481         // Check selected fields in the updated resource.
482         Assert.assertEquals(updatedLoanoutCommon.getLoanReturnDate(),
483                 loanoutCommon.getLoanReturnDate(),
484                 "Data in updated object did not match submitted data.");
485
486         LoanedObjectStatusGroupList updatedStatusGroupList =
487                 updatedLoanoutCommon.getLoanedObjectStatusGroupList();
488         Assert.assertNotNull(updatedStatusGroupList);
489         List<LoanedObjectStatusGroup> updatedStatusGroups =
490                 updatedStatusGroupList.getLoanedObjectStatusGroup();
491         Assert.assertNotNull(updatedStatusGroups);
492         Assert.assertTrue(updatedStatusGroups.size() > 0);
493         Assert.assertNotNull(updatedStatusGroups.get(0));
494         Assert.assertEquals(updatedLoanedObjectStatus,
495                 updatedStatusGroups.get(0).getLoanedObjectStatus(),
496                 "Data in updated object did not match submitted data.");
497
498         // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
499         if (logger.isDebugEnabled()) {
500             logger.debug("UTF-8 data sent=" + loanoutCommon.getLoanOutNote() + "\n"
501                     + "UTF-8 data received=" + updatedLoanoutCommon.getLoanOutNote());
502         }
503         Assert.assertTrue(updatedLoanoutCommon.getLoanOutNote().contains(getUTF8DataFragment()),
504                 "UTF-8 data retrieved '" + updatedLoanoutCommon.getLoanOutNote()
505                 + "' does not contain expected data '" + getUTF8DataFragment());
506         Assert.assertEquals(updatedLoanoutCommon.getLoanOutNote(),
507                 loanoutCommon.getLoanOutNote(),
508                 "Data in updated object did not match submitted data.");
509     }
510
511     // Failure outcomes
512     // Placeholders until the three tests below can be uncommented.
513     // See Issue CSPACE-401.
514     /* (non-Javadoc)
515      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
516      */
517     @Override
518     public void updateWithEmptyEntityBody(String testName) throws Exception {
519         //Should this really be empty?
520     }
521
522     /* (non-Javadoc)
523      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
524      */
525     @Override
526     public void updateWithMalformedXml(String testName) throws Exception {
527         //Should this really be empty?
528     }
529
530     /* (non-Javadoc)
531      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
532      */
533     @Override
534     public void updateWithWrongXmlSchema(String testName) throws Exception {
535         //Should this really be empty?
536     }
537
538     /*
539     @Override
540     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
541     dependsOnMethods = {"create", "update", "testSubmitRequest"})
542     public void updateWithEmptyEntityBody(String testName) throws Exception {
543
544     if (logger.isDebugEnabled()) {
545     logger.debug(testBanner(testName, CLASS_NAME));
546     }
547     // Perform setup.
548     setupUpdateWithEmptyEntityBody();
549
550     // Submit the request to the service and store the response.
551     String method = REQUEST_TYPE.httpMethodName();
552     String url = getResourceURL(knownResourceId);
553     String mediaType = MediaType.APPLICATION_XML;
554     final String entity = "";
555     int statusCode = submitRequest(method, url, mediaType, entity);
556
557     // Check the status code of the response: does it match
558     // the expected response(s)?
559     if(logger.isDebugEnabled()){
560     logger.debug(testName + ": url=" + url +
561     " status=" + statusCode);
562     }
563     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
564     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
565     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
566     }
567
568     @Override
569     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
570     dependsOnMethods = {"create", "update", "testSubmitRequest"})
571     public void updateWithMalformedXml(String testName) throws Exception {
572
573     if (logger.isDebugEnabled()) {
574     logger.debug(testBanner(testName, CLASS_NAME));
575     }
576     // Perform setup.
577     setupUpdateWithMalformedXml();
578
579     // Submit the request to the service and store the response.
580     String method = REQUEST_TYPE.httpMethodName();
581     String url = getResourceURL(knownResourceId);
582     String mediaType = MediaType.APPLICATION_XML;
583     final String entity = MALFORMED_XML_DATA;
584     int statusCode = submitRequest(method, url, mediaType, entity);
585
586     // Check the status code of the response: does it match
587     // the expected response(s)?
588     if(logger.isDebugEnabled()){
589     logger.debug(testName + ": url=" + url +
590     " status=" + statusCode);
591     }
592     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
593     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
594     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
595     }
596
597     @Override
598     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
599     dependsOnMethods = {"create", "update", "testSubmitRequest"})
600     public void updateWithWrongXmlSchema(String testName) throws Exception {
601
602     if (logger.isDebugEnabled()) {
603     logger.debug(testBanner(testName, CLASS_NAME));
604     }
605     // Perform setup.
606     setupUpdateWithWrongXmlSchema();
607
608     // Submit the request to the service and store the response.
609     String method = REQUEST_TYPE.httpMethodName();
610     String url = getResourceURL(knownResourceId);
611     String mediaType = MediaType.APPLICATION_XML;
612     final String entity = WRONG_XML_SCHEMA_DATA;
613     int statusCode = submitRequest(method, url, mediaType, entity);
614
615     // Check the status code of the response: does it match
616     // the expected response(s)?
617     if(logger.isDebugEnabled()){
618     logger.debug(testName + ": url=" + url +
619     " status=" + statusCode);
620     }
621     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
622     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
623     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
624     }
625      */
626
627     /* (non-Javadoc)
628      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
629      */
630     @Override
631     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
632     dependsOnMethods = {"update", "testSubmitRequest"})
633     public void updateNonExistent(String testName) throws Exception {
634
635         if (logger.isDebugEnabled()) {
636             logger.debug(testBanner(testName, CLASS_NAME));
637         }
638         // Perform setup.
639         setupUpdateNonExistent();
640
641         // Submit the request to the service and store the response.
642         // Note: The ID used in this 'create' call may be arbitrary.
643         // The only relevant ID may be the one used in update(), below.
644         LoanoutClient client = new LoanoutClient();
645         PoxPayloadOut multipart = createLoanoutInstance(NON_EXISTENT_ID);
646         ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
647         int statusCode = res.getStatus();
648
649         // Check the status code of the response: does it match
650         // the expected response(s)?
651         if (logger.isDebugEnabled()) {
652             logger.debug(testName + ": status = " + statusCode);
653         }
654         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
655                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
656         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
657     }
658
659     // ---------------------------------------------------------------
660     // CRUD tests : DELETE tests
661     // ---------------------------------------------------------------
662     // Success outcomes
663     /* (non-Javadoc)
664      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
665      */
666     @Override
667     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
668     dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
669     public void delete(String testName) throws Exception {
670
671         if (logger.isDebugEnabled()) {
672             logger.debug(testBanner(testName, CLASS_NAME));
673         }
674         // Perform setup.
675         setupDelete();
676
677         // Submit the request to the service and store the response.
678         LoanoutClient client = new LoanoutClient();
679         ClientResponse<Response> res = client.delete(knownResourceId);
680         int statusCode = res.getStatus();
681
682         // Check the status code of the response: does it match
683         // the expected response(s)?
684         if (logger.isDebugEnabled()) {
685             logger.debug(testName + ": status = " + statusCode);
686         }
687         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
688                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
689         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
690     }
691
692     // Failure outcomes
693     /* (non-Javadoc)
694      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
695      */
696     @Override
697     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
698     dependsOnMethods = {"delete"})
699     public void deleteNonExistent(String testName) throws Exception {
700
701         if (logger.isDebugEnabled()) {
702             logger.debug(testBanner(testName, CLASS_NAME));
703         }
704         // Perform setup.
705         setupDeleteNonExistent();
706
707         // Submit the request to the service and store the response.
708         LoanoutClient client = new LoanoutClient();
709         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
710         int statusCode = res.getStatus();
711
712         // Check the status code of the response: does it match
713         // the expected response(s)?
714         if (logger.isDebugEnabled()) {
715             logger.debug(testName + ": status = " + statusCode);
716         }
717         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
718                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
719         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
720     }
721
722     // ---------------------------------------------------------------
723     // Utility tests : tests of code used in tests above
724     // ---------------------------------------------------------------
725     /**
726      * Tests the code for manually submitting data that is used by several
727      * of the methods above.
728      */
729     @Test(dependsOnMethods = {"create", "read"})
730     public void testSubmitRequest() {
731
732         // Expected status code: 200 OK
733         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
734
735         // Submit the request to the service and store the response.
736         String method = ServiceRequestType.READ.httpMethodName();
737         String url = getResourceURL(knownResourceId);
738         int statusCode = submitRequest(method, url);
739
740         // Check the status code of the response: does it match
741         // the expected response(s)?
742         if (logger.isDebugEnabled()) {
743             logger.debug("testSubmitRequest: url=" + url
744                     + " status=" + statusCode);
745         }
746         Assert.assertEquals(statusCode, EXPECTED_STATUS);
747
748     }
749
750     // ---------------------------------------------------------------
751     // Utility methods used by tests above
752     // ---------------------------------------------------------------
753     /* (non-Javadoc)
754      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
755      */
756     @Override
757     public String getServicePathComponent() {
758         return LoanoutClient.SERVICE_PATH_COMPONENT;
759     }
760
761     @Override
762     protected PoxPayloadOut createInstance(String identifier) {
763         return createLoanoutInstance(identifier);
764     }
765     
766     /**
767      * Creates the loanout instance.
768      *
769      * @param identifier the identifier
770      * @return the multipart output
771      */
772     private PoxPayloadOut createLoanoutInstance(String identifier) {
773         return createLoanoutInstance(
774                 "loanoutNumber-" + identifier,
775                 "returnDate-" + identifier);
776     }
777
778     /**
779      * Creates the loanout instance.
780      *
781      * @param loanOutNumber the loan out number
782      * @param returnDate the return date
783      * @return the multipart output
784      */
785     private PoxPayloadOut createLoanoutInstance(String loanOutNumber,
786             String returnDate) {
787         LoansoutCommon loanoutCommon = new LoansoutCommon();
788         loanoutCommon.setLoanOutNumber(loanOutNumber);
789         loanoutCommon.setLoanReturnDate(returnDate);
790         loanoutCommon.setBorrower(
791                 "urn:cspace:org.collectionspace.demo:orgauthority:name(TestOrgAuth):organization:name(Northern Climes Museum)'Northern Climes Museum'");
792         loanoutCommon.setBorrowersContact(
793                 "urn:cspace:org.collectionspace.demo:personauthority:name(TestPersonAuth):person:name(Chris Contact)'Chris Contact'");
794         loanoutCommon.setLoanPurpose("Allow people in cold climes to share the magic of Surfboards of the 1960s.");
795         LoanedObjectStatusGroupList statusGroupList = new LoanedObjectStatusGroupList();
796         List<LoanedObjectStatusGroup> statusGroups = statusGroupList.getLoanedObjectStatusGroup();
797         LoanedObjectStatusGroup statusGroup = new LoanedObjectStatusGroup();
798         statusGroup.setLoanedObjectStatus("returned");
799         statusGroup.setLoanedObjectStatusNote("Left under the front mat.");
800         statusGroups.add(statusGroup);
801         loanoutCommon.setLoanedObjectStatusGroupList(statusGroupList);
802         loanoutCommon.setLoanOutNote(getUTF8DataFragment());  // For UTF-8 tests
803
804         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
805         PayloadOutputPart commonPart =
806                 multipart.addPart(loanoutCommon, MediaType.APPLICATION_XML_TYPE);
807         commonPart.setLabel(new LoanoutClient().getCommonPartName());
808
809         if (logger.isDebugEnabled()) {
810             logger.debug("to be created, loanout common");
811             logger.debug(objectAsXmlString(loanoutCommon, LoansoutCommon.class));
812             // logger.debug(multipart.toXML());
813         }
814
815         return multipart;
816     }
817
818     @Override
819     protected String getServiceName() {
820         return LoanoutClient.SERVICE_NAME;
821     }
822 }