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