From 7a9fc529b4a7a0a8dcacfbbe414de483ccc4c426 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Tue, 8 Feb 2022 21:58:55 +0100 Subject: [PATCH] add account details --- .../hs/fulda/de/ci/exam/project/Account.java | 18 ++++++++ .../de/ci/exam/project/AccountRepository.java | 9 ++++ .../hs/fulda/de/ci/exam/project/Person.java | 42 ++++++++++++++++++ .../fulda/de/ci/exam/project/AccountTest.java | 43 ++++++++++++++++--- .../fulda/de/ci/exam/project/PersonTest.java | 4 +- 5 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java 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 ed2736c..ae0868a 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 @@ -1,6 +1,8 @@ package hs.fulda.de.ci.exam.project; public class Account { + private AccountRepository accountRepository; + public Account(String id, String password, AccountStatus status) { this.id = id; this.password = password; @@ -50,6 +52,22 @@ public class Account { return false; } + public void validatePersonDetails(Person person){ + person.validateName(); + person.validateAddress(); + person.validateEmailAddress(); + person.validatePhoneNumber(); + } + + public boolean addAccountDetails(String name, Address address, String email, String phone){ + + Person person = new Person(name, address, email, phone); + validatePersonDetails(person); + if(accountRepository.addPersonalDetails(person)){ + return true; + }; + return false; + } } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java b/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java new file mode 100644 index 0000000..9f2d215 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java @@ -0,0 +1,9 @@ +package hs.fulda.de.ci.exam.project; + +import java.util.ArrayList; + +public interface AccountRepository { + ArrayList findAll(); + void save(Account account); + boolean addPersonalDetails(Person person); +} diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Person.java b/src/main/java/hs/fulda/de/ci/exam/project/Person.java index 34674cd..cb8f70c 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Person.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Person.java @@ -1,6 +1,8 @@ package hs.fulda.de.ci.exam.project; import java.util.HashSet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Person { private String name; @@ -26,4 +28,44 @@ public class Person { public String getPhone() { return phone; } + + public void validateName() { + if(this.name.isBlank()) + throw new RuntimeException(("Name Cannot be null or empty")); + } + + public void validateAddress(){ + if(this.address.equals(null)) + throw new RuntimeException(("Address Cannot be null")); + } + + public void validateEmailAddress(){ + Pattern pattern = Pattern.compile("^(.+)@(.+)$"); + Matcher matcher = pattern.matcher(this.email); + if(email.isBlank()){ + throw new RuntimeException("Email cannot be blank"); + } + if(!matcher.matches()){ + throw new RuntimeException("Email address is not Valid"); + } + + } + + public void validatePhoneNumber(){ + if(this.phone.isBlank()) { + throw new RuntimeException("Phone Number Cannot be null or empty"); + } + if(this.phone.length()>13) { + throw new RuntimeException("Phone Number is too long"); + } + if(this.phone.length()<9) { + throw new RuntimeException("Phone Number is too short"); + } + if(this.phone.matches("\\d")) { + throw new RuntimeException("Phone Number Contain only digits"); + } + if(!this.phone.startsWith("0")) { + throw new RuntimeException("Phone Number should start with 0"); + } + } } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/AccountTest.java b/src/test/java/hs/fulda/de/ci/exam/project/AccountTest.java index e020cb8..e757a98 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/AccountTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/AccountTest.java @@ -1,20 +1,53 @@ package hs.fulda.de.ci.exam.project; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.ArrayList; +import java.util.Date; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.when; +@TestInstance(TestInstance.Lifecycle.PER_CLASS) public class AccountTest { + + @BeforeAll + public void setupAll(){ + System.out.println("Should Print Before All Tests"); + } + Address address1; + Account account2; + Person person1; + Account account1; + + @BeforeEach + public void setup() { + address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany"); + account2 = new Account("453", "notactive", Account.AccountStatus.CANCELED); + person1 = new Person("Max Mustermann", address1, "max.mustermann@gmail.com", "015147890206"); + account1 = new Account("123", "password", Account.AccountStatus.ACTIVE); + } @Test - void test_resetPassword(){ - Account account1 = new Account("123", "password", Account.AccountStatus.ACTIVE); - Account account2 = new Account("453", "notactive", Account.AccountStatus.CANCELED); + void test_resetPassword(){ account1.resetPassword("newpass"); account2.resetPassword("notgood"); assertEquals("newpass", account1.getPassword(),"Password successfully changed."); assertEquals("notactive", account2.getPassword(),"Activate your account to change your password"); + } + @Test + @DisplayName("Should Not Create Itinerary when Starting Airport is null") + public void shouldThrowRuntimeExceptionWhenPersonNameIsNull(){ + assertThrows(RuntimeException.class, () -> { + account1.addAccountDetails(null, address1,"max.mustermann@gmail.com", "015147890206" ); + }); } } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java b/src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java index 30db8bf..6a08e97 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java @@ -1,6 +1,9 @@ package hs.fulda.de.ci.exam.project; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import static org.assertj.core.api.Assertions.assertThat; @@ -26,5 +29,4 @@ public class PersonTest { void test_getPhone() { assertThat(person1.getPhone()).describedAs("get person phone").isEqualTo("015147890206"); } - }