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