]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9ac0801ea8d492e77354f1d38f2f29eb5104cc3f
[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 java.util.Random;
28 import javax.ws.rs.core.Response;
29
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.client.PermissionClient;
32 import org.collectionspace.services.client.RoleClient;
33 import org.collectionspace.services.authorization.PermissionValue;
34 import org.collectionspace.services.authorization.Role;
35 import org.collectionspace.services.authorization.RolesList;
36 import org.collectionspace.services.authorization.perms.Permission;
37 import org.collectionspace.services.client.RoleFactory;
38 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
39
40 import org.testng.Assert;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * RoleServiceTest, carries out tests against a
50  * deployed and running Role Service.
51  * 
52  * $LastChangedRevision: 917 $
53  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
54  */
55 public class RoleServiceTest extends AbstractServiceTestImpl<RolesList, Role, Role, Role> {
56
57     /** The logger. */
58     private final static String CLASS_NAME = RoleServiceTest.class.getName();
59     private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60
61     // Used to create unique identifiers
62     static private final Random random = new Random(System.currentTimeMillis());
63     
64         private static final String PERM_1_RL_RESOURCE = "ROLE_TEST_PERMVALUE_RESOURCE_1";
65         private static final String PERM_1_RL_ACTIONGROUP = "RL";
66         private static final String PERM_2_RL_RESOURCE = "ROLE_TEST_PERMVALUE_RESOURCE_2";
67         private static final String PERM_2_RL_ACTIONGROUP = "CRUL";
68         private static final String PERM_3_RL_RESOURCE = "ROLE_TEST_PERMVALUE_RESOURCE_3";
69         private static final String PERM_3_RL_ACTIONGROUP = "CRUDL";
70
71
72     // Instance variables specific to this test.
73     /** The known resource id. */
74     private String knownRoleName = "ROLE_USERS_MOCK-1";
75     private String knownRoleDisplayName = "ROLE_DISPLAYNAME_USERS_MOCK-1";
76     private String verifyResourceId = null;
77     private String verifyRoleName = "collections_manager_mock-1";
78     //
79     // Permission values
80     //
81     private List<PermissionValue> permissionValues = RoleFactory.EMPTY_PERMVALUE_LIST;
82
83     @Override
84     public String getServiceName() { 
85         return RoleClient.SERVICE_NAME;
86     }
87     
88     @Override
89     protected String getServicePathComponent() throws Exception {
90         return new RoleClient().getServicePathComponent();
91     }
92
93     /**
94      * The entity type expected from the JAX-RS Response object
95      */
96     public Class<Role> getEntityResponseType() {
97         return Role.class;
98     }
99     
100     /* (non-Javadoc)
101      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
102      */
103     @Override
104     protected CollectionSpaceClient getClientInstance() throws Exception {
105         return new RoleClient();
106     }
107
108         @Override
109         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
110         return new RoleClient(clientPropertiesFilename);
111         }
112         
113     /**
114      * Seed data.
115      * @throws Exception 
116      */
117     @BeforeClass(alwaysRun = true)
118     public void seedData() throws Exception {
119         permissionValues = new ArrayList<PermissionValue>();
120         permissionValues.add(createPermissionValueInstance(PERM_1_RL_RESOURCE, PERM_1_RL_ACTIONGROUP));
121         permissionValues.add(createPermissionValueInstance(PERM_2_RL_RESOURCE, PERM_2_RL_ACTIONGROUP));
122         permissionValues.add(createPermissionValueInstance(PERM_3_RL_RESOURCE, PERM_3_RL_ACTIONGROUP));
123     }
124     
125     private PermissionValue createPermissionValueInstance(String resource, String actionGroup) {
126                 PermissionValue permValue = new PermissionValue();
127                 permValue.setResourceName(resource);
128                 permValue.setActionGroup(actionGroup);
129                 return permValue;
130         }
131
132         /**
133      * Clean up.
134      * @throws Exception 
135      */
136     @AfterClass(alwaysRun = true)
137     @Override
138     public void cleanUp() throws Exception {
139         String noTest = System.getProperty("noTestCleanup");
140         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
141             if (logger.isDebugEnabled()) {
142                 logger.debug("Skipping Cleanup phase ...");
143             }
144             return;
145         }
146         if (logger.isDebugEnabled()) {
147             logger.debug("Cleaning up temporary resources created for testing ...");
148         }
149         //
150         // Delete the permissions we indirectly created with when we created via the "createRoleWithPerms()" test 
151         //
152         for (PermissionValue pv : permissionValues) {
153             deletePermission(pv);
154         }
155         
156         //
157         // Call our parent cleanup method.
158         //
159         super.cleanUp();
160     }
161     
162
163     /*
164      * Use a resource name and action group value to find and delete a permission record/resource.
165      */
166     private void deletePermission(PermissionValue permissionValue) throws Exception {
167         int statusCode = Response.Status.OK.getStatusCode();
168         PermissionClient client = new PermissionClient();
169         Permission permission = client.read(permissionValue.getResourceName(), permissionValue.getActionGroup());
170         if (permission != null) {
171             Response res = client.delete(permission.getCsid());
172             try {
173                 statusCode = res.getStatus();
174             } finally {
175                 res.close();
176             }
177         } else {
178                 //
179                 // Something bad happened.
180                 //
181                 statusCode = Response.Status.BAD_REQUEST.getStatusCode();               
182         }
183         
184         if (statusCode != Response.Status.OK.getStatusCode()) {
185                 String msg = String.format("Could not delete test Permission record: resource name='%s', actionGroup='%s'.",
186                                 permissionValue.getResourceName(), permissionValue.getActionGroup());
187                 logger.error(msg);              
188         }
189         }
190
191         /* (non-Javadoc)
192      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
193      */
194     @Override
195     public void readPaginatedList(String testName) throws Exception {
196         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
197     }
198
199     // ---------------------------------------------------------------
200     // CRUD tests : CREATE tests
201     // ---------------------------------------------------------------
202     // Success outcomes
203
204     @Override
205     public void create(String testName) throws Exception {
206         // Perform setup, such as initializing the type of service request
207         // (e.g. CREATE, DELETE), its valid and expected status codes, and
208         // its associated HTTP method name (e.g. POST, DELETE).
209         setupCreate();
210
211         // Submit the request to the service and store the response.
212         RoleClient client = new RoleClient();
213         Role role = createRoleInstance(knownRoleName, "All users are required to be in this role",
214                 true, RoleFactory.EMPTY_PERMVALUE_LIST);
215         Response res = client.create(role);
216         try {
217                 int statusCode = res.getStatus();
218                 // Check the status code of the response: does it match
219                 // the expected response(s)?
220                 //
221                 // Specifically:
222                 // Does it fall within the set of valid status codes?
223                 // Does it exactly match the expected status code?
224                 if (logger.isDebugEnabled()) {
225                     logger.debug(testName + ": status = " + statusCode);
226                 }
227                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
228                         invalidStatusCodeMessage(testRequestType, statusCode));
229                 Assert.assertEquals(statusCode, testExpectedStatusCode);
230         
231                 // Store the ID returned from this create operation
232                 // for additional tests below.
233                 knownResourceId = extractId(res);
234                 //allResourceIdsCreated.add(knownResourceId);  //FIXME: 
235                 if (logger.isDebugEnabled()) {
236                     logger.debug(testName + ": knownResourceId=" + knownResourceId);
237                 }
238         } finally {
239                 res.close();
240         }
241     }
242     
243     @Test(dataProvider = "testName", 
244                 dependsOnMethods = {"CRUDTests"})
245     public void createRoleWithPerms(String testName) throws Exception {
246         // Perform setup, such as initializing the type of service request
247         // (e.g. CREATE, DELETE), its valid and expected status codes, and
248         // its associated HTTP method name (e.g. POST, DELETE).
249         setupCreate();
250
251         // Submit the request to the service and store the response.
252         RoleClient client = new RoleClient();
253         Role role = createRoleInstance("CREATE_ROLE_WITH_PERMS" + System.currentTimeMillis(), "A role created with perms.",
254                 true, permissionValues);
255         Response res = client.create(role);
256         try {
257                 int statusCode = res.getStatus();
258                 // Check the status code of the response: does it match
259                 // the expected response(s)?
260                 //
261                 // Specifically:
262                 // Does it fall within the set of valid status codes?
263                 // Does it exactly match the expected status code?
264                 if (logger.isDebugEnabled()) {
265                     logger.debug(testName + ": status = " + statusCode);
266                 }
267                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
268                         invalidStatusCodeMessage(testRequestType, statusCode));
269                 Assert.assertEquals(statusCode, testExpectedStatusCode);
270         
271                 // Store the ID returned from this create operation
272                 // for additional tests below.
273                 String csid = extractId(res);
274                 allResourceIdsCreated.add(csid);
275                 if (logger.isDebugEnabled()) {
276                     logger.debug(testName + ": role with perms ID=" + csid);
277                 }
278         } finally {
279                 res.close();
280         }
281     }    
282     
283     @Test(dataProvider = "testName", 
284                 dependsOnMethods = {"CRUDTests"})    
285     public void createDupliateRole(String testName) throws Exception {
286         // Perform setup, such as initializing the type of service request
287         // (e.g. CREATE, DELETE), its valid and expected status codes, and
288         // its associated HTTP method name (e.g. POST, DELETE).
289         setupDuplicate();
290
291         // Submit the request to the service and store the response.
292         RoleClient client = new RoleClient();
293         Role role = createRoleInstance(knownRoleName, "This is a duplicate of an existing role.",
294                 true, RoleFactory.EMPTY_PERMVALUE_LIST);
295         Response res = client.create(role);
296         try {
297                 int statusCode = res.getStatus();
298                 if (statusCode == 201) {
299                         // We expected NOT to create a new role, so if we did then we need to keep track of it and eventually delete it.
300                         String duplicateRole = extractId(res);
301                         allResourceIdsCreated.add(duplicateRole);
302                 }
303                 // Check the status code of the response: does it match
304                 // the expected response(s)?
305                 //
306                 // Specifically:
307                 // Does it exactly match the expected status code?
308                 if (logger.isDebugEnabled()) {
309                     logger.debug(testName + ": status = " + statusCode);
310                 }
311                 Assert.assertEquals(statusCode, testExpectedStatusCode);
312         
313         } finally {             
314                 res.close();
315         }
316     }
317     
318     @Test(dataProvider = "testName", 
319                 dependsOnMethods = {"CRUDTests"})
320     public void createWithDisplayname(String testName) throws Exception {
321         // Perform setup, such as initializing the type of service request
322         // (e.g. CREATE, DELETE), its valid and expected status codes, and
323         // its associated HTTP method name (e.g. POST, DELETE).
324         setupCreate();
325
326         // Submit the request to the service and store the response.
327         RoleClient client = new RoleClient();
328         Role role = createRoleInstance(knownRoleName + "_" + knownRoleDisplayName,
329                 "all users are required to be in this role", true);
330         role.setDisplayName(knownRoleDisplayName);
331         Response res = client.create(role);
332         try {
333                 int statusCode = res.getStatus();
334                 // Check the status code of the response: does it match
335                 // the expected response(s)?
336                 //
337                 // Specifically:
338                 // Does it fall within the set of valid status codes?
339                 // Does it exactly match the expected status code?
340                 if (logger.isDebugEnabled()) {
341                     logger.debug(testName + ": status = " + statusCode);
342                 }
343                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
344                         invalidStatusCodeMessage(testRequestType, statusCode));
345                 Assert.assertEquals(statusCode, testExpectedStatusCode);
346         
347                 // Store the ID returned from this create operation
348                 // for additional tests below.
349                 String csid = extractId(res);
350                 allResourceIdsCreated.add(csid);
351                 if (logger.isDebugEnabled()) {
352                     logger.debug(testName + ": csid=" + csid);
353                 }
354         } finally {
355                 res.close();
356         }
357     }
358     
359
360     /**
361      * Creates the for unique role.
362      *
363      * @param testName the test name
364      * @throws Exception the exception
365      */
366     @Test(dataProvider = "testName",
367                 dependsOnMethods = {"CRUDTests"})
368     public void createForUniqueRole(String testName) throws Exception {
369         setupCreate();
370
371         // Submit the request to the service and store the response.
372         RoleClient client = new RoleClient();
373         Role role = createRoleInstance(knownRoleName,
374                 "role users", true);
375         Response res = client.create(role);
376         try {
377                 int statusCode = res.getStatus();
378         
379                 if (logger.isDebugEnabled()) {
380                         logger.debug(testName + ": Role with name \"" +
381                                         knownRoleName + "\" should already exist, so this request should fail.");
382                     logger.debug(testName + ": status = " + statusCode);
383                     logger.debug(testName + ": " + res);
384                 }
385                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
386                         invalidStatusCodeMessage(testRequestType, statusCode));
387                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
388         } finally {
389                 res.close();
390         }
391     }
392     
393     /**
394      * Creates the for unique display name of role.
395      *
396      * @param testName the test name
397      * @throws Exception the exception
398      */
399     @Test(dataProvider = "testName",
400                 dependsOnMethods = {"createWithDisplayname"})
401     public void createForUniqueDisplayRole(String testName) throws Exception {
402         setupCreate();
403
404         // Submit the request to the service and store the response.
405         RoleClient client = new RoleClient();
406         Role role = createRoleInstance(knownRoleName + createIdentifier(),
407                 "role users with non-unique display name",
408                 true);
409         role.setDisplayName(knownRoleDisplayName);
410         Response res = client.create(role);
411         try {
412                 int statusCode = res.getStatus();
413         
414                 if (logger.isDebugEnabled()) {
415                         logger.debug(testName + ": Role with name \"" +
416                                         knownRoleName + "\" should already exist, so this request should fail.");
417                     logger.debug(testName + ": status = " + statusCode);
418                     logger.debug(testName + ": " + res);
419                 }
420                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
421                         invalidStatusCodeMessage(testRequestType, statusCode));
422                 if (statusCode != Response.Status.BAD_REQUEST.getStatusCode()) {
423                     // If the test fails then we've just created a Role that we need to delete, so
424                     // store the ID returned from this create operation.
425                     String csid = extractId(res);
426                     allResourceIdsCreated.add(csid);
427                     if (logger.isDebugEnabled()) {
428                         logger.debug(testName + ": csid=" + csid);
429                     }
430                         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
431                 }
432         } finally {
433                 res.close();
434         }
435     }
436     
437     protected String createIdentifier() {
438         long identifier = System.currentTimeMillis() + random.nextInt();
439         return Long.toString(identifier);
440     }
441
442     /**
443      * Creates the without role name.
444      *
445      * @param testName the test name
446      * @throws Exception the exception
447      */
448     @Test(dataProvider = "testName",
449                 dependsOnMethods = {"CRUDTests"})
450     public void createWithoutRoleName(String testName) throws Exception {
451         setupCreate();
452
453         // Submit the request to the service and store the response.
454         RoleClient client = new RoleClient();
455         Role role = createRoleInstance("",
456                 "role for users",
457                 false);
458         Response res = client.create(role);
459         try {
460                 int statusCode = res.getStatus();
461                 // Does it exactly match the expected status code?
462                 if (logger.isDebugEnabled()) {
463                     logger.debug(testName + ": status = " + statusCode);
464                 }
465                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
466                         invalidStatusCodeMessage(testRequestType, statusCode));
467                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
468         } finally {
469                 res.close();
470         }
471     }
472
473     //to not cause uniqueness violation for role, createList is removed
474     /* (non-Javadoc)
475      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
476      */
477     @Override
478 //    @Test(dataProvider = "testName",
479 //      dependsOnMethods = {"createWithDisplayname"})
480     public void createList(String testName) throws Exception {
481         setupCreate();
482
483         // Submit the request to the service and store the response.
484         RoleClient client = new RoleClient();
485         //create a role with lowercase role name without role prefix
486         //the service should make it upper case and add the role prefix
487         Role role1 = createRoleInstance(verifyRoleName,
488                 "collection manager", true);
489         Response res = client.create(role1);
490         try {
491                 int statusCode = res.getStatus();
492                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
493                         invalidStatusCodeMessage(testRequestType, statusCode));
494                 Assert.assertEquals(statusCode, testExpectedStatusCode);
495                 verifyResourceId = extractId(res);
496                 allResourceIdsCreated.add(verifyResourceId);
497         } finally {
498                 res.close();
499         }
500
501         Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
502                 "collections curator", true);
503         res = client.create(role2);
504         try {
505                 int statusCode = res.getStatus();
506                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
507                         invalidStatusCodeMessage(testRequestType, statusCode));
508                 Assert.assertEquals(statusCode, testExpectedStatusCode);
509                 Assert.assertEquals(statusCode, testExpectedStatusCode);
510                 allResourceIdsCreated.add(extractId(res));
511         } finally {
512                 res.close();
513         }
514
515         Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
516                 "moving image admin", true);
517         res = client.create(role3);
518         try {
519                 int statusCode = res.getStatus();
520                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
521                         invalidStatusCodeMessage(testRequestType, statusCode));
522                 Assert.assertEquals(statusCode, testExpectedStatusCode);
523                 Assert.assertEquals(statusCode, testExpectedStatusCode);
524                 allResourceIdsCreated.add(extractId(res));
525         } finally {
526                 res.close();
527         }
528     }
529
530     // Failure outcomes
531     // Placeholders until the three tests below can be uncommented.
532     // See Issue CSPACE-401.
533     /* (non-Javadoc)
534      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
535      */
536     @Override
537     public void createWithEmptyEntityBody(String testName) throws Exception {
538         //FIXME: Should this test really be empty?  If so, please comment accordingly.
539     }
540
541     /* (non-Javadoc)
542      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
543      */
544     @Override
545     public void createWithMalformedXml(String testName) throws Exception {
546         //FIXME: Should this test really be empty?  If so, please comment accordingly.
547     }
548
549     /* (non-Javadoc)
550      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
551      */
552     @Override
553     public void createWithWrongXmlSchema(String testName) throws Exception {
554         //FIXME: Should this test really be empty?  If so, please comment accordingly.
555     }
556
557     // ---------------------------------------------------------------
558     // CRUD tests : READ tests
559     // ---------------------------------------------------------------
560     // Success outcomes
561     /* (non-Javadoc)
562      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
563      */
564     @Override
565 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
566 //    dependsOnMethods = {"createForUniqueRole"})
567     public void read(String testName) throws Exception {
568         // Perform setup.
569         setupRead();
570
571         // Submit the request to the service and store the response.
572         RoleClient client = new RoleClient();
573         Response res = client.read(knownResourceId);
574         try {
575                 int statusCode = res.getStatus();
576         
577                 // Check the status code of the response: does it match
578                 // the expected response(s)?
579                 if (logger.isDebugEnabled()) {
580                     logger.debug(testName + ": status = " + statusCode);
581                 }
582                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
583                         invalidStatusCodeMessage(testRequestType, statusCode));
584                 Assert.assertEquals(statusCode, testExpectedStatusCode);
585         
586                 Role output = res.readEntity(Role.class);
587                 Assert.assertNotNull(output);
588         } finally {
589                 res.close();
590         }
591     }
592
593     @Test(dataProvider = "testName",
594                 dependsOnMethods = {"CRUDTests"})
595     public void readToVerify(String testName) throws Exception {
596         // Perform setup.
597         setupRead();
598
599         // Submit the request to the service and store the response.
600         RoleClient client = new RoleClient();
601         Response res = client.read(verifyResourceId);
602         try {
603                 int statusCode = res.getStatus();
604                 
605                 // Check the status code of the response: does it match
606                 // the expected response(s)?
607                 if (logger.isDebugEnabled()) {
608                     logger.debug(testName + ": status = " + statusCode);
609                 }
610                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
611                         invalidStatusCodeMessage(testRequestType, statusCode));
612                 Assert.assertEquals(statusCode, testExpectedStatusCode);
613         
614                 Role output = res.readEntity(Role.class);
615                 Assert.assertNotNull(output);
616         
617                 //FIXME: Tenant ID of "1" should not be hard coded
618                 String roleNameToVerify = "ROLE_" +
619                         "1_" +
620                         verifyRoleName.toUpperCase();
621                 Assert.assertEquals(output.getRoleName(), roleNameToVerify,
622                         "RoleName fix did not work!");
623         } finally {
624                 res.close();
625         }
626     }
627     // Failure outcomes
628     /* (non-Javadoc)
629      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
630      */
631
632     @Override
633 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
634 //    dependsOnMethods = {"read"})
635     public void readNonExistent(String testName) throws Exception {
636         // Perform setup.
637         setupReadNonExistent();
638
639         // Submit the request to the service and store the response.
640         RoleClient client = new RoleClient();
641         Response res = client.read(NON_EXISTENT_ID);
642         try {
643             int statusCode = res.getStatus();
644                 // Check the status code of the response: does it match
645                 // the expected response(s)?
646                 if (logger.isDebugEnabled()) {
647                     logger.debug(testName + ": status = " + statusCode);
648                 }
649                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
650                         invalidStatusCodeMessage(testRequestType, statusCode));
651                 Assert.assertEquals(statusCode, testExpectedStatusCode);
652         } finally {
653                 res.close();
654         }
655     }
656
657     // ---------------------------------------------------------------
658     // CRUD tests : READ_LIST tests
659     // ---------------------------------------------------------------
660     // Success outcomes
661     /* (non-Javadoc)
662      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
663      */
664     @Override
665 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
666 //    dependsOnMethods = {"createList", "read"})
667     public void readList(String testName) throws Exception {
668         // Perform setup.
669         setupReadList();
670
671         // Submit the request to the service and store the response.
672         RoleClient client = new RoleClient();
673         Response res = client.readList();
674         try {
675                 // Check the status code of the response: does it match
676                 // the expected response(s)?
677                 assertStatusCode(res, testName);
678                 RolesList list = res.readEntity(RolesList.class);
679                 // Optionally output additional data about list members for debugging.
680                 boolean iterateThroughList = true;
681                 if (iterateThroughList && logger.isDebugEnabled()) {
682                     printList(testName, list);
683                 }
684         } finally {
685                 if (res != null) {
686                 res.close();
687             }
688         }
689     }
690
691     /**
692      * Search role name.
693      *
694      * @param testName the test name
695      * @throws Exception the exception
696      */
697     @Test(dataProvider = "testName",
698                 dependsOnMethods = {"CRUDTests"})
699     public void searchRoleName(String testName) throws Exception {
700         // Perform setup.
701         setupReadList();
702
703         // Submit the request to the service and store the response.
704         RoleClient client = new RoleClient();
705         Response res = client.readSearchList("movingImage");
706         try {
707                 assertStatusCode(res, testName);
708                 RolesList list = res.readEntity(RolesList.class);
709                 int EXPECTED_ITEMS = 1;
710                 if (logger.isDebugEnabled()) {
711                     logger.debug(testName + ": received = " + list.getRole().size()
712                             + " expected=" + EXPECTED_ITEMS);
713                 }
714                 Assert.assertEquals(EXPECTED_ITEMS, list.getRole().size());
715                 // Optionally output additional data about list members for debugging.
716                 boolean iterateThroughList = true;
717                 if (iterateThroughList && logger.isDebugEnabled()) {
718                     printList(testName, list);
719                 }
720         } finally {
721                 if (res != null) {
722                 res.close();
723             }
724         }
725     }
726
727     // Failure outcomes
728     // None at present.
729     // ---------------------------------------------------------------
730     // CRUD tests : UPDATE tests
731     // ---------------------------------------------------------------
732     // Success outcomes
733     /* (non-Javadoc)
734      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
735      */
736     @Override
737 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
738 //    dependsOnMethods = {"read", "readList", "readNonExistent"})
739     public void update(String testName) throws Exception {
740         // Perform setup.
741         setupUpdate();
742
743         Role roleToUpdate = new Role();
744         roleToUpdate.setCsid(knownResourceId);
745         roleToUpdate.setRoleName(knownRoleName);
746         roleToUpdate.setDisplayName(knownRoleName);
747         
748         // Update the content of this resource.
749         roleToUpdate.setDescription("updated role description");
750         if (logger.isDebugEnabled()) {
751             logger.debug("updated object");
752             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();            
753             logger.debug(objectAsXmlString(objectFactory.createRole(roleToUpdate),
754                     Role.class));
755         }
756         RoleClient client = new RoleClient();
757         // Submit the request to the service and store the response.
758         Response res = client.update(knownResourceId, roleToUpdate);
759         try {
760                 // Check the status code of the response: does it match the expected response(s)?
761             int statusCode = res.getStatus();
762                 if (logger.isDebugEnabled()) {
763                     logger.debug(testName + ": status = " + statusCode);
764                 }
765                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
766                         invalidStatusCodeMessage(testRequestType, statusCode));
767                 Assert.assertEquals(statusCode, testExpectedStatusCode);
768         
769         
770                 Role roleUpdated = res.readEntity(Role.class);
771                 Assert.assertNotNull(roleUpdated);
772         
773                 Assert.assertEquals(roleUpdated.getDescription(),
774                         roleToUpdate.getDescription(),
775                         "Data in updated object did not match submitted data.");
776         } finally {
777                 res.close();
778         }
779     }
780     
781     @Test(dataProvider = "testName",
782                 dependsOnMethods = {"CRUDTests"})
783         public void verifyProtectionReadOnly(String testName) throws Exception {
784         // Setup to create a new role
785         setupCreate();
786         Role role = createRoleInstance(knownRoleName+"_PT", "Just a temp", true);
787         role.setMetadataProtection(RoleClient.IMMUTABLE);
788         role.setPermsProtection(RoleClient.IMMUTABLE);
789         //
790         // Submit the create request to the service and store the response.
791         //
792         RoleClient client = new RoleClient();        
793         Response res = client.create(role);
794         String testResourceId = null;
795         try {
796             int statusCode = res.getStatus();
797                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
798                         invalidStatusCodeMessage(testRequestType, statusCode));
799                 Assert.assertEquals(statusCode, testExpectedStatusCode);
800                 // Store the ID returned from this create operation
801                 // for additional tests below.
802                 testResourceId = extractId(res);
803                 allResourceIdsCreated.add(testResourceId);
804                 if (logger.isDebugEnabled()) {
805                     logger.debug(testName + ": testResourceId=" + testResourceId);
806                 }
807         } finally {
808                 res.close();
809         }
810         
811         // Next, read back the role we just created.
812         setupRead();
813         // Submit the request to the service and store the response.
814         Response roleRes = client.read(testResourceId);
815         try {
816             int statusCode = roleRes.getStatus();
817                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
818                         invalidStatusCodeMessage(testRequestType, statusCode));
819                 Assert.assertEquals(statusCode, testExpectedStatusCode);
820         
821                 Role roleRead = roleRes.readEntity(Role.class);
822                 Assert.assertNotNull(roleRead);
823                 String mdProtection = roleRead.getMetadataProtection();
824                 String permsProtection = roleRead.getPermsProtection();
825                 if (logger.isTraceEnabled()) {
826                     logger.trace(testName + ": metadataProtection=" + mdProtection);
827                     logger.trace(testName + ": permsProtection=" + permsProtection);
828                 }
829                 Assert.assertFalse(role.getMetadataProtection().equals(mdProtection),
830                                 "Role allowed create to set the metadata protection flag.");
831                 Assert.assertFalse(role.getPermsProtection().equals(permsProtection),
832                                 "Role allowed create to set the perms protection flag.");
833         } finally {
834                 res.close();
835         }
836         // Finally, update the role with changes and verify
837         setupUpdate();
838
839         Role roleToUpdate = createRoleInstance(knownRoleName+"_PT", "Just a temp", true);
840         roleToUpdate.setMetadataProtection(RoleClient.IMMUTABLE);
841         roleToUpdate.setPermsProtection(RoleClient.IMMUTABLE);
842
843         // Submit the request to the service and store the response.
844         roleRes = client.update(testResourceId, roleToUpdate);
845         try {
846                 // Check the status code of the response: does it match the expected response(s)?
847                 int statusCode = roleRes.getStatus();
848                 if (logger.isDebugEnabled()) {
849                         logger.debug(testName + ": status = " + statusCode);
850                 }
851                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
852                                 invalidStatusCodeMessage(testRequestType, statusCode));
853                 Assert.assertEquals(statusCode, testExpectedStatusCode);
854         
855         
856                 Role roleUpdated = roleRes.readEntity(Role.class);
857                 Assert.assertNotNull(roleUpdated);
858                 if (logger.isDebugEnabled()) {
859                     logger.debug(testName + "Updated role: ");
860                     org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();            
861                     logger.debug(objectAsXmlString(objectFactory.createRole(roleUpdated),
862                             Role.class));            
863                 }
864         
865                 Assert.assertFalse(RoleClient.IMMUTABLE.equalsIgnoreCase(roleUpdated.getMetadataProtection()),
866                                 "Role allowed update of the metadata protection flag.");
867                 Assert.assertFalse(RoleClient.IMMUTABLE.equalsIgnoreCase(roleUpdated.getPermsProtection()),
868                                 "Role allowed update of the perms protection flag.");
869         } finally {
870                 res.close();
871         }
872     }
873
874         @Test(dataProvider = "testName",
875                         dependsOnMethods = {"CRUDTests"})
876     public void updateNotAllowed(String testName) throws Exception {
877
878         // Perform setup.
879         setupUpdate();
880
881         Role roleToUpdate = new Role();
882         roleToUpdate.setCsid(knownResourceId);
883         // Update the content of this resource.
884         roleToUpdate.setRoleName("UPDATED-ROLE_USERS_TEST");
885         roleToUpdate.setDisplayName("UPDATED-ROLE_USERS_TEST");
886         if (logger.isDebugEnabled()) {
887             logger.debug("updated object");
888             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();            
889             logger.debug(objectAsXmlString(objectFactory.createRole(roleToUpdate),
890                     Role.class));
891         }
892         RoleClient client = new RoleClient();
893         // Submit the request to the service and store the response.
894         Response res = client.update(knownResourceId, roleToUpdate);
895         try {
896                 int statusCode = res.getStatus();
897                 // Check the status code of the response: does it match the expected response(s)?
898                 if (logger.isDebugEnabled()) {
899                     logger.debug(testName + ": status = " + statusCode);
900                 }
901                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
902                         invalidStatusCodeMessage(testRequestType, statusCode));
903                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
904         } finally {
905                 res.close();
906         }
907     }
908
909     // Failure outcomes
910     // Placeholders until the three tests below can be uncommented.
911     // See Issue CSPACE-401.
912     /* (non-Javadoc)
913      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
914      */
915     @Override
916     public void updateWithEmptyEntityBody(String testName) throws Exception {
917         //FIXME: Should this test really be empty?  If so, please comment accordingly.
918     }
919
920     /* (non-Javadoc)
921      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
922      */
923     @Override
924     public void updateWithMalformedXml(String testName) throws Exception {
925         //FIXME: Should this test really be empty?  If so, please comment accordingly.
926     }
927
928     /* (non-Javadoc)
929      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
930      */
931     @Override
932     public void updateWithWrongXmlSchema(String testName) throws Exception {
933         //FIXME: Should this test really be empty?  If so, please comment accordingly.
934     }
935
936     /* (non-Javadoc)
937      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
938      */
939     
940     @Override
941 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
942 //    dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
943     public void updateNonExistent(String testName) throws Exception {
944         // Perform setup.
945         setupUpdateNonExistent();
946
947         // Submit the request to the service and store the response.
948         //
949         // Note: The ID used in this 'create' call may be arbitrary.
950         // The only relevant ID may be the one used in updateRole(), below.
951         RoleClient client = new RoleClient();
952         Role role = createRoleInstance("ROLE_XXX",
953                 "xxx",
954                 true);
955         Response res = client.update(NON_EXISTENT_ID, role);
956         try {
957                 int statusCode = res.getStatus();
958         
959                 // Check the status code of the response: does it match
960                 // the expected response(s)?
961                 if (logger.isDebugEnabled()) {
962                     logger.debug(testName + ": status = " + statusCode);
963                 }
964                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
965                         invalidStatusCodeMessage(testRequestType, statusCode));
966                 Assert.assertEquals(statusCode, testExpectedStatusCode);
967         } finally {
968                 res.close();
969         }
970     }
971
972     // ---------------------------------------------------------------
973     // CRUD tests : DELETE tests
974     // ---------------------------------------------------------------
975     // Success outcomes
976     /* (non-Javadoc)
977      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
978      */
979     @Override
980     public void delete(String testName) throws Exception {
981         // Do nothing since other tests like "updateNotAllowed" need the "known resource" that this test
982         // deletes.  Once all those tests get run, the "localDelete" method will call the base class' delete
983         // method that will delete the "known resource".
984     }
985     
986     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
987                 dependsOnMethods = {"updateNotAllowed", "createForUniqueRole", "createForUniqueDisplayRole"})
988     public void localDelete(String testName) throws Exception {
989         super.delete(testName);
990     }
991
992     // Failure outcomes
993     /* (non-Javadoc)
994      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
995      */
996     @Override
997 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
998 //    dependsOnMethods = {"delete"})
999     public void deleteNonExistent(String testName) throws Exception {
1000         // Perform setup.
1001         setupDeleteNonExistent();
1002
1003         // Submit the request to the service and store the response.
1004         RoleClient client = new RoleClient();
1005         Response res = client.delete(NON_EXISTENT_ID);
1006         try {
1007                 int statusCode = res.getStatus();
1008         
1009                 // Check the status code of the response: does it match
1010                 // the expected response(s)?
1011                 if (logger.isDebugEnabled()) {
1012                     logger.debug(testName + ": status = " + statusCode);
1013                 }
1014                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
1015                         invalidStatusCodeMessage(testRequestType, statusCode));
1016                 Assert.assertEquals(statusCode, testExpectedStatusCode);
1017         } finally {
1018                 res.close();
1019         }
1020     }
1021     
1022     // ---------------------------------------------------------------
1023     // Search tests
1024     // ---------------------------------------------------------------
1025     
1026     @Override
1027 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
1028     public void searchWorkflowDeleted(String testName) throws Exception {
1029         // Fixme: null test for now, overriding test in base class
1030     }    
1031
1032     // ---------------------------------------------------------------
1033     // Utility tests : tests of code used in tests above
1034     // ---------------------------------------------------------------
1035     /**
1036      * Tests the code for manually submitting data that is used by several
1037      * of the methods above.
1038      * @throws Exception 
1039      */
1040
1041     // ---------------------------------------------------------------
1042     // Utility methods used by tests above
1043     // ---------------------------------------------------------------
1044     /**
1045      * create role instance
1046      * @param roleName
1047      * @param description
1048      * @param useRoleName
1049      * @return role
1050      */
1051     public Role createRoleInstance(String roleName,
1052             String description,
1053             boolean useRoleName,
1054             List<PermissionValue> permValueList) {
1055
1056         Role role = RoleFactory.createRoleInstance(roleName,
1057                         roleName, //the display name
1058                         description,
1059                 useRoleName,
1060                 permValueList);
1061         
1062         if (logger.isDebugEnabled()) {
1063             logger.debug("to be created, role");
1064             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();            
1065             logger.debug(objectAsXmlString(objectFactory.createRole(role),
1066                     Role.class));
1067         }
1068         
1069         return role;
1070     }
1071     
1072     public Role createRoleInstance(String roleName,
1073             String description,
1074             boolean useRoleName) {
1075         return this.createRoleInstance(roleName, description, useRoleName, RoleFactory.EMPTY_PERMVALUE_LIST);
1076     }
1077     
1078     /**
1079      * Prints the list.
1080      *
1081      * @param testName the test name
1082      * @param list the list
1083      * @return the int
1084      */
1085     protected void printList(String testName, RolesList list) {
1086         for (Role role : list.getRole()) {
1087             logger.debug(testName + " role csid=" + role.getCsid()
1088                     + " name=" + role.getRoleName()
1089                     + " desc=" + role.getDescription());
1090         }
1091     }
1092
1093         @Override
1094         protected Role createInstance(String commonPartName, String identifier) {
1095                 // TODO Auto-generated method stub
1096                 return null;
1097         }
1098
1099         @Override
1100         protected Role updateInstance(Role commonPartObject) {
1101                 // TODO Auto-generated method stub
1102                 return null;
1103         }
1104
1105         @Override
1106         protected void compareUpdatedInstances(Role original, Role updated)
1107                         throws Exception {
1108                 // TODO Auto-generated method stub
1109                 
1110         }
1111
1112         @Override
1113         protected Class<RolesList> getCommonListType() {
1114                 // TODO Auto-generated method stub
1115                 return null;
1116         }
1117
1118     /*
1119      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
1120      * refer to this method in their @Test annotation declarations.
1121      */
1122     @Override
1123     @Test(dataProvider = "testName",
1124                 dependsOnMethods = {
1125                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
1126     public void CRUDTests(String testName) {
1127         // Do nothing.  Simply here to for a TestNG execution order for our tests
1128     }
1129
1130         @Override
1131         protected long getSizeOfList(RolesList list) {
1132                 return list.getRole().size();
1133         }
1134 }