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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c)) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.authentication.client.test;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
31 //import org.apache.commons.codec.binary.Base64;
32 import org.jboss.resteasy.client.ClientResponse;
33 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
34 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
39 import org.collectionspace.services.account.AccountTenant;
40 import org.collectionspace.services.client.AccountClient;
41 import org.collectionspace.services.account.AccountsCommon;
42 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
43 import org.collectionspace.services.client.CollectionObjectClient;
44 import org.collectionspace.services.client.CollectionSpaceClient;
45 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
46 import org.collectionspace.services.client.test.BaseServiceTest;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * AuthenticationServiceTest uses CollectionObject service to test
56 * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
59 public class AuthenticationServiceTest extends AbstractServiceTestImpl {
61 /** The known resource id. */
62 private String knownResourceId = null;
63 private String barneyAccountId = null;
66 final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
69 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
72 protected String getServicePathComponent() {
73 // no need to return anything but null since no auth resources are
79 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
82 protected CollectionSpaceClient getClientInstance() {
83 return new AccountClient();
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
90 protected AbstractCommonList getAbstractCommonList(
91 ClientResponse<AbstractCommonList> response) {
92 throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
95 @Test(dataProvider = "testName")
97 public void readPaginatedList(String testName) throws Exception {
98 // Test not supported.
101 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
102 public void createAccounts(String testName) throws Exception {
103 // Perform setup, such as initializing the type of service request
104 // (e.g. CREATE, DELETE), its valid and expected status codes, and
105 // its associated HTTP method name (e.g. POST, DELETE).
106 setupCreate(testName);
107 AccountClient accountClient = new AccountClient();
109 accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
111 accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
113 accountClient.setProperty(
114 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
115 // Submit the request to the service and store the response.
116 AccountsCommon account =
117 createAccountInstance("barney", "barney08", "barney@dinoland.com", "1");
118 ClientResponse<Response> res = accountClient.create(account);
119 int statusCode = res.getStatus();
121 if (logger.isDebugEnabled()) {
122 logger.debug(testName + ": barney status = " + statusCode);
124 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
125 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
126 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
128 // Store the ID returned from this create operation
129 // for additional tests below.
130 barneyAccountId = extractId(res);
131 if (logger.isDebugEnabled()) {
132 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
135 account = createAccountInstance("babybop", "babybop09", "babybop@dinoland.com", "non-existent");
136 res = accountClient.create(account);
137 statusCode = res.getStatus();
139 if (logger.isDebugEnabled()) {
140 logger.debug(testName + ": babybop status = " + statusCode);
142 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
143 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
144 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
150 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
152 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
153 dependsOnMethods = {"createAccounts"})
155 public void create(String testName) {
156 setupCreate(testName);
157 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
158 String identifier = BaseServiceTest.createIdentifier();
159 MultipartOutput multipart = createCollectionObjectInstance(
160 collectionObjectClient.getCommonPartName(), identifier);
162 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
164 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
166 collectionObjectClient.setProperty(
167 CollectionSpaceClient.PASSWORD_PROPERTY, "barney08");
169 collectionObjectClient.setupHttpClient();
170 collectionObjectClient.setProxy();
171 } catch (Exception e) {
172 logger.error("create: caught " + e.getMessage());
175 ClientResponse<Response> res = collectionObjectClient.create(multipart);
176 if (logger.isDebugEnabled()) {
177 logger.debug("create: status = " + res.getStatus());
179 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(), "expected " + Response.Status.CREATED.getStatusCode());
181 // Store the ID returned from this create operation for additional tests
183 knownResourceId = extractId(res);
187 * Creates the collection object instance without password.
189 @Test(dependsOnMethods = {"createAccounts"})
190 public void createWithoutPassword() {
191 banner("createWithoutPassword");
192 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
193 String identifier = BaseServiceTest.createIdentifier();
194 MultipartOutput multipart = createCollectionObjectInstance(
195 collectionObjectClient.getCommonPartName(), identifier);
197 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
199 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
201 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
203 collectionObjectClient.setupHttpClient();
204 collectionObjectClient.setProxy();
205 } catch (Exception e) {
206 logger.error("createWithoutPassword: caught " + e.getMessage());
209 ClientResponse<Response> res = collectionObjectClient.create(multipart);
210 if (logger.isDebugEnabled()) {
211 logger.debug("createWithoutPassword: status = " + res.getStatus());
213 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
217 * Creates the collection object with unknown user
219 @Test(dependsOnMethods = {"createAccounts"})
220 public void createWithUnknownUser() {
221 banner("createWithUnknownUser");
222 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
223 String identifier = BaseServiceTest.createIdentifier();
224 MultipartOutput multipart = createCollectionObjectInstance(
225 collectionObjectClient.getCommonPartName(), identifier);
227 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
229 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
231 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY,
234 collectionObjectClient.setupHttpClient();
235 collectionObjectClient.setProxy();
236 } catch (Exception e) {
237 logger.error("createWithUnknownUser: caught " + e.getMessage());
240 ClientResponse<Response> res = collectionObjectClient.create(multipart);
241 if (logger.isDebugEnabled()) {
242 logger.debug("createWithUnknownUser: status = " + res.getStatus());
244 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
248 * Creates the collection object instance with incorrect password.
250 @Test(dependsOnMethods = {"createAccounts"})
251 public void createWithIncorrectPassword() {
252 banner("createWithIncorrectPassword");
253 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
254 String identifier = BaseServiceTest.createIdentifier();
255 MultipartOutput multipart = createCollectionObjectInstance(
256 collectionObjectClient.getCommonPartName(), identifier);
258 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
260 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
262 collectionObjectClient.setProperty(
263 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
265 collectionObjectClient.setupHttpClient();
266 collectionObjectClient.setProxy();
267 } catch (Exception e) {
268 logger.error("createWithIncorrectPassword: caught " + e.getMessage());
271 ClientResponse<Response> res = collectionObjectClient.create(multipart);
272 if (logger.isDebugEnabled()) {
273 logger.debug("createWithIncorrectPassword: status = " + res.getStatus());
275 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
279 * Creates the collection object instance with incorrect user password.
281 @Test(dependsOnMethods = {"createAccounts"})
282 public void createWithIncorrectUserPassword() {
283 banner("createWithIncorrectUserPassword");
284 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
285 String identifier = BaseServiceTest.createIdentifier();
286 MultipartOutput multipart = createCollectionObjectInstance(
287 collectionObjectClient.getCommonPartName(), identifier);
289 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
291 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
293 collectionObjectClient.setProperty(
294 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
296 collectionObjectClient.setupHttpClient();
297 collectionObjectClient.setProxy();
298 } catch (Exception e) {
299 logger.error("createWithIncorrectUserPassword: caught " + e.getMessage());
302 ClientResponse<Response> res = collectionObjectClient.create(multipart);
303 if (logger.isDebugEnabled()) {
304 logger.debug("createWithIncorrectUserPassword: status = "
307 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
311 * Creates the collection object instance with incorrect user password.
313 @Test(dependsOnMethods = {"createAccounts"})
314 public void createWithoutTenant() {
315 banner("createWithoutTenant");
316 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
317 String identifier = BaseServiceTest.createIdentifier();
318 MultipartOutput multipart = createCollectionObjectInstance(
319 collectionObjectClient.getCommonPartName(), identifier);
321 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
323 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
325 collectionObjectClient.setProperty(
326 CollectionSpaceClient.PASSWORD_PROPERTY, "babybop09");
328 collectionObjectClient.setupHttpClient();
329 collectionObjectClient.setProxy();
330 } catch (Exception e) {
331 logger.error("createWithoutTenant: caught " + e.getMessage());
334 ClientResponse<Response> res = collectionObjectClient.create(multipart);
335 if (logger.isDebugEnabled()) {
336 logger.debug("createWithoutTenant: status = "
339 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
343 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
346 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
347 dependsOnMethods = {"create"})
348 public void delete(String testName) {
349 setupDelete(testName);
350 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
351 collectionObjectClient = new CollectionObjectClient();
353 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
355 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
357 collectionObjectClient.setProperty(
358 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
360 collectionObjectClient.setupHttpClient();
361 collectionObjectClient.setProxy();
362 } catch (Exception e) {
363 logger.error("deleteCollectionObject: caught " + e.getMessage());
366 if (logger.isDebugEnabled()) {
367 logger.debug("Calling deleteCollectionObject:" + knownResourceId);
369 ClientResponse<Response> res = collectionObjectClient.delete(knownResourceId);
370 if (logger.isDebugEnabled()) {
371 logger.debug("deleteCollectionObject: status = " + res.getStatus());
373 Assert.assertEquals(res.getStatus(),
374 Response.Status.OK.getStatusCode(), "expected " + Response.Status.OK.getStatusCode());
377 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
378 dependsOnMethods = {"delete"})
379 public void deleteAccounts(String testName) throws Exception {
382 setupDelete(testName);
383 AccountClient accountClient = new AccountClient();
385 accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
387 accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
389 accountClient.setProperty(
390 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
391 // Submit the request to the service and store the response.
392 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
393 int statusCode = res.getStatus();
394 if (logger.isDebugEnabled()) {
395 logger.debug(testName + ": barney status = " + statusCode);
397 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
398 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
401 // ---------------------------------------------------------------
402 // Utility methods used by tests above
403 // ---------------------------------------------------------------
405 * Creates the collection object instance.
407 * @param commonPartName the common part name
408 * @param identifier the identifier
410 * @return the multipart output
412 private MultipartOutput createCollectionObjectInstance(
413 String commonPartName, String identifier) {
414 return createCollectionObjectInstance(commonPartName, "objectNumber-" + identifier, "objectName-" + identifier);
418 * Creates the collection object instance.
420 * @param commonPartName the common part name
421 * @param objectNumber the object number
422 * @param objectName the object name
424 * @return the multipart output
426 private MultipartOutput createCollectionObjectInstance(
427 String commonPartName, String objectNumber, String objectName) {
428 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
430 collectionObject.setObjectNumber(objectNumber);
431 collectionObject.setObjectName(objectName);
432 MultipartOutput multipart = new MultipartOutput();
433 OutputPart commonPart = multipart.addPart(collectionObject,
434 MediaType.APPLICATION_XML_TYPE);
435 commonPart.getHeaders().add("label", commonPartName);
437 if (logger.isDebugEnabled()) {
438 logger.debug("to be created, collectionobject common ",
439 collectionObject, CollectionobjectsCommon.class);
444 private AccountsCommon createAccountInstance(String screenName,
445 String passwd, String email, String tenantId) {
447 AccountsCommon account = new AccountsCommon();
448 account.setScreenName(screenName);
449 account.setUserId(screenName);
450 //jaxb would encode b64
451 account.setPassword(passwd.getBytes());
452 account.setEmail(email);
453 account.setPhone("1234567890");
454 List<AccountTenant> atl = new ArrayList<AccountTenant>();
456 AccountTenant at = new AccountTenant();
457 at.setTenantId(tenantId);//for testing purposes
459 //disable 2nd tenant till tenant identification is in effect
460 //on the service side for 1-n user-tenants
461 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
462 // at2.setId(UUID.randomUUID().toString());
463 // at2.setName("collectionspace.org");
465 account.setTenants(atl);
467 if (logger.isDebugEnabled()) {
468 logger.debug("to be created, account common");
469 logger.debug(objectAsXmlString(account,
470 AccountsCommon.class));
477 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
480 public void createList(String testName) throws Exception {
481 //FIXME: Should this test really be empty? If so, please comment accordingly.
485 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
488 public void createWithEmptyEntityBody(String testName) throws Exception {
489 //FIXME: Should this test really be empty? If so, please comment accordingly.
493 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
496 public void createWithMalformedXml(String testName) throws Exception {
497 //FIXME: Should this test really be empty? If so, please comment accordingly.
501 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
504 public void createWithWrongXmlSchema(String testName) throws Exception {
505 //FIXME: Should this test really be empty? If so, please comment accordingly.
509 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
512 public void read(String testName) throws Exception {
513 //FIXME: Should this test really be empty? If so, please comment accordingly.
517 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
520 public void readNonExistent(String testName) throws Exception {
521 //FIXME: Should this test really be empty? If so, please comment accordingly.
525 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
528 public void readList(String testName) throws Exception {
529 //FIXME: Should this test really be empty? If so, please comment accordingly.
533 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
536 public void update(String testName) throws Exception {
537 //FIXME: Should this test really be empty? If so, please comment accordingly.
541 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
544 public void updateWithEmptyEntityBody(String testName) throws Exception {
545 //FIXME: Should this test really be empty? If so, please comment accordingly.
549 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
552 public void updateWithMalformedXml(String testName) throws Exception {
553 //FIXME: Should this test really be empty? If so, please comment accordingly.
557 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
560 public void updateWithWrongXmlSchema(String testName) throws Exception {
561 //FIXME: Should this test really be empty? If so, please comment accordingly.
565 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
568 public void updateNonExistent(String testName) throws Exception {
569 //FIXME: Should this test really be empty? If so, please comment accordingly.
573 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
576 public void deleteNonExistent(String testName) throws Exception {
577 //FIXME: Should this test really be empty? If so, please comment accordingly.