Browse Source

Merge commit '0ebcc656f863464819bae23b464a86de2050f9e4' into HEAD

feature-pr-payment
JenkinsSonaImron 2 years ago
parent
commit
584b5b4791
  1. 37
      src/main/java/hs/fulda/de/ci/exam/project/Account.java
  2. 12
      src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java
  3. 46
      src/main/java/hs/fulda/de/ci/exam/project/AccountService.java
  4. 140
      src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java

37
src/main/java/hs/fulda/de/ci/exam/project/Account.java

@ -33,6 +33,15 @@ public class Account {
this.status = status;
}
public boolean isEnabled() {
if(status == AccountStatus.ACTIVE) return true;
return false;
}
public String getPasswordHash() {
return new PasswordEncoder().encode(password);
}
public enum AccountStatus {
ACTIVE,
CLOSED,
@ -66,5 +75,33 @@ public class Account {
accountRepository.addPersonalDetails(person, id);
}
public void validateId() {
if(this.id.isBlank())
throw new RuntimeException(("Id Cannot be null or empty"));
}
public void validatePassword(){
if(this.password.isBlank())
throw new RuntimeException(("Id Cannot be null or empty"));
}
public void validateAccountStatus(){
if(this.status.equals(null))
throw new RuntimeException(("Id Cannot be null or empty"));
}
}
class PasswordEncoder{
String encode(String password){
int p = 31;
int m = (int) Math.pow(10, 9) + 9;
int hash_value = 0;
int p_pow = 1;
for (char c : password.toCharArray()) {
hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
p_pow = (p_pow * p) % m;
}
return Integer.toString(hash_value);
}
}

12
src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java

@ -14,16 +14,26 @@ public class AccountRepository {
}
return false;
}
Account findById(String id){
Account account = accountList.get(id);
return account;
}
private String generateKey(Account account) {
return String.format("%s", account.getId());
}
public Collection<Account> findAll() {
return accountList.values();
}
public void save(Account account){
public boolean save(Account account){
accountList.put(generateKey(account), account);
return true;
}
public void addPersonalDetails(Person person, String id){
personalInfo.put(accountList.get(id), person);
}
public void delete(Account account) {
accountList.remove(generateKey(account));
}
}

46
src/main/java/hs/fulda/de/ci/exam/project/AccountService.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;
}
}

140
src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java

@ -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));
}
}
Loading…
Cancel
Save