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