]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ba1401788e6ecce4c19020fd0c5dd1f234bebb73
[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         int statusCode = res.getStatus();
268         try {
269             // Check the status code of the response: does it match
270             // the expected response(s)?
271             if (logger.isDebugEnabled()) {
272                 logger.debug(testName + ": status = " + statusCode);
273             }
274             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
275                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
276             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
277
278             AccountRole output = res.getEntity();
279             Assert.assertNotNull(output);
280         } finally {
281             res.releaseConnection();
282         }
283     }
284
285     // Failure outcomes
286     /* (non-Javadoc)
287      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
288      */
289     @Override
290     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
291     public void readNonExistent(String testName) throws Exception {
292
293         if (logger.isDebugEnabled()) {
294             testBanner(testName, CLASS_NAME);
295         }
296
297         // Perform setup.
298         setupReadNonExistent();
299
300         // Submit the request to the service and store the response.
301         AccountRoleClient client = new AccountRoleClient();
302         ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID);
303         int statusCode = res.getStatus();
304         try {
305             // Check the status code of the response: does it match
306             // the expected response(s)?
307             if (logger.isDebugEnabled()) {
308                 logger.debug(testName + ": status = " + statusCode);
309             }
310             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
311                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
312             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
313         } finally {
314             res.releaseConnection();
315         }
316     }
317
318     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
319     dependsOnMethods = {"create"})
320     public void readNoRelationship(String testName) throws Exception {
321
322         if (logger.isDebugEnabled()) {
323             testBanner(testName, CLASS_NAME);
324         }
325
326         // Perform setup.
327         setupRead();
328
329         // Submit the request to the service and store the response.
330         AccountRoleClient client = new AccountRoleClient();
331         ClientResponse<AccountRole> res = client.read(
332                 accValues.get("acc-role-user2").getAccountId());
333         int statusCode = res.getStatus();
334         try {
335             // Check the status code of the response: does it match
336             // the expected response(s)?
337             if (logger.isDebugEnabled()) {
338                 logger.debug(testName + ": status = " + statusCode);
339             }
340             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
341                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
342             Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
343             AccountRole output = res.getEntity();
344
345             if(logger.isDebugEnabled()) {
346                 org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
347                 String sOutput = objectAsXmlString(objectFactory.createAccountRole(output), AccountRole.class);
348                 logger.debug(testName + " received " + sOutput);
349             }
350         } finally {
351             res.releaseConnection();
352         }
353     }
354     
355     /*
356      * In this test, for setup, we associate both test roles ("ROLE_CO1", "ROLE_CO2") with the test account "acc-role-user2".
357      * After we've performed this setup, our call to "/role/{csid}/accountroles" should contain an AccountRole that has
358      * a list of 1 account -the test user account we associated during setup.
359      */
360     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
361             dependsOnMethods = {"delete"})
362     public void readRoleAccounts(String testName) throws Exception {
363
364         if (logger.isDebugEnabled()) {
365             testBanner(testName, CLASS_NAME);
366         }
367
368                 /*
369                  * Setup a temp local scope for local variables that we need to create the AccountRole for
370                  * the setup of the read tests.
371                  */
372         {
373                 setupCreate();
374
375                 // Associate "acc-role-user2" with all the roles.
376                 AccountValue av = accValues.get("acc-role-user2");
377                 AccountRole accRole = createAccountRoleInstance(av,
378                         roleValues.values(), true, true);
379                 AccountRoleClient client = new AccountRoleClient();
380                 ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
381                 int statusCode = res.getStatus();
382                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
383                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
384                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
385         }
386
387         //
388         // Now read the list of accounts associated with the role "ROLE_CO1".
389         // There should be just the "acc-role-user2" account.
390         //
391         setupRead();        
392         RoleClient roleClient = new RoleClient();
393         
394         // Submit the request to the service and store the response.
395         ClientResponse<AccountRole> res = roleClient.readRoleAccounts(
396                         roleValues.get("ROLE_CO1").getRoleId());
397         int statusCode = res.getStatus();
398         try {
399             // Check the status code of the response: does it match
400             // the expected response(s)?
401             if (logger.isDebugEnabled()) {
402                 logger.debug(testName + ": status = " + statusCode);
403             }
404             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
405                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
406             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
407             AccountRole output = res.getEntity();
408             
409             // Now verify that the role has 2 accounts associate to it.
410             Assert.assertEquals(output.getAccount().size(), 1);
411
412             String sOutput = objectAsXmlString(output, AccountRole.class);
413             if(logger.isDebugEnabled()) {
414                 logger.debug(testName + " received " + sOutput);
415             }
416         } finally {
417             res.releaseConnection();
418         }
419     }    
420
421     // ---------------------------------------------------------------
422     // CRUD tests : READ_LIST tests
423     // ---------------------------------------------------------------
424     // Success outcomes
425     /* (non-Javadoc)
426      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
427      */
428     @Override
429     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
430     dependsOnMethods = {"createList", "read"})
431     public void readList(String testName) throws Exception {
432         //FIXME: Should this test really be empty?  If so, please comment accordingly.
433     }
434
435     // Failure outcomes
436     // None at present.
437     // ---------------------------------------------------------------
438     // CRUD tests : UPDATE tests
439     // ---------------------------------------------------------------
440     // Success outcomes
441     /* (non-Javadoc)
442      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
443      */
444     @Override
445     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
446     dependsOnMethods = {"read", "readList", "readNonExistent"})
447     public void update(String testName) throws Exception {
448         //FIXME: Should this test really be empty?  If so, please comment accordingly.
449     }
450
451     // Failure outcomes
452     // Placeholders until the three tests below can be uncommented.
453     // See Issue CSPACE-401.
454     /* (non-Javadoc)
455      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
456      */
457     @Override
458     public void updateWithEmptyEntityBody(String testName) throws Exception {
459         //FIXME: Should this test really be empty?  If so, please comment accordingly.
460     }
461
462     /* (non-Javadoc)
463      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
464      */
465     @Override
466     public void updateWithMalformedXml(String testName) throws Exception {
467         //FIXME: Should this test really be empty?  If so, please comment accordingly.
468     }
469
470     /* (non-Javadoc)
471      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
472      */
473     @Override
474     public void updateWithWrongXmlSchema(String testName) throws Exception {
475         //FIXME: Should this test really be empty?  If so, please comment accordingly.
476     }
477
478     /* (non-Javadoc)
479      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
480      */
481     @Override
482     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
483     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
484     public void updateNonExistent(String testName) throws Exception {
485         //FIXME: Should this test really be empty?  If so, please comment accordingly.
486     }
487
488     // ---------------------------------------------------------------
489     // CRUD tests : DELETE tests
490     // ---------------------------------------------------------------
491     // Success outcomes
492     /* (non-Javadoc)
493      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
494      */
495     @Override
496     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
497     dependsOnMethods = {"read"})
498     public void delete(String testName) throws Exception {
499
500         if (logger.isDebugEnabled()) {
501             testBanner(testName, CLASS_NAME);
502         }
503
504         // Perform setup.
505         setupDelete();
506         
507         //
508         // Lookup a know account, and delete all of its role relationships
509         //
510         AccountRoleClient client = new AccountRoleClient();
511         ClientResponse<AccountRole> readResponse = client.read(
512                 accValues.get("acc-role-user1").getAccountId());
513         AccountRole toDelete = null;
514         try {
515                 toDelete = readResponse.getEntity();
516         } catch (Throwable e) {
517                 e.printStackTrace();
518         } finally {
519                 readResponse.releaseConnection();
520         }
521
522         ClientResponse<Response> res = client.delete(
523                 toDelete.getAccount().get(0).getAccountId(), toDelete);
524         try {
525             int statusCode = res.getStatus();
526             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
527                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
528             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
529         } finally {
530             res.releaseConnection();
531         }
532         
533         //
534         // recreate 'acc-role-user1' account and roles
535         //
536 //        create(testName);
537         
538         /*
539         setupDelete();
540         
541         //
542         // Lookup a know account, and delete all of its role relationships
543         //
544         readResponse = client.read(
545                 accValues.get("acc-role-user1").getAccountId());
546         toDelete = null;
547         try {
548                 toDelete = readResponse.getEntity();
549         } catch (Throwable e) {
550                 e.printStackTrace();
551         } finally {
552                 readResponse.releaseConnection();
553         }
554
555         res = client.delete(toDelete.getAccount().get(0).getAccountId());
556         try {
557             int statusCode = res.getStatus();
558             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
559                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
560             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
561         } finally {
562             res.releaseConnection();
563         }
564         */
565         
566     }
567
568     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
569             dependsOnMethods = {"read"})
570         public void deleteLockedAccount(String testName) throws Exception {
571
572         if (logger.isDebugEnabled()) {
573                 testBanner(testName, CLASS_NAME);
574         }
575         
576         findPrebuiltAdminAccount();
577
578         // Perform setup.
579         EXPECTED_STATUS_CODE = Response.Status.FORBIDDEN.getStatusCode();
580         REQUEST_TYPE = ServiceRequestType.DELETE;
581         testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE);
582
583         AccountRoleClient client = new AccountRoleClient();
584         ClientResponse<Response> res = client.delete(prebuiltAdminCSID);
585         try {
586                 int statusCode = res.getStatus();
587                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
588                                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
589                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
590         } finally {
591                 res.releaseConnection();
592         }
593     }
594
595     // Failure outcomes
596     /* (non-Javadoc)
597      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
598      */
599     @Override
600     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
601     public void deleteNonExistent(String testName) throws Exception {
602         //ignoring this test as the service side returns 200 now even if it does
603         //not find a record in the db
604     }
605     
606     // ---------------------------------------------------------------
607     // Search tests
608     // ---------------------------------------------------------------
609     
610     @Override
611     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
612     public void searchWorkflowDeleted(String testName) throws Exception {
613         // Fixme: null test for now, overriding test in base class
614     }    
615
616     // ---------------------------------------------------------------
617     // Utility tests : tests of code used in tests above
618     // ---------------------------------------------------------------
619     /**
620      * Tests the code for manually submitting data that is used by several
621      * of the methods above.
622      * @throws Exception 
623      */
624     @Test(dependsOnMethods = {"create"})
625     public void testSubmitRequest() throws Exception {  //FIXME:  REM - This is not testing a submit /accounts/*/accountroles, but instead just to /accounts
626
627         // Expected status code: 200 OK
628         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
629
630         // Submit the request to the service and store the response.
631         String method = ServiceRequestType.READ.httpMethodName();
632         String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
633         int statusCode = submitRequest(method, url);
634
635         // Check the status code of the response: does it match
636         // the expected response(s)?
637         if (logger.isDebugEnabled()) {
638             logger.debug("testSubmitRequest: url=" + url
639                     + " status=" + statusCode);
640         }
641         Assert.assertEquals(statusCode, EXPECTED_STATUS);
642
643     }
644
645     // ---------------------------------------------------------------
646     // Utility methods used by tests above
647     // ---------------------------------------------------------------
648     /**
649      * Creates the account role instance.
650      *
651      * @param av the av
652      * @param rvs the rvs
653      * @param usePermId the use perm id
654      * @param useRoleId the use role id
655      * @return the account role
656      */
657     static public AccountRole createAccountRoleInstance(AccountValue pv,
658             Collection<RoleValue> rvs,
659             boolean usePermId,
660             boolean useRoleId) {
661
662         AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
663                 pv, rvs, usePermId, useRoleId);
664
665         if (logger.isDebugEnabled()) {
666             logger.debug("to be created, accRole common");
667             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
668             logger.debug(objectAsXmlString(objectFactory.createAccountRole(accRole), AccountRole.class));
669         }
670         return accRole;
671     }
672
673     /**
674      * Clean up.
675      */
676     @AfterClass(alwaysRun = true)
677     @Override
678     public void cleanUp() {
679
680         setupDelete();
681
682         String noTest = System.getProperty("noTestCleanup");
683         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
684             if (logger.isDebugEnabled()) {
685                 logger.debug("Skipping Cleanup phase ...");
686             }
687             return;
688         }
689         if (logger.isDebugEnabled()) {
690             logger.debug("Cleaning up temporary resources created for testing ...");
691         }
692
693
694         for (AccountValue pv : accValues.values()) {
695             deleteAccount(pv.getAccountId());
696         }
697
698         for (RoleValue rv : roleValues.values()) {
699             deleteRole(rv.getRoleId());
700         }
701     }
702
703     /**
704      * Creates the account.
705      *
706      * @param userName the user name
707      * @param email the email
708      * @return the string
709      */
710     private String createAccount(String userName, String email) {
711
712         if (logger.isDebugEnabled()) {
713             testBanner("createAccount");
714         }
715
716         setupCreate();
717
718         AccountClient accClient = new AccountClient();
719         AccountsCommon account = AccountFactory.createAccountInstance(
720                 userName, userName, userName, email, accClient.getTenantId(),
721                 true, false, true, true);
722         ClientResponse<Response> res = accClient.create(account);
723         int statusCode = res.getStatus();
724         if (logger.isDebugEnabled()) {
725             logger.debug("createAccount: userName=" + userName
726                     + " status = " + statusCode);
727         }
728         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
729                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
730         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
731         res.releaseConnection();
732         return extractId(res);
733     }
734
735     private void findPrebuiltAdminAccount() {
736         // Search for the prebuilt admin user and then hold its CSID
737         if(prebuiltAdminCSID == null) {
738             setupReadList();
739             AccountClient client = new AccountClient();
740             ClientResponse<AccountsCommonList> res =
741                     client.readSearchList(null, this.prebuiltAdminUserId, null);
742             AccountsCommonList list = res.getEntity();
743             int statusCode = res.getStatus();
744
745             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
746             List<AccountListItem> items = list.getAccountListItem();
747             Assert.assertEquals(1, items.size(), "Found more than one Admin account!");
748             AccountListItem item = items.get(0);
749             prebuiltAdminCSID = item.getCsid();
750             if (logger.isDebugEnabled()) {
751                 logger.debug("Found Admin Account with ID: " + prebuiltAdminCSID);
752             }
753         }
754     }
755     
756     /**
757      * Delete account.
758      *
759      * @param accId the acc id
760      */
761     private void deleteAccount(String accId) {
762
763         if (logger.isDebugEnabled()) {
764             testBanner("deleteAccount");
765         }
766
767         setupDelete();
768
769         AccountClient accClient = new AccountClient();
770         ClientResponse<Response> res = accClient.delete(accId);
771         int statusCode = res.getStatus();
772         try {
773             if (logger.isDebugEnabled()) {
774                 logger.debug("deleteAccount: delete account id="
775                         + accId + " status=" + statusCode);
776             }
777             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
778                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
779             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
780         } finally {
781             res.releaseConnection();
782         }
783     }
784
785     /**
786      * Creates the role.
787      *
788      * @param roleName the role name
789      * @return the string
790      */
791     private String createRole(String roleName) {
792         if (logger.isDebugEnabled()) {
793             testBanner("createRole");
794         }
795         setupCreate();
796         RoleClient roleClient = new RoleClient();
797
798         Role role = RoleFactory.createRoleInstance(roleName,
799                         roleName, //the display name
800                 "role for " + roleName, true);
801         ClientResponse<Response> res = roleClient.create(role);
802         int statusCode = res.getStatus();
803         if (logger.isDebugEnabled()) {
804             logger.debug("createRole: name=" + roleName
805                     + " status = " + statusCode);
806         }
807         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
808                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
809         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
810         res.releaseConnection();
811         return extractId(res);
812     }
813
814     /**
815      * Delete role.
816      *
817      * @param roleId the role id
818      */
819     private void deleteRole(String roleId) {
820         if (logger.isDebugEnabled()) {
821             testBanner("deleteRole");
822         }
823         setupDelete();
824         RoleClient roleClient = new RoleClient();
825         ClientResponse<Response> res = roleClient.delete(roleId);
826         int statusCode = res.getStatus();
827         try {
828             if (logger.isDebugEnabled()) {
829                 logger.debug("deleteRole: delete role id=" + roleId
830                         + " status=" + statusCode);
831             }
832             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
833                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
834             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
835         } finally {
836             res.releaseConnection();
837         }
838     }
839
840         @Override
841         protected String getServiceName() {
842                 // AccountRoles service is a sub-service of the Account service, so we return Account's service name
843                 return AccountClient.SERVICE_NAME;
844         }
845 }