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