]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3887bbcc77bc5467c18f88d38f42ce85ee4d699a
[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.List;
26
27 import javax.ws.rs.core.Response;
28
29 import org.collectionspace.services.client.AccountClient;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.account.AccountsCommon;
32 import org.collectionspace.services.account.AccountsCommonList;
33 import org.collectionspace.services.account.AccountListItem;
34 import org.collectionspace.services.account.Status;
35 import org.collectionspace.services.client.AccountFactory;
36 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * AccountServiceTest, carries out tests against a
44  * deployed and running Account Service.
45  * 
46  * $LastChangedRevision: 917 $
47  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
48  */
49 public class AccountServiceTest extends AbstractServiceTestImpl<AccountsCommonList, AccountsCommon, AccountsCommon, AccountsCommon> {
50
51     /** The Constant logger. */
52     private final String CLASS_NAME = AccountServiceTest.class.getName();
53     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
54
55     // Instance variables specific to this test.
56     private String prebuiltAdminCSID = null;
57     private String prebuiltAdminUserId = "admin@core.collectionspace.org";
58     private String knownUserId = "barney";
59     private String knownUserPassword = "hithere08";
60     /** The add tenant. */
61     static boolean addTenant = true;
62
63     @Override
64     public String getServiceName() {
65         return AccountClient.SERVICE_NAME;
66     }
67     
68     /**
69      * The entity type expected from the JAX-RS Response object
70      */
71     public Class<AccountsCommon> getEntityResponseType() {
72         return AccountsCommon.class;
73     }
74     
75     /*
76      * This method is called only by the parent class, AbstractServiceTestImpl
77      */
78     /* (non-Javadoc)
79      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
80      */
81     @Override
82     protected String getServicePathComponent() throws Exception {
83         return new AccountClient().getServicePathComponent();
84     }
85
86     /* (non-Javadoc)
87      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
88      */
89     @Override
90     protected CollectionSpaceClient getClientInstance() throws Exception {
91         return new AccountClient();
92     }
93
94         @Override
95         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
96         return new AccountClient(clientPropertiesFilename);
97         }
98         
99     /* (non-Javadoc)
100      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
101      */
102     @Override
103     protected AccountsCommonList getCommonList(Response response) {
104         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
105         throw new UnsupportedOperationException();
106     }
107     
108     protected Class<AccountsCommonList> getCommonListType() {
109         return (Class<AccountsCommonList>) AccountsCommonList.class;
110     }    
111
112     @Override
113     public void readPaginatedList(String testName) throws Exception {
114         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
115     }
116
117     // ---------------------------------------------------------------
118     // CRUD tests : CREATE tests
119     // ---------------------------------------------------------------
120     // Success outcomes
121     
122     /**
123      * Creates the for unique user.
124      *
125      * @param testName the test name
126      * @throws Exception the exception
127      */
128     @Test(dataProvider = "testName",
129                 dependsOnMethods = {"CRUDTests"})
130     public void createForUniqueUser(String testName) throws Exception {
131         setupCreate();
132
133         // Submit the request to the service and store the response.
134         AccountClient client = new AccountClient();
135         AccountsCommon account =
136                 createAccountInstance("barney1", knownUserId, knownUserPassword,
137                 "barney@dinoland.com",
138                 client.getTenantId(), true, false, true, true);
139
140         Response res = client.create(account);
141         try {
142                 int statusCode = res.getStatus();
143                 if (logger.isDebugEnabled()) {
144                     logger.debug(testName + ": status = " + statusCode);
145                 }
146                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
147                         invalidStatusCodeMessage(testRequestType, statusCode));
148                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
149         } finally {
150                 res.close();
151         }
152     }
153
154     /**
155      * Creates the with invalid tenant.
156      *
157      * @param testName the test name
158      * @throws Exception the exception
159      */
160     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
161     public void createWithInvalidTenant(String testName) throws Exception {
162         setupCreate();
163
164         // Submit the request to the service and store the response.
165         AccountClient client = new AccountClient();
166         AccountsCommon account =
167                 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
168                 client.getTenantId(), true, true, true, true);
169         Response res = client.create(account);
170         try {
171                 int statusCode = res.getStatus();
172                 // Does it exactly match the expected status code?
173                 if (logger.isDebugEnabled()) {
174                     logger.debug(testName + ": status = " + statusCode);
175                 }
176                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
177                         invalidStatusCodeMessage(testRequestType, statusCode));
178                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
179         } finally {
180                 res.close();
181         }
182     }
183
184     /**
185      * Creates the without user.
186      *
187      * @param testName the test name
188      * @throws Exception the exception
189      */
190     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
191     public void createWithoutUser(String testName) throws Exception {
192         setupCreate();
193
194         // Submit the request to the service and store the response.
195         AccountClient client = new AccountClient();
196         AccountsCommon account =
197                 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
198                 client.getTenantId(), true, false, false, true);
199         Response res = client.create(account);
200         try {
201                 int statusCode = res.getStatus();
202                 // Does it exactly match the expected status code?
203                 if (logger.isDebugEnabled()) {
204                     logger.debug(testName + ": status = " + statusCode);
205                 }
206                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
207                         invalidStatusCodeMessage(testRequestType, statusCode));
208                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
209         } finally {
210                 res.close();
211         }
212     }
213
214     /**
215      * Creates the with invalid email.
216      *
217      * @param testName the test name
218      * @throws Exception the exception
219      */
220     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
221     public void createWithInvalidEmail(String testName) throws Exception {
222         setupCreate();
223
224         // Submit the request to the service and store the response.
225         AccountClient client = new AccountClient();
226         AccountsCommon account =
227                 createAccountInstance("babybop", "babybop", "hithere08", "babybop.dinoland.com",
228                 client.getTenantId(), true, false, true, true);
229         Response res = client.create(account);
230         try {
231                 int statusCode = res.getStatus();
232                 // Does it exactly match the expected status code?
233                 if (logger.isDebugEnabled()) {
234                     logger.debug(testName + ": status = " + statusCode);
235                 }
236                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
237                         invalidStatusCodeMessage(testRequestType, statusCode));
238                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
239         } finally {
240                 res.close();
241         }
242     }
243
244     /**
245      * Creates the without screen name.
246      *
247      * @param testName the test name
248      * @throws Exception the exception
249      */
250     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
251     public void createWithoutScreenName(String testName) throws Exception {
252         setupCreate();
253
254         // Submit the request to the service and store the response.
255         AccountClient client = new AccountClient();
256         AccountsCommon account =
257                 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
258                 client.getTenantId(), false, false, true, true);
259         Response res = client.create(account);
260         try {
261                 int statusCode = res.getStatus();
262                 // Does it exactly match the expected status code?
263                 if (logger.isDebugEnabled()) {
264                     logger.debug(testName + ": status = " + statusCode);
265                 }
266                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
267                         invalidStatusCodeMessage(testRequestType, statusCode));
268                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
269         } finally {
270                 res.close();
271         }
272     }
273
274     /**
275      * Creates the with invalid password.
276      *
277      * @param testName the test name
278      * @throws Exception the exception
279      */
280     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
281     public void createWithInvalidPassword(String testName) throws Exception {
282         setupCreate();
283
284         // Submit the request to the service and store the response.
285         AccountClient client = new AccountClient();
286         AccountsCommon account =
287                 createAccountInstance("babybop", "babybop", "shpswd", "babybop@dinoland.com",
288                 client.getTenantId(), true, false, true, true);
289         Response res = client.create(account);
290         try {
291                 int statusCode = res.getStatus();
292                 // Does it exactly match the expected status code?
293                 if (logger.isDebugEnabled()) {
294                     logger.debug(testName + ": status = " + statusCode);
295                 }
296                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
297                         invalidStatusCodeMessage(testRequestType, statusCode));
298                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
299         } finally {
300                 res.close();
301         }
302     }
303
304     /**
305      * Creates the with most invalid.
306      *
307      * @param testName the test name
308      * @throws Exception the exception
309      */
310     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
311     public void createWithMostInvalid(String testName) throws Exception {
312         setupCreate();
313
314         // Submit the request to the service and store the response.
315         AccountClient client = new AccountClient();
316         AccountsCommon account =
317                 createAccountInstance("babybop", "babybop", "hithere08", "babybop/dinoland.com",
318                 client.getTenantId(), false, true, false, false);
319         Response res = client.create(account);
320         try {
321                 int statusCode = res.getStatus();
322                 // Does it exactly match the expected status code?
323                 if (logger.isDebugEnabled()) {
324                     logger.debug(testName + ": status = " + statusCode);
325                 }
326                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
327                         invalidStatusCodeMessage(testRequestType, statusCode));
328                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
329         } finally {
330                 res.close();
331         }
332     }
333
334     //
335     // To avoid uniqueness violations for accounts, createList is removed
336     //
337     @Override
338     public void createList(String testName) throws Exception {
339         setupCreate();
340         // Submit the request to the service and store the response.
341         AccountClient client = new AccountClient();
342         AccountsCommon account1 =
343                 createAccountInstance("curious", "curious", "hithere08", "curious@george.com",
344                 client.getTenantId(), true, false, true, true);
345         Response res = client.create(account1);
346         try {
347                 int statusCode = res.getStatus();
348                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
349                         invalidStatusCodeMessage(testRequestType, statusCode));
350                 Assert.assertEquals(statusCode, testExpectedStatusCode);
351                 allResourceIdsCreated.add(extractId(res));
352         } finally {
353                 res.close();
354         }
355
356         AccountsCommon account2 =
357                 createAccountInstance("tom", "tom", "hithere09", "tom@jerry.com",
358                 client.getTenantId(), true, false, true, true);
359         res = client.create(account2);
360         try {
361                 int statusCode = res.getStatus();
362                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
363                         invalidStatusCodeMessage(testRequestType, statusCode));
364                 Assert.assertEquals(statusCode, testExpectedStatusCode);
365                 Assert.assertEquals(statusCode, testExpectedStatusCode);
366                 allResourceIdsCreated.add(extractId(res));
367         } finally {
368                 res.close();
369         }
370
371         AccountsCommon account3 =
372                 createAccountInstance("mj", "mj", "hithere10", "mj@dinoland.com",
373                 client.getTenantId(), true, false, true, true);
374         res = client.create(account3);
375         try {
376                 int statusCode = res.getStatus();
377                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
378                         invalidStatusCodeMessage(testRequestType, statusCode));
379                 Assert.assertEquals(statusCode, testExpectedStatusCode);
380                 Assert.assertEquals(statusCode, testExpectedStatusCode);
381                 allResourceIdsCreated.add(extractId(res));
382         } finally {
383                 res.close();
384         }
385     }
386
387     //
388     // Tests with expected failure outcomes
389     //
390     // Placeholders until the three tests below can be uncommented.
391     // See Issue CSPACE-401.
392     @Override
393     public void createWithEmptyEntityBody(String testName) throws Exception {
394         //FIXME: Should this test really be empty?  If so, please comment accordingly.
395     }
396
397     /* (non-Javadoc)
398      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
399      */
400     @Override
401     public void createWithMalformedXml(String testName) throws Exception {
402         //FIXME: Should this test really be empty?  If so, please comment accordingly.
403     }
404
405     /* (non-Javadoc)
406      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
407      */
408     @Override
409     public void createWithWrongXmlSchema(String testName) throws Exception {
410         //FIXME: Should this test really be empty?  If so, please comment accordingly.
411     }
412
413     // ---------------------------------------------------------------
414     // CRUD tests : READ_LIST tests
415     // ---------------------------------------------------------------
416     // Success outcomes
417
418     /**
419      * Search screen name.
420      *
421      * @param testName the test name
422      * @throws Exception the exception
423      */
424     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
425     public void searchScreenName(String testName) throws Exception {
426         // Perform setup.
427         setupReadList();
428
429         // Submit the request to the service and store the response.
430         AccountClient client = new AccountClient();
431         Response res =
432                 client.readSearchList("tom", null, null);
433         try {
434                 assertStatusCode(res, testName);                
435                 AccountsCommonList list = res.readEntity(AccountsCommonList.class);     
436                 Assert.assertEquals(1, list.getAccountListItem().size());
437                 // Optionally output additional data about list members for debugging.
438                 boolean iterateThroughList = true;
439                 if (iterateThroughList && logger.isDebugEnabled()) {
440                     printList(testName, list);
441                 }
442         } finally {
443                 if (res != null) {
444                 res.close();
445             }
446         }
447     }
448
449     @Override
450     @Test(dataProvider = "testName")
451     public void searchWorkflowDeleted(String testName) throws Exception {
452         // Fixme: null test for now, overriding test in base class
453     }
454
455     /**
456      * Search user id.
457      *
458      * @param testName the test name
459      * @throws Exception the exception
460      */
461     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
462     public void searchUserId(String testName) throws Exception {
463         // Perform setup.
464         setupReadList();
465
466         // Submit the request to the service and store the response.
467         AccountClient client = new AccountClient();
468         Response res = client.readSearchList(null, "tom", null);
469         try {
470                 assertStatusCode(res, testName);                
471                 AccountsCommonList list = res.readEntity(AccountsCommonList.class);
472                 Assert.assertEquals(1, list.getAccountListItem().size());
473                 // Optionally output additional data about list members for debugging.
474                 boolean iterateThroughList = true;
475                 if (iterateThroughList && logger.isDebugEnabled()) {
476                     printList(testName, list);
477                 }
478         } finally {
479                 if (res != null) {
480                 res.close();
481             }
482         }
483     }
484
485     /**
486      * Search email.
487      *
488      * @param testName the test name
489      * @throws Exception the exception
490      */
491     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
492     public void searchEmail(String testName) throws Exception {
493         // Perform setup.
494         setupReadList();
495
496         // Submit the request to the service and store the response.
497         AccountClient client = new AccountClient();
498         Response res = client.readSearchList(null, null, "dinoland");
499         try {
500                 assertStatusCode(res, testName);                
501                 AccountsCommonList list = res.readEntity(AccountsCommonList.class);
502                 Assert.assertEquals(2, list.getAccountListItem().size());
503                 // Optionally output additional data about list members for debugging.
504                 boolean iterateThroughList = true;
505                 if (iterateThroughList && logger.isDebugEnabled()) {
506                     printList(testName, list);
507                 }
508         } finally {
509                 if (res != null) {
510                 res.close();
511             }
512         }
513     }
514
515     /**
516      * Search screen name email.
517      *
518      * @param testName the test name
519      * @throws Exception the exception
520      */
521     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
522     public void searchScreenNameEmail(String testName) throws Exception {
523         // Perform setup.
524         setupReadList();
525
526         // Submit the request to the service and store the response.
527         AccountClient client = new AccountClient();
528         Response res = client.readSearchList("tom", null, "jerry");
529         try {
530                 assertStatusCode(res, testName);
531                 AccountsCommonList list = res.readEntity(AccountsCommonList.class);
532                 Assert.assertEquals(1, list.getAccountListItem().size());
533                 // Optionally output additional data about list members for debugging.
534                 boolean iterateThroughList = true;
535                 if (iterateThroughList && logger.isDebugEnabled()) {
536                     printList(testName, list);
537                 }
538         } finally {
539                 if (res != null) {
540                 res.close();
541             }
542         }
543     }
544
545     // ---------------------------------------------------------------
546     // CRUD tests : UPDATE tests
547     // ---------------------------------------------------------------
548
549     /**
550      * Update password.
551      *
552      * @param testName the test name
553      * @throws Exception the exception
554      */
555     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
556     public void updatePassword(String testName) throws Exception {
557         // Perform setup.
558         setupUpdate();
559
560         AccountClient client = new AccountClient();
561         Response res = client.read(knownResourceId);
562         AccountsCommon accountFound = null;
563         try {
564                 if (logger.isDebugEnabled()) {
565                     logger.debug(testName + ": read status = " + res.getStatus());
566                 }
567                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
568         
569                 if (logger.isDebugEnabled()) {
570                     logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
571                 }
572                 accountFound = res.readEntity(AccountsCommon.class);
573                 Assert.assertNotNull(accountFound);
574         } finally {
575                 res.close();
576         }
577
578         //create a new account object to test partial updates
579         AccountsCommon accountToUpdate = new AccountsCommon();
580         accountToUpdate.setCsid(knownResourceId);
581         accountToUpdate.setUserId(accountFound.getUserId());
582         //change password
583         accountToUpdate.setPassword("imagination".getBytes());
584         if (logger.isDebugEnabled()) {
585             logger.debug(testName + ": updated object");
586             logger.debug(objectAsXmlString(accountToUpdate,
587                     AccountsCommon.class));
588         }
589
590         // Submit the request to the service and store the response.
591         res = client.update(knownResourceId, accountToUpdate);
592         int statusCode = res.getStatus();
593         try {
594                 // Check the status code of the response: does it match the expected response(s)?
595                 if (logger.isDebugEnabled()) {
596                     logger.debug(testName + ": status = " + statusCode);
597                 }
598                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
599                         invalidStatusCodeMessage(testRequestType, statusCode));
600                 Assert.assertEquals(statusCode, testExpectedStatusCode);
601         
602                 AccountsCommon accountUpdated = res.readEntity(AccountsCommon.class);
603                 Assert.assertNotNull(accountUpdated);
604         
605 //              Assert.assertEquals(accountUpdated.getPassword(),
606 //                      accountFound.getPassword(),
607 //                      "Data in updated object did not match submitted data.");
608         } finally {
609                 res.close();
610         }
611     }
612
613     /**
614      * Update password without user.
615      *
616      * @param testName the test name
617      * @throws Exception the exception
618      */
619     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
620     public void updatePasswordWithoutUser(String testName) throws Exception {
621         // Perform setup.
622         setupUpdate();
623
624         AccountsCommon accountToUpdate = new AccountsCommon();
625         accountToUpdate.setCsid(knownResourceId);
626         accountToUpdate.setUserId(null);
627         //change password
628         accountToUpdate.setPassword("imagination".getBytes());
629         if (logger.isDebugEnabled()) {
630             logger.debug(testName + " : updated object");
631             logger.debug(objectAsXmlString(accountToUpdate,
632                     AccountsCommon.class));
633         }
634
635         AccountClient client = new AccountClient();
636         // Submit the request to the service and store the response.
637         Response res = client.update(knownResourceId, accountToUpdate);
638         try {
639                 int statusCode = res.getStatus();
640                 // Check the status code of the response: does it match the expected response(s)?
641                 if (logger.isDebugEnabled()) {
642                     logger.debug(testName + ": status = " + statusCode);
643                 }
644                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
645                         invalidStatusCodeMessage(testRequestType, statusCode));
646                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
647         } finally {
648                 res.close();
649         }
650     }
651
652     /**
653      * Update invalid password.
654      *
655      * @param testName the test name
656      * @throws Exception the exception
657      */
658     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
659     public void updateInvalidPassword(String testName) throws Exception {
660         // Perform setup.
661         setupUpdate();
662         AccountClient client = new AccountClient();
663         Response res = client.read(knownResourceId);
664         AccountsCommon accountFound = null;
665         try {
666                 if (logger.isDebugEnabled()) {
667                     logger.debug(testName + ": read status = " + res.getStatus());
668                 }
669                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
670         
671                 if (logger.isDebugEnabled()) {
672                     logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
673                 }
674                 accountFound = res.readEntity(AccountsCommon.class);
675         } finally {
676                 res.close();
677         }
678
679         AccountsCommon accountToUpdate = new AccountsCommon();
680         accountToUpdate.setCsid(knownResourceId);
681         accountToUpdate.setUserId(accountFound.getUserId());
682         Assert.assertNotNull(accountToUpdate);
683
684         //change password
685         accountToUpdate.setPassword("abc123".getBytes());
686         if (logger.isDebugEnabled()) {
687             logger.debug(testName + ": updated object");
688             logger.debug(objectAsXmlString(accountToUpdate,
689                     AccountsCommon.class));
690         }
691
692         // Submit the request to the service and store the response.
693         res = client.update(knownResourceId, accountToUpdate);
694         try {
695                 int statusCode = res.getStatus();
696                 // Check the status code of the response: does it match the expected response(s)?
697                 if (logger.isDebugEnabled()) {
698                     logger.debug(testName + ": status = " + statusCode);
699                 }
700                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
701                         invalidStatusCodeMessage(testRequestType, statusCode));
702                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
703         } finally {
704                 res.close();
705         }
706     }
707     
708     private void findPrebuiltAdminAccount() throws Exception {
709         // Search for the prebuilt admin user and then hold its CSID
710         if (prebuiltAdminCSID == null) {
711             setupReadList();
712             AccountClient client = new AccountClient();
713             Response res = client.readSearchList(null, this.prebuiltAdminUserId, null);
714             try {
715                     assertStatusCode(res, "findPrebuiltAdminAccount");
716                     AccountsCommonList list = res.readEntity(AccountsCommonList.class);
717                     List<AccountListItem> items = list.getAccountListItem();
718                     Assert.assertEquals(1, items.size(), "Found more than one Admin account!");
719                     AccountListItem item = items.get(0);
720                     prebuiltAdminCSID = item.getCsid();
721                     if (logger.isDebugEnabled()) {
722                         logger.debug("Found Admin Account with ID: " + prebuiltAdminCSID);
723                     }
724             } finally {
725                 if (res != null) {
726                     res.close();
727                 }
728             }
729         }
730     }
731     
732     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
733         public void verifyMetadataProtection(String testName) throws Exception {
734         findPrebuiltAdminAccount();
735         // Try to update the metadata - it should just get ignored
736         // Perform setup.
737         setupUpdate();
738
739         AccountClient client = new AccountClient();
740         Response res = client.read(prebuiltAdminCSID);
741         AccountsCommon accountFound = null;
742         try {
743                 if (logger.isDebugEnabled()) {
744                     logger.debug(testName + ": read status = " + res.getStatus());
745                 }
746                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
747         
748                 if (logger.isDebugEnabled()) {
749                     logger.debug("Did get on Admin Account to update with ID: " + prebuiltAdminCSID);
750                 }
751                 accountFound = res.readEntity(AccountsCommon.class);
752                 Assert.assertNotNull(accountFound);
753         } finally {
754                 res.close();
755         }
756         
757         //create a new account object to test partial updates
758         AccountsCommon accountToUpdate = new AccountsCommon();
759         accountToUpdate.setCsid(prebuiltAdminCSID);
760         accountToUpdate.setUserId(accountFound.getUserId());
761         // Update the content of this resource.
762         accountToUpdate.setEmail("updated-" + accountFound.getEmail());
763         if (logger.isDebugEnabled()) {
764             logger.debug("updated object");
765             logger.debug(objectAsXmlString(accountFound,
766                     AccountsCommon.class));
767         }
768
769         // Submit the request to the service and store the response.
770         res = client.update(prebuiltAdminCSID, accountToUpdate);
771         try {
772                 int statusCode = res.getStatus();
773                 // Check the status code of the response: does it match the expected response(s)?
774                 if (logger.isDebugEnabled()) {
775                     logger.debug(testName + ": status = " + statusCode);
776                 }
777                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
778                         invalidStatusCodeMessage(testRequestType, statusCode));
779                 // Note that the error is not returned, it is just ignored
780                 Assert.assertEquals(statusCode, testExpectedStatusCode);
781         
782                 AccountsCommon accountUpdated = res.readEntity(AccountsCommon.class);
783                 Assert.assertNotNull(accountUpdated);
784         
785                 Assert.assertFalse(accountUpdated.getEmail().equals(accountToUpdate.getEmail()),
786                         "Admin Account (with metadata lock) allowed update to change the email!");
787         } finally {
788                 res.close();
789         }
790     }
791     
792     
793     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
794         public void verifyProtectionReadOnly(String testName) throws Exception {
795         setupCreate();
796
797         // Submit the request to the service and store the response.
798         AccountClient client = new AccountClient();
799         AccountsCommon account = createAccountInstance("mdTest", "mdTest", "mdTestPW", "md@test.com", 
800                                 client.getTenantId(), true, false, true, true);
801         account.setMetadataProtection(AccountClient.IMMUTABLE);
802         account.setRolesProtection(AccountClient.IMMUTABLE);
803         Response res = client.create(account);
804         String testResourceId = null;
805         try {
806                 assertStatusCode(res, testName);
807                 // Store the ID returned from this create operation
808                 // for additional tests below.
809                 testResourceId = extractId(res);
810                 allResourceIdsCreated.add(testResourceId);
811                 if (logger.isDebugEnabled()) {
812                     logger.debug(testName + ": testResourceId=" + testResourceId);
813                 }
814         } finally {
815                 if (res != null) {
816                 res.close();
817             }
818         }
819         
820         setupRead();
821
822         // Submit the request to the service and store the response.
823         Response accountRes = client.read(testResourceId);
824         try {
825                 assertStatusCode(accountRes, testName);
826                 AccountsCommon accountRead = accountRes.readEntity(AccountsCommon.class);
827                 Assert.assertNotNull(accountRead);
828                 String mdProtection = accountRead.getMetadataProtection();
829                 String rolesProtection = accountRead.getRolesProtection();
830                 if (logger.isTraceEnabled()) {
831                     logger.trace(testName + ": metadataProtection=" + mdProtection);
832                     logger.trace(testName + ": rolesProtection=" + rolesProtection);
833                 }
834                 Assert.assertFalse(account.getMetadataProtection().equals(mdProtection),
835                                 "Account allowed create to set the metadata protection flag.");
836                 Assert.assertFalse(account.getRolesProtection().equals(rolesProtection),
837                                 "Account allowed create to set the perms protection flag.");
838         } finally {
839                 if (accountRes != null) {
840                         accountRes.close();
841             }
842         }
843         
844         setupUpdate();
845
846         AccountsCommon accountToUpdate = createAccountInstance("mdTest", "mdTest", "mdTestPW", "md@test.com", 
847                                         client.getTenantId(), true, false, true, true);
848         accountToUpdate.setMetadataProtection(AccountClient.IMMUTABLE);
849         accountToUpdate.setRolesProtection(AccountClient.IMMUTABLE);
850
851         // Submit the request to the service and store the response.
852         accountRes = client.update(testResourceId, accountToUpdate);
853         try {
854                 assertStatusCode(accountRes, testName);
855                 AccountsCommon accountUpdated = accountRes.readEntity(AccountsCommon.class);
856                 Assert.assertNotNull(accountUpdated);
857                 if (logger.isDebugEnabled()) {
858                     logger.debug(testName + "Updated account: ");
859                     logger.debug(objectAsXmlString(accountUpdated, AccountsCommon.class));
860                 }
861                 Assert.assertFalse(
862                                 AccountClient.IMMUTABLE.equalsIgnoreCase(accountUpdated.getMetadataProtection()),
863                                 "Account allowed update of the metadata protection flag.");
864                 Assert.assertFalse(
865                                 AccountClient.IMMUTABLE.equalsIgnoreCase(accountUpdated.getRolesProtection()),
866                                 "Account allowed update of the roles protection flag.");
867         } finally {
868                 if (accountRes != null) {
869                         accountRes.close();
870             }
871         }
872     }
873
874     /**
875      * Deactivate.
876      *
877      * @param testName the test name
878      * @throws Exception the exception
879      */
880     @Test(dataProvider = "testName", dependsOnMethods = {"updatePasswordWithoutUser"})
881     public void deactivate(String testName) throws Exception {
882         // Perform setup.
883         setupUpdate();
884
885         AccountClient client = new AccountClient();
886         Response res = client.read(knownResourceId);
887         AccountsCommon accountFound = null;
888         try {
889                 if (logger.isDebugEnabled()) {
890                     logger.debug(testName + ": read status = " + res.getStatus());
891                 }
892                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
893         
894                 if (logger.isDebugEnabled()) {
895                     logger.debug("got object to update with ID: " + knownResourceId);
896                 }
897                 accountFound = res.readEntity(AccountsCommon.class);
898         } finally {
899                 res.close();
900         }
901         
902         //create a new account object to test partial updates
903         AccountsCommon accountToUpdate = new AccountsCommon();
904         accountToUpdate.setCsid(knownResourceId);
905         accountToUpdate.setUserId(accountFound.getUserId());
906
907         // Update the content of this resource.
908         accountToUpdate.setStatus(Status.INACTIVE);
909         if (logger.isDebugEnabled()) {
910             logger.debug("updated object");
911             logger.debug(objectAsXmlString(accountToUpdate,
912                     AccountsCommon.class));
913         }
914
915         // Submit the request to the service and store the response.
916         res = client.update(knownResourceId, accountToUpdate);
917         try {
918                 int statusCode = res.getStatus();
919                 // Check the status code of the response: does it match the expected response(s)?
920                 if (logger.isDebugEnabled()) {
921                     logger.debug(testName + ": status = " + statusCode);
922                 }
923                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
924                         invalidStatusCodeMessage(testRequestType, statusCode));
925                 Assert.assertEquals(statusCode, testExpectedStatusCode);
926         
927                 AccountsCommon accountUpdated = res.readEntity(AccountsCommon.class);
928                 Assert.assertNotNull(accountUpdated);
929         
930                 Assert.assertEquals(accountUpdated.getStatus(),
931                         accountToUpdate.getStatus(),
932                         "Data in updated object did not match submitted data.");
933         } finally {
934                 res.close();
935         }
936     }
937
938     // Failure outcomes
939     // Placeholders until the three tests below can be uncommented.
940     // See Issue CSPACE-401.
941     /* (non-Javadoc)
942      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
943      */
944     @Override
945     public void updateWithEmptyEntityBody(String testName) throws Exception {
946         //FIXME: Should this test really be empty?  If so, please comment accordingly.
947     }
948
949     /* (non-Javadoc)
950      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
951      */
952     @Override
953     public void updateWithMalformedXml(String testName) throws Exception {
954         //FIXME: Should this test really be empty?  If so, please comment accordingly.
955     }
956
957     /* (non-Javadoc)
958      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
959      */
960     @Override
961     public void updateWithWrongXmlSchema(String testName) throws Exception {
962         //FIXME: Should this test really be empty?  If so, please comment accordingly.
963     }
964
965     @Override
966     @Test(dataProvider = "testName", dependsOnMethods = {"deactivate", "CRUDTests"})
967     public void updateNonExistent(String testName) throws Exception {
968         // Perform setup.
969         setupUpdateNonExistent();
970
971         // Submit the request to the service and store the response.
972         //
973         // Note: The ID used in this 'create' call may be arbitrary.
974         // The only relevant ID may be the one used in updateAccount(), below.
975         AccountClient client = new AccountClient();
976         AccountsCommon account = createAccountInstance("simba", "simba", "tiger", "simba@lionking.com",
977                 client.getTenantId(), true, false, true, true);
978         Response res = client.update(NON_EXISTENT_ID, account);
979         try {
980                 int statusCode = res.getStatus();
981         
982                 // Check the status code of the response: does it match
983                 // the expected response(s)?
984                 if (logger.isDebugEnabled()) {
985                     logger.debug(testName + ": status = " + statusCode);
986                 }
987                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
988                         invalidStatusCodeMessage(testRequestType, statusCode));
989                 Assert.assertEquals(statusCode, testExpectedStatusCode);
990         } finally {
991                 res.close();
992         }
993     }
994
995     /**
996      * Update wrong user.
997      *
998      * @param testName the test name
999      * @throws Exception the exception
1000      */
1001     @Test(dataProvider = "testName", dependsOnMethods = {"deactivate", "CRUDTests"})
1002     public void updateWrongUser(String testName) throws Exception {
1003         setupUpdate();
1004
1005         // Submit the request to the service and store the response.
1006         //
1007         // Note: The ID used in this 'create' call may be arbitrary.
1008         // The only relevant ID may be the one used in updateAccount(), below.
1009         AccountClient client = new AccountClient();
1010         Response res = client.read(knownResourceId);
1011         AccountsCommon accountToUpdate = null;
1012         try {
1013                 if (logger.isDebugEnabled()) {
1014                     logger.debug(testName + ": read status = " + res.getStatus());
1015                 }
1016                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
1017         
1018                 if (logger.isDebugEnabled()) {
1019                     logger.debug("got object to update with ID: " + knownResourceId);
1020                 }
1021                 accountToUpdate = res.readEntity(AccountsCommon.class);
1022                 Assert.assertNotNull(accountToUpdate);
1023         } finally {
1024                 res.close();
1025         }
1026
1027         accountToUpdate.setUserId("barneyFake");
1028         if (logger.isDebugEnabled()) {
1029             logger.debug("updated object with wrongUser");
1030             logger.debug(objectAsXmlString(accountToUpdate,
1031                     AccountsCommon.class));
1032         }
1033
1034         res = client.update(knownResourceId, accountToUpdate);
1035         try {
1036                 int statusCode = res.getStatus();
1037         
1038                 // Check the status code of the response: does it match
1039                 // the expected response(s)?
1040                 if (logger.isDebugEnabled()) {
1041                     logger.debug(testName + ": status = " + statusCode);
1042                 }
1043                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
1044                         invalidStatusCodeMessage(testRequestType, statusCode));
1045                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
1046         } finally {
1047                 res.close();
1048         }
1049     }
1050
1051     // ---------------------------------------------------------------
1052     // CRUD tests : DELETE tests
1053     // ---------------------------------------------------------------
1054     // Success outcomes
1055     
1056     @Override
1057     public void delete(String testName) throws Exception {
1058         // Do nothing because this test is not ready to delete the "knownResourceId".
1059         // Instead, the method localDelete() will get called later in the dependency chain. The
1060         // method localDelete() has a dependency on the test "updateWrongUser".  Once the "updateWrongUser"
1061         // test is run, the localDelete() test/method will get run.  The localDelete() test/method in turn
1062         // calls the inherited delete() test/method.
1063     }
1064     
1065     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests", "updateWrongUser"})
1066     public void localDelete(String testName) throws Exception {
1067         // Because of issues with TestNG not allowing @Test annotations on on override methods,
1068         // and because we want the "updateWrongUser" to run before the "delete" test, we need
1069         // this method.  This method will call super.delete() after all the dependencies have been
1070         // met.
1071         super.delete(testName);
1072     }
1073
1074     // ---------------------------------------------------------------
1075     // Utility methods used by tests above
1076     // ---------------------------------------------------------------
1077     /**
1078      * create account instance
1079      * @param screenName
1080      * @param userName
1081      * @param passwd
1082      * @param email
1083      * @param useScreenName
1084      * @param invalidTenant
1085      * @param useUser
1086      * @param usePassword
1087      * @return
1088      */
1089     AccountsCommon createAccountInstance(String screenName,
1090             String userName, String passwd, String email, String tenantId,
1091             boolean useScreenName, boolean invalidTenant, boolean useUser, boolean usePassword) {
1092
1093         AccountsCommon account = AccountFactory.createAccountInstance(screenName,
1094                 userName, passwd, email, tenantId, useScreenName,
1095                 invalidTenant, useUser, usePassword);
1096         if (logger.isDebugEnabled()) {
1097             logger.debug("to be created, account common");
1098             logger.debug(objectAsXmlString(account,
1099                     AccountsCommon.class));
1100         }
1101         
1102         return account;
1103     }
1104
1105
1106     /**
1107      * Prints the list.
1108      *
1109      * @param testName the test name
1110      * @param list the list
1111      */
1112     @Override
1113     protected void printList(String testName, AccountsCommonList list) {
1114         AccountsCommonList acl = (AccountsCommonList)list;
1115         List<AccountListItem> items =
1116                 acl.getAccountListItem();
1117         int i = 0;
1118         for (AccountListItem item : items) {
1119             logger.debug(testName + ": list-item[" + i + "] csid="
1120                     + item.getCsid());
1121             logger.debug(testName + ": list-item[" + i + "] screenName="
1122                     + item.getScreenName());
1123             logger.debug(testName + ": list-item[" + i + "] URI="
1124                     + item.getUri());
1125             i++;
1126         }
1127     }
1128
1129         @Override
1130         protected AccountsCommon createInstance(String commonPartName,
1131                         String identifier) throws Exception {
1132                 AccountClient client = new AccountClient();
1133         AccountsCommon account =
1134                 createAccountInstance(knownUserId, knownUserId, knownUserPassword,
1135                 "barney@dinoland.com", client.getTenantId(),
1136                 true, false, true, true);
1137         return account;
1138         }
1139
1140     /*
1141      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
1142      * refer to this method in their @Test annotation declarations.
1143      */
1144     @Override
1145     @Test(dataProvider = "testName",
1146                 dependsOnMethods = {
1147                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
1148     public void CRUDTests(String testName) {
1149         // Do nothing.  Simply here to for a TestNG execution order for our tests
1150     }
1151
1152         @Override
1153         protected AccountsCommon updateInstance(AccountsCommon accountsCommon) {
1154                 AccountsCommon result = new AccountsCommon();
1155                 
1156                 result.setCsid(knownResourceId);
1157                 result.setUserId(accountsCommon.getUserId());
1158         // Update the content of this resource.
1159                 result.setEmail("updated-" + accountsCommon.getEmail());
1160                 
1161                 return result;
1162         }
1163
1164         @Override
1165         protected void compareUpdatedInstances(AccountsCommon original,
1166                         AccountsCommon updated) throws Exception {
1167         Assert.assertEquals(original.getEmail(), updated.getEmail(),
1168                 "Data in updated object did not match submitted data.");
1169         }
1170         
1171         @Override
1172         protected long getSizeOfList(AccountsCommonList list) {
1173                 return list.getTotalItems();
1174         }
1175 }