Java tutorial
/* * TestUserService.java * Copyright (c) 2009 Felix Cachaldora Sanchez * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */ package com.wickettasks.business.services.user; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.wickettasks.business.AbstractWicketTasksTests; import com.wickettasks.business.entities.user.User; import com.wickettasks.business.exceptions.user.ExistingUserException; import com.wickettasks.business.exceptions.user.IncorrectLoginException; public class TestUserService extends AbstractWicketTasksTests { @Autowired private UserService userService; public void testAddUser() { User user = null; try { user = this.userService.add("test@email.com", "password"); } catch (ExistingUserException e) { e.printStackTrace(); } assertEquals(Boolean.TRUE, Boolean.valueOf(user != null)); } @SuppressWarnings("null") public void testAddExistingUser() { User user = null; try { user = this.userService.add("test@email.com", "password"); } catch (ExistingUserException e) { throw new AssertionError(); } assertEquals(Boolean.TRUE, Boolean.valueOf(user != null)); assertEquals(Boolean.TRUE, Boolean.valueOf("password".equals(user.getPassword()))); assertEquals(Boolean.TRUE, Boolean.valueOf("test@email.com".equals(user.getEmail()))); } @Test(expected = IllegalArgumentException.class) public void emailAndPasswordAreMandatoryToCreateAnUser() { try { this.userService.add(null, null); } catch (ExistingUserException e) { throw new AssertionError(e); } } @Test(expected = IllegalArgumentException.class) public void anEmailIsMandatoryToCreateAnUser() { try { this.userService.add(null, "password"); } catch (ExistingUserException e) { throw new AssertionError(e); } } @Test(expected = IllegalArgumentException.class) public void aPasswordIsMandatoryToCreateAnUser() { try { this.userService.add("test@email.com", null); } catch (ExistingUserException e) { throw new AssertionError(e); } } @Test public void testFindUserByEmail() { User user = null; try { user = this.userService.add("test2@email.com", "password"); } catch (ExistingUserException e) { throw new AssertionError(e); } User foundUser = this.userService.findByEmail(user.getEmail()); assertEquals(user, foundUser); } @Test public void findUserByEmailReturnsNullForNonExistingEmails() { assertEquals(null, this.userService.findByEmail("test@email.com")); } @SuppressWarnings("null") @Test public void testAuthenticatingExistingUserWithCorrectPassword() { try { this.userService.add("test@email.com", "password"); } catch (ExistingUserException e) { throw new AssertionError(); } User myAuthenticatedUser = null; try { myAuthenticatedUser = this.userService.authenticate("test@email.com", "password"); } catch (IncorrectLoginException e) { throw new AssertionError(); } assertEquals(Boolean.TRUE, Boolean.valueOf(myAuthenticatedUser != null)); assertEquals(Boolean.TRUE, Boolean.valueOf("password".equals(myAuthenticatedUser.getPassword()))); assertEquals(Boolean.TRUE, Boolean.valueOf("test@email.com".equals(myAuthenticatedUser.getEmail()))); } @Test(expected = IncorrectLoginException.class) public void testAuthenticatingExistingUserWithWrongPassword() throws IncorrectLoginException { try { this.userService.add("test@email.com", "password"); } catch (ExistingUserException e) { throw new AssertionError(); } this.userService.authenticate("test@email.com", "passwoasdadadard"); } @Test(expected = IncorrectLoginException.class) public void testAuthenticatingNonExistingUserWithWrongPassword() throws IncorrectLoginException { this.userService.authenticate("test@email.com", "password"); } }