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;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.apache.commons.codec.binary.Base64;
30 import org.collectionspace.services.account.AccountTenant;
31 import org.collectionspace.services.client.AccountClient;
32 import org.collectionspace.services.account.AccountsCommon;
33 import org.jboss.resteasy.client.ClientResponse;
34 import org.testng.Assert;
35 import org.testng.annotations.Test;
37 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
38 import org.collectionspace.services.client.CollectionObjectClient;
39 import org.collectionspace.services.client.CollectionSpaceClient;
40 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
42 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * AuthenticationServiceTest uses CollectionObject service to test
50 * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
53 public class AuthenticationServiceTest extends AbstractServiceTestImpl {
55 /** The known resource id. */
56 private String knownResourceId = null;
57 private String barneyAccountId = null;
60 final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
63 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
66 protected String getServicePathComponent() {
67 // no need to return anything but null since no auth resources are
72 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
73 public void createAccounts(String testName) throws Exception {
74 // Perform setup, such as initializing the type of service request
75 // (e.g. CREATE, DELETE), its valid and expected status codes, and
76 // its associated HTTP method name (e.g. POST, DELETE).
77 setupCreate(testName);
78 AccountClient accountClient = new AccountClient();
80 accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
82 accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
84 accountClient.setProperty(
85 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
86 // Submit the request to the service and store the response.
87 AccountsCommon account =
88 createAccountInstance("barney", "barney08", "barney@dinoland.com", "1");
89 ClientResponse<Response> res = accountClient.create(account);
90 int statusCode = res.getStatus();
92 if (logger.isDebugEnabled()) {
93 logger.debug(testName + ": barney status = " + statusCode);
95 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
96 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
97 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
99 // Store the ID returned from this create operation
100 // for additional tests below.
101 barneyAccountId = extractId(res);
102 if (logger.isDebugEnabled()) {
103 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
106 account = createAccountInstance("babybop", "babybop09", "babybop@dinoland.com", "non-existent");
107 res = accountClient.create(account);
108 statusCode = res.getStatus();
110 if (logger.isDebugEnabled()) {
111 logger.debug(testName + ": babybop status = " + statusCode);
113 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
114 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
115 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
121 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
123 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
124 dependsOnMethods = {"createAccounts"})
126 public void create(String testName) {
127 setupCreate(testName);
128 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
129 String identifier = this.createIdentifier();
130 MultipartOutput multipart = createCollectionObjectInstance(
131 collectionObjectClient.getCommonPartName(), identifier);
133 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
135 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
137 collectionObjectClient.setProperty(
138 CollectionSpaceClient.PASSWORD_PROPERTY, "barney08");
140 collectionObjectClient.setupHttpClient();
141 collectionObjectClient.setProxy();
142 } catch (Exception e) {
143 logger.error("create: caught " + e.getMessage());
146 ClientResponse<Response> res = collectionObjectClient.create(multipart);
147 if (logger.isDebugEnabled()) {
148 logger.debug("create: status = " + res.getStatus());
150 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(), "expected " + Response.Status.CREATED.getStatusCode());
152 // Store the ID returned from this create operation for additional tests
154 knownResourceId = extractId(res);
158 * Creates the collection object instance without password.
160 @Test(dependsOnMethods = {"createAccounts"})
161 public void createWithoutPassword() {
162 banner("createWithoutPassword");
163 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
164 String identifier = this.createIdentifier();
165 MultipartOutput multipart = createCollectionObjectInstance(
166 collectionObjectClient.getCommonPartName(), identifier);
168 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
170 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
172 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
174 collectionObjectClient.setupHttpClient();
175 collectionObjectClient.setProxy();
176 } catch (Exception e) {
177 logger.error("createWithoutPassword: caught " + e.getMessage());
180 ClientResponse<Response> res = collectionObjectClient.create(multipart);
181 if (logger.isDebugEnabled()) {
182 logger.debug("createWithoutPassword: status = " + res.getStatus());
184 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
188 * Creates the collection object with unknown user
190 @Test(dependsOnMethods = {"createAccounts"})
191 public void createWithUnknownUser() {
192 banner("createWithUnknownUser");
193 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
194 String identifier = this.createIdentifier();
195 MultipartOutput multipart = createCollectionObjectInstance(
196 collectionObjectClient.getCommonPartName(), identifier);
198 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
200 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
202 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY,
205 collectionObjectClient.setupHttpClient();
206 collectionObjectClient.setProxy();
207 } catch (Exception e) {
208 logger.error("createWithUnknownUser: caught " + e.getMessage());
211 ClientResponse<Response> res = collectionObjectClient.create(multipart);
212 if (logger.isDebugEnabled()) {
213 logger.debug("createWithUnknownUser: status = " + res.getStatus());
215 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
219 * Creates the collection object instance with incorrect password.
221 @Test(dependsOnMethods = {"createAccounts"})
222 public void createWithIncorrectPassword() {
223 banner("createWithIncorrectPassword");
224 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
225 String identifier = this.createIdentifier();
226 MultipartOutput multipart = createCollectionObjectInstance(
227 collectionObjectClient.getCommonPartName(), identifier);
229 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
231 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
233 collectionObjectClient.setProperty(
234 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
236 collectionObjectClient.setupHttpClient();
237 collectionObjectClient.setProxy();
238 } catch (Exception e) {
239 logger.error("createWithIncorrectPassword: caught " + e.getMessage());
242 ClientResponse<Response> res = collectionObjectClient.create(multipart);
243 if (logger.isDebugEnabled()) {
244 logger.debug("createWithIncorrectPassword: status = " + res.getStatus());
246 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
250 * Creates the collection object instance with incorrect user password.
252 @Test(dependsOnMethods = {"createAccounts"})
253 public void createWithIncorrectUserPassword() {
254 banner("createWithIncorrectUserPassword");
255 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
256 String identifier = this.createIdentifier();
257 MultipartOutput multipart = createCollectionObjectInstance(
258 collectionObjectClient.getCommonPartName(), identifier);
260 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
262 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
264 collectionObjectClient.setProperty(
265 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
267 collectionObjectClient.setupHttpClient();
268 collectionObjectClient.setProxy();
269 } catch (Exception e) {
270 logger.error("createWithIncorrectUserPassword: caught " + e.getMessage());
273 ClientResponse<Response> res = collectionObjectClient.create(multipart);
274 if (logger.isDebugEnabled()) {
275 logger.debug("createWithIncorrectUserPassword: status = "
278 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
282 * Creates the collection object instance with incorrect user password.
284 @Test(dependsOnMethods = {"createAccounts"})
285 public void createWithoutTenant() {
286 banner("createWithoutTenant");
287 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
288 String identifier = this.createIdentifier();
289 MultipartOutput multipart = createCollectionObjectInstance(
290 collectionObjectClient.getCommonPartName(), identifier);
292 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
294 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
296 collectionObjectClient.setProperty(
297 CollectionSpaceClient.PASSWORD_PROPERTY, "babybop09");
299 collectionObjectClient.setupHttpClient();
300 collectionObjectClient.setProxy();
301 } catch (Exception e) {
302 logger.error("createWithoutTenant: caught " + e.getMessage());
305 ClientResponse<Response> res = collectionObjectClient.create(multipart);
306 if (logger.isDebugEnabled()) {
307 logger.debug("createWithoutTenant: status = "
310 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
314 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
317 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
318 dependsOnMethods = {"create"})
319 public void delete(String testName) {
320 setupDelete(testName);
321 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
322 collectionObjectClient = new CollectionObjectClient();
324 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
326 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
328 collectionObjectClient.setProperty(
329 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
331 collectionObjectClient.setupHttpClient();
332 collectionObjectClient.setProxy();
333 } catch (Exception e) {
334 logger.error("deleteCollectionObject: caught " + e.getMessage());
337 if (logger.isDebugEnabled()) {
338 logger.debug("Calling deleteCollectionObject:" + knownResourceId);
340 ClientResponse<Response> res = collectionObjectClient.delete(knownResourceId);
341 if (logger.isDebugEnabled()) {
342 logger.debug("deleteCollectionObject: status = " + res.getStatus());
344 Assert.assertEquals(res.getStatus(),
345 Response.Status.OK.getStatusCode(), "expected " + Response.Status.OK.getStatusCode());
348 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
349 dependsOnMethods = {"delete"})
350 public void deleteAccounts(String testName) throws Exception {
353 setupDelete(testName);
354 AccountClient accountClient = new AccountClient();
356 accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
358 accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
360 accountClient.setProperty(
361 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
362 // Submit the request to the service and store the response.
363 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
364 int statusCode = res.getStatus();
365 if (logger.isDebugEnabled()) {
366 logger.debug(testName + ": barney status = " + statusCode);
368 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
372 // ---------------------------------------------------------------
373 // Utility methods used by tests above
374 // ---------------------------------------------------------------
376 * Creates the collection object instance.
378 * @param commonPartName the common part name
379 * @param identifier the identifier
381 * @return the multipart output
383 private MultipartOutput createCollectionObjectInstance(
384 String commonPartName, String identifier) {
385 return createCollectionObjectInstance(commonPartName, "objectNumber-" + identifier, "objectName-" + identifier);
389 * Creates the collection object instance.
391 * @param commonPartName the common part name
392 * @param objectNumber the object number
393 * @param objectName the object name
395 * @return the multipart output
397 private MultipartOutput createCollectionObjectInstance(
398 String commonPartName, String objectNumber, String objectName) {
399 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
401 collectionObject.setObjectNumber(objectNumber);
402 collectionObject.setObjectName(objectName);
403 MultipartOutput multipart = new MultipartOutput();
404 OutputPart commonPart = multipart.addPart(collectionObject,
405 MediaType.APPLICATION_XML_TYPE);
406 commonPart.getHeaders().add("label", commonPartName);
408 if (logger.isDebugEnabled()) {
409 logger.debug("to be created, collectionobject common ",
410 collectionObject, CollectionobjectsCommon.class);
415 private AccountsCommon createAccountInstance(String screenName,
416 String passwd, String email, String tenantId) {
418 AccountsCommon account = new AccountsCommon();
419 account.setScreenName(screenName);
420 account.setUserId(screenName);
421 account.setPassword(Base64.encodeBase64(passwd.getBytes()));
422 account.setEmail(email);
423 account.setPhone("1234567890");
424 List<AccountTenant> atl = new ArrayList<AccountTenant>();
426 AccountTenant at = new AccountTenant();
427 at.setTenantId(tenantId);//for testing purposes
429 //disable 2nd tenant till tenant identification is in effect
430 //on the service side for 1-n user-tenants
431 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
432 // at2.setId(UUID.randomUUID().toString());
433 // at2.setName("collectionspace.org");
435 account.setTenants(atl);
437 if (logger.isDebugEnabled()) {
438 logger.debug("to be created, account common");
439 logger.debug(objectAsXmlString(account,
440 AccountsCommon.class));
447 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
450 public void createList(String testName) throws Exception {
454 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
457 public void createWithEmptyEntityBody(String testName) throws Exception {
461 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
464 public void createWithMalformedXml(String testName) throws Exception {
468 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
471 public void createWithWrongXmlSchema(String testName) throws Exception {
475 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
478 public void read(String testName) throws Exception {
482 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
485 public void readNonExistent(String testName) throws Exception {
489 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
492 public void readList(String testName) throws Exception {
496 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
499 public void update(String testName) throws Exception {
503 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
506 public void updateWithEmptyEntityBody(String testName) throws Exception {
510 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
513 public void updateWithMalformedXml(String testName) throws Exception {
517 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
520 public void updateWithWrongXmlSchema(String testName) throws Exception {
524 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
527 public void updateNonExistent(String testName) throws Exception {
531 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
534 public void deleteNonExistent(String testName) throws Exception {