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