]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
053da69a652f6d5576339a5001d6684b49791453
[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.account.client.test;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Hashtable;
28 import java.util.List;
29 import javax.ws.rs.core.Response;
30
31 //import org.collectionspace.services.authorization.AccountRolesList;
32 import org.collectionspace.services.account.AccountsCommon;
33 import org.collectionspace.services.account.AccountsCommonList;
34 import org.collectionspace.services.account.AccountListItem;
35
36 import org.collectionspace.services.authorization.AccountRole;
37 import org.collectionspace.services.authorization.AccountValue;
38 import org.collectionspace.services.authorization.Role;
39 import org.collectionspace.services.authorization.RoleValue;
40 import org.collectionspace.services.client.AccountClient;
41 import org.collectionspace.services.client.AccountFactory;
42 import org.collectionspace.services.client.AccountRoleClient;
43 import org.collectionspace.services.client.AccountRoleFactory;
44 import org.collectionspace.services.client.CollectionSpaceClient;
45 import org.collectionspace.services.client.RoleClient;
46 import org.collectionspace.services.client.RoleFactory;
47 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
48 import org.collectionspace.services.client.test.ServiceRequestType;
49 import org.collectionspace.services.jaxb.AbstractCommonList;
50 import org.jboss.resteasy.client.ClientResponse;
51
52
53 import org.testng.Assert;
54 import org.testng.annotations.Test;
55
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58 import org.testng.annotations.AfterClass;
59 import org.testng.annotations.BeforeClass;
60
61 /**
62  * AccountServiceTest, carries out tests against a
63  * deployed and running Account, Role and AccountRole Services.
64  * 
65  * $LastChangedRevision: 917 $
66  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
67  */
68 public class AccountRoleServiceTest extends AbstractServiceTestImpl {
69
70     /** The Constant logger. */
71     private final static String CLASS_NAME = AccountRoleServiceTest.class.getName();
72     private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
73     // Instance variables specific to this test.
74     /** The known resource id. */
75     private String knownResourceId = null;
76     private String prebuiltAdminCSID = null;
77     private String prebuiltAdminUserId = "admin@core.collectionspace.org";
78     /** The all resource ids created. */
79     private List<String> allResourceIdsCreated = new ArrayList<String>();
80     /** The acc values. */
81     private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
82     /** The role values. */
83     private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
84     /*
85      * This method is called only by the parent class, AbstractServiceTestImpl
86      */
87
88     /* (non-Javadoc)
89      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
90      */
91     @Override
92     protected String getServicePathComponent() {
93         return new AccountRoleClient().getServicePathComponent();
94     }
95
96     /**
97      * Seed data.
98      */
99     @BeforeClass(alwaysRun = true)
100     public void seedData() {
101         String userId = "acc-role-user1";
102         String accId = createAccount(userId, "acc-role-user1-test@cspace.org");
103         AccountValue ava = new AccountValue();
104         ava.setScreenName(userId);
105         ava.setUserId(userId);
106         ava.setAccountId(accId);
107         accValues.put(ava.getScreenName(), ava);
108
109         String userId2 = "acc-role-user2";
110         String coAccId = createAccount(userId2, "acc-role-user2-test@cspace.org");
111         AccountValue avc = new AccountValue();
112         avc.setScreenName(userId2);
113         avc.setUserId(userId2);
114         avc.setAccountId(coAccId);
115         accValues.put(avc.getScreenName(), avc);
116
117         String rn1 = "ROLE_CO1";
118         String r1RoleId = createRole(rn1);
119         RoleValue rv1 = new RoleValue();
120         rv1.setRoleId(r1RoleId);
121         rv1.setRoleName(rn1);
122         roleValues.put(rv1.getRoleName(), rv1);
123
124         String rn2 = "ROLE_CO2";
125         String r2RoleId = createRole(rn2);
126         RoleValue rv2 = new RoleValue();
127         rv2.setRoleId(r2RoleId);
128         rv2.setRoleName(rn2);
129         roleValues.put(rv2.getRoleName(), rv2);
130     }
131
132     /* (non-Javadoc)
133      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
134      */
135     @Override
136     protected CollectionSpaceClient getClientInstance() {
137         return new AccountRoleClient();
138     }
139
140     /* (non-Javadoc)
141      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
142      */
143     @Override
144     protected AbstractCommonList getAbstractCommonList(
145             ClientResponse<AbstractCommonList> response) {
146         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
147         throw new UnsupportedOperationException();
148     }
149
150     /* (non-Javadoc)
151      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
152      */
153     @Test(dataProvider = "testName")
154     @Override
155     public void readPaginatedList(String testName) throws Exception {
156         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
157     }
158
159     // ---------------------------------------------------------------
160     // CRUD tests : CREATE tests
161     // ---------------------------------------------------------------
162     // Success outcomes
163     /* (non-Javadoc)
164      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
165      */
166     @Override
167     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
168     public void create(String testName) throws Exception {
169
170         if (logger.isDebugEnabled()) {
171             testBanner(testName, CLASS_NAME);
172         }
173         // Perform setup, such as initializing the type of service request
174         // (e.g. CREATE, DELETE), its valid and expected status codes, and
175         // its associated HTTP method name (e.g. POST, DELETE).
176         setupCreate();
177
178         // Submit the request to the service and store the response.
179         AccountValue av = accValues.get("acc-role-user1");
180         AccountRole accRole = createAccountRoleInstance(av,
181                 roleValues.values(), true, true);
182         AccountRoleClient client = new AccountRoleClient();
183         ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
184         try {
185             int statusCode = res.getStatus();
186
187             if (logger.isDebugEnabled()) {
188                 logger.debug(testName + ": status = " + statusCode);
189             }
190             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
191                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
192             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
193
194             // Store the ID returned from this create operation
195             // for additional tests below.
196             //this is is not important in case of this relationship
197             knownResourceId = extractId(res);
198             if (logger.isDebugEnabled()) {
199                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
200             }
201         } finally {
202             res.releaseConnection();
203         }
204     }
205
206     //to not cause uniqueness violation for accRole, createList is removed
207     /* (non-Javadoc)
208      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
209      */
210     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
211     dependsOnMethods = {"create"})
212     @Override
213     public void createList(String testName) throws Exception {
214         //FIXME: Should this test really be empty?  If so, please comment accordingly.
215     }
216
217     // Failure outcomes
218     // Placeholders until the three tests below can be uncommented.
219     // See Issue CSPACE-401.
220     /* (non-Javadoc)
221      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
222      */
223     @Override
224     public void createWithEmptyEntityBody(String testName) throws Exception {
225         //FIXME: Should this test really be empty?  If so, please comment accordingly.
226     }
227
228     /* (non-Javadoc)
229      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
230      */
231     @Override
232     public void createWithMalformedXml(String testName) throws Exception {
233         //FIXME: Should this test really be empty?  If so, please comment accordingly.
234     }
235
236     /* (non-Javadoc)
237      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
238      */
239     @Override
240     public void createWithWrongXmlSchema(String testName) throws Exception {
241         //FIXME: Should this test really be empty?  If so, please comment accordingly.
242     }
243
244     // ---------------------------------------------------------------
245     // CRUD tests : READ tests
246     // ---------------------------------------------------------------
247     // Success outcomes
248     /* (non-Javadoc)
249      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
250      */
251     @Override
252     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
253     dependsOnMethods = {"create"})
254     public void read(String testName) throws Exception {
255
256         if (logger.isDebugEnabled()) {
257             testBanner(testName, CLASS_NAME);
258         }
259
260         // Perform setup.
261         setupRead();
262
263         // Submit the request to the service and store the response.
264         AccountRoleClient client = new AccountRoleClient();
265         ClientResponse<AccountRole> res = client.read(
266                 accValues.get("acc-role-user1").getAccountId());
267         assertStatusCode(res, testName);
268         try {
269             AccountRole output = res.getEntity();
270             Assert.assertNotNull(output);
271         } finally {
272             res.releaseConnection();
273         }
274     }
275
276     // Failure outcomes
277     /* (non-Javadoc)
278      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
279      */
280     @Override
281     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
282     public void readNonExistent(String testName) throws Exception {
283
284         if (logger.isDebugEnabled()) {
285             testBanner(testName, CLASS_NAME);
286         }
287
288         // Perform setup.
289         setupReadNonExistent();
290
291         // Submit the request to the service and store the response.
292         AccountRoleClient client = new AccountRoleClient();
293         ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID);
294         int statusCode = res.getStatus();
295         try {
296             // Check the status code of the response: does it match
297             // the expected response(s)?
298             if (logger.isDebugEnabled()) {
299                 logger.debug(testName + ": status = " + statusCode);
300             }
301             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
302                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
303             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
304         } finally {
305             res.releaseConnection();
306         }
307     }
308
309     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
310     dependsOnMethods = {"create"})
311     public void readNoRelationship(String testName) throws Exception {
312
313         if (logger.isDebugEnabled()) {
314             testBanner(testName, CLASS_NAME);
315         }
316
317         // Perform setup.
318         setupRead();
319
320         // Submit the request to the service and store the response.
321         AccountRoleClient client = new AccountRoleClient();
322         ClientResponse<AccountRole> res = client.read(
323                 accValues.get("acc-role-user2").getAccountId());
324         assertStatusCode(res, testName);
325         try {
326             AccountRole output = res.getEntity();
327
328             if(logger.isDebugEnabled()) {
329                 org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
330                 String sOutput = objectAsXmlString(objectFactory.createAccountRole(output), AccountRole.class);
331                 logger.debug(testName + " received " + sOutput);
332             }
333         } finally {
334             res.releaseConnection();
335         }
336     }
337     
338     /*
339      * In this test, for setup, we associate both test roles ("ROLE_CO1", "ROLE_CO2") with the test account "acc-role-user2".
340      * After we've performed this setup, our call to "/role/{csid}/accountroles" should contain an AccountRole that has
341      * a list of 1 account -the test user account we associated during setup.
342      */
343     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
344             dependsOnMethods = {"delete"})
345     public void readRoleAccounts(String testName) throws Exception {
346
347         if (logger.isDebugEnabled()) {
348             testBanner(testName, CLASS_NAME);
349         }
350
351                 /*
352                  * Setup a temp local scope for local variables that we need to create the AccountRole for
353                  * the setup of the read tests.
354                  */
355         {
356                 setupCreate();
357
358                 // Associate "acc-role-user2" with all the roles.
359                 AccountValue av = accValues.get("acc-role-user2");
360                 AccountRole accRole = createAccountRoleInstance(av,
361                         roleValues.values(), true, true);
362                 AccountRoleClient client = new AccountRoleClient();
363                 ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
364                 int statusCode = res.getStatus();
365                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
368         }
369
370         //
371         // Now read the list of accounts associated with the role "ROLE_CO1".
372         // There should be just the "acc-role-user2" account.
373         //
374         setupRead();        
375         RoleClient roleClient = new RoleClient();
376         
377         // Submit the request to the service and store the response.
378         ClientResponse<AccountRole> res = roleClient.readRoleAccounts(
379                         roleValues.get("ROLE_CO1").getRoleId());
380         assertStatusCode(res, testName);
381         try {
382             AccountRole output = res.getEntity();
383             
384             // Now verify that the role has 2 accounts associate to it.
385             Assert.assertEquals(output.getAccount().size(), 1);
386
387             String sOutput = objectAsXmlString(output, AccountRole.class);
388             if(logger.isDebugEnabled()) {
389                 logger.debug(testName + " received " + sOutput);
390             }
391         } finally {
392             res.releaseConnection();
393         }
394     }    
395
396     // ---------------------------------------------------------------
397     // CRUD tests : READ_LIST tests
398     // ---------------------------------------------------------------
399     // Success outcomes
400     /* (non-Javadoc)
401      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
402      */
403     @Override
404     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
405     dependsOnMethods = {"createList", "read"})
406     public void readList(String testName) throws Exception {
407         //FIXME: Should this test really be empty?  If so, please comment accordingly.
408     }
409
410     // Failure outcomes
411     // None at present.
412     // ---------------------------------------------------------------
413     // CRUD tests : UPDATE tests
414     // ---------------------------------------------------------------
415     // Success outcomes
416     /* (non-Javadoc)
417      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
418      */
419     @Override
420     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
421     dependsOnMethods = {"read", "readList", "readNonExistent"})
422     public void update(String testName) throws Exception {
423         //FIXME: Should this test really be empty?  If so, please comment accordingly.
424     }
425
426     // Failure outcomes
427     // Placeholders until the three tests below can be uncommented.
428     // See Issue CSPACE-401.
429     /* (non-Javadoc)
430      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
431      */
432     @Override
433     public void updateWithEmptyEntityBody(String testName) throws Exception {
434         //FIXME: Should this test really be empty?  If so, please comment accordingly.
435     }
436
437     /* (non-Javadoc)
438      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
439      */
440     @Override
441     public void updateWithMalformedXml(String testName) throws Exception {
442         //FIXME: Should this test really be empty?  If so, please comment accordingly.
443     }
444
445     /* (non-Javadoc)
446      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
447      */
448     @Override
449     public void updateWithWrongXmlSchema(String testName) throws Exception {
450         //FIXME: Should this test really be empty?  If so, please comment accordingly.
451     }
452
453     /* (non-Javadoc)
454      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
455      */
456     @Override
457     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
458     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
459     public void updateNonExistent(String testName) throws Exception {
460         //FIXME: Should this test really be empty?  If so, please comment accordingly.
461     }
462
463     // ---------------------------------------------------------------
464     // CRUD tests : DELETE tests
465     // ---------------------------------------------------------------
466     // Success outcomes
467     /* (non-Javadoc)
468      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
469      */
470     @Override
471     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
472     dependsOnMethods = {"read"})
473     public void delete(String testName) throws Exception {
474
475         if (logger.isDebugEnabled()) {
476             testBanner(testName, CLASS_NAME);
477         }
478
479         // Perform setup.
480         setupDelete();
481         
482         //
483         // Lookup a know account, and delete all of its role relationships
484         //
485         AccountRoleClient client = new AccountRoleClient();
486         ClientResponse<AccountRole> readResponse = client.read(
487                 accValues.get("acc-role-user1").getAccountId());
488         AccountRole toDelete = null;
489         try {
490                 toDelete = readResponse.getEntity();
491         } catch (Throwable e) {
492                 e.printStackTrace();
493         } finally {
494                 readResponse.releaseConnection();
495         }
496
497         ClientResponse<Response> res = client.delete(
498                 toDelete.getAccount().get(0).getAccountId(), toDelete);
499         try {
500             int statusCode = res.getStatus();
501             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
502                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
503             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504         } finally {
505             res.releaseConnection();
506         }
507         
508         //
509         // recreate 'acc-role-user1' account and roles
510         //
511 //        create(testName);
512         
513         /*
514         setupDelete();
515         
516         //
517         // Lookup a know account, and delete all of its role relationships
518         //
519         readResponse = client.read(
520                 accValues.get("acc-role-user1").getAccountId());
521         toDelete = null;
522         try {
523                 toDelete = readResponse.getEntity();
524         } catch (Throwable e) {
525                 e.printStackTrace();
526         } finally {
527                 readResponse.releaseConnection();
528         }
529
530         res = client.delete(toDelete.getAccount().get(0).getAccountId());
531         try {
532             int statusCode = res.getStatus();
533             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
534                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
535             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
536         } finally {
537             res.releaseConnection();
538         }
539         */
540         
541     }
542
543     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
544             dependsOnMethods = {"read"})
545         public void deleteLockedAccount(String testName) throws Exception {
546
547         if (logger.isDebugEnabled()) {
548                 testBanner(testName, CLASS_NAME);
549         }
550         
551         findPrebuiltAdminAccount();
552
553         // Perform setup.
554         EXPECTED_STATUS_CODE = Response.Status.FORBIDDEN.getStatusCode();
555         REQUEST_TYPE = ServiceRequestType.DELETE;
556         testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE);
557
558         AccountRoleClient client = new AccountRoleClient();
559         ClientResponse<Response> res = client.delete(prebuiltAdminCSID);
560         try {
561                 int statusCode = res.getStatus();
562                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
563                                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
564                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
565         } finally {
566                 res.releaseConnection();
567         }
568     }
569
570     // Failure outcomes
571     /* (non-Javadoc)
572      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
573      */
574     @Override
575     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
576     public void deleteNonExistent(String testName) throws Exception {
577         //ignoring this test as the service side returns 200 now even if it does
578         //not find a record in the db
579     }
580     
581     // ---------------------------------------------------------------
582     // Search tests
583     // ---------------------------------------------------------------
584     
585     @Override
586     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
587     public void searchWorkflowDeleted(String testName) throws Exception {
588         // Fixme: null test for now, overriding test in base class
589     }    
590
591     // ---------------------------------------------------------------
592     // Utility tests : tests of code used in tests above
593     // ---------------------------------------------------------------
594     /**
595      * Tests the code for manually submitting data that is used by several
596      * of the methods above.
597      * @throws Exception 
598      */
599     @Test(dependsOnMethods = {"create"})
600     public void testSubmitRequest() throws Exception {  //FIXME:  REM - This is not testing a submit /accounts/*/accountroles, but instead just to /accounts
601
602         // Expected status code: 200 OK
603         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
604
605         // Submit the request to the service and store the response.
606         String method = ServiceRequestType.READ.httpMethodName();
607         String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
608         int statusCode = submitRequest(method, url);
609
610         // Check the status code of the response: does it match
611         // the expected response(s)?
612         if (logger.isDebugEnabled()) {
613             logger.debug("testSubmitRequest: url=" + url
614                     + " status=" + statusCode);
615         }
616         Assert.assertEquals(statusCode, EXPECTED_STATUS);
617
618     }
619
620     // ---------------------------------------------------------------
621     // Utility methods used by tests above
622     // ---------------------------------------------------------------
623     /**
624      * Creates the account role instance.
625      *
626      * @param av the av
627      * @param rvs the rvs
628      * @param usePermId the use perm id
629      * @param useRoleId the use role id
630      * @return the account role
631      */
632     static public AccountRole createAccountRoleInstance(AccountValue pv,
633             Collection<RoleValue> rvs,
634             boolean usePermId,
635             boolean useRoleId) {
636
637         AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
638                 pv, rvs, usePermId, useRoleId);
639
640         if (logger.isDebugEnabled()) {
641             logger.debug("to be created, accRole common");
642             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
643             logger.debug(objectAsXmlString(objectFactory.createAccountRole(accRole), AccountRole.class));
644         }
645         return accRole;
646     }
647
648     /**
649      * Clean up.
650      */
651     @AfterClass(alwaysRun = true)
652     @Override
653     public void cleanUp() {
654
655         setupDelete();
656
657         String noTest = System.getProperty("noTestCleanup");
658         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
659             if (logger.isDebugEnabled()) {
660                 logger.debug("Skipping Cleanup phase ...");
661             }
662             return;
663         }
664         if (logger.isDebugEnabled()) {
665             logger.debug("Cleaning up temporary resources created for testing ...");
666         }
667
668
669         for (AccountValue pv : accValues.values()) {
670             deleteAccount(pv.getAccountId());
671         }
672
673         for (RoleValue rv : roleValues.values()) {
674             deleteRole(rv.getRoleId());
675         }
676     }
677
678     /**
679      * Creates the account.
680      *
681      * @param userName the user name
682      * @param email the email
683      * @return the string
684      */
685     private String createAccount(String userName, String email) {
686
687         if (logger.isDebugEnabled()) {
688             testBanner("createAccount");
689         }
690
691         setupCreate();
692
693         AccountClient accClient = new AccountClient();
694         AccountsCommon account = AccountFactory.createAccountInstance(
695                 userName, userName, userName, email, accClient.getTenantId(),
696                 true, false, true, true);
697         ClientResponse<Response> res = accClient.create(account);
698         int statusCode = res.getStatus();
699         if (logger.isDebugEnabled()) {
700             logger.debug("createAccount: userName=" + userName
701                     + " status = " + statusCode);
702         }
703         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
704                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
705         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
706         res.releaseConnection();
707         return extractId(res);
708     }
709
710     private void findPrebuiltAdminAccount() {
711         // Search for the prebuilt admin user and then hold its CSID
712         if(prebuiltAdminCSID == null) {
713             setupReadList();
714             AccountClient client = new AccountClient();
715             ClientResponse<AccountsCommonList> res =
716                     client.readSearchList(null, this.prebuiltAdminUserId, null);
717             assertStatusCode(res, "findPrebuiltAdminAccount");
718             AccountsCommonList list = res.getEntity();
719             
720             List<AccountListItem> items = list.getAccountListItem();
721             Assert.assertEquals(1, items.size(), "Found more than one Admin account!");
722             AccountListItem item = items.get(0);
723             prebuiltAdminCSID = item.getCsid();
724             if (logger.isDebugEnabled()) {
725                 logger.debug("Found Admin Account with ID: " + prebuiltAdminCSID);
726             }
727         }
728     }
729     
730     /**
731      * Delete account.
732      *
733      * @param accId the acc id
734      */
735     private void deleteAccount(String accId) {
736
737         if (logger.isDebugEnabled()) {
738             testBanner("deleteAccount");
739         }
740
741         setupDelete();
742
743         AccountClient accClient = new AccountClient();
744         ClientResponse<Response> res = accClient.delete(accId);
745         int statusCode = res.getStatus();
746         try {
747             if (logger.isDebugEnabled()) {
748                 logger.debug("deleteAccount: delete account id="
749                         + accId + " status=" + statusCode);
750             }
751             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
752                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
753             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
754         } finally {
755             res.releaseConnection();
756         }
757     }
758
759     /**
760      * Creates the role.
761      *
762      * @param roleName the role name
763      * @return the string
764      */
765     private String createRole(String roleName) {
766         if (logger.isDebugEnabled()) {
767             testBanner("createRole");
768         }
769         setupCreate();
770         RoleClient roleClient = new RoleClient();
771
772         Role role = RoleFactory.createRoleInstance(roleName,
773                         roleName, //the display name
774                 "role for " + roleName, true);
775         ClientResponse<Response> res = roleClient.create(role);
776         int statusCode = res.getStatus();
777         if (logger.isDebugEnabled()) {
778             logger.debug("createRole: name=" + roleName
779                     + " status = " + statusCode);
780         }
781         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
782                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
783         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
784         res.releaseConnection();
785         return extractId(res);
786     }
787
788     /**
789      * Delete role.
790      *
791      * @param roleId the role id
792      */
793     private void deleteRole(String roleId) {
794         if (logger.isDebugEnabled()) {
795             testBanner("deleteRole");
796         }
797         setupDelete();
798         RoleClient roleClient = new RoleClient();
799         ClientResponse<Response> res = roleClient.delete(roleId);
800         int statusCode = res.getStatus();
801         try {
802             if (logger.isDebugEnabled()) {
803                 logger.debug("deleteRole: delete role id=" + roleId
804                         + " status=" + statusCode);
805             }
806             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
807                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
808             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
809         } finally {
810             res.releaseConnection();
811         }
812     }
813
814         @Override
815         protected String getServiceName() {
816                 // AccountRoles service is a sub-service of the Account service, so we return Account's service name
817                 return AccountClient.SERVICE_NAME;
818         }
819 }