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