JenkinsSonaImron
3 years ago
4 changed files with 234 additions and 1 deletions
-
37src/main/java/hs/fulda/de/ci/exam/project/Account.java
-
12src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java
-
46src/main/java/hs/fulda/de/ci/exam/project/AccountService.java
-
140src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java
@ -0,0 +1,46 @@ |
|||
package hs.fulda.de.ci.exam.project; |
|||
|
|||
import java.util.Date; |
|||
|
|||
public class AccountService { |
|||
private final AccountRepository accountRepository; |
|||
private final PasswordEncoder passwordEncoder; |
|||
|
|||
public AccountService(AccountRepository accountRepository, PasswordEncoder passwordEncoder) { |
|||
this.accountRepository = accountRepository; |
|||
this.passwordEncoder = passwordEncoder; |
|||
} |
|||
public boolean isValidAccount(String id, String password){ |
|||
Account account = accountRepository.findById(id); |
|||
return isEnabledAccount(account) && isValidPassword(account, password); |
|||
} |
|||
|
|||
private boolean isEnabledAccount(Account account) { |
|||
return account!= null && account.isEnabled(); |
|||
} |
|||
|
|||
private boolean isValidPassword(Account account, String password) { |
|||
String encodedPassword = passwordEncoder.encode(password); |
|||
return encodedPassword.equals(account.getPasswordHash()); |
|||
} |
|||
public void validateAccount(Account account){ |
|||
account.validateAccountStatus(); |
|||
account.validateId(); |
|||
account.validatePassword(); |
|||
} |
|||
|
|||
public void checkIfAccountAlreadyExist(Account account){ |
|||
if(accountRepository.checkIfAccountAlreadyExist(account)){ |
|||
throw new RuntimeException("Account Already Exists"); |
|||
} |
|||
} |
|||
public boolean createAccount(String id, String password, Account.AccountStatus accountStatus){ |
|||
Account account = new Account(id, password, accountStatus); |
|||
validateAccount(account); |
|||
checkIfAccountAlreadyExist(account); |
|||
accountRepository.save(account); |
|||
return true; |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,140 @@ |
|||
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)); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue