You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.9 KiB
140 lines
4.9 KiB
package hs.fulda.de.ci.exam.project;
|
|
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.jupiter.api.DisplayName;
|
|
import org.mockito.ArgumentCaptor;
|
|
import org.mockito.InOrder;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
|
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
|
import static org.hamcrest.CoreMatchers.is;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
import static org.mockito.Mockito.*;
|
|
|
|
public class AccountServiceTest {
|
|
private static final String PASSWORD = "password";
|
|
|
|
private static final Account ENABLED_USER =
|
|
new Account("user id", "hash", Account.AccountStatus.ACTIVE);
|
|
|
|
private static final Account DISABLED_USER =
|
|
new Account("disabled user id", "disabled user password hash", Account.AccountStatus.CLOSED);
|
|
|
|
private AccountRepository accountRepository;
|
|
private PasswordEncoder passwordEncoder;
|
|
private AccountService accountService;
|
|
|
|
@Before
|
|
public void setup() {
|
|
accountRepository = createAccountRepository();
|
|
passwordEncoder = createPasswordEncoder();
|
|
accountService = new AccountService(accountRepository, passwordEncoder);
|
|
}
|
|
|
|
@Test
|
|
public void shouldBeValidForValidCredentials(){
|
|
boolean accountIsValid = accountService.isValidAccount(ENABLED_USER.getId(), PASSWORD);
|
|
assertTrue(accountIsValid);
|
|
|
|
verify(accountRepository).findById(ENABLED_USER.getId());
|
|
|
|
verify(passwordEncoder).encode(PASSWORD);
|
|
}
|
|
|
|
@Test
|
|
public void shouldBEInvalidForInvalidId() {
|
|
boolean accountIsValid = accountService.isValidAccount("invalid id", PASSWORD);
|
|
assertFalse(accountIsValid);
|
|
|
|
InOrder inOrder = inOrder(accountRepository, passwordEncoder);
|
|
inOrder.verify(accountRepository).findById("invalid id");
|
|
inOrder.verify(passwordEncoder, never()).encode(anyString());
|
|
}
|
|
|
|
@Test
|
|
public void shouldBeInvalidForInvalid(){
|
|
boolean accountIsValid = accountService.isValidAccount("invalid id", PASSWORD);
|
|
assertFalse(accountIsValid);
|
|
|
|
InOrder inOrder = inOrder(accountRepository, passwordEncoder);
|
|
inOrder.verify(accountRepository).findById("invalid id");
|
|
inOrder.verify(passwordEncoder, never()).encode(anyString());
|
|
}
|
|
|
|
@Test
|
|
public void shouldBeInvalidForDisabledUser(){
|
|
boolean accountIsValid =
|
|
accountService.isValidAccount(DISABLED_USER.getId(), PASSWORD);
|
|
assertFalse(accountIsValid);
|
|
|
|
verify(accountRepository).findById(DISABLED_USER.getId());
|
|
verify(passwordEncoder, never()).encode(anyString());
|
|
}
|
|
|
|
@Test
|
|
public void shouldBeInvalidForInvalidPassword() {
|
|
boolean accountIsValid =
|
|
accountService.isValidAccount(ENABLED_USER.getId(), "invalid");
|
|
assertFalse(accountIsValid);
|
|
|
|
ArgumentCaptor<String> passwordCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
|
verify(passwordEncoder).encode(passwordCaptor.capture());
|
|
assertEquals("invalid", passwordCaptor.getValue());
|
|
}
|
|
|
|
private AccountRepository createAccountRepository() {
|
|
AccountRepository mock = mock(AccountRepository.class);
|
|
when(mock.findById(ENABLED_USER.getId())).thenReturn(ENABLED_USER);
|
|
when(mock.findById(DISABLED_USER.getId())).thenReturn(DISABLED_USER);
|
|
return mock;
|
|
}
|
|
|
|
private PasswordEncoder createPasswordEncoder() {
|
|
PasswordEncoder mock = mock(PasswordEncoder.class);
|
|
when(mock.encode(anyString())).thenReturn("any password hash");
|
|
when(mock.encode(PASSWORD)).thenReturn(ENABLED_USER.getPasswordHash());
|
|
return mock;
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Should Not Create Account when Id is null")
|
|
public void shouldThrowRuntimeExceptionWhenIdIsNull(){
|
|
assertThrows(RuntimeException.class, () -> {
|
|
accountService.createAccount(null, "pwd", Account.AccountStatus.ACTIVE);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Should Not Create Account when password is blank")
|
|
public void shouldThrowRuntimeExceptionWhenPasswordIsNull(){
|
|
assertThrows(RuntimeException.class, () -> {
|
|
accountService.createAccount("John", "", Account.AccountStatus.ACTIVE);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Should Not Create Account when status is null")
|
|
public void shouldThrowRuntimeExceptionWhenStatusIsNull(){
|
|
assertThrows(RuntimeException.class, () -> {
|
|
accountService.createAccount("John", "", null);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void verifyCreateAccount() {
|
|
|
|
when(accountRepository.save(any(Account.class))).thenReturn(true);
|
|
|
|
assertTrue(accountService.createAccount("John", "pwd", Account.AccountStatus.ACTIVE));
|
|
|
|
verify(accountRepository).save(any(Account.class));
|
|
verify(accountRepository, times(1)).checkIfAccountAlreadyExist(any(Account.class));
|
|
verify(accountRepository, never()).delete(any(Account.class));
|
|
}
|
|
|
|
}
|