]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
881d9343734ff1b33d8828ceda9e87257f4a8846
[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.client.test;
24
25 import java.util.List;
26 import javax.ws.rs.core.Response;
27
28 import org.apache.commons.codec.binary.Base64;
29 import org.collectionspace.services.client.AccountClient;
30 import org.collectionspace.services.account.AccountsCommon;
31 import org.collectionspace.services.account.AccountsCommonList;
32 import org.jboss.resteasy.client.ClientResponse;
33
34 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * AccountServiceTest, carries out tests against a
43  * deployed and running Account Service.
44  * 
45  * $LastChangedRevision: 917 $
46  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
47  */
48 public class AccountServiceTest extends AbstractServiceTest {
49
50     private final Logger logger =
51             LoggerFactory.getLogger(AccountServiceTest.class);
52     // Instance variables specific to this test.
53     private AccountClient client = new AccountClient();
54     private String knownResourceId = null;
55
56     /*
57      * This method is called only by the parent class, AbstractServiceTest
58      */
59     @Override
60     protected String getServicePathComponent() {
61         return client.getServicePathComponent();
62     }
63
64     // ---------------------------------------------------------------
65     // CRUD tests : CREATE tests
66     // ---------------------------------------------------------------
67     // Success outcomes
68     @Override
69     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class)
70     public void create(String testName) throws Exception {
71
72         // Perform setup, such as initializing the type of service request
73         // (e.g. CREATE, DELETE), its valid and expected status codes, and
74         // its associated HTTP method name (e.g. POST, DELETE).
75         setupCreate(testName);
76
77         // Submit the request to the service and store the response.
78         AccountsCommon account =
79                 createAccountInstance("sanjay", "hello");
80         ClientResponse<Response> res = client.create(account);
81         int statusCode = res.getStatus();
82
83         // Check the status code of the response: does it match
84         // the expected response(s)?
85         //
86         // Specifically:
87         // Does it fall within the set of valid status codes?
88         // Does it exactly match the expected status code?
89         if (logger.isDebugEnabled()) {
90             logger.debug(testName + ": status = " + statusCode);
91         }
92         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
93                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
94         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
95
96         // Store the ID returned from this create operation
97         // for additional tests below.
98         knownResourceId = extractId(res);
99         if (logger.isDebugEnabled()) {
100             logger.debug(testName + ": knownResourceId=" + knownResourceId);
101         }
102     }
103
104     /* (non-Javadoc)
105      * @see org.collectionspace.services.client.test.ServiceTest#createList()
106      */
107     @Override
108     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
109     dependsOnMethods = {"create"})
110     public void createList(String testName) throws Exception {
111         for (int i = 0; i < 3; i++) {
112             create(testName);
113         }
114     }
115
116     // Failure outcomes
117     // Placeholders until the three tests below can be uncommented.
118     // See Issue CSPACE-401.
119     @Override
120     public void createWithEmptyEntityBody(String testName) throws Exception {
121     }
122
123     @Override
124     public void createWithMalformedXml(String testName) throws Exception {
125     }
126
127     @Override
128     public void createWithWrongXmlSchema(String testName) throws Exception {
129     }
130
131     // ---------------------------------------------------------------
132     // CRUD tests : READ tests
133     // ---------------------------------------------------------------
134     // Success outcomes
135     @Override
136     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
137     dependsOnMethods = {"create"})
138     public void read(String testName) throws Exception {
139
140         // Perform setup.
141         setupRead(testName);
142
143         // Submit the request to the service and store the response.
144         ClientResponse<AccountsCommon> res = client.read(knownResourceId);
145         int statusCode = res.getStatus();
146
147         // Check the status code of the response: does it match
148         // the expected response(s)?
149         if (logger.isDebugEnabled()) {
150             logger.debug(testName + ": status = " + statusCode);
151         }
152         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
153                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
154         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155
156         AccountsCommon output = (AccountsCommon) res.getEntity();
157         Assert.assertNotNull(output);
158     }
159
160     // Failure outcomes
161     @Override
162     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
163     dependsOnMethods = {"read"})
164     public void readNonExistent(String testName) throws Exception {
165
166         // Perform setup.
167         setupReadNonExistent(testName);
168
169         // Submit the request to the service and store the response.
170         ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
171         int statusCode = res.getStatus();
172
173         // Check the status code of the response: does it match
174         // the expected response(s)?
175         if (logger.isDebugEnabled()) {
176             logger.debug(testName + ": status = " + statusCode);
177         }
178         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181     }
182
183     // ---------------------------------------------------------------
184     // CRUD tests : READ_LIST tests
185     // ---------------------------------------------------------------
186     // Success outcomes
187     @Override
188     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
189     dependsOnMethods = {"createList", "read"})
190     public void readList(String testName) throws Exception {
191
192         // Perform setup.
193         setupReadList(testName);
194
195         // Submit the request to the service and store the response.
196         ClientResponse<AccountsCommonList> res = client.readList();
197         AccountsCommonList list = res.getEntity();
198         int statusCode = res.getStatus();
199
200         // Check the status code of the response: does it match
201         // the expected response(s)?
202         if (logger.isDebugEnabled()) {
203             logger.debug(testName + ": status = " + statusCode);
204         }
205         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
206                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
207         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
208
209         // Optionally output additional data about list members for debugging.
210         boolean iterateThroughList = false;
211         if (iterateThroughList && logger.isDebugEnabled()) {
212             List<AccountsCommonList.AccountListItem> items =
213                     list.getAccountListItem();
214             int i = 0;
215
216             for (AccountsCommonList.AccountListItem item : items) {
217                 logger.debug(testName + ": list-item[" + i + "] csid=" +
218                         item.getCsid());
219                 logger.debug(testName + ": list-item[" + i + "] anchorName=" +
220                         item.getAnchorName());
221                 logger.debug(testName + ": list-item[" + i + "] URI=" +
222                         item.getUri());
223                 i++;
224
225             }
226         }
227     }
228
229     // Failure outcomes
230     // None at present.
231     // ---------------------------------------------------------------
232     // CRUD tests : UPDATE tests
233     // ---------------------------------------------------------------
234     // Success outcomes
235     @Override
236     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
237     dependsOnMethods = {"read"})
238     public void update(String testName) throws Exception {
239
240         // Perform setup.
241         setupUpdate(testName);
242
243         ClientResponse<AccountsCommon> res =
244                 client.read(knownResourceId);
245         if (logger.isDebugEnabled()) {
246             logger.debug(testName + ": read status = " + res.getStatus());
247         }
248         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
249
250         if (logger.isDebugEnabled()) {
251             logger.debug("got object to update with ID: " + knownResourceId);
252         }
253         MultipartInput input = (MultipartInput) res.getEntity();
254         AccountsCommon toUpdateAccount =
255                 (AccountsCommon) extractPart(input,
256                 client.getCommonPartName(), AccountsCommon.class);
257         Assert.assertNotNull(toUpdateAccount);
258
259         // Update the content of this resource.
260         toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
261         toUpdateAccount.setPhone("updated-" + toUpdateAccount.getPhone());
262         if (logger.isDebugEnabled()) {
263             logger.debug("updated object");
264             logger.debug(objectAsXmlString(toUpdateAccount,
265                     AccountsCommon.class));
266         }
267
268         // Submit the request to the service and store the response.
269         res = client.update(knownResourceId, toUpdateAccount);
270         int statusCode = res.getStatus();
271         // Check the status code of the response: does it match the expected response(s)?
272         if (logger.isDebugEnabled()) {
273             logger.debug(testName + ": status = " + statusCode);
274         }
275         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
276                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
277         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
278
279
280         AccountsCommon updatedAccount = (AccountsCommon)res.getEntity();
281         Assert.assertNotNull(updatedAccount);
282
283         Assert.assertEquals(updatedAccount.getEmail(),
284                 toUpdateAccount.getPhone(),
285                 "Data in updated object did not match submitted data.");
286
287     }
288
289     // Failure outcomes
290     // Placeholders until the three tests below can be uncommented.
291     // See Issue CSPACE-401.
292     @Override
293     public void updateWithEmptyEntityBody(String testName) throws Exception {
294     }
295
296     @Override
297     public void updateWithMalformedXml(String testName) throws Exception {
298     }
299
300     @Override
301     public void updateWithWrongXmlSchema(String testName) throws Exception {
302     }
303
304     @Override
305     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
306     dependsOnMethods = {"update", "testSubmitRequest"})
307     public void updateNonExistent(String testName) throws Exception {
308
309         // Perform setup.
310         setupUpdateNonExistent(testName);
311
312         // Submit the request to the service and store the response.
313         //
314         // Note: The ID used in this 'create' call may be arbitrary.
315         // The only relevant ID may be the one used in updateAccount(), below.
316         AccountsCommon account =
317                 createAccountInstance("dalal", "junk");
318         ClientResponse<AccountsCommon> res =
319                 client.update(NON_EXISTENT_ID, account);
320         int statusCode = res.getStatus();
321
322         // Check the status code of the response: does it match
323         // the expected response(s)?
324         if (logger.isDebugEnabled()) {
325             logger.debug(testName + ": status = " + statusCode);
326         }
327         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
328                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
329         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
330     }
331
332     // ---------------------------------------------------------------
333     // CRUD tests : DELETE tests
334     // ---------------------------------------------------------------
335     // Success outcomes
336     @Override
337     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
338     dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
339     public void delete(String testName) throws Exception {
340
341         // Perform setup.
342         setupDelete(testName);
343
344         // Submit the request to the service and store the response.
345         ClientResponse<Response> res = client.delete(knownResourceId);
346         int statusCode = res.getStatus();
347
348         // Check the status code of the response: does it match
349         // the expected response(s)?
350         if (logger.isDebugEnabled()) {
351             logger.debug(testName + ": status = " + statusCode);
352         }
353         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
354                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
355         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
356     }
357
358     // Failure outcomes
359     @Override
360     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
361     dependsOnMethods = {"delete"})
362     public void deleteNonExistent(String testName) throws Exception {
363
364         // Perform setup.
365         setupDeleteNonExistent(testName);
366
367         // Submit the request to the service and store the response.
368         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
369         int statusCode = res.getStatus();
370
371         // Check the status code of the response: does it match
372         // the expected response(s)?
373         if (logger.isDebugEnabled()) {
374             logger.debug(testName + ": status = " + statusCode);
375         }
376         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
377                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
378         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
379     }
380
381     // ---------------------------------------------------------------
382     // Utility tests : tests of code used in tests above
383     // ---------------------------------------------------------------
384     /**
385      * Tests the code for manually submitting data that is used by several
386      * of the methods above.
387      */
388     @Test(dependsOnMethods = {"create", "read"})
389     public void testSubmitRequest() throws Exception {
390
391         // Expected status code: 200 OK
392         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
393
394         // Submit the request to the service and store the response.
395         String method = ServiceRequestType.READ.httpMethodName();
396         String url = getResourceURL(knownResourceId);
397         int statusCode = submitRequest(method, url);
398
399         // Check the status code of the response: does it match
400         // the expected response(s)?
401         if (logger.isDebugEnabled()) {
402             logger.debug("testSubmitRequest: url=" + url +
403                     " status=" + statusCode);
404         }
405         Assert.assertEquals(statusCode, EXPECTED_STATUS);
406
407     }
408
409     // ---------------------------------------------------------------
410     // Utility methods used by tests above
411     // ---------------------------------------------------------------
412     private AccountsCommon createAccountInstance(String anchorName,
413             String passwd) {
414
415         AccountsCommon account = new AccountsCommon();
416         account.setAnchorName(anchorName);
417         account.setUserName(anchorName);
418         byte[] b64passwd = Base64.encodeBase64(passwd.getBytes());
419         account.setPassword(b64passwd);
420
421         if (logger.isDebugEnabled()) {
422             logger.debug("to be created, account common");
423             logger.debug(objectAsXmlString(account,
424                     AccountsCommon.class));
425         }
426         return account;
427
428     }
429
430 }