package com.walters.sms;
import com.walters.common.args.CheckArg;
import com.walters.sms.domain.Role;
import com.walters.sms.domain.User;
import org.junit.*;
import static org.junit.Assert.*;
public class UserServiceTest
{
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
public static final String ADMINISTRATOR = "Administrator";
public static final int ROLE_COUNT = 1;
private IUserService objectUnderTest;
private static IRoleService roleService;
private static Role role;
@BeforeClass
public static void setUp()
{
roleService = getServiceLocator().getService(IRoleService.class);
role = roleService.findRoleByName(ADMINISTRATOR);
}
@AfterClass
public static void tearDown()
{
roleService = null;
role = null;
}
@Before
public void executeBeforeEachTest()
{
objectUnderTest = getServiceLocator().getService(IUserService.class);
roleService = getServiceLocator().getService(IRoleService.class);
assertEquals(0, objectUnderTest.getUserCount());
Assert.assertNotNull(role);
Assert.assertEquals(ROLE_COUNT, roleService.getRoleCount());
}
@After
public void executeAfterEachTest() throws Exception
{
if (CheckArg.isNotNull(objectUnderTest))
{
objectUnderTest.deleteAll();
objectUnderTest = null;
}
Assert.assertEquals(ROLE_COUNT, roleService.getRoleCount());
}
@Test
public void testSaveUser() throws Exception
{
System.out.println("testSaveUser()");
User user = getNewUser(USERNAME, PASSWORD);
assertNull(user.getId());
user = objectUnderTest.saveUser(user);
assertNotNull(user.getId());
assertEquals(1, objectUnderTest.getUserCount());
}
@Test
public void testUsernameUniquenessOnSave()
{
System.out.println("testUsernameUniquenessOnSave()");
User user = getNewUser(USERNAME, PASSWORD);
user = objectUnderTest.saveUser(user);
//check for username uniqueness
final User anotherUser = User.newInstance(user);
assertNotSame(user, anotherUser);
assertEquals(user, anotherUser);
try
{
objectUnderTest.saveUser(anotherUser);
fail("User's username must be unique");
}
catch (final Throwable ex)
{
assertTrue(Exception.class.isAssignableFrom(UsernameNotUniqueException.class));
assertEquals(1, objectUnderTest.getUserCount());
}
}
@Test
public void testFindByUserByUsernameAndPassword() throws Exception
{
System.out.println("testFindByUserByUsernameAndPassword()");
User user = getNewUser(USERNAME, PASSWORD);
user = objectUnderTest.saveUser(user);
final User userFound = objectUnderTest.findUserByUsernameAndPassword(USERNAME, PASSWORD);
assertNotNull(userFound);
assertEquals(user, userFound);
assertNull(objectUnderTest.findUserByUsernameAndPassword("wrongUsername", PASSWORD));
assertNull(objectUnderTest.findUserByUsernameAndPassword(USERNAME, "wrongPassword"));
assertNull(objectUnderTest.findUserByUsernameAndPassword(null, PASSWORD));
assertNull(objectUnderTest.findUserByUsernameAndPassword(USERNAME, null));
}
@Test
public void testHasUsername() throws Exception
{
System.out.println("testHasUsername()");
final User user = getNewUser(USERNAME, PASSWORD);
objectUnderTest.saveUser(user);
assertTrue(objectUnderTest.hasUserWithUsername(USERNAME));
assertFalse(objectUnderTest.hasUserWithUsername("aUsername"));
}
@Test
public void testGetUserCount() throws Exception
{
System.out.println("testGetUserCount()");
final User user = getNewUser(USERNAME, PASSWORD);
objectUnderTest.saveUser(user);
assertEquals(1, objectUnderTest.getUserCount());
}
@Test
public void testDeleteAll() throws Exception
{
System.out.println("testDeleteAll()");
final User user = getNewUser(USERNAME, PASSWORD);
objectUnderTest.saveUser(user);
assertEquals(1, objectUnderTest.getUserCount());
objectUnderTest.deleteAll();
assertEquals(0, objectUnderTest.getUserCount());
}
private User getNewUser(final String username, final String password)
{
final User user = UserDataHelper.createUser(username, password);
user.setRole(role);
return user;
}
private static IServiceLocator getServiceLocator()
{
return ServiceLocator.getInstance();
}
}