]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bb609762fe2ddddfbbe156ea2e0e3bc8ab06d0eb
[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.authorization.client.test;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.Response;
28
29 import org.collectionspace.services.client.RoleClient;
30 import org.collectionspace.services.authorization.Role;
31 import org.collectionspace.services.authorization.RolesList;
32 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
33 import org.collectionspace.services.client.test.ServiceRequestType;
34 import org.jboss.resteasy.client.ClientResponse;
35
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
38
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.testng.annotations.AfterClass;
42
43 /**
44  * RoleServiceTest, carries out tests against a
45  * deployed and running Role Service.
46  * 
47  * $LastChangedRevision: 917 $
48  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
49  */
50 public class RoleServiceTest extends AbstractServiceTestImpl {
51
52     static private final Logger logger =
53             LoggerFactory.getLogger(RoleServiceTest.class);
54     // Instance variables specific to this test.
55     private String knownResourceId = null;
56     private List<String> allResourceIdsCreated = new ArrayList();
57     boolean addTenant = true;
58     /*
59      * This method is called only by the parent class, AbstractServiceTestImpl
60      */
61
62     @Override
63     protected String getServicePathComponent() {
64         return new RoleClient().getServicePathComponent();
65     }
66
67     // ---------------------------------------------------------------
68     // CRUD tests : CREATE tests
69     // ---------------------------------------------------------------
70     // Success outcomes
71     @Override
72     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
73     public void create(String testName) throws Exception {
74
75         // Perform setup, such as initializing the type of service request
76         // (e.g. CREATE, DELETE), its valid and expected status codes, and
77         // its associated HTTP method name (e.g. POST, DELETE).
78         setupCreate(testName);
79
80         // Submit the request to the service and store the response.
81         RoleClient client = new RoleClient();
82         Role role = createRoleInstance("ROLE_USERS_TEST",
83                 "all users are required to be in this role",
84                 true);
85         ClientResponse<Response> res = client.create(role);
86         int statusCode = res.getStatus();
87
88         // Check the status code of the response: does it match
89         // the expected response(s)?
90         //
91         // Specifically:
92         // Does it fall within the set of valid status codes?
93         // Does it exactly match the expected status code?
94         if (logger.isDebugEnabled()) {
95             logger.debug(testName + ": status = " + statusCode);
96         }
97         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
98                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
99         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
100
101         // Store the ID returned from this create operation
102         // for additional tests below.
103         knownResourceId = extractId(res);
104         if (logger.isDebugEnabled()) {
105             logger.debug(testName + ": knownResourceId=" + knownResourceId);
106         }
107     }
108
109     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
110     dependsOnMethods = {"create"})
111     public void createForUniqueRole(String testName) throws Exception {
112
113         setupCreate(testName);
114
115         // Submit the request to the service and store the response.
116         RoleClient client = new RoleClient();
117         Role role = createRoleInstance("ROLE_USERS",
118                 "role users",
119                 true);
120         ClientResponse<Response> res = client.create(role);
121         int statusCode = res.getStatus();
122
123         if (logger.isDebugEnabled()) {
124             logger.debug(testName + ": status = " + statusCode);
125         }
126         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128         Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
129     }
130
131     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
132     dependsOnMethods = {"create"})
133     public void createWithoutRoleName(String testName) throws Exception {
134
135         setupCreate(testName);
136
137         // Submit the request to the service and store the response.
138         RoleClient client = new RoleClient();
139         Role role = createRoleInstance("ROLE_USERS",
140                 "role for users",
141                 false);
142         ClientResponse<Response> res = client.create(role);
143         int statusCode = res.getStatus();
144         // Does it exactly match the expected status code?
145         if (logger.isDebugEnabled()) {
146             logger.debug(testName + ": status = " + statusCode);
147         }
148         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
149                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
151     }
152
153     //to not cause uniqueness violation for role, createList is removed
154     @Override
155     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
156     dependsOnMethods = {"create"})
157     public void createList(String testName) throws Exception {
158
159         setupCreate(testName);
160
161         // Submit the request to the service and store the response.
162          RoleClient client = new RoleClient();
163        Role role1 = createRoleInstance("ROLE_COLLECTIONS_MANGER_TEST",
164                 "collection manager",
165                 true);
166         ClientResponse<Response> res = client.create(role1);
167         int statusCode = res.getStatus();
168         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
169                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
170         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
171         allResourceIdsCreated.add(extractId(res));
172
173         Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
174                 "collections curator",
175                 true);
176         res = client.create(role2);
177         statusCode = res.getStatus();
178         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
182         allResourceIdsCreated.add(extractId(res));
183
184         Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
185                 "moving image admin",
186                 true);
187         res = client.create(role3);
188         statusCode = res.getStatus();
189         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
190                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
191         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
192         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
193         allResourceIdsCreated.add(extractId(res));
194     }
195
196     // Failure outcomes
197     // Placeholders until the three tests below can be uncommented.
198     // See Issue CSPACE-401.
199     @Override
200     public void createWithEmptyEntityBody(String testName) throws Exception {
201     }
202
203     @Override
204     public void createWithMalformedXml(String testName) throws Exception {
205     }
206
207     @Override
208     public void createWithWrongXmlSchema(String testName) throws Exception {
209     }
210
211     // ---------------------------------------------------------------
212     // CRUD tests : READ tests
213     // ---------------------------------------------------------------
214     // Success outcomes
215     @Override
216     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
217     dependsOnMethods = {"create"})
218     public void read(String testName) throws Exception {
219
220         // Perform setup.
221         setupRead(testName);
222
223         // Submit the request to the service and store the response.
224         RoleClient client = new RoleClient();
225         ClientResponse<Role> res = client.read(knownResourceId);
226         int statusCode = res.getStatus();
227
228         // Check the status code of the response: does it match
229         // the expected response(s)?
230         if (logger.isDebugEnabled()) {
231             logger.debug(testName + ": status = " + statusCode);
232         }
233         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
234                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
235         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
236
237         Role output = (Role) res.getEntity();
238         Assert.assertNotNull(output);
239     }
240
241     // Failure outcomes
242     @Override
243     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
244     dependsOnMethods = {"read"})
245     public void readNonExistent(String testName) throws Exception {
246
247         // Perform setup.
248         setupReadNonExistent(testName);
249
250         // Submit the request to the service and store the response.
251         RoleClient client = new RoleClient();
252         ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
253         int statusCode = res.getStatus();
254
255         // Check the status code of the response: does it match
256         // the expected response(s)?
257         if (logger.isDebugEnabled()) {
258             logger.debug(testName + ": status = " + statusCode);
259         }
260         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
261                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
262         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
263     }
264
265     // ---------------------------------------------------------------
266     // CRUD tests : READ_LIST tests
267     // ---------------------------------------------------------------
268     // Success outcomes
269     @Override
270     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
271     dependsOnMethods = {"createList", "read"})
272     public void readList(String testName) throws Exception {
273
274         // Perform setup.
275         setupReadList(testName);
276
277         // Submit the request to the service and store the response.
278         RoleClient client = new RoleClient();
279         ClientResponse<RolesList> res = client.readList();
280         RolesList list = res.getEntity();
281         int statusCode = res.getStatus();
282
283         // Check the status code of the response: does it match
284         // the expected response(s)?
285         if (logger.isDebugEnabled()) {
286             logger.debug(testName + ": status = " + statusCode);
287         }
288         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291
292         // Optionally output additional data about list members for debugging.
293         boolean iterateThroughList = true;
294         if (iterateThroughList && logger.isDebugEnabled()) {
295             printList(testName, list);
296         }
297     }
298
299     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
300     dependsOnMethods = {"createList", "read"})
301     public void searchRoleName(String testName) throws Exception {
302
303         // Perform setup.
304         setupReadList(testName);
305
306         // Submit the request to the service and store the response.
307         RoleClient client = new RoleClient();
308         ClientResponse<RolesList> res = client.readSearchList("movingImage");
309         RolesList list = res.getEntity();
310         int statusCode = res.getStatus();
311         // Check the status code of the response: does it match
312         // the expected response(s)?
313         if (logger.isDebugEnabled()) {
314             logger.debug(testName + ": status = " + statusCode);
315         }
316         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
317                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
318         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
319         int EXPECTED_ITEMS = 1;
320         if (logger.isDebugEnabled()) {
321             logger.debug(testName + ": received = " + list.getRoles().size()
322                     + " expected=" + EXPECTED_ITEMS);
323         }
324         Assert.assertEquals(EXPECTED_ITEMS, list.getRoles().size());
325         // Optionally output additional data about list members for debugging.
326         boolean iterateThroughList = true;
327         if (iterateThroughList && logger.isDebugEnabled()) {
328             printList(testName, list);
329         }
330     }
331
332     // Failure outcomes
333     // None at present.
334     // ---------------------------------------------------------------
335     // CRUD tests : UPDATE tests
336     // ---------------------------------------------------------------
337     // Success outcomes
338     @Override
339     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
340     dependsOnMethods = {"read", "readList", "readNonExistent"})
341     public void update(String testName) throws Exception {
342
343         // Perform setup.
344         setupUpdate(testName);
345
346         RoleClient client = new RoleClient();
347         ClientResponse<Role> res =
348                 client.read(knownResourceId);
349         if (logger.isDebugEnabled()) {
350             logger.debug(testName + ": read status = " + res.getStatus());
351         }
352         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
353
354         if (logger.isDebugEnabled()) {
355             logger.debug("got object to update with ID: " + knownResourceId);
356         }
357         Role toUpdateRole =
358                 (Role) res.getEntity();
359         Assert.assertNotNull(toUpdateRole);
360
361         // Update the content of this resource.
362         toUpdateRole.setRoleName("updated-" + toUpdateRole.getRoleName());
363         if (logger.isDebugEnabled()) {
364             logger.debug("updated object");
365             logger.debug(objectAsXmlString(toUpdateRole,
366                     Role.class));
367         }
368
369         // Submit the request to the service and store the response.
370         res = client.update(knownResourceId, toUpdateRole);
371         int statusCode = res.getStatus();
372         // Check the status code of the response: does it match the expected response(s)?
373         if (logger.isDebugEnabled()) {
374             logger.debug(testName + ": status = " + statusCode);
375         }
376         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
377                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
378         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
379
380
381         Role updatedRole = (Role) res.getEntity();
382         Assert.assertNotNull(updatedRole);
383
384         Assert.assertEquals(updatedRole.getRoleName(),
385                 toUpdateRole.getRoleName(),
386                 "Data in updated object did not match submitted data.");
387     }
388
389     // Failure outcomes
390     // Placeholders until the three tests below can be uncommented.
391     // See Issue CSPACE-401.
392     @Override
393     public void updateWithEmptyEntityBody(String testName) throws Exception {
394     }
395
396     @Override
397     public void updateWithMalformedXml(String testName) throws Exception {
398     }
399
400     @Override
401     public void updateWithWrongXmlSchema(String testName) throws Exception {
402     }
403
404     @Override
405     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
406     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
407     public void updateNonExistent(String testName) throws Exception {
408
409         // Perform setup.
410         setupUpdateNonExistent(testName);
411
412         // Submit the request to the service and store the response.
413         //
414         // Note: The ID used in this 'create' call may be arbitrary.
415         // The only relevant ID may be the one used in updateRole(), below.
416         RoleClient client = new RoleClient();
417         Role role = createRoleInstance("ROLE_XXX",
418                 "xxx",
419                 true);
420         ClientResponse<Role> res =
421                 client.update(NON_EXISTENT_ID, role);
422         int statusCode = res.getStatus();
423
424         // Check the status code of the response: does it match
425         // the expected response(s)?
426         if (logger.isDebugEnabled()) {
427             logger.debug(testName + ": status = " + statusCode);
428         }
429         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
430                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
431         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
432     }
433
434     // ---------------------------------------------------------------
435     // CRUD tests : DELETE tests
436     // ---------------------------------------------------------------
437     // Success outcomes
438     @Override
439     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
440     dependsOnMethods = {"update"})
441     public void delete(String testName) throws Exception {
442
443         // Perform setup.
444         setupDelete(testName);
445
446         // Submit the request to the service and store the response.
447         RoleClient client = new RoleClient();
448         ClientResponse<Response> res = client.delete(knownResourceId);
449         int statusCode = res.getStatus();
450
451         // Check the status code of the response: does it match
452         // the expected response(s)?
453         if (logger.isDebugEnabled()) {
454             logger.debug(testName + ": status = " + statusCode);
455         }
456         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
457                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
458         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
459     }
460
461     // Failure outcomes
462     @Override
463     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
464     dependsOnMethods = {"delete"})
465     public void deleteNonExistent(String testName) throws Exception {
466
467         // Perform setup.
468         setupDeleteNonExistent(testName);
469
470         // Submit the request to the service and store the response.
471         RoleClient client = new RoleClient();
472         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
473         int statusCode = res.getStatus();
474
475         // Check the status code of the response: does it match
476         // the expected response(s)?
477         if (logger.isDebugEnabled()) {
478             logger.debug(testName + ": status = " + statusCode);
479         }
480         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
481                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
482         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
483     }
484
485     // ---------------------------------------------------------------
486     // Utility tests : tests of code used in tests above
487     // ---------------------------------------------------------------
488     /**
489      * Tests the code for manually submitting data that is used by several
490      * of the methods above.
491      */
492     @Test(dependsOnMethods = {"create", "read"})
493     public void testSubmitRequest() throws Exception {
494
495         // Expected status code: 200 OK
496         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
497
498         // Submit the request to the service and store the response.
499         String method = ServiceRequestType.READ.httpMethodName();
500         String url = getResourceURL(knownResourceId);
501         int statusCode = submitRequest(method, url);
502
503         // Check the status code of the response: does it match
504         // the expected response(s)?
505         if (logger.isDebugEnabled()) {
506             logger.debug("testSubmitRequest: url=" + url
507                     + " status=" + statusCode);
508         }
509         Assert.assertEquals(statusCode, EXPECTED_STATUS);
510
511     }
512
513     // ---------------------------------------------------------------
514     // Utility methods used by tests above
515     // ---------------------------------------------------------------
516     /**
517      * create role instance
518      * @param roleName
519      * @param description
520      * @param useRoleName
521      * @return
522      */
523     static Role createRoleInstance(String roleName,
524             String description,
525             boolean useRoleName) {
526
527         Role role = new Role();
528         if (useRoleName) {
529             role.setRoleName(roleName);
530         }
531         role.setDescription(description);
532         if (logger.isDebugEnabled()) {
533             logger.debug("to be created, role common");
534             logger.debug(objectAsXmlString(role, Role.class));
535         }
536         return role;
537
538     }
539
540     @AfterClass(alwaysRun = true)
541     public void cleanUp() {
542         setupDelete("delete");
543         if (logger.isDebugEnabled()) {
544             logger.debug("Cleaning up temporary resources created for testing ...");
545         }
546         RoleClient client = new RoleClient();
547         for (String resourceId : allResourceIdsCreated) {
548             // Note: Any non-success responses are ignored and not reported.
549             ClientResponse<Response> res = client.delete(resourceId);
550             int statusCode = res.getStatus();
551             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
552                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
553             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
554         }
555     }
556
557     private int printList(String testName, RolesList list) {
558
559         int i = 0;
560
561         for (Role role : list.getRoles()) {
562             logger.debug(testName + " role csid=" + role.getCsid()
563                     + " name=" + role.getRoleName()
564                     + " desc=" + role.getDescription());
565             i++;
566         }
567         return i;
568     }
569 }