Browse Source

Should Not Create Account when Id is null

feature-pr-AccountService
Sona Markosyan 3 years ago
parent
commit
36e9726d97
  1. 15
      src/main/java/hs/fulda/de/ci/exam/project/Account.java
  2. 21
      src/main/java/hs/fulda/de/ci/exam/project/AccountService.java
  3. 12
      src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java

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

@ -75,6 +75,21 @@ public class Account {
accountRepository.addPersonalDetails(person, id); 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{ class PasswordEncoder{
String encode(String password){ String encode(String password){

21
src/main/java/hs/fulda/de/ci/exam/project/AccountService.java

@ -1,5 +1,7 @@
package hs.fulda.de.ci.exam.project; package hs.fulda.de.ci.exam.project;
import java.util.Date;
public class AccountService { public class AccountService {
private final AccountRepository accountRepository; private final AccountRepository accountRepository;
private final PasswordEncoder passwordEncoder; private final PasswordEncoder passwordEncoder;
@ -21,4 +23,23 @@ public class AccountService {
String encodedPassword = passwordEncoder.encode(password); String encodedPassword = passwordEncoder.encode(password);
return encodedPassword.equals(account.getPasswordHash()); 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);
}
} }

12
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.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.InOrder; import org.mockito.InOrder;
import java.util.ArrayList;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -95,4 +99,12 @@ public class AccountServiceTest {
return mock; 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);
});
}
} }
Loading…
Cancel
Save