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