]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
903634d33f351c56a59235bdc48edcb9af559733
[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.RelationClient;
30 import org.collectionspace.services.relation.RelationsCommon;
31 import org.collectionspace.services.relation.RelationsCommonList;
32 import org.collectionspace.services.relation.RelationshipType;
33
34 import org.jboss.resteasy.client.ClientResponse;
35
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
41
42 /**
43  * RelationServiceTest, carries out tests against a
44  * deployed and running Relation Service.
45  * 
46  * $LastChangedRevision$
47  * $LastChangedDate$
48  */
49 public class RelationServiceTest extends AbstractServiceTest {
50
51     private RelationClient client = new RelationClient();
52     final String SERVICE_PATH_COMPONENT = "relations";
53     private String knownResourceId = null;
54
55     // ---------------------------------------------------------------
56     // CRUD tests : CREATE tests
57     // ---------------------------------------------------------------
58     // Success outcomes
59     @Override
60     @Test
61     public void create() {
62
63         // Perform setup, such as initializing the type of service request
64         // (e.g. CREATE, DELETE), its valid and expected status codes, and
65         // its associated HTTP method name (e.g. POST, DELETE).
66         setupCreate();
67
68         // Submit the request to the service and store the response.
69         String identifier = createIdentifier();
70         MultipartOutput multipart = createRelationInstance(identifier);
71         ClientResponse<Response> res = client.create(multipart);
72         int statusCode = res.getStatus();
73
74         // Check the status code of the response: does it match
75         // the expected response(s)?
76         //
77         // Does it fall within the set of valid status codes?
78         // Does it exactly match the expected status code?
79         verbose("create: status = " + statusCode);
80         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
81                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
82         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
83
84         // Store the ID returned from this create operation for
85         // additional tests below.
86         knownResourceId = extractId(res);
87         verbose("create: knownResourceId=" + knownResourceId);
88     }
89
90     @Override
91     @Test(dependsOnMethods = {"create"})
92     public void createList() {
93         for(int i = 0; i < 3; i++){
94             create();
95         }
96     }
97
98     // Failure outcomes
99     // Placeholders until the three tests below can be uncommented.
100     // See Issue CSPACE-401.
101     public void createWithEmptyEntityBody() {
102     }
103
104     public void createWithMalformedXml() {
105     }
106
107     public void createWithWrongXmlSchema() {
108     }
109
110     /*
111     @Override
112     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
113     public void createWithEmptyEntityBody() {
114     
115     // Perform setup.
116     setupCreateWithEmptyEntityBody();
117
118     // Submit the request to the service and store the response.
119     String method = REQUEST_TYPE.httpMethodName();
120     String url = getServiceRootURL();
121     String mediaType = MediaType.APPLICATION_XML;
122     final String entity = "";
123     int statusCode = submitRequest(method, url, mediaType, entity);
124
125     // Check the status code of the response: does it match
126     // the expected response(s)?
127     verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
128     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
129     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
130     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
131     }
132
133     @Override
134     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
135     public void createWithMalformedXml() {
136     
137     // Perform setup.
138     setupCreateWithMalformedXml();
139
140     // Submit the request to the service and store the response.
141     String method = REQUEST_TYPE.httpMethodName();
142     String url = getServiceRootURL();
143     String mediaType = MediaType.APPLICATION_XML;
144     final String entity = MALFORMED_XML_DATA; // Constant from base class.
145     int statusCode = submitRequest(method, url, mediaType, entity);
146
147     // Check the status code of the response: does it match
148     // the expected response(s)?
149     verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
150     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
151     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
152     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
153     }
154
155     @Override
156     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
157     public void createWithWrongXmlSchema() {
158     
159     // Perform setup.
160     setupCreateWithWrongXmlSchema();
161
162     // Submit the request to the service and store the response.
163     String method = REQUEST_TYPE.httpMethodName();
164     String url = getServiceRootURL();
165     String mediaType = MediaType.APPLICATION_XML;
166     final String entity = WRONG_XML_SCHEMA_DATA;
167     int statusCode = submitRequest(method, url, mediaType, entity);
168
169     // Check the status code of the response: does it match
170     // the expected response(s)?
171     verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
172     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175     }
176      */
177     // ---------------------------------------------------------------
178     // CRUD tests : READ tests
179     // ---------------------------------------------------------------
180     // Success outcomes
181     @Override
182     @Test(dependsOnMethods = {"create"})
183     public void read() {
184
185         // Perform setup.
186         setupRead();
187
188         // Submit the request to the service and store the response.
189         ClientResponse<MultipartInput> res = client.read(knownResourceId);
190         int statusCode = res.getStatus();
191
192         // Check the status code of the response: does it match
193         // the expected response(s)?
194         verbose("read: status = " + statusCode);
195         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
198
199         // Verify that the resource identifier 
200         //FIXME: remove the following try catch once Aron fixes signatures
201         try{
202             MultipartInput input = (MultipartInput) res.getEntity();
203             RelationsCommon relation = (RelationsCommon) extractPart(input,
204                     client.getCommonPartName(), RelationsCommon.class);
205             Assert.assertNotNull(relation);
206         } catch(Exception e){
207             throw new RuntimeException(e);
208         }
209
210     }
211
212     // Failure outcomes
213     @Override
214     @Test(dependsOnMethods = {"read"})
215     public void readNonExistent() {
216
217         // Perform setup.
218         setupReadNonExistent();
219
220         // Submit the request to the service and store the response.
221         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
222         int statusCode = res.getStatus();
223
224         // Check the status code of the response: does it match
225         // the expected response(s)?
226         verbose("readNonExistent: status = " + res.getStatus());
227         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
230     }
231
232     // ---------------------------------------------------------------
233     // CRUD tests : READ_LIST tests
234     // ---------------------------------------------------------------
235     // Success outcomes
236     @Override
237     @Test(dependsOnMethods = {"createList", "read"})
238     public void readList() {
239
240         // Perform setup.
241         setupReadList();
242
243         // Submit the request to the service and store the response.
244         ClientResponse<RelationsCommonList> res = client.readList();
245         RelationsCommonList list = res.getEntity();
246         int statusCode = res.getStatus();
247
248         // Check the status code of the response: does it match
249         // the expected response(s)?
250         verbose("readList: status = " + res.getStatus());
251         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
252                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
253         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
254
255         // Optionally output additional data about list members for debugging.
256         boolean iterateThroughList = false;
257         if(iterateThroughList && logger.isDebugEnabled()){
258             List<RelationsCommonList.RelationListItem> items =
259                     list.getRelationListItem();
260             int i = 0;
261             for(RelationsCommonList.RelationListItem item : items){
262                 verbose("readList: list-item[" + i + "] csid=" +
263                         item.getCsid());
264                 verbose("readList: list-item[" + i + "] URI=" +
265                         item.getUri());
266                 i++;
267             }
268         }
269
270     }
271
272     // Failure outcomes
273     // None at present.
274     // ---------------------------------------------------------------
275     // CRUD tests : UPDATE tests
276     // ---------------------------------------------------------------
277     // Success outcomes
278     @Override
279     @Test(dependsOnMethods = {"read"})
280     public void update() {
281
282         // Perform setup.
283         setupUpdate();
284         try{
285             // Retrieve an existing resource that we can update.
286             ClientResponse<MultipartInput> res =
287                     client.read(knownResourceId);
288             verbose("update: read status = " + res.getStatus());
289             Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
290             verbose("Got object to update with ID: " + knownResourceId);
291             MultipartInput input = (MultipartInput) res.getEntity();
292             RelationsCommon relation = (RelationsCommon) extractPart(input,
293                         client.getCommonPartName(), RelationsCommon.class);
294             Assert.assertNotNull(relation);
295
296             // Update the content of this resource.
297             relation.setDocumentId1("updated-" + relation.getDocumentId1());
298             relation.setDocumentType1("updated-" + relation.getDocumentType1());
299             relation.setDocumentId2("updated-" + relation.getDocumentId2());
300             relation.setDocumentType2("updated-" + relation.getDocumentType2());
301             verbose("updated object", relation, RelationsCommon.class);
302
303             // Submit the request to the service and store the response.
304             MultipartOutput output = new MultipartOutput();
305             OutputPart commonPart = output.addPart(relation, MediaType.APPLICATION_XML_TYPE);
306             commonPart.getHeaders().add("label", client.getCommonPartName());
307             res = client.update(knownResourceId, output);
308             int statusCode = res.getStatus();
309             // Check the status code of the response: does it match the expected response(s)?
310             verbose("update: status = " + res.getStatus());
311             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
312                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
313             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
314
315                         input = (MultipartInput) res.getEntity();
316                         RelationsCommon updatedObject = (RelationsCommon) extractPart(
317                                         input, client.getCommonPartName(),
318                                         RelationsCommon.class);
319                         Assert.assertNotNull(updatedObject);
320
321             final String msg =
322                     "Data in updated object did not match submitted data.";
323             Assert.assertEquals(
324                     updatedObject.getDocumentId1(), relation.getDocumentId1(), msg);
325             Assert.assertEquals(
326                     updatedObject.getDocumentType1(), relation.getDocumentType1(), msg);
327             Assert.assertEquals(
328                     updatedObject.getDocumentId2(), relation.getDocumentId2(), msg);
329             Assert.assertEquals(
330                     updatedObject.getDocumentType2(), relation.getDocumentType2(), msg);
331         }catch(Exception e){
332             e.printStackTrace();
333         }
334
335     }
336
337     // Failure outcomes
338     // Placeholders until the three tests below can be uncommented.
339     // See Issue CSPACE-401.
340     public void updateWithEmptyEntityBody() {
341     }
342
343     public void updateWithMalformedXml() {
344     }
345
346     public void updateWithWrongXmlSchema() {
347     }
348
349     /*
350     @Override
351     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
352     public void updateWithEmptyEntityBody() {
353     
354     // Perform setup.
355     setupUpdateWithEmptyEntityBody();
356
357     // Submit the request to the service and store the response.
358     String method = REQUEST_TYPE.httpMethodName();
359     String url = getResourceURL(knownResourceId);
360     String mediaType = MediaType.APPLICATION_XML;
361     final String entity = "";
362     int statusCode = submitRequest(method, url, mediaType, entity);
363
364     // Check the status code of the response: does it match
365     // the expected response(s)?
366     verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
367     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
368     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
369     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
370     }
371
372     @Override
373     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
374     public void updateWithMalformedXml() {
375
376     // Perform setup.
377     setupUpdateWithMalformedXml();
378
379     // Submit the request to the service and store the response.
380     String method = REQUEST_TYPE.httpMethodName();
381     String url = getResourceURL(knownResourceId);
382     String mediaType = MediaType.APPLICATION_XML;
383     final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
384     int statusCode = submitRequest(method, url, mediaType, entity);
385
386     // Check the status code of the response: does it match
387     // the expected response(s)?
388     verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
389     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
390     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
391     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
392     }
393
394     @Override
395     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
396     public void updateWithWrongXmlSchema() {
397     
398     // Perform setup.
399     setupUpdateWithWrongXmlSchema();
400
401     // Submit the request to the service and store the response.
402     String method = REQUEST_TYPE.httpMethodName();
403     String url = getResourceURL(knownResourceId);
404     String mediaType = MediaType.APPLICATION_XML;
405     final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
406     int statusCode = submitRequest(method, url, mediaType, entity);
407
408     // Check the status code of the response: does it match
409     // the expected response(s)?
410     verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
411     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
412     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
413     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
414     }
415      */
416     @Override
417     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
418     public void updateNonExistent() {
419
420         // Perform setup.
421         setupUpdateNonExistent();
422
423         // Submit the request to the service and store the response.
424         // Note: The ID used in this 'create' call may be arbitrary.
425         // The only relevant ID may be the one used in update(), below.
426         MultipartOutput multipart = createRelationInstance(NON_EXISTENT_ID);
427         ClientResponse<MultipartInput> res =
428                 client.update(NON_EXISTENT_ID, multipart);
429         int statusCode = res.getStatus();
430
431         // Check the status code of the response: does it match
432         // the expected response(s)?
433         verbose("updateNonExistent: status = " + res.getStatus());
434         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
435                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
436         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
437     }
438
439     // ---------------------------------------------------------------
440     // CRUD tests : DELETE tests
441     // ---------------------------------------------------------------
442     // Success outcomes
443     @Override
444     @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
445     public void delete() {
446
447         // Perform setup.
448         setupDelete();
449
450         // Submit the request to the service and store the response.
451         ClientResponse<Response> res = client.delete(knownResourceId);
452         int statusCode = res.getStatus();
453
454         // Check the status code of the response: does it match
455         // the expected response(s)?
456         verbose("delete: status = " + res.getStatus());
457         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
458                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
459         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
460     }
461
462     // Failure outcomes
463     @Override
464     @Test(dependsOnMethods = {"delete"})
465     public void deleteNonExistent() {
466
467         // Perform setup.
468         setupDeleteNonExistent();
469
470         // Submit the request to the service and store the response.
471         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
472         int statusCode = res.getStatus();
473
474         // Check the status code of the response: does it match
475         // the expected response(s)?
476         verbose("deleteNonExistent: status = " + res.getStatus());
477         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
480     }
481
482     // ---------------------------------------------------------------
483     // RELATE_OBJECT tests
484     // ---------------------------------------------------------------
485     @Test(dependsOnMethods = {"create"})
486     public void relateObjects() {
487     }
488
489     // ---------------------------------------------------------------
490     // Utility tests : tests of code used in tests above
491     // ---------------------------------------------------------------
492     /**
493      * Tests the code for manually submitting data that is used by several
494      * of the methods above.
495      */
496     @Test(dependsOnMethods = {"create", "read"})
497     public void testSubmitRequest() {
498
499         // Expected status code: 200 OK
500         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
501
502         // Submit the request to the service and store the response.
503         String method = ServiceRequestType.READ.httpMethodName();
504         String url = getResourceURL(knownResourceId);
505         int statusCode = submitRequest(method, url);
506
507         // Check the status code of the response: does it match
508         // the expected response(s)?
509         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
510         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
511
512     }
513
514     // ---------------------------------------------------------------
515     // Utility methods used by tests above
516     // ---------------------------------------------------------------
517     @Override
518     public String getServicePathComponent() {
519         return SERVICE_PATH_COMPONENT;
520     }
521
522     private MultipartOutput createRelationInstance(String identifier) {
523         RelationsCommon relation = new RelationsCommon();
524         fillRelation(relation, identifier);
525
526         MultipartOutput multipart = new MultipartOutput();
527         OutputPart commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
528         commonPart.getHeaders().add("label", client.getCommonPartName());
529         verbose("to be created, relation common ", relation, RelationsCommon.class);
530
531         return multipart;
532     }
533
534     /**
535      * Fills the relation.
536      * 
537      * @param identifier the identifier
538      * 
539      * @return the relation
540      */
541     private void fillRelation(RelationsCommon relation, String identifier) {
542         fillRelation(relation, "Subject-" + identifier,
543                 "SubjectType-" + identifier + "-type",
544                 "Object-" + identifier,
545                 "ObjectType-" + identifier + "-type",
546                 RelationshipType.COLLECTIONOBJECT_INTAKE);
547     }
548
549     /**
550      * Fills the relation.
551      * 
552      * @param documentId1 the document id1
553      * @param documentType1 the document type1
554      * @param documentId2 the document id2
555      * @param documentType2 the document type2
556      * @param rt the rt
557      * 
558      * @return the relation
559      */
560     private void fillRelation(RelationsCommon relation,
561             String documentId1, String documentType1,
562             String documentId2, String documentType2,
563             RelationshipType rt) {
564         relation.setDocumentId1(documentId1);
565         relation.setDocumentType1(documentType1);
566         relation.setDocumentId2(documentId2);
567         relation.setDocumentType2(documentType2);
568
569         relation.setRelationshipType(rt);
570     }
571 }