]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e6c1d507b5d039b398db75dcb0eacc4b90965885
[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         //ignoring this test as the service side returns 200 now even if it does
309         //not find a record in the db
310     }
311
312     // ---------------------------------------------------------------
313     // Utility tests : tests of code used in tests above
314     // ---------------------------------------------------------------
315     /**
316      * Tests the code for manually submitting data that is used by several
317      * of the methods above.
318      */
319     @Test(dependsOnMethods = {"create"})
320     public void testSubmitRequest() throws Exception {
321
322         // Expected status code: 200 OK
323         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
324
325         // Submit the request to the service and store the response.
326         String method = ServiceRequestType.READ.httpMethodName();
327         String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
328         int statusCode = submitRequest(method, url);
329
330         // Check the status code of the response: does it match
331         // the expected response(s)?
332         if (logger.isDebugEnabled()) {
333             logger.debug("testSubmitRequest: url=" + url
334                     + " status=" + statusCode);
335         }
336         Assert.assertEquals(statusCode, EXPECTED_STATUS);
337
338     }
339
340     // ---------------------------------------------------------------
341     // Utility methods used by tests above
342     // ---------------------------------------------------------------
343     /**
344      * create accRolerole instance
345      * @param accId
346      * @param roleValues array of role ids
347      * @param userPermId
348      * @param useRoleId
349      * @return
350      */
351     static public AccountRole createAccountRoleInstance(AccountValue pv,
352             Collection<RoleValue> rvs,
353             boolean usePermId,
354             boolean useRoleId) {
355
356         AccountRole accRole = new AccountRole();
357         //service consume is not required to provide subject as it is determined
358         //from URI used
359 //        accRole.setSubject(SubjectType.ROLE);
360         if (usePermId) {
361             ArrayList<AccountValue> pvs = new ArrayList<AccountValue>();
362             pvs.add(pv);
363             accRole.setAccounts(pvs);
364         }
365         if (useRoleId) {
366             //FIXME is there a better way?
367             ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
368             for (RoleValue rv : rvs) {
369                 rvas.add(rv);
370             }
371             accRole.setRoles(rvas);
372         }
373
374         if (logger.isDebugEnabled()) {
375             logger.debug("to be created, accRole common");
376             logger.debug(objectAsXmlString(accRole, AccountRole.class));
377         }
378         return accRole;
379     }
380
381     @AfterClass(alwaysRun = true)
382     public void cleanUp() {
383         setupDelete("delete");
384         if (logger.isDebugEnabled()) {
385             logger.debug("clenaup: Cleaning up temporary resources created for testing ...");
386         }
387         AccountRoleClient client = new AccountRoleClient();
388         for (String resourceId : allResourceIdsCreated) {
389
390             // Note: Any non-success responses are ignored and not reported.
391             ClientResponse<Response> res = client.delete(resourceId, "123");
392             int statusCode = res.getStatus();
393             if (logger.isDebugEnabled()) {
394                 logger.debug("clenaup: delete relationships for accission id="
395                         + resourceId + " status=" + statusCode);
396             }
397             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
398                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
399             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
400         }
401
402         for (AccountValue pv : accValues.values()) {
403             deleteAccount(pv.getAccountId());
404         }
405
406         for (RoleValue rv : roleValues.values()) {
407             deleteRole(rv.getRoleId());
408         }
409     }
410
411     private String createAccount(String userName, String email) {
412         setupCreate();
413         AccountClient accClient = new AccountClient();
414         AccountsCommon account = AccountFactory.createAccountInstance(
415                 userName, userName, userName, email,
416                 true, true, false, true, true);
417         ClientResponse<Response> res = accClient.create(account);
418         int statusCode = res.getStatus();
419         if (logger.isDebugEnabled()) {
420             logger.debug("createAccount: userName=" + userName
421                     + " status = " + statusCode);
422         }
423         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
426         return extractId(res);
427     }
428
429     private void deleteAccount(String accId) {
430         setupDelete();
431         AccountClient accClient = new AccountClient();
432         ClientResponse<Response> res = accClient.delete(accId);
433         int statusCode = res.getStatus();
434         if (logger.isDebugEnabled()) {
435             logger.debug("deleteAccount: delete account id="
436                     + accId + " status=" + statusCode);
437         }
438         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
439                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
440         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
441     }
442
443     private String createRole(String roleName) {
444         setupCreate();
445         RoleClient roleClient = new RoleClient();
446
447         Role role = RoleFactory.createRoleInstance(roleName,
448                 "role for " + roleName, true);
449         ClientResponse<Response> res = roleClient.create(role);
450         int statusCode = res.getStatus();
451         if (logger.isDebugEnabled()) {
452             logger.debug("createRole: name=" + roleName
453                     + " status = " + statusCode);
454         }
455         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
456                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
457         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
458         return extractId(res);
459     }
460
461     private void deleteRole(String roleId) {
462         setupDelete();
463         RoleClient roleClient = new RoleClient();
464         ClientResponse<Response> res = roleClient.delete(roleId);
465         int statusCode = res.getStatus();
466         if (logger.isDebugEnabled()) {
467             logger.debug("deleteRole: delete role id=" + roleId
468                     + " status=" + statusCode);
469         }
470         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
471                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
472         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473     }
474 }