Browse Source

Merge commit 'cf44c6aeeb7e07d4cbccf33a17d26d989d8aee31' into HEAD

feature-pr-payment
JenkinsSonaImron 2 years ago
parent
commit
12b42029ad
  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. 56
      src/main/java/hs/fulda/de/ci/exam/project/Admin.java
  4. 5
      src/main/java/hs/fulda/de/ci/exam/project/Aircraft.java
  5. 31
      src/main/java/hs/fulda/de/ci/exam/project/AircraftRepository.java
  6. 8
      src/main/java/hs/fulda/de/ci/exam/project/Flight.java
  7. 32
      src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java
  8. 12
      src/test/java/hs/fulda/de/ci/exam/project/AccountTest.java
  9. 158
      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();
}
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);
}
}

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

56
src/main/java/hs/fulda/de/ci/exam/project/Admin.java

@ -0,0 +1,56 @@
package hs.fulda.de.ci.exam.project;
import java.io.IOException;
public class Admin extends Person{
AccountRepository accountRepository;
FlightRepository flightRepository;
AircraftRepository aircraftRepository;
public Admin(String name, Address address, String email, String phone) {
super(name, address, email, phone);
}
public Aircraft addAircraft(String name, String model, int manufacturerYear) throws IOException {
if(name.isBlank()) throw new RuntimeException("Name cannot be null or empty");
if(model.isBlank()) throw new RuntimeException("Model cannot be null or empty");
if(manufacturerYear < 0) throw new RuntimeException("Year cannot be zero");
Aircraft aircraft = new Aircraft(name, model, manufacturerYear);
aircraftRepository.save(aircraft);
return aircraft;
}
public String searchAircraft(String name){
String aircraftDetails = aircraftRepository.findAircraftByAircraftName(name);
if(aircraftDetails.isBlank()){
throw new RuntimeException("Aircraft does not exist.");
}
return aircraftDetails;
}
public String searchFlights(String flightNumber){
String flightDetails = flightRepository.findFlightByFlightNumber(flightNumber);
if(flightDetails.isBlank()){
throw new RuntimeException("Flight does not exist.");
}
return flightDetails;
}
public Flight addFlight(String flightNumber, Airport departure, Airport arrival, int durationInMinutes ) throws IOException {
if(flightNumber.isBlank()) throw new RuntimeException("FlightNumber cannot be null or empty");
if(departure.equals(null)) throw new RuntimeException("Departure cannot be null or empty");
if(arrival.equals(null)) throw new RuntimeException("Arrival cannot be null or empty");
if(durationInMinutes < 0) throw new RuntimeException("Duration cannot be negative");
Flight flight = new Flight(flightNumber, departure, arrival, durationInMinutes );
flightRepository.save(flight);
return 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/Aircraft.java

@ -33,4 +33,9 @@ public class Aircraft {
public void addFlight(Flight flight) {
flights.add(flight);
}
@Override
public String toString() {
return "Aircraft = {" + "name=" + name + '\'' + ", model=" + model + ", manufacturingYear='" + manYear +'}';
}
}

31
src/main/java/hs/fulda/de/ci/exam/project/AircraftRepository.java

@ -0,0 +1,31 @@
package hs.fulda.de.ci.exam.project;
import java.io.*;
import java.util.Scanner;
public class AircraftRepository {
boolean save(Aircraft aircraft) throws IOException {
FileWriter fw = new FileWriter("aircraft_list.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(aircraft.toString());
bw.newLine();
bw.close();
return true;
};
String findAircraftByAircraftName(String AircraftName){
File file = new File("aircraft_list.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.matches("(.*)"+AircraftName+"(.*)")) {
return line;
}
}
} catch(FileNotFoundException e) {
System.out.println("There are no aircrafts added yet. Please add a aircraft first");
}
return "";
}
}

8
src/main/java/hs/fulda/de/ci/exam/project/Flight.java

@ -45,4 +45,12 @@ public class Flight {
return durationInMinutes;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
@Override
public String toString() {
return "Flight = {" + "flightNumber=" + flightNumber + '\'' + ", departure=" + departure + ", arrival='" + arrival + ", durationInMinutes=" + durationInMinutes + '}';
}
}

32
src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java

@ -0,0 +1,32 @@
package hs.fulda.de.ci.exam.project;
import java.io.*;
import java.util.Scanner;
public class FlightRepository {
boolean save(Flight flight) throws IOException {
FileWriter fw = new FileWriter("flights.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(flight.toString());
bw.newLine();
bw.close();
return true;
};
String findFlightByFlightNumber(String flightNumber){
File file = new File("flights.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.matches("(.*)"+flightNumber+"(.*)")) {
return line;
}
}
} catch(FileNotFoundException e) {
System.out.println("There are no flights added yet. Please add a flight");
}
return "";
}
}

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")
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";

158
src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java

@ -0,0 +1,158 @@
package hs.fulda.de.ci.exam.project;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class AdminTest {
Address address_fr;
Airport airport_fr;
Address address1_ist;
Airport airport1_ist;
Flight flight1;
Address address1;
Aircraft aircraft;
@BeforeEach
public void setup() {
aircraft = new Aircraft("Boeing", "787", 2003);
address_fr = new Address("Frankfurt str", "Frankfurt", "Hessen", "63023", "Germany");
airport_fr = new Airport("Fraport", address_fr, "1234");
address1_ist = new Address("Istanbul str", "Istanbul", "Fatih", "9019", "Turkey");
airport1_ist = new Airport("Istanbul", address1_ist, "5678");
flight1 = new Flight("1", airport_fr, airport1_ist, 140);
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");
}
@Mock
private FlightRepository flightRepository;
@Test
public void test_addFlight_returnsNewFlight() throws IOException {
try {
when(flightRepository.save(any(Flight.class))).thenReturn(true);
} catch (IOException e) {
e.printStackTrace();
}
try {
admin.addFlight("1", airport_fr, airport1_ist, 140);
} catch (IOException e) {
e.printStackTrace();
}
assertNotNull(admin.addFlight("1", airport_fr, airport1_ist, 140));
}
@Test
public void testAddFlight_throwsException() throws IOException {
when(flightRepository.save(any(Flight.class))).thenThrow(IOException.class);
assertThrows(IOException.class, () -> {
admin.addFlight("1", airport_fr, airport1_ist, 140);
});
}
@Test
public void shouldThrowRuntimeExceptionWhenNull(){
assertThrows(RuntimeException.class, () -> {
admin.addFlight(null, airport_fr, airport1_ist, 140);
});
assertThrows(RuntimeException.class, () -> {
admin.addFlight("1", null, airport1_ist, 140);
});
assertThrows(RuntimeException.class, () -> {
admin.addFlight("1", airport_fr, null, 140);
});
assertThrows(RuntimeException.class, () -> {
admin.addFlight("1", airport_fr, airport1_ist, -40);
});
}
@Test
public void test_searchFlight(){
when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn(flight1.toString());
assertNotNull(admin.searchFlights("1"));
assertEquals(admin.searchFlights("1"), flight1.toString());
}
@Test
public void test_searchFlightThrowsExceptionWhenBlank(){
when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn("");
assertThrows(RuntimeException.class, () -> {
admin.searchFlights("1");
});
}
@Mock
private AircraftRepository aircraftRepository;
@Test
public void test_addAircraft_returnsNewAircraft() throws IOException {
try {
when(aircraftRepository.save(any(Aircraft.class))).thenReturn(true);
} catch (IOException e) {
e.printStackTrace();
}
assertNotNull(admin.addAircraft("Boeing", "787", 2003));
}
@Test
public void testAddAircraft_throwsException() throws IOException {
when(aircraftRepository.save(any(Aircraft.class))).thenThrow(IOException.class);
assertThrows(IOException.class, () -> {
admin.addAircraft("Boeing", "787", 2003);
});
}
@Test
public void addAircraftShouldThrowRuntimeExceptionWhenNull(){
assertThrows(RuntimeException.class, () -> {
admin.addAircraft(null, "787", 2003);
});
assertThrows(RuntimeException.class, () -> {
admin.addAircraft("Boeing", "", 2003);
});
assertThrows(RuntimeException.class, () -> {
admin.addAircraft("Boeing", "787", -2003);
});
}
@Test
public void test_searchAircraft(){
when(aircraftRepository.findAircraftByAircraftName(any(String.class))).thenReturn(aircraft.toString());
assertNotNull(admin.searchAircraft("Boeing"));
assertEquals(admin.searchAircraft("Boeing"), aircraft.toString());
}
}
Loading…
Cancel
Save