|
@@ -0,0 +1,239 @@
|
|
|
+package com.mooctest.crowd.domain.domainobject;
|
|
|
+
|
|
|
+import com.mooctest.crowd.domain.command.LoginCommand;
|
|
|
+import com.mooctest.crowd.domain.exception.PasswordErrorException;
|
|
|
+import com.mooctest.crowd.domain.exception.UserNotExistException;
|
|
|
+import com.mooctest.crowd.domain.repository.UserRepo;
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Rule;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.rules.ExpectedException;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+import org.mockito.runners.MockitoJUnitRunner;
|
|
|
+
|
|
|
+import static org.junit.Assert.assertTrue;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+@RunWith(MockitoJUnitRunner.class)
|
|
|
+public class UserTest {
|
|
|
+ @InjectMocks
|
|
|
+ private User user = new User();
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private UserRepo userRepo;
|
|
|
+
|
|
|
+ private LoginCommand loginCommand = new LoginCommand();
|
|
|
+
|
|
|
+ @Rule
|
|
|
+ public ExpectedException thrown = ExpectedException.none();
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp(){
|
|
|
+ MockitoAnnotations.initMocks(this);
|
|
|
+
|
|
|
+ loginCommand.setMobileNum("13657094936");
|
|
|
+ loginCommand.setPassword("123456");
|
|
|
+
|
|
|
+ user.setMobile("13657094936");
|
|
|
+ user.setPassword("4QrcOUm6Wau+VuBX8g+IPg==");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_return_true_when_login_success() throws PasswordErrorException, UserNotExistException {
|
|
|
+ // arrange
|
|
|
+ when(userRepo.getByMobileNum("13657094936")).thenReturn(user);
|
|
|
+ // action
|
|
|
+ boolean success = user.login(loginCommand.getMobileNum(),loginCommand.getPassword());
|
|
|
+ // assert
|
|
|
+ assertTrue(success);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test(expected = UserNotExistException.class)
|
|
|
+ public void should_throw_when_user_not_exist() throws UserNotExistException, PasswordErrorException {
|
|
|
+ // arrange
|
|
|
+ loginCommand.setMobileNum("NOT_EXIST");
|
|
|
+ when(userRepo.getByMobileNum("NOT_EXIST")).thenReturn(null);
|
|
|
+ user.login(loginCommand.getMobileNum(),loginCommand.getPassword());
|
|
|
+ }
|
|
|
+
|
|
|
+//
|
|
|
+// @Test(expected = PasswordErrorException.class)
|
|
|
+// public void should_throw_exception_when_password_error() throws PasswordErrorException, AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// LoginCommand loginCommand = new LoginCommand();
|
|
|
+// loginCommand.setMobileNum("EXIST_ACCOUNT");
|
|
|
+// loginCommand.setPassword("ERROR_PASSWORD");
|
|
|
+// IAccountRepo IAccountRepo = new AccountUTRepo();
|
|
|
+// Account account = IAccountRepo.getByMobileNum(loginCommand.getMobileNum());
|
|
|
+// // assert
|
|
|
+// account.login(loginCommand);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void should_update_password_when_modify_password() throws AccountNotExistException, PasswordErrorException {
|
|
|
+// // arrange
|
|
|
+// ModifyPasswordCommand modifyPasswordCommand = new ModifyPasswordCommand();
|
|
|
+// modifyPasswordCommand.setMobileNum("EXIST_ACCOUNT");
|
|
|
+// modifyPasswordCommand.setOldPassword("PASSWORD");
|
|
|
+// modifyPasswordCommand.setNewPassword("654321");
|
|
|
+//
|
|
|
+// Account account = accountUTRepo.getByMobileNum(modifyPasswordCommand.getMobileNum());
|
|
|
+// // action
|
|
|
+// boolean result = account.modifyPassword(modifyPasswordCommand);
|
|
|
+// // assert
|
|
|
+// assertTrue(result);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void should_return_permissions_when_account_login_success() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// Account account = this.getSuccessAccount();
|
|
|
+// // action
|
|
|
+// List<Permission> permissions = account.distributePermission();
|
|
|
+// // assert
|
|
|
+// assertEquals(permissions.get(0).getId(), new Long(1));
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void should_return_roles_when_account_login_success() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// Account account = this.getSuccessAccount();
|
|
|
+// // action
|
|
|
+// List<Role> roles = account.distributeRole();
|
|
|
+// // assert
|
|
|
+// assertEquals(roles.get(0).getId(), new Long(1));
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void should_return_account_when_view_by_account_id() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// AccountCommand accountCommand = new AccountCommand();
|
|
|
+// accountCommand.setId(123L);
|
|
|
+//
|
|
|
+// // action
|
|
|
+// Account accountUTRepoByID = accountUTRepo.getByID(accountCommand.getId());
|
|
|
+// // assert
|
|
|
+// assertEquals(accountUTRepoByID.getId(), accountCommand.getId());
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test(expected = AccountNotExistException.class)
|
|
|
+// public void should_throw_when_view_account_not_exist_by_account_id() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// AccountCommand accountCommand = new AccountCommand();
|
|
|
+// accountCommand.setId(111L);
|
|
|
+// // action
|
|
|
+// accountUTRepo.getByID(accountCommand.getId());
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void should_return_account_list_when_view_by_account_ids() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// Long[] ids = {123L};
|
|
|
+// // action
|
|
|
+// List<Account> accountList = accountUTRepo.getByIDList(ids);
|
|
|
+// // assert
|
|
|
+// assertEquals(accountList.get(0).getId(), ids[0]);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test(expected = AccountNotExistException.class)
|
|
|
+// public void should_throw_when_view_not_exist_account_by_account_ids() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// Long[] ids = {123L,111L};
|
|
|
+//
|
|
|
+// // action
|
|
|
+// accountUTRepo.getByIDList(ids);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void should_return_false_when_add_exist_account(){
|
|
|
+// // arrange
|
|
|
+// Account account = this.createCorrectAccount();
|
|
|
+// // action
|
|
|
+// Account resultAccount = accountUTRepo.addAccount(account);
|
|
|
+// // assert
|
|
|
+// assertEquals(resultAccount.getMobileNum(),account.getMobileNum());
|
|
|
+// }
|
|
|
+//
|
|
|
+// // @Test(expected = AccountNotExistException.class)
|
|
|
+//// public void should_throw_when_add_account_success() throws AccountNotExistException {
|
|
|
+//// // arrange
|
|
|
+//// Account account = this.createDiscorrectAccount();
|
|
|
+//// // action
|
|
|
+//// accountUTRepo.addAccount(account);
|
|
|
+//// }
|
|
|
+// @Test
|
|
|
+// public void should_return_true_when_remove_account_success() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// Account account = this.createCorrectAccount();
|
|
|
+// // action
|
|
|
+// boolean result = accountUTRepo.removeAccount(account);
|
|
|
+// // assert
|
|
|
+// assertTrue(result);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test(expected = AccountNotExistException.class)
|
|
|
+// public void should_throw_when_remove_not_exist_account() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// Account account = this.createDiscorrectAccount();
|
|
|
+// // action
|
|
|
+// accountUTRepo.removeAccount(account);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void should_return_true_when_update_account_success() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// Account account = this.createCorrectAccount();
|
|
|
+// account.setPassword("UPDATE_PASSWORD");
|
|
|
+// // action
|
|
|
+// Account updateAccount = accountUTRepo.updateAccount(account);
|
|
|
+// // assert
|
|
|
+// assertEquals(updateAccount.getPassword(),account.getPassword());
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test(expected = AccountNotExistException.class)
|
|
|
+// public void should_throw_when_update_not_exist_account() throws AccountNotExistException {
|
|
|
+// // arrange
|
|
|
+// Account account = this.createDiscorrectAccount();
|
|
|
+// // action
|
|
|
+// accountUTRepo.updateAccount(account);
|
|
|
+// }
|
|
|
+//
|
|
|
+// private LoginCommand getSuccessLoginCommand() {
|
|
|
+// LoginCommand loginCommand = new LoginCommand();
|
|
|
+// loginCommand.setMobileNum("EXIST_ACCOUNT");
|
|
|
+// loginCommand.setPassword("PASSWORD");
|
|
|
+// loginCommand.setCheckCode("123456");
|
|
|
+// return loginCommand;
|
|
|
+// }
|
|
|
+//
|
|
|
+// private Account getSuccessAccount() throws AccountNotExistException {
|
|
|
+// LoginCommand loginCommand = getSuccessLoginCommand();
|
|
|
+// IAccountRepo IAccountRepo = new AccountUTRepo();
|
|
|
+// return IAccountRepo.getByMobileNum(loginCommand.getMobileNum());
|
|
|
+// }
|
|
|
+//
|
|
|
+// private Account createCorrectAccount(){
|
|
|
+// AccountCommand accountCommand = new AccountCommand();
|
|
|
+// accountCommand.setMobileNum("EXIST_ACCOUNT");
|
|
|
+// accountCommand.setPassword("PASSWORD");
|
|
|
+// Account account = new Account();
|
|
|
+// account.setMobileNum(accountCommand.getMobileNum());
|
|
|
+// account.setPassword(accountCommand.getPassword());
|
|
|
+// return account;
|
|
|
+// }
|
|
|
+//
|
|
|
+// private Account createDiscorrectAccount(){
|
|
|
+// AccountCommand accountCommand = new AccountCommand();
|
|
|
+// accountCommand.setMobileNum("NOT_EXIST_ACCOUNT");
|
|
|
+// accountCommand.setPassword("PASSWORD");
|
|
|
+// Account account = new Account();
|
|
|
+// account.setMobileNum(accountCommand.getMobileNum());
|
|
|
+// account.setPassword(accountCommand.getPassword());
|
|
|
+// return account;
|
|
|
+// }
|
|
|
+
|
|
|
+}
|