diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Account.java b/src/main/java/hs/fulda/de/ci/exam/project/Account.java index 7097e7e..882db6a 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Account.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Account.java @@ -75,6 +75,21 @@ 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){ diff --git a/src/main/java/hs/fulda/de/ci/exam/project/AccountService.java b/src/main/java/hs/fulda/de/ci/exam/project/AccountService.java index 789ecba..051e0b9 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/AccountService.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/AccountService.java @@ -1,5 +1,7 @@ package hs.fulda.de.ci.exam.project; +import java.util.Date; + public class AccountService { private final AccountRepository accountRepository; private final PasswordEncoder passwordEncoder; @@ -21,4 +23,23 @@ public class AccountService { 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 void createAccount(String id, String password, Account.AccountStatus accountStatus){ + Account account = new Account(id, password, accountStatus); + validateAccount(account); + checkIfAccountAlreadyExist(account); + accountRepository.save(account); + } + + } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java b/src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java index e80e849..326576a 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java @@ -2,9 +2,13 @@ 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.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; @@ -95,4 +99,12 @@ public class AccountServiceTest { 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); + }); + } + }