]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a23530cb88a1899ffd6e85a77ab4b7cd18e2952b
[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         } finally {
517                 readResponse.releaseConnection();
518         }
519
520         ClientResponse<Response> res = client.delete(
521                 toDelete.getAccount().get(0).getAccountId(), toDelete);
522         try {
523             int statusCode = res.getStatus();
524             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
525                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
526             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
527         } finally {
528             res.releaseConnection();
529         }
530         
531         //
532         // recreate 'acc-role-user1' account and roles
533         //
534         create(testName);
535         setupDelete();
536         
537         //
538         // Lookup a know account, and delete all of its role relationships
539         //
540         readResponse = client.read(
541                 accValues.get("acc-role-user1").getAccountId());
542         toDelete = null;
543         try {
544                 toDelete = readResponse.getEntity();
545         } finally {
546                 readResponse.releaseConnection();
547         }
548
549         res = client.delete(toDelete.getAccount().get(0).getAccountId());
550         try {
551             int statusCode = res.getStatus();
552             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
553                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
554             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
555         } finally {
556             res.releaseConnection();
557         }
558         
559     }
560
561     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
562             dependsOnMethods = {"read"})
563         public void deleteLockedAccount(String testName) throws Exception {
564
565         if (logger.isDebugEnabled()) {
566                 testBanner(testName, CLASS_NAME);
567         }
568         
569         findPrebuiltAdminAccount();
570
571         // Perform setup.
572         EXPECTED_STATUS_CODE = Response.Status.FORBIDDEN.getStatusCode();
573         REQUEST_TYPE = ServiceRequestType.DELETE;
574         testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE);
575
576         AccountRoleClient client = new AccountRoleClient();
577         ClientResponse<Response> res = client.delete(prebuiltAdminCSID);
578         try {
579                 int statusCode = res.getStatus();
580                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
581                                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
582                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
583         } finally {
584                 res.releaseConnection();
585         }
586     }
587
588     // Failure outcomes
589     /* (non-Javadoc)
590      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
591      */
592     @Override
593     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
594     public void deleteNonExistent(String testName) throws Exception {
595         //ignoring this test as the service side returns 200 now even if it does
596         //not find a record in the db
597     }
598     
599     // ---------------------------------------------------------------
600     // Search tests
601     // ---------------------------------------------------------------
602     
603     @Override
604     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
605     public void searchWorkflowDeleted(String testName) throws Exception {
606         // Fixme: null test for now, overriding test in base class
607     }    
608
609     // ---------------------------------------------------------------
610     // Utility tests : tests of code used in tests above
611     // ---------------------------------------------------------------
612     /**
613      * Tests the code for manually submitting data that is used by several
614      * of the methods above.
615      * @throws Exception 
616      */
617     @Test(dependsOnMethods = {"create"})
618     public void testSubmitRequest() throws Exception {  //FIXME:  REM - This is not testing a submit /accounts/*/accountroles, but instead just to /accounts
619
620         // Expected status code: 200 OK
621         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
622
623         // Submit the request to the service and store the response.
624         String method = ServiceRequestType.READ.httpMethodName();
625         String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
626         int statusCode = submitRequest(method, url);
627
628         // Check the status code of the response: does it match
629         // the expected response(s)?
630         if (logger.isDebugEnabled()) {
631             logger.debug("testSubmitRequest: url=" + url
632                     + " status=" + statusCode);
633         }
634         Assert.assertEquals(statusCode, EXPECTED_STATUS);
635
636     }
637
638     // ---------------------------------------------------------------
639     // Utility methods used by tests above
640     // ---------------------------------------------------------------
641     /**
642      * Creates the account role instance.
643      *
644      * @param av the av
645      * @param rvs the rvs
646      * @param usePermId the use perm id
647      * @param useRoleId the use role id
648      * @return the account role
649      */
650     static public AccountRole createAccountRoleInstance(AccountValue pv,
651             Collection<RoleValue> rvs,
652             boolean usePermId,
653             boolean useRoleId) {
654
655         AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
656                 pv, rvs, usePermId, useRoleId);
657
658         if (logger.isDebugEnabled()) {
659             logger.debug("to be created, accRole common");
660             org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
661             logger.debug(objectAsXmlString(objectFactory.createAccountRole(accRole), AccountRole.class));
662         }
663         return accRole;
664     }
665
666     /**
667      * Clean up.
668      */
669     @AfterClass(alwaysRun = true)
670     @Override
671     public void cleanUp() {
672
673         setupDelete();
674
675         String noTest = System.getProperty("noTestCleanup");
676         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
677             if (logger.isDebugEnabled()) {
678                 logger.debug("Skipping Cleanup phase ...");
679             }
680             return;
681         }
682         if (logger.isDebugEnabled()) {
683             logger.debug("Cleaning up temporary resources created for testing ...");
684         }
685
686
687         for (AccountValue pv : accValues.values()) {
688             deleteAccount(pv.getAccountId());
689         }
690
691         for (RoleValue rv : roleValues.values()) {
692             deleteRole(rv.getRoleId());
693         }
694     }
695
696     /**
697      * Creates the account.
698      *
699      * @param userName the user name
700      * @param email the email
701      * @return the string
702      */
703     private String createAccount(String userName, String email) {
704
705         if (logger.isDebugEnabled()) {
706             testBanner("createAccount");
707         }
708
709         setupCreate();
710
711         AccountClient accClient = new AccountClient();
712         AccountsCommon account = AccountFactory.createAccountInstance(
713                 userName, userName, userName, email, accClient.getTenantId(),
714                 true, false, true, true);
715         ClientResponse<Response> res = accClient.create(account);
716         int statusCode = res.getStatus();
717         if (logger.isDebugEnabled()) {
718             logger.debug("createAccount: userName=" + userName
719                     + " status = " + statusCode);
720         }
721         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
722                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
723         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
724         res.releaseConnection();
725         return extractId(res);
726     }
727
728     private void findPrebuiltAdminAccount() {
729         // Search for the prebuilt admin user and then hold its CSID
730         if(prebuiltAdminCSID == null) {
731             setupReadList();
732             AccountClient client = new AccountClient();
733             ClientResponse<AccountsCommonList> res =
734                     client.readSearchList(null, this.prebuiltAdminUserId, null);
735             AccountsCommonList list = res.getEntity();
736             int statusCode = res.getStatus();
737
738             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
739             List<AccountListItem> items = list.getAccountListItem();
740             Assert.assertEquals(1, items.size(), "Found more than one Admin account!");
741             AccountListItem item = items.get(0);
742             prebuiltAdminCSID = item.getCsid();
743             if (logger.isDebugEnabled()) {
744                 logger.debug("Found Admin Account with ID: " + prebuiltAdminCSID);
745             }
746         }
747     }
748     
749     /**
750      * Delete account.
751      *
752      * @param accId the acc id
753      */
754     private void deleteAccount(String accId) {
755
756         if (logger.isDebugEnabled()) {
757             testBanner("deleteAccount");
758         }
759
760         setupDelete();
761
762         AccountClient accClient = new AccountClient();
763         ClientResponse<Response> res = accClient.delete(accId);
764         int statusCode = res.getStatus();
765         try {
766             if (logger.isDebugEnabled()) {
767                 logger.debug("deleteAccount: delete account id="
768                         + accId + " status=" + statusCode);
769             }
770             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
771                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
772             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
773         } finally {
774             res.releaseConnection();
775         }
776     }
777
778     /**
779      * Creates the role.
780      *
781      * @param roleName the role name
782      * @return the string
783      */
784     private String createRole(String roleName) {
785         if (logger.isDebugEnabled()) {
786             testBanner("createRole");
787         }
788         setupCreate();
789         RoleClient roleClient = new RoleClient();
790
791         Role role = RoleFactory.createRoleInstance(roleName,
792                         roleName, //the display name
793                 "role for " + roleName, true);
794         ClientResponse<Response> res = roleClient.create(role);
795         int statusCode = res.getStatus();
796         if (logger.isDebugEnabled()) {
797             logger.debug("createRole: name=" + roleName
798                     + " status = " + statusCode);
799         }
800         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
801                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
802         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
803         res.releaseConnection();
804         return extractId(res);
805     }
806
807     /**
808      * Delete role.
809      *
810      * @param roleId the role id
811      */
812     private void deleteRole(String roleId) {
813         if (logger.isDebugEnabled()) {
814             testBanner("deleteRole");
815         }
816         setupDelete();
817         RoleClient roleClient = new RoleClient();
818         ClientResponse<Response> res = roleClient.delete(roleId);
819         int statusCode = res.getStatus();
820         try {
821             if (logger.isDebugEnabled()) {
822                 logger.debug("deleteRole: delete role id=" + roleId
823                         + " status=" + statusCode);
824             }
825             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
826                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
827             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
828         } finally {
829             res.releaseConnection();
830         }
831     }
832
833         @Override
834         protected String getServiceName() {
835                 // AccountRoles service is a sub-service of the Account service, so we return Account's service name
836                 return AccountClient.SERVICE_NAME;
837         }
838 }