]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6ec65bdc3a9f4e6d9bfad6844f77197e59acb5ce
[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
45 /**
46  * RoleServiceTest, carries out tests against a
47  * deployed and running Role Service.
48  * 
49  * $LastChangedRevision: 917 $
50  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
51  */
52 public class RoleServiceTest extends AbstractServiceTestImpl {
53
54     /** The logger. */
55     private final static String CLASS_NAME = RoleServiceTest.class.getName();
56     private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
57     
58     // Instance variables specific to this test.
59     /** The known resource id. */
60     private String knownResourceId = null;
61     private String knownRoleName = "ROLE_USERS_MOCK";
62     private String knownRoleDisplayName = "ROLE_DISPLAYNAME_USERS_MOCK";
63     private String verifyResourceId = null;
64     private String verifyRoleName = "collections_manager_mock";
65 //    private List<String> allResourceIdsCreated = new ArrayList<String>();
66
67     @Override
68     public String getServiceName() { 
69         return RoleClient.SERVICE_NAME;
70     }
71     
72     @Override
73     protected String getServicePathComponent() {
74         return new RoleClient().getServicePathComponent();
75     }
76
77     /* (non-Javadoc)
78      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79      */
80     @Override
81     protected CollectionSpaceClient getClientInstance() {
82         return new RoleClient();
83     }
84
85     /* (non-Javadoc)
86      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
87      */
88     @Override
89     protected AbstractCommonList getAbstractCommonList(
90             ClientResponse<AbstractCommonList> response) {
91         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
92         throw new UnsupportedOperationException();
93     }
94
95     /* (non-Javadoc)
96      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
97      */
98     @Test(dataProvider = "testName")
99     @Override
100     public void readPaginatedList(String testName) throws Exception {
101         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
102     }
103
104     // ---------------------------------------------------------------
105     // CRUD tests : CREATE tests
106     // ---------------------------------------------------------------
107     // Success outcomes
108     /* (non-Javadoc)
109      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
110      */
111     @Override
112     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
113     public void create(String testName) throws Exception {
114
115         if (logger.isDebugEnabled()) {
116             logger.debug(testBanner(testName, CLASS_NAME));
117         }
118         // Perform setup, such as initializing the type of service request
119         // (e.g. CREATE, DELETE), its valid and expected status codes, and
120         // its associated HTTP method name (e.g. POST, DELETE).
121         setupCreate();
122
123         // Submit the request to the service and store the response.
124         RoleClient client = new RoleClient();
125         Role role = createRoleInstance(knownRoleName,
126                 "all users are required to be in this role",
127                 true);
128         ClientResponse<Response> res = client.create(role);
129         int statusCode = res.getStatus();
130
131         // Check the status code of the response: does it match
132         // the expected response(s)?
133         //
134         // Specifically:
135         // Does it fall within the set of valid status codes?
136         // Does it exactly match the expected status code?
137         if (logger.isDebugEnabled()) {
138             logger.debug(testName + ": status = " + statusCode);
139         }
140         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
142         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
143
144         // Store the ID returned from this create operation
145         // for additional tests below.
146         knownResourceId = extractId(res);
147         if (logger.isDebugEnabled()) {
148             logger.debug(testName + ": knownResourceId=" + knownResourceId);
149         }
150     }
151     
152     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
153     public void createWithDisplayname(String testName) throws Exception {
154
155         if (logger.isDebugEnabled()) {
156             logger.debug(testBanner(testName, CLASS_NAME));
157         }
158         // Perform setup, such as initializing the type of service request
159         // (e.g. CREATE, DELETE), its valid and expected status codes, and
160         // its associated HTTP method name (e.g. POST, DELETE).
161         setupCreate();
162
163         // Submit the request to the service and store the response.
164         RoleClient client = new RoleClient();
165         Role role = createRoleInstance(knownRoleName + "_" + knownRoleDisplayName,
166                 "all users are required to be in this role",
167                 true);
168         role.setDisplayName(knownRoleDisplayName);
169         ClientResponse<Response> res = client.create(role);
170         int statusCode = res.getStatus();
171
172         // Check the status code of the response: does it match
173         // the expected response(s)?
174         //
175         // Specifically:
176         // Does it fall within the set of valid status codes?
177         // Does it exactly match the expected status code?
178         if (logger.isDebugEnabled()) {
179             logger.debug(testName + ": status = " + statusCode);
180         }
181         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
182                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
183         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
184
185         // Store the ID returned from this create operation
186         // for additional tests below.
187         String csid = extractId(res);
188         allResourceIdsCreated.add(csid);
189         if (logger.isDebugEnabled()) {
190             logger.debug(testName + ": csid=" + csid);
191         }
192     }
193     
194
195     /**
196      * Creates the for unique role.
197      *
198      * @param testName the test name
199      * @throws Exception the exception
200      */
201     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
202     public void createForUniqueRole(String testName) throws Exception {
203
204         if (logger.isDebugEnabled()) {
205             logger.debug(testBanner(testName, CLASS_NAME));
206         }
207         setupCreate();
208
209         // Submit the request to the service and store the response.
210         RoleClient client = new RoleClient();
211         Role role = createRoleInstance(knownRoleName,
212                 "role users",
213                 true);
214         ClientResponse<Response> res = client.create(role);
215         int statusCode = res.getStatus();
216
217         if (logger.isDebugEnabled()) {
218                 logger.debug(testName + ": Role with name \"" +
219                                 knownRoleName + "\" should already exist, so this request should fail.");
220             logger.debug(testName + ": status = " + statusCode);
221             logger.debug(testName + ": " + res);
222         }
223         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
224                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
225         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
226     }
227     
228     /**
229      * Creates the for unique display name of role.
230      *
231      * @param testName the test name
232      * @throws Exception the exception
233      */
234     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"createWithDisplayname"})
235     public void createForUniqueDisplayRole(String testName) throws Exception {
236
237         if (logger.isDebugEnabled()) {
238             logger.debug(testBanner(testName, CLASS_NAME));
239         }
240         setupCreate();
241
242         // Submit the request to the service and store the response.
243         RoleClient client = new RoleClient();
244         Role role = createRoleInstance(knownRoleName + System.currentTimeMillis(),
245                 "role users with non-unique display name",
246                 true);
247         role.setDisplayName(knownRoleDisplayName);
248         ClientResponse<Response> res = client.create(role);
249         int statusCode = res.getStatus();
250
251         if (logger.isDebugEnabled()) {
252                 logger.debug(testName + ": Role with name \"" +
253                                 knownRoleName + "\" should already exist, so this request should fail.");
254             logger.debug(testName + ": status = " + statusCode);
255             logger.debug(testName + ": " + res);
256         }
257         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
258                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
259         if (statusCode != Response.Status.BAD_REQUEST.getStatusCode()) {
260             // If the test fails then we've just created a Role that we need to delete, so
261             // store the ID returned from this create operation.
262             String csid = extractId(res);
263             allResourceIdsCreated.add(csid);
264             if (logger.isDebugEnabled()) {
265                 logger.debug(testName + ": csid=" + csid);
266             }
267                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
268         }
269     }
270
271     /**
272      * Creates the without role name.
273      *
274      * @param testName the test name
275      * @throws Exception the exception
276      */
277     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
278     dependsOnMethods = {"create"})
279     public void createWithoutRoleName(String testName) throws Exception {
280
281         if (logger.isDebugEnabled()) {
282             logger.debug(testBanner(testName, CLASS_NAME));
283         }
284         setupCreate();
285
286         // Submit the request to the service and store the response.
287         RoleClient client = new RoleClient();
288         Role role = createRoleInstance("",
289                 "role for users",
290                 false);
291         ClientResponse<Response> res = client.create(role);
292         int statusCode = res.getStatus();
293         // Does it exactly match the expected status code?
294         if (logger.isDebugEnabled()) {
295             logger.debug(testName + ": status = " + statusCode);
296         }
297         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
298                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
299         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
300     }
301
302     //to not cause uniqueness violation for role, createList is removed
303     /* (non-Javadoc)
304      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
305      */
306     @Override
307     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
308     dependsOnMethods = {"createWithDisplayname"})
309     public void createList(String testName) throws Exception {
310
311         if (logger.isDebugEnabled()) {
312             logger.debug(testBanner(testName, CLASS_NAME));
313         }
314         setupCreate();
315
316         // Submit the request to the service and store the response.
317         RoleClient client = new RoleClient();
318         //create a role with lowercase role name without role prefix
319         //the service should make it upper case and add the role prefix
320         Role role1 = createRoleInstance(verifyRoleName,
321                 "collection manager",
322                 true);
323         ClientResponse<Response> res = client.create(role1);
324         int statusCode = res.getStatus();
325         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
326                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
327         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
328         verifyResourceId = extractId(res);
329         allResourceIdsCreated.add(verifyResourceId);
330
331         Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
332                 "collections curator",
333                 true);
334         res = client.create(role2);
335         statusCode = res.getStatus();
336         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
337                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
338         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
339         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
340         allResourceIdsCreated.add(extractId(res));
341
342         Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
343                 "moving image admin",
344                 true);
345         res = client.create(role3);
346         statusCode = res.getStatus();
347         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
348                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
349         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
351         allResourceIdsCreated.add(extractId(res));
352     }
353
354     // Failure outcomes
355     // Placeholders until the three tests below can be uncommented.
356     // See Issue CSPACE-401.
357     /* (non-Javadoc)
358      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
359      */
360     @Override
361     public void createWithEmptyEntityBody(String testName) throws Exception {
362         //FIXME: Should this test really be empty?  If so, please comment accordingly.
363     }
364
365     /* (non-Javadoc)
366      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
367      */
368     @Override
369     public void createWithMalformedXml(String testName) throws Exception {
370         //FIXME: Should this test really be empty?  If so, please comment accordingly.
371     }
372
373     /* (non-Javadoc)
374      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
375      */
376     @Override
377     public void createWithWrongXmlSchema(String testName) throws Exception {
378         //FIXME: Should this test really be empty?  If so, please comment accordingly.
379     }
380
381     // ---------------------------------------------------------------
382     // CRUD tests : READ tests
383     // ---------------------------------------------------------------
384     // Success outcomes
385     /* (non-Javadoc)
386      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
387      */
388     @Override
389     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
390     dependsOnMethods = {"createForUniqueRole"})
391     public void read(String testName) throws Exception {
392
393         if (logger.isDebugEnabled()) {
394             logger.debug(testBanner(testName, CLASS_NAME));
395         }
396         // Perform setup.
397         setupRead();
398
399         // Submit the request to the service and store the response.
400         RoleClient client = new RoleClient();
401         ClientResponse<Role> res = client.read(knownResourceId);
402         int statusCode = res.getStatus();
403
404         // Check the status code of the response: does it match
405         // the expected response(s)?
406         if (logger.isDebugEnabled()) {
407             logger.debug(testName + ": status = " + statusCode);
408         }
409         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
410                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
411         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
412
413         Role output = (Role) res.getEntity();
414         Assert.assertNotNull(output);
415     }
416
417     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
418     dependsOnMethods = {"createList"})
419     public void readToVerify(String testName) throws Exception {
420
421         // Perform setup.
422         setupRead();
423
424         // Submit the request to the service and store the response.
425         RoleClient client = new RoleClient();
426         ClientResponse<Role> res = client.read(verifyResourceId);
427         int statusCode = res.getStatus();
428
429         // Check the status code of the response: does it match
430         // the expected response(s)?
431         if (logger.isDebugEnabled()) {
432             logger.debug(testName + ": status = " + statusCode);
433         }
434         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
435                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
436         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
437
438         Role output = (Role) res.getEntity();
439         Assert.assertNotNull(output);
440
441         //FIXME: Tenant ID of "1" should not be hard coded
442         String roleNameToVerify = "ROLE_" +
443                 "1_" +
444                 verifyRoleName.toUpperCase();
445         Assert.assertEquals(output.getRoleName(), roleNameToVerify,
446                 "RoleName fix did not work!");
447     }
448     // Failure outcomes
449     /* (non-Javadoc)
450      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
451      */
452
453     @Override
454     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
455     dependsOnMethods = {"read"})
456     public void readNonExistent(String testName) throws Exception {
457
458         if (logger.isDebugEnabled()) {
459             logger.debug(testBanner(testName, CLASS_NAME));
460         }
461         // Perform setup.
462         setupReadNonExistent();
463
464         // Submit the request to the service and store the response.
465         RoleClient client = new RoleClient();
466         ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
467         int statusCode = res.getStatus();
468
469         // Check the status code of the response: does it match
470         // the expected response(s)?
471         if (logger.isDebugEnabled()) {
472             logger.debug(testName + ": status = " + statusCode);
473         }
474         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
477     }
478
479     // ---------------------------------------------------------------
480     // CRUD tests : READ_LIST tests
481     // ---------------------------------------------------------------
482     // Success outcomes
483     /* (non-Javadoc)
484      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
485      */
486     @Override
487     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
488     dependsOnMethods = {"createList", "read"})
489     public void readList(String testName) throws Exception {
490
491         if (logger.isDebugEnabled()) {
492             logger.debug(testBanner(testName, CLASS_NAME));
493         }
494         // Perform setup.
495         setupReadList();
496
497         // Submit the request to the service and store the response.
498         RoleClient client = new RoleClient();
499         ClientResponse<RolesList> res = client.readList();
500         RolesList list = res.getEntity();
501         int statusCode = res.getStatus();
502
503         // Check the status code of the response: does it match
504         // the expected response(s)?
505         if (logger.isDebugEnabled()) {
506             logger.debug(testName + ": status = " + statusCode);
507         }
508         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
509                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
510         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
511
512         // Optionally output additional data about list members for debugging.
513         boolean iterateThroughList = true;
514         if (iterateThroughList && logger.isDebugEnabled()) {
515             printList(testName, list);
516         }
517     }
518
519     /**
520      * Search role name.
521      *
522      * @param testName the test name
523      * @throws Exception the exception
524      */
525     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
526     dependsOnMethods = {"createList", "read"})
527     public void searchRoleName(String testName) throws Exception {
528
529         if (logger.isDebugEnabled()) {
530             logger.debug(testBanner(testName, CLASS_NAME));
531         }
532         // Perform setup.
533         setupReadList();
534
535         // Submit the request to the service and store the response.
536         RoleClient client = new RoleClient();
537         ClientResponse<RolesList> res = client.readSearchList("movingImage");
538         RolesList list = res.getEntity();
539         int statusCode = res.getStatus();
540         // Check the status code of the response: does it match
541         // the expected response(s)?
542         if (logger.isDebugEnabled()) {
543             logger.debug(testName + ": status = " + statusCode);
544         }
545         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
546                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
547         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
548         int EXPECTED_ITEMS = 1;
549         if (logger.isDebugEnabled()) {
550             logger.debug(testName + ": received = " + list.getRole().size()
551                     + " expected=" + EXPECTED_ITEMS);
552         }
553         Assert.assertEquals(EXPECTED_ITEMS, list.getRole().size());
554         // Optionally output additional data about list members for debugging.
555         boolean iterateThroughList = true;
556         if (iterateThroughList && logger.isDebugEnabled()) {
557             printList(testName, list);
558         }
559     }
560
561     // Failure outcomes
562     // None at present.
563     // ---------------------------------------------------------------
564     // CRUD tests : UPDATE tests
565     // ---------------------------------------------------------------
566     // Success outcomes
567     /* (non-Javadoc)
568      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
569      */
570     @Override
571     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
572     dependsOnMethods = {"read", "readList", "readNonExistent"})
573     public void update(String testName) throws Exception {
574
575         if (logger.isDebugEnabled()) {
576             logger.debug(testBanner(testName, CLASS_NAME));
577         }
578         // Perform setup.
579         setupUpdate();
580
581         Role roleToUpdate = new Role();
582         roleToUpdate.setCsid(knownResourceId);
583         roleToUpdate.setRoleName(knownRoleName);
584         roleToUpdate.setDisplayName(knownRoleName);
585         
586         // Update the content of this resource.
587         roleToUpdate.setDescription("updated role description");
588         if (logger.isDebugEnabled()) {
589             logger.debug("updated object");
590             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();            
591             logger.debug(objectAsXmlString(objectFactory.createRole(roleToUpdate),
592                     Role.class));
593         }
594         RoleClient client = new RoleClient();
595         // Submit the request to the service and store the response.
596         ClientResponse<Role> res = client.update(knownResourceId, roleToUpdate);
597         int statusCode = res.getStatus();
598         // Check the status code of the response: does it match the expected response(s)?
599         if (logger.isDebugEnabled()) {
600             logger.debug(testName + ": status = " + statusCode);
601         }
602         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
603                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
604         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
605
606
607         Role roleUpdated = (Role) res.getEntity();
608         Assert.assertNotNull(roleUpdated);
609
610         Assert.assertEquals(roleUpdated.getDescription(),
611                 roleToUpdate.getDescription(),
612                 "Data in updated object did not match submitted data.");
613     }
614     
615     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
616                 dependsOnMethods = {"read", "readList", "readNonExistent"})
617         public void verifyProtectionReadOnly(String testName) throws Exception {
618
619         if (logger.isDebugEnabled()) {
620                 logger.debug(testBanner(testName, CLASS_NAME));
621         }
622         
623         setupCreate();
624
625         // Submit the request to the service and store the response.
626         RoleClient client = new RoleClient();
627         Role role = createRoleInstance(knownRoleName+"_PT", "Just a temp", true);
628         role.setMetadataProtection(RoleClient.IMMUTABLE);
629         role.setPermsProtection(RoleClient.IMMUTABLE);
630         ClientResponse<Response> res = client.create(role);
631         int statusCode = res.getStatus();
632         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
633                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
634         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
635
636         // Store the ID returned from this create operation
637         // for additional tests below.
638         String testResourceId = extractId(res);
639         allResourceIdsCreated.add(testResourceId);
640         if (logger.isDebugEnabled()) {
641             logger.debug(testName + ": testResourceId=" + testResourceId);
642         }
643         setupRead();
644
645         // Submit the request to the service and store the response.
646         ClientResponse<Role> roleRes = client.read(testResourceId);
647         statusCode = roleRes.getStatus();
648
649         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
650                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
651         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
652
653         Role roleRead = (Role) roleRes.getEntity();
654         Assert.assertNotNull(roleRead);
655         String mdProtection = roleRead.getMetadataProtection();
656         String permsProtection = roleRead.getPermsProtection();
657         if (logger.isTraceEnabled()) {
658             logger.trace(testName + ": metadataProtection=" + mdProtection);
659             logger.trace(testName + ": permsProtection=" + permsProtection);
660         }
661         Assert.assertFalse(role.getMetadataProtection().equals(mdProtection),
662                         "Role allowed create to set the metadata protection flag.");
663         Assert.assertFalse(role.getPermsProtection().equals(permsProtection),
664                         "Role allowed create to set the perms protection flag.");
665         
666         setupUpdate();
667
668         Role roleToUpdate = createRoleInstance(knownRoleName+"_PT", "Just a temp", true);
669         roleToUpdate.setMetadataProtection(RoleClient.IMMUTABLE);
670         roleToUpdate.setPermsProtection(RoleClient.IMMUTABLE);
671
672         // Submit the request to the service and store the response.
673         roleRes = client.update(testResourceId, roleToUpdate);
674         statusCode = roleRes.getStatus();
675         // Check the status code of the response: does it match the expected response(s)?
676         if (logger.isDebugEnabled()) {
677                 logger.debug(testName + ": status = " + statusCode);
678         }
679         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
680                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
681         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
682
683
684         Role roleUpdated = (Role) roleRes.getEntity();
685         Assert.assertNotNull(roleUpdated);
686         if (logger.isDebugEnabled()) {
687             logger.debug(testName + "Updated role: ");
688             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();            
689             logger.debug(objectAsXmlString(objectFactory.createRole(roleUpdated),
690                     Role.class));            
691         }
692
693         Assert.assertFalse(
694                         RoleClient.IMMUTABLE.equalsIgnoreCase(roleUpdated.getMetadataProtection()),
695                         "Role allowed update of the metadata protection flag.");
696         Assert.assertFalse(
697                         RoleClient.IMMUTABLE.equalsIgnoreCase(roleUpdated.getPermsProtection()),
698                         "Role allowed update of the perms protection flag.");
699     }
700
701
702
703     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
704     dependsOnMethods = {"read", "readList", "readNonExistent"})
705     public void updateNotAllowed(String testName) throws Exception {
706
707         // Perform setup.
708         setupUpdate();
709
710         Role roleToUpdate = new Role();
711         roleToUpdate.setCsid(knownResourceId);
712         // Update the content of this resource.
713         roleToUpdate.setRoleName("UPDATED-ROLE_USERS_TEST");
714         roleToUpdate.setDisplayName("UPDATED-ROLE_USERS_TEST");
715         if (logger.isDebugEnabled()) {
716             logger.debug("updated object");
717             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();            
718             logger.debug(objectAsXmlString(objectFactory.createRole(roleToUpdate),
719                     Role.class));
720         }
721         RoleClient client = new RoleClient();
722         // Submit the request to the service and store the response.
723         ClientResponse<Role> res = client.update(knownResourceId, roleToUpdate);
724         int statusCode = res.getStatus();
725         // Check the status code of the response: does it match the expected response(s)?
726         if (logger.isDebugEnabled()) {
727             logger.debug(testName + ": status = " + statusCode);
728         }
729         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
730                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
731         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
732
733     }
734
735     // Failure outcomes
736     // Placeholders until the three tests below can be uncommented.
737     // See Issue CSPACE-401.
738     /* (non-Javadoc)
739      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
740      */
741     @Override
742     public void updateWithEmptyEntityBody(String testName) throws Exception {
743         //FIXME: Should this test really be empty?  If so, please comment accordingly.
744     }
745
746     /* (non-Javadoc)
747      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
748      */
749     @Override
750     public void updateWithMalformedXml(String testName) throws Exception {
751         //FIXME: Should this test really be empty?  If so, please comment accordingly.
752     }
753
754     /* (non-Javadoc)
755      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
756      */
757     @Override
758     public void updateWithWrongXmlSchema(String testName) throws Exception {
759         //FIXME: Should this test really be empty?  If so, please comment accordingly.
760     }
761
762     /* (non-Javadoc)
763      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
764      */
765     @Override
766     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
767     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
768     public void updateNonExistent(String testName) throws Exception {
769
770         if (logger.isDebugEnabled()) {
771             logger.debug(testBanner(testName, CLASS_NAME));
772         }
773         // Perform setup.
774         setupUpdateNonExistent();
775
776         // Submit the request to the service and store the response.
777         //
778         // Note: The ID used in this 'create' call may be arbitrary.
779         // The only relevant ID may be the one used in updateRole(), below.
780         RoleClient client = new RoleClient();
781         Role role = createRoleInstance("ROLE_XXX",
782                 "xxx",
783                 true);
784         ClientResponse<Role> res =
785                 client.update(NON_EXISTENT_ID, role);
786         int statusCode = res.getStatus();
787
788         // Check the status code of the response: does it match
789         // the expected response(s)?
790         if (logger.isDebugEnabled()) {
791             logger.debug(testName + ": status = " + statusCode);
792         }
793         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
794                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
795         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
796     }
797
798     // ---------------------------------------------------------------
799     // CRUD tests : DELETE tests
800     // ---------------------------------------------------------------
801     // Success outcomes
802     /* (non-Javadoc)
803      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
804      */
805     @Override
806     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
807     dependsOnMethods = {"updateNotAllowed", "testSubmitRequest", "verifyProtectionReadOnly"})
808     public void delete(String testName) throws Exception {
809
810         if (logger.isDebugEnabled()) {
811             logger.debug(testBanner(testName, CLASS_NAME));
812         }
813         // Perform setup.
814         setupDelete();
815
816         // Submit the request to the service and store the response.
817         RoleClient client = new RoleClient();
818         ClientResponse<Response> res = client.delete(knownResourceId);
819         int statusCode = res.getStatus();
820
821         // Check the status code of the response: does it match
822         // the expected response(s)?
823         if (logger.isDebugEnabled()) {
824             logger.debug(testName + ": status = " + statusCode);
825         }
826         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
827                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
828         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
829     }
830
831     // Failure outcomes
832     /* (non-Javadoc)
833      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
834      */
835     @Override
836     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
837     dependsOnMethods = {"delete"})
838     public void deleteNonExistent(String testName) throws Exception {
839
840         if (logger.isDebugEnabled()) {
841             logger.debug(testBanner(testName, CLASS_NAME));
842         }
843         // Perform setup.
844         setupDeleteNonExistent();
845
846         // Submit the request to the service and store the response.
847         RoleClient client = new RoleClient();
848         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
849         int statusCode = res.getStatus();
850
851         // Check the status code of the response: does it match
852         // the expected response(s)?
853         if (logger.isDebugEnabled()) {
854             logger.debug(testName + ": status = " + statusCode);
855         }
856         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
857                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
858         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
859     }
860     
861     // ---------------------------------------------------------------
862     // Search tests
863     // ---------------------------------------------------------------
864     
865     @Override
866     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
867     public void searchWorkflowDeleted(String testName) throws Exception {
868         // Fixme: null test for now, overriding test in base class
869     }    
870
871     // ---------------------------------------------------------------
872     // Utility tests : tests of code used in tests above
873     // ---------------------------------------------------------------
874     /**
875      * Tests the code for manually submitting data that is used by several
876      * of the methods above.
877      * @throws Exception 
878      */
879
880     @Test(dependsOnMethods = {"create"})
881     public void testSubmitRequest() throws Exception {
882
883         // Expected status code: 200 OK
884         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
885
886         // Submit the request to the service and store the response.
887         String method = ServiceRequestType.READ.httpMethodName();
888         String url = getResourceURL(knownResourceId);
889         int statusCode = submitRequest(method, url);
890
891         // Check the status code of the response: does it match
892         // the expected response(s)?
893         if (logger.isDebugEnabled()) {
894             logger.debug("testSubmitRequest: url=" + url
895                     + " status=" + statusCode);
896         }
897         Assert.assertEquals(statusCode, EXPECTED_STATUS);
898
899     }
900
901     // ---------------------------------------------------------------
902     // Utility methods used by tests above
903     // ---------------------------------------------------------------
904     /**
905      * create role instance
906      * @param roleName
907      * @param description
908      * @param useRoleName
909      * @return role
910      */
911     public Role createRoleInstance(String roleName,
912             String description,
913             boolean useRoleName) {
914
915         Role role = RoleFactory.createRoleInstance(roleName,
916                         roleName, //the display name
917                         description,
918                 useRoleName);
919         if (logger.isDebugEnabled()) {
920             logger.debug("to be created, role");
921             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();            
922             logger.debug(objectAsXmlString(objectFactory.createRole(role),
923                     Role.class));
924         }
925         return role;
926
927     }
928
929     /**
930      * Prints the list.
931      *
932      * @param testName the test name
933      * @param list the list
934      * @return the int
935      */
936     private int printList(String testName, RolesList list) {
937
938         int i = 0;
939
940         for (Role role : list.getRole()) {
941             logger.debug(testName + " role csid=" + role.getCsid()
942                     + " name=" + role.getRoleName()
943                     + " desc=" + role.getDescription());
944             i++;
945         }
946         return i;
947     }
948 }