]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6616b18b61e766ab1ffc0faa89e80b6563ebcfd4
[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 RoleClient client = new RoleClient();
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 client.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         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         Role role = createRoleInstance("ROLE_USERS",
117                 "role users",
118                 true);
119         ClientResponse<Response> res = client.create(role);
120         int statusCode = res.getStatus();
121
122         if (logger.isDebugEnabled()) {
123             logger.debug(testName + ": status = " + statusCode);
124         }
125         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
126                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
127         Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
128     }
129
130     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
131     dependsOnMethods = {"create"})
132     public void createWithoutRoleName(String testName) throws Exception {
133
134         setupCreate(testName);
135
136         // Submit the request to the service and store the response.
137         Role role = createRoleInstance("ROLE_USERS",
138                 "role for users",
139                 false);
140         ClientResponse<Response> res = client.create(role);
141         int statusCode = res.getStatus();
142         // Does it exactly match the expected status code?
143         if (logger.isDebugEnabled()) {
144             logger.debug(testName + ": status = " + statusCode);
145         }
146         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
147                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
148         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
149     }
150
151     //to not cause uniqueness violation for role, createList is removed
152     @Override
153     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
154     dependsOnMethods = {"create"})
155     public void createList(String testName) throws Exception {
156
157         setupCreate(testName);
158         // Submit the request to the service and store the response.
159         Role role1 = createRoleInstance("ROLE_COLLECTIONS_MANGER_TEST",
160                 "collection manager",
161                 true);
162         ClientResponse<Response> res = client.create(role1);
163         int statusCode = res.getStatus();
164         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
165                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
166         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
167         allResourceIdsCreated.add(extractId(res));
168
169         Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
170                 "collections curator",
171                 true);
172         res = client.create(role2);
173         statusCode = res.getStatus();
174         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178         allResourceIdsCreated.add(extractId(res));
179
180         Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
181                 "moving image admin",
182                 true);
183         res = client.create(role3);
184         statusCode = res.getStatus();
185         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
186                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
187         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
188         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
189         allResourceIdsCreated.add(extractId(res));
190     }
191
192     // Failure outcomes
193     // Placeholders until the three tests below can be uncommented.
194     // See Issue CSPACE-401.
195     @Override
196     public void createWithEmptyEntityBody(String testName) throws Exception {
197     }
198
199     @Override
200     public void createWithMalformedXml(String testName) throws Exception {
201     }
202
203     @Override
204     public void createWithWrongXmlSchema(String testName) throws Exception {
205     }
206
207     // ---------------------------------------------------------------
208     // CRUD tests : READ tests
209     // ---------------------------------------------------------------
210     // Success outcomes
211     @Override
212     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
213     dependsOnMethods = {"create"})
214     public void read(String testName) throws Exception {
215
216         // Perform setup.
217         setupRead(testName);
218
219         // Submit the request to the service and store the response.
220         ClientResponse<Role> res = client.read(knownResourceId);
221         int statusCode = res.getStatus();
222
223         // Check the status code of the response: does it match
224         // the expected response(s)?
225         if (logger.isDebugEnabled()) {
226             logger.debug(testName + ": status = " + statusCode);
227         }
228         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
229                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
230         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
231
232         Role output = (Role) res.getEntity();
233         Assert.assertNotNull(output);
234     }
235
236     // Failure outcomes
237     @Override
238     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
239     dependsOnMethods = {"read"})
240     public void readNonExistent(String testName) throws Exception {
241
242         // Perform setup.
243         setupReadNonExistent(testName);
244
245         // Submit the request to the service and store the response.
246         ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
247         int statusCode = res.getStatus();
248
249         // Check the status code of the response: does it match
250         // the expected response(s)?
251         if (logger.isDebugEnabled()) {
252             logger.debug(testName + ": status = " + statusCode);
253         }
254         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
255                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
256         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
257     }
258
259     // ---------------------------------------------------------------
260     // CRUD tests : READ_LIST tests
261     // ---------------------------------------------------------------
262     // Success outcomes
263     @Override
264     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
265     dependsOnMethods = {"createList", "read"})
266     public void readList(String testName) throws Exception {
267
268         // Perform setup.
269         setupReadList(testName);
270
271         // Submit the request to the service and store the response.
272         ClientResponse<RolesList> res = client.readList();
273         RolesList list = res.getEntity();
274         int statusCode = res.getStatus();
275
276         // Check the status code of the response: does it match
277         // the expected response(s)?
278         if (logger.isDebugEnabled()) {
279             logger.debug(testName + ": status = " + statusCode);
280         }
281         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
282                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
283         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
284
285         // Optionally output additional data about list members for debugging.
286         boolean iterateThroughList = true;
287         if (iterateThroughList && logger.isDebugEnabled()) {
288             printList(testName, list);
289         }
290     }
291
292     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
293     dependsOnMethods = {"createList", "read"})
294     public void searchRoleName(String testName) throws Exception {
295
296         // Perform setup.
297         setupReadList(testName);
298
299         // Submit the request to the service and store the response.
300         ClientResponse<RolesList> res = client.readSearchList("movingImage");
301         RolesList list = res.getEntity();
302         int statusCode = res.getStatus();
303         // Check the status code of the response: does it match
304         // the expected response(s)?
305         if (logger.isDebugEnabled()) {
306             logger.debug(testName + ": status = " + statusCode);
307         }
308         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
309                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
310         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
311         int EXPECTED_ITEMS = 1;
312         if (logger.isDebugEnabled()) {
313             logger.debug(testName + ": received = " + list.getRoles().size()
314                     + " expected=" + EXPECTED_ITEMS);
315         }
316         Assert.assertEquals(EXPECTED_ITEMS, list.getRoles().size());
317         // Optionally output additional data about list members for debugging.
318         boolean iterateThroughList = true;
319         if (iterateThroughList && logger.isDebugEnabled()) {
320             printList(testName, list);
321         }
322     }
323
324     // Failure outcomes
325     // None at present.
326     // ---------------------------------------------------------------
327     // CRUD tests : UPDATE tests
328     // ---------------------------------------------------------------
329     // Success outcomes
330     @Override
331     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
332     dependsOnMethods = {"read", "readList", "readNonExistent"})
333     public void update(String testName) throws Exception {
334
335         // Perform setup.
336         setupUpdate(testName);
337
338
339         ClientResponse<Role> res =
340                 client.read(knownResourceId);
341         if (logger.isDebugEnabled()) {
342             logger.debug(testName + ": read status = " + res.getStatus());
343         }
344         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
345
346         if (logger.isDebugEnabled()) {
347             logger.debug("got object to update with ID: " + knownResourceId);
348         }
349         Role toUpdateRole =
350                 (Role) res.getEntity();
351         Assert.assertNotNull(toUpdateRole);
352
353         // Update the content of this resource.
354         toUpdateRole.setRoleName("updated-" + toUpdateRole.getRoleName());
355         if (logger.isDebugEnabled()) {
356             logger.debug("updated object");
357             logger.debug(objectAsXmlString(toUpdateRole,
358                     Role.class));
359         }
360
361         // Submit the request to the service and store the response.
362         res = client.update(knownResourceId, toUpdateRole);
363         int statusCode = res.getStatus();
364         // Check the status code of the response: does it match the expected response(s)?
365         if (logger.isDebugEnabled()) {
366             logger.debug(testName + ": status = " + statusCode);
367         }
368         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
371
372
373         Role updatedRole = (Role) res.getEntity();
374         Assert.assertNotNull(updatedRole);
375
376         Assert.assertEquals(updatedRole.getRoleName(),
377                 toUpdateRole.getRoleName(),
378                 "Data in updated object did not match submitted data.");
379     }
380
381     // Failure outcomes
382     // Placeholders until the three tests below can be uncommented.
383     // See Issue CSPACE-401.
384     @Override
385     public void updateWithEmptyEntityBody(String testName) throws Exception {
386     }
387
388     @Override
389     public void updateWithMalformedXml(String testName) throws Exception {
390     }
391
392     @Override
393     public void updateWithWrongXmlSchema(String testName) throws Exception {
394     }
395
396     @Override
397     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
398     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
399     public void updateNonExistent(String testName) throws Exception {
400
401         // Perform setup.
402         setupUpdateNonExistent(testName);
403
404         // Submit the request to the service and store the response.
405         //
406         // Note: The ID used in this 'create' call may be arbitrary.
407         // The only relevant ID may be the one used in updateRole(), below.
408         Role role = createRoleInstance("ROLE_XXX",
409                 "xxx",
410                 true);
411         ClientResponse<Role> res =
412                 client.update(NON_EXISTENT_ID, role);
413         int statusCode = res.getStatus();
414
415         // Check the status code of the response: does it match
416         // the expected response(s)?
417         if (logger.isDebugEnabled()) {
418             logger.debug(testName + ": status = " + statusCode);
419         }
420         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
421                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
422         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
423     }
424
425     // ---------------------------------------------------------------
426     // CRUD tests : DELETE tests
427     // ---------------------------------------------------------------
428     // Success outcomes
429     @Override
430     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
431     dependsOnMethods = {"update"})
432     public void delete(String testName) throws Exception {
433
434         // Perform setup.
435         setupDelete(testName);
436
437         // Submit the request to the service and store the response.
438         ClientResponse<Response> res = client.delete(knownResourceId);
439         int statusCode = res.getStatus();
440
441         // Check the status code of the response: does it match
442         // the expected response(s)?
443         if (logger.isDebugEnabled()) {
444             logger.debug(testName + ": status = " + statusCode);
445         }
446         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
447                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
448         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
449     }
450
451     // Failure outcomes
452     @Override
453     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
454     dependsOnMethods = {"delete"})
455     public void deleteNonExistent(String testName) throws Exception {
456
457         // Perform setup.
458         setupDeleteNonExistent(testName);
459
460         // Submit the request to the service and store the response.
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     static Role createRoleInstance(String roleName,
513             String description,
514             boolean useRoleName) {
515
516         Role role = new Role();
517         if (useRoleName) {
518             role.setRoleName(roleName);
519         }
520         role.setDescription(description);
521         if (logger.isDebugEnabled()) {
522             logger.debug("to be created, role common");
523             logger.debug(objectAsXmlString(role, Role.class));
524         }
525         return role;
526
527     }
528
529     @AfterClass(alwaysRun = true)
530     public void cleanUp() {
531         setupDelete("delete");
532         if (logger.isDebugEnabled()) {
533             logger.debug("Cleaning up temporary resources created for testing ...");
534         }
535         for (String resourceId : allResourceIdsCreated) {
536             // Note: Any non-success responses are ignored and not reported.
537             ClientResponse<Response> res = client.delete(resourceId);
538             int statusCode = res.getStatus();
539             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
540                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
541             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
542         }
543     }
544
545     private int printList(String testName, RolesList list) {
546
547         int i = 0;
548
549         for (Role role : list.getRoles()) {
550             logger.debug(testName + " role csid=" + role.getCsid()
551                     + " name=" + role.getRoleName()
552                     + " desc=" + role.getDescription());
553             i++;
554         }
555         return i;
556     }
557 }