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