From 615b73a9abfab2441695955942e5d2bc749958b3 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Wed, 9 Feb 2022 18:34:53 +0100 Subject: [PATCH] block user as an admin --- .../hs/fulda/de/ci/exam/project/Account.java | 4 +-- .../de/ci/exam/project/AccountRepository.java | 30 +++++++++++++--- .../hs/fulda/de/ci/exam/project/Admin.java | 27 +++++++++++++++ .../de/ci/exam/project/FlightRepository.java | 5 +++ .../fulda/de/ci/exam/project/AccountTest.java | 12 +++---- .../fulda/de/ci/exam/project/AdminTest.java | 34 +++++++++++++++++++ 6 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 src/main/java/hs/fulda/de/ci/exam/project/Admin.java create mode 100644 src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java create mode 100644 src/test/java/hs/fulda/de/ci/exam/project/AdminTest.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 c4c9007..b5bbaa8 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 @@ -59,11 +59,11 @@ public class Account { person.validatePhoneNumber(); } - public void addAccountDetails(String name, Address address, String email, String phone){ + public void addAccountDetails(String id, String name, Address address, String email, String phone){ Person person = new Person(name, address, email, phone); validatePersonDetails(person); - accountRepository.addPersonalDetails(person); + accountRepository.addPersonalDetails(person, id); } } 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 index 9f2d215..db9d3e8 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java @@ -1,9 +1,29 @@ package hs.fulda.de.ci.exam.project; -import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; -public interface AccountRepository { - ArrayList findAll(); - void save(Account account); - boolean addPersonalDetails(Person person); +public class AccountRepository { + Map accountList = new ConcurrentHashMap(); + Map personalInfo = new ConcurrentHashMap<>(); + + public boolean checkIfAccountAlreadyExist(Account account){ + if(accountList.containsKey(generateKey(account))){ + return true; + } + return false; + } + private String generateKey(Account account) { + return String.format("%s", account.getId()); + } + public Collection findAll() { + return accountList.values(); + } + public void save(Account account){ + accountList.put(generateKey(account), account); + } + public void addPersonalDetails(Person person, String id){ + personalInfo.put(accountList.get(id), person); + } } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Admin.java b/src/main/java/hs/fulda/de/ci/exam/project/Admin.java new file mode 100644 index 0000000..84cea28 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/Admin.java @@ -0,0 +1,27 @@ +package hs.fulda.de.ci.exam.project; + +public class Admin extends Person{ + + AccountRepository accountRepository; + FlightRepository flightRepository; + public Admin(String name, Address address, String email, String phone) { + super(name, address, email, phone); + } + + public void addAircraft(){ + + } + + public void addFlight(String flightNumber, Airport departure, Airport arrival, int durationInMinutes ){ + Flight flight = new Flight(flightNumber, departure, arrival, durationInMinutes ); + flightRepository.save(flight); + } + + public boolean blockUser(Account user){ + if(accountRepository.checkIfAccountAlreadyExist(user)){ + user.setStatus(Account.AccountStatus.BLOCKED); + return true; + } + return false; + } +} diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java b/src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java new file mode 100644 index 0000000..29eed52 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java @@ -0,0 +1,5 @@ +package hs.fulda.de.ci.exam.project; + +public interface FlightRepository { + void save(Flight flight); +} 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 419ddc3..8084b06 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 @@ -52,7 +52,7 @@ public class AccountTest { @DisplayName("Should Not add Account details when Person Name is null") public void shouldThrowRuntimeExceptionWhenPersonNameIsNull(){ assertThrows(RuntimeException.class, () -> { - account1.addAccountDetails(null, address1,"max.mustermann@gmail.com", "0151283290" ); + account1.addAccountDetails("1",null, address1,"max.mustermann@gmail.com", "0151283290" ); }); } @@ -60,7 +60,7 @@ public class AccountTest { @DisplayName("Should Not add Account details when Address is null") public void shouldThrowRuntimeExceptionWhenPersonAddressIsNull(){ assertThrows(RuntimeException.class, () -> { - account1.addAccountDetails("John", null,"max.mustermann@gmail.com", "0151283290" ); + account1.addAccountDetails("1", "John", null,"max.mustermann@gmail.com", "0151283290" ); }); } @@ -69,7 +69,7 @@ public class AccountTest { @MethodSource("phoneNumberList") public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String phoneNumber){ assertThrows(RuntimeException.class, () -> { - account1.addAccountDetails("John", address1,"max.mustermann@gmail.com", phoneNumber); + account1.addAccountDetails("1","John", address1,"max.mustermann@gmail.com", phoneNumber); }); } @@ -81,10 +81,10 @@ public class AccountTest { @DisplayName("Should Not add Account details when Email is blank") public void shouldThrowRuntimeExceptionWhenEmailIsNull(){ assertThrows(RuntimeException.class, () -> { - account1.addAccountDetails("John", address1," ", "0151283290" ); + account1.addAccountDetails("1", "John", address1," ", "0151283290" ); }); assertThrows(RuntimeException.class, () -> { - account1.addAccountDetails("John", address1,null, "0151283290" ); + account1.addAccountDetails("1", "John", address1,null, "0151283290" ); }); } @@ -93,7 +93,7 @@ public class AccountTest { @DisplayName("Should Not add Account details when Email Address is not valid") public void shouldThrowRuntimeExceptionWhenEmailIsInvalid(String email){ try{ - account1.addAccountDetails("John", address1, email, "0151283290"); + account1.addAccountDetails("1","John", address1, email, "0151283290"); } catch(final RuntimeException e){ String msg2 = "Email address is not Valid"; diff --git a/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java b/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java new file mode 100644 index 0000000..47bde88 --- /dev/null +++ b/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java @@ -0,0 +1,34 @@ +package hs.fulda.de.ci.exam.project; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class AdminTest { + + final Address address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany"); + + @InjectMocks + Admin admin = new Admin("Max Muster", address1, "example@gmail.com", "012345678" ); + + @Mock + private AccountRepository accountRepository; + + @Test + public void test_blockUser() { + ArrayList users = new ArrayList<>(); + Account user = new Account("john5", "password", Account.AccountStatus.ACTIVE); + when(accountRepository.checkIfAccountAlreadyExist(user)).thenReturn(true); + + admin.blockUser(user); + + assertEquals(Account.AccountStatus.BLOCKED, user.getStatus(), "Status successfully changed"); + } +}