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