]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f3874785a6bbd0508cc9a820122c5d93de47064a
[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         Role roleToUpdate = new Role();
348         roleToUpdate.setCsid(knownResourceId);
349
350         // Update the content of this resource.
351         roleToUpdate.setRoleName("updated-role");
352         if (logger.isDebugEnabled()) {
353             logger.debug("updated object");
354             logger.debug(objectAsXmlString(roleToUpdate,
355                     Role.class));
356         }
357         RoleClient client = new RoleClient();
358         // Submit the request to the service and store the response.
359         ClientResponse<Role> res = client.update(knownResourceId, roleToUpdate);
360         int statusCode = res.getStatus();
361         // Check the status code of the response: does it match the expected response(s)?
362         if (logger.isDebugEnabled()) {
363             logger.debug(testName + ": status = " + statusCode);
364         }
365         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
368
369
370         Role roleUpdated = (Role) res.getEntity();
371         Assert.assertNotNull(roleUpdated);
372
373         Assert.assertEquals(roleUpdated.getRoleName(),
374                 roleToUpdate.getRoleName(),
375                 "Data in updated object did not match submitted data.");
376     }
377
378     // Failure outcomes
379     // Placeholders until the three tests below can be uncommented.
380     // See Issue CSPACE-401.
381     @Override
382     public void updateWithEmptyEntityBody(String testName) throws Exception {
383     }
384
385     @Override
386     public void updateWithMalformedXml(String testName) throws Exception {
387     }
388
389     @Override
390     public void updateWithWrongXmlSchema(String testName) throws Exception {
391     }
392
393     @Override
394     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
395     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
396     public void updateNonExistent(String testName) throws Exception {
397
398         // Perform setup.
399         setupUpdateNonExistent(testName);
400
401         // Submit the request to the service and store the response.
402         //
403         // Note: The ID used in this 'create' call may be arbitrary.
404         // The only relevant ID may be the one used in updateRole(), below.
405         RoleClient client = new RoleClient();
406         Role role = createRoleInstance("ROLE_XXX",
407                 "xxx",
408                 true);
409         ClientResponse<Role> res =
410                 client.update(NON_EXISTENT_ID, role);
411         int statusCode = res.getStatus();
412
413         // Check the status code of the response: does it match
414         // the expected response(s)?
415         if (logger.isDebugEnabled()) {
416             logger.debug(testName + ": status = " + statusCode);
417         }
418         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
419                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
420         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421     }
422
423     // ---------------------------------------------------------------
424     // CRUD tests : DELETE tests
425     // ---------------------------------------------------------------
426     // Success outcomes
427     @Override
428     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
429     dependsOnMethods = {"update"})
430     public void delete(String testName) throws Exception {
431
432         // Perform setup.
433         setupDelete(testName);
434
435         // Submit the request to the service and store the response.
436         RoleClient client = new RoleClient();
437         ClientResponse<Response> res = client.delete(knownResourceId);
438         int statusCode = res.getStatus();
439
440         // Check the status code of the response: does it match
441         // the expected response(s)?
442         if (logger.isDebugEnabled()) {
443             logger.debug(testName + ": status = " + statusCode);
444         }
445         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
446                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
447         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
448     }
449
450     // Failure outcomes
451     @Override
452     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
453     dependsOnMethods = {"delete"})
454     public void deleteNonExistent(String testName) throws Exception {
455
456         // Perform setup.
457         setupDeleteNonExistent(testName);
458
459         // Submit the request to the service and store the response.
460         RoleClient client = new RoleClient();
461         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
462         int statusCode = res.getStatus();
463
464         // Check the status code of the response: does it match
465         // the expected response(s)?
466         if (logger.isDebugEnabled()) {
467             logger.debug(testName + ": status = " + statusCode);
468         }
469         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
472     }
473
474     // ---------------------------------------------------------------
475     // Utility tests : tests of code used in tests above
476     // ---------------------------------------------------------------
477     /**
478      * Tests the code for manually submitting data that is used by several
479      * of the methods above.
480      */
481     @Test(dependsOnMethods = {"create", "read"})
482     public void testSubmitRequest() throws Exception {
483
484         // Expected status code: 200 OK
485         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
486
487         // Submit the request to the service and store the response.
488         String method = ServiceRequestType.READ.httpMethodName();
489         String url = getResourceURL(knownResourceId);
490         int statusCode = submitRequest(method, url);
491
492         // Check the status code of the response: does it match
493         // the expected response(s)?
494         if (logger.isDebugEnabled()) {
495             logger.debug("testSubmitRequest: url=" + url
496                     + " status=" + statusCode);
497         }
498         Assert.assertEquals(statusCode, EXPECTED_STATUS);
499
500     }
501
502     // ---------------------------------------------------------------
503     // Utility methods used by tests above
504     // ---------------------------------------------------------------
505     /**
506      * create role instance
507      * @param roleName
508      * @param description
509      * @param useRoleName
510      * @return
511      */
512     public Role createRoleInstance(String roleName,
513             String description,
514             boolean useRoleName) {
515
516         Role role = RoleFactory.createRoleInstance(roleName, description,
517                 useRoleName);
518         if (logger.isDebugEnabled()) {
519             logger.debug("to be created, role common");
520             logger.debug(objectAsXmlString(role, Role.class));
521         }
522         return role;
523
524     }
525
526     @AfterClass(alwaysRun = true)
527     public void cleanUp() {
528         setupDelete("delete");
529         String noTest = System.getProperty("noTestCleanup");
530         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
531             if (logger.isDebugEnabled()) {
532                 logger.debug("Skipping Cleanup phase ...");
533             }
534             return;
535         }
536         if (logger.isDebugEnabled()) {
537             logger.debug("Cleaning up temporary resources created for testing ...");
538         }
539         RoleClient client = new RoleClient();
540         for (String resourceId : allResourceIdsCreated) {
541             ClientResponse<Response> res = client.delete(resourceId);
542             int statusCode = res.getStatus();
543             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
544                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
545             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
546         }
547     }
548
549     private int printList(String testName, RolesList list) {
550
551         int i = 0;
552
553         for (Role role : list.getRoles()) {
554             logger.debug(testName + " role csid=" + role.getCsid()
555                     + " name=" + role.getRoleName()
556                     + " desc=" + role.getDescription());
557             i++;
558         }
559         return i;
560     }
561 }