]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0a893e67597297d2c4ae435e26e45d8a651b6f19
[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.account.AccountsCommon;
32 import org.collectionspace.services.authorization.AccountRole;
33 import org.collectionspace.services.authorization.AccountValue;
34 import org.collectionspace.services.authorization.Role;
35 import org.collectionspace.services.authorization.RoleValue;
36 import org.collectionspace.services.client.AccountClient;
37 import org.collectionspace.services.client.AccountFactory;
38 import org.collectionspace.services.client.AccountRoleClient;
39 import org.collectionspace.services.client.CollectionSpaceClient;
40 import org.collectionspace.services.client.RoleClient;
41 import org.collectionspace.services.client.RoleFactory;
42 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
43 import org.collectionspace.services.client.test.ServiceRequestType;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.jboss.resteasy.client.ClientResponse;
46
47
48 import org.testng.Assert;
49 import org.testng.annotations.Test;
50
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.BeforeClass;
55
56 /**
57  * AccountServiceTest, carries out tests against a
58  * deployed and running Account, Role and AccountRole Services.
59  * 
60  * $LastChangedRevision: 917 $
61  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
62  */
63 public class AccountRoleServiceTest extends AbstractServiceTestImpl {
64
65     static private final Logger logger =
66             LoggerFactory.getLogger(AccountRoleServiceTest.class);
67     // Instance variables specific to this test.
68     private String knownResourceId = null;
69     private List<String> allResourceIdsCreated = new ArrayList<String>();
70     private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
71     private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
72     /*
73      * This method is called only by the parent class, AbstractServiceTestImpl
74      */
75
76     @Override
77     protected String getServicePathComponent() {
78         return new AccountRoleClient().getServicePathComponent();
79     }
80
81     @BeforeClass(alwaysRun = true)
82     public void seedData() {
83         String ra = "acc-role-user1";
84         String accId = createAccount(ra, "acc-role-test@cspace.org");
85         AccountValue ava = new AccountValue();
86         ava.setScreenName(ra);
87         ava.setUserId(ra);
88         ava.setAccountId(accId);
89         accValues.put(ava.getScreenName(), ava);
90
91         String rc = "acc-role-user2";
92         String coAccId = createAccount(rc, "acc-role-test@cspace.org");
93         AccountValue avc = new AccountValue();
94         avc.setScreenName(rc);
95         avc.setUserId(rc);
96         avc.setAccountId(coAccId);
97         accValues.put(avc.getScreenName(), avc);
98
99         String ri = "acc-role-user3";
100         String iAccId = createAccount(ri, "acc-role-test@cspace.org");
101         AccountValue avi = new AccountValue();
102         avi.setScreenName(ri);
103         avi.setUserId(ri);
104         avi.setAccountId(iAccId);
105         accValues.put(avi.getScreenName(), avi);
106
107         String rn1 = "ROLE_CO1";
108         String r1RoleId = createRole(rn1);
109         RoleValue rv1 = new RoleValue();
110         rv1.setRoleId(r1RoleId);
111         rv1.setRoleName(rn1);
112         roleValues.put(rv1.getRoleName(), rv1);
113
114         String rn2 = "ROLE_CO2";
115         String r2RoleId = createRole(rn2);
116         RoleValue rv2 = new RoleValue();
117         rv2.setRoleId(r2RoleId);
118         rv2.setRoleName(rn2);
119         roleValues.put(rv2.getRoleName(), rv2);
120     }
121
122     /* (non-Javadoc)
123      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
124      */
125     @Override
126     protected CollectionSpaceClient getClientInstance() {
127         return new AccountRoleClient();
128     }
129     
130     /* (non-Javadoc)
131      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
132      */
133     @Override
134         protected AbstractCommonList getAbstractCommonList(
135                         ClientResponse<AbstractCommonList> response) {
136         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
137         throw new UnsupportedOperationException();
138     }
139     
140         @Test(dataProvider = "testName")
141         @Override
142     public void readPaginatedList(String testName) throws Exception {
143                 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
144         }    
145     
146     // ---------------------------------------------------------------
147     // CRUD tests : CREATE tests
148     // ---------------------------------------------------------------
149     // Success outcomes
150     @Override
151     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
152     public void create(String testName) throws Exception {
153
154         // Perform setup, such as initializing the type of service request
155         // (e.g. CREATE, DELETE), its valid and expected status codes, and
156         // its associated HTTP method name (e.g. POST, DELETE).
157         setupCreate(testName);
158
159         // Submit the request to the service and store the response.
160         AccountValue pv = accValues.get("acc-role-user1");
161         AccountRole accRole = createAccountRoleInstance(pv,
162                 roleValues.values(), true, true);
163         AccountRoleClient client = new AccountRoleClient();
164         ClientResponse<Response> res = client.create(pv.getAccountId(), accRole);
165         int statusCode = res.getStatus();
166
167         if (logger.isDebugEnabled()) {
168             logger.debug(testName + ": status = " + statusCode);
169         }
170         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
173
174         // Store the ID returned from this create operation
175         // for additional tests below.
176         //this is is not important in case of this relationship
177         knownResourceId = extractId(res);
178         if (logger.isDebugEnabled()) {
179             logger.debug(testName + ": knownResourceId=" + knownResourceId);
180         }
181     }
182
183     //to not cause uniqueness violation for accRole, createList is removed
184     @Override
185     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
186     dependsOnMethods = {"create"})
187     public void createList(String testName) throws Exception {
188         //FIXME: Should this test really be empty?  If so, please comment accordingly.
189     }
190
191     // Failure outcomes
192     // Placeholders until the three tests below can be uncommented.
193     // See Issue CSPACE-401.
194     @Override
195     public void createWithEmptyEntityBody(String testName) throws Exception {
196         //FIXME: Should this test really be empty?  If so, please comment accordingly.
197     }
198
199     @Override
200     public void createWithMalformedXml(String testName) throws Exception {
201         //FIXME: Should this test really be empty?  If so, please comment accordingly.
202     }
203
204     @Override
205     public void createWithWrongXmlSchema(String testName) throws Exception {
206         //FIXME: Should this test really be empty?  If so, please comment accordingly.
207     }
208
209     // ---------------------------------------------------------------
210     // CRUD tests : READ tests
211     // ---------------------------------------------------------------
212     // Success outcomes
213     @Override
214     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
215     dependsOnMethods = {"create"})
216     public void read(String testName) throws Exception {
217
218         // Perform setup.
219         setupRead(testName);
220
221         // Submit the request to the service and store the response.
222         AccountRoleClient client = new AccountRoleClient();
223         ClientResponse<AccountRole> res = client.read(
224                 accValues.get("acc-role-user1").getAccountId(), "123");
225         int statusCode = res.getStatus();
226
227         // Check the status code of the response: does it match
228         // the expected response(s)?
229         if (logger.isDebugEnabled()) {
230             logger.debug(testName + ": status = " + statusCode);
231         }
232         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
235
236         AccountRole output = (AccountRole) res.getEntity();
237         Assert.assertNotNull(output);
238     }
239
240     // Failure outcomes
241     @Override
242     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
243     public void readNonExistent(String testName) throws Exception {
244
245         // Perform setup.
246         setupReadNonExistent(testName);
247
248         // Submit the request to the service and store the response.
249         AccountRoleClient client = new AccountRoleClient();
250         ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID, "123");
251         int statusCode = res.getStatus();
252
253         // Check the status code of the response: does it match
254         // the expected response(s)?
255         if (logger.isDebugEnabled()) {
256             logger.debug(testName + ": status = " + statusCode);
257         }
258         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
259                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
260         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
261     }
262
263     // ---------------------------------------------------------------
264     // CRUD tests : READ_LIST tests
265     // ---------------------------------------------------------------
266     // Success outcomes
267     @Override
268     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
269     dependsOnMethods = {"createList", "read"})
270     public void readList(String testName) throws Exception {
271         //FIXME: Should this test really be empty?  If so, please comment accordingly.
272     }
273
274     // Failure outcomes
275     // None at present.
276     // ---------------------------------------------------------------
277     // CRUD tests : UPDATE tests
278     // ---------------------------------------------------------------
279     // Success outcomes
280     @Override
281     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
282     dependsOnMethods = {"read", "readList", "readNonExistent"})
283     public void update(String testName) throws Exception {
284         //FIXME: Should this test really be empty?  If so, please comment accordingly.
285     }
286
287     // Failure outcomes
288     // Placeholders until the three tests below can be uncommented.
289     // See Issue CSPACE-401.
290     @Override
291     public void updateWithEmptyEntityBody(String testName) throws Exception {
292         //FIXME: Should this test really be empty?  If so, please comment accordingly.
293     }
294
295     @Override
296     public void updateWithMalformedXml(String testName) throws Exception {
297         //FIXME: Should this test really be empty?  If so, please comment accordingly.
298     }
299
300     @Override
301     public void updateWithWrongXmlSchema(String testName) throws Exception {
302         //FIXME: Should this test really be empty?  If so, please comment accordingly.
303     }
304
305     @Override
306     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
307     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
308     public void updateNonExistent(String testName) throws Exception {
309         //FIXME: Should this test really be empty?  If so, please comment accordingly.
310     }
311
312     // ---------------------------------------------------------------
313     // CRUD tests : DELETE tests
314     // ---------------------------------------------------------------
315     // Success outcomes
316     @Override
317     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
318     dependsOnMethods = {"read"})
319     public void delete(String testName) throws Exception {
320
321         // Perform setup.
322         setupDelete(testName);
323
324         // Submit the request to the service and store the response.
325         AccountRoleClient client = new AccountRoleClient();
326         ClientResponse<Response> res = client.delete(
327                 accValues.get("acc-role-user1").getAccountId(), "123");
328         int statusCode = res.getStatus();
329
330         // Check the status code of the response: does it match
331         // the expected response(s)?
332         if (logger.isDebugEnabled()) {
333             logger.debug(testName + ": status = " + statusCode);
334         }
335         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
338     }
339
340     // Failure outcomes
341     /* (non-Javadoc)
342      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
343      */
344     @Override
345     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
346     public void deleteNonExistent(String testName) throws Exception {
347         //ignoring this test as the service side returns 200 now even if it does
348         //not find a record in the db
349     }
350
351     // ---------------------------------------------------------------
352     // Utility tests : tests of code used in tests above
353     // ---------------------------------------------------------------
354     /**
355      * Tests the code for manually submitting data that is used by several
356      * of the methods above.
357      */
358     @Test(dependsOnMethods = {"create"})
359     public void testSubmitRequest() throws Exception {
360
361         // Expected status code: 200 OK
362         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
363
364         // Submit the request to the service and store the response.
365         String method = ServiceRequestType.READ.httpMethodName();
366         String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
367         int statusCode = submitRequest(method, url);
368
369         // Check the status code of the response: does it match
370         // the expected response(s)?
371         if (logger.isDebugEnabled()) {
372             logger.debug("testSubmitRequest: url=" + url
373                     + " status=" + statusCode);
374         }
375         Assert.assertEquals(statusCode, EXPECTED_STATUS);
376
377     }
378
379     // ---------------------------------------------------------------
380     // Utility methods used by tests above
381     // ---------------------------------------------------------------
382     /**
383      * Creates the account role instance.
384      *
385      * @param pv the pv
386      * @param rvs the rvs
387      * @param usePermId the use perm id
388      * @param useRoleId the use role id
389      * @return the account role
390      */
391     static public AccountRole createAccountRoleInstance(AccountValue pv,
392             Collection<RoleValue> rvs,
393             boolean usePermId,
394             boolean useRoleId) {
395
396         AccountRole accRole = new AccountRole();
397         //service consume is not required to provide subject as it is determined
398         //from URI used
399 //        accRole.setSubject(SubjectType.ROLE);
400         if (usePermId) {
401             ArrayList<AccountValue> pvs = new ArrayList<AccountValue>();
402             pvs.add(pv);
403             accRole.setAccounts(pvs);
404         }
405         if (useRoleId) {
406             //FIXME is there a better way?
407             ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
408             for (RoleValue rv : rvs) {
409                 rvas.add(rv);
410             }
411             accRole.setRoles(rvas);
412         }
413
414         if (logger.isDebugEnabled()) {
415             logger.debug("to be created, accRole common");
416             logger.debug(objectAsXmlString(accRole, AccountRole.class));
417         }
418         return accRole;
419     }
420
421     @AfterClass(alwaysRun = true)
422     public void cleanUp() {
423         setupDelete("delete");
424         String noTest = System.getProperty("noTestCleanup");
425         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
426             if (logger.isDebugEnabled()) {
427                 logger.debug("Skipping Cleanup phase ...");
428             }
429             return;
430         }
431         if (logger.isDebugEnabled()) {
432             logger.debug("Cleaning up temporary resources created for testing ...");
433         }
434         AccountRoleClient client = new AccountRoleClient();
435         for (String resourceId : allResourceIdsCreated) {
436
437             ClientResponse<Response> res = client.delete(resourceId, "123");
438             int statusCode = res.getStatus();
439             if (logger.isDebugEnabled()) {
440                 logger.debug("clenaup: delete relationships for accission id="
441                         + resourceId + " status=" + statusCode);
442             }
443             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
446         }
447
448         for (AccountValue pv : accValues.values()) {
449             deleteAccount(pv.getAccountId());
450         }
451
452         for (RoleValue rv : roleValues.values()) {
453             deleteRole(rv.getRoleId());
454         }
455     }
456
457     private String createAccount(String userName, String email) {
458         setupCreate();
459         AccountClient accClient = new AccountClient();
460         AccountsCommon account = AccountFactory.createAccountInstance(
461                 userName, userName, userName, email,
462                 true, true, false, true, true);
463         ClientResponse<Response> res = accClient.create(account);
464         int statusCode = res.getStatus();
465         if (logger.isDebugEnabled()) {
466             logger.debug("createAccount: userName=" + userName
467                     + " status = " + statusCode);
468         }
469         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
472         return extractId(res);
473     }
474
475     private void deleteAccount(String accId) {
476         setupDelete();
477         AccountClient accClient = new AccountClient();
478         ClientResponse<Response> res = accClient.delete(accId);
479         int statusCode = res.getStatus();
480         if (logger.isDebugEnabled()) {
481             logger.debug("deleteAccount: delete account id="
482                     + accId + " status=" + statusCode);
483         }
484         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
485                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
486         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
487     }
488
489     private String createRole(String roleName) {
490         setupCreate();
491         RoleClient roleClient = new RoleClient();
492
493         Role role = RoleFactory.createRoleInstance(roleName,
494                 "role for " + roleName, true);
495         ClientResponse<Response> res = roleClient.create(role);
496         int statusCode = res.getStatus();
497         if (logger.isDebugEnabled()) {
498             logger.debug("createRole: name=" + roleName
499                     + " status = " + statusCode);
500         }
501         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
502                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
503         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504         return extractId(res);
505     }
506
507     private void deleteRole(String roleId) {
508         setupDelete();
509         RoleClient roleClient = new RoleClient();
510         ClientResponse<Response> res = roleClient.delete(roleId);
511         int statusCode = res.getStatus();
512         if (logger.isDebugEnabled()) {
513             logger.debug("deleteRole: delete role id=" + roleId
514                     + " status=" + statusCode);
515         }
516         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
517                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
518         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
519     }
520 }