Browse Source

block user as an admin

feature-pr-Admin
Sona Markosyan 3 years ago
parent
commit
615b73a9ab
  1. 4
      src/main/java/hs/fulda/de/ci/exam/project/Account.java
  2. 30
      src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java
  3. 27
      src/main/java/hs/fulda/de/ci/exam/project/Admin.java
  4. 5
      src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java
  5. 12
      src/test/java/hs/fulda/de/ci/exam/project/AccountTest.java
  6. 34
      src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java

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

@ -59,11 +59,11 @@ public class Account {
person.validatePhoneNumber(); 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); Person person = new Person(name, address, email, phone);
validatePersonDetails(person); validatePersonDetails(person);
accountRepository.addPersonalDetails(person);
accountRepository.addPersonalDetails(person, id);
} }
} }

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

@ -1,9 +1,29 @@
package hs.fulda.de.ci.exam.project; 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<Account> findAll();
void save(Account account);
boolean addPersonalDetails(Person person);
public class AccountRepository {
Map<String, Account> accountList = new ConcurrentHashMap<String, Account>();
Map<Account, Person> 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<Account> 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);
}
} }

27
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;
}
}

5
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);
}

12
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") @DisplayName("Should Not add Account details when Person Name is null")
public void shouldThrowRuntimeExceptionWhenPersonNameIsNull(){ public void shouldThrowRuntimeExceptionWhenPersonNameIsNull(){
assertThrows(RuntimeException.class, () -> { 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") @DisplayName("Should Not add Account details when Address is null")
public void shouldThrowRuntimeExceptionWhenPersonAddressIsNull(){ public void shouldThrowRuntimeExceptionWhenPersonAddressIsNull(){
assertThrows(RuntimeException.class, () -> { 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") @MethodSource("phoneNumberList")
public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String phoneNumber){ public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String phoneNumber){
assertThrows(RuntimeException.class, () -> { 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") @DisplayName("Should Not add Account details when Email is blank")
public void shouldThrowRuntimeExceptionWhenEmailIsNull(){ public void shouldThrowRuntimeExceptionWhenEmailIsNull(){
assertThrows(RuntimeException.class, () -> { assertThrows(RuntimeException.class, () -> {
account1.addAccountDetails("John", address1," ", "0151283290" );
account1.addAccountDetails("1", "John", address1," ", "0151283290" );
}); });
assertThrows(RuntimeException.class, () -> { 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") @DisplayName("Should Not add Account details when Email Address is not valid")
public void shouldThrowRuntimeExceptionWhenEmailIsInvalid(String email){ public void shouldThrowRuntimeExceptionWhenEmailIsInvalid(String email){
try{ try{
account1.addAccountDetails("John", address1, email, "0151283290");
account1.addAccountDetails("1","John", address1, email, "0151283290");
} }
catch(final RuntimeException e){ catch(final RuntimeException e){
String msg2 = "Email address is not Valid"; String msg2 = "Email address is not Valid";

34
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<Account> 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");
}
}
Loading…
Cancel
Save