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