JenkinsSonaImron
3 years ago
9 changed files with 323 additions and 13 deletions
-
4src/main/java/hs/fulda/de/ci/exam/project/Account.java
-
30src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java
-
56src/main/java/hs/fulda/de/ci/exam/project/Admin.java
-
5src/main/java/hs/fulda/de/ci/exam/project/Aircraft.java
-
31src/main/java/hs/fulda/de/ci/exam/project/AircraftRepository.java
-
8src/main/java/hs/fulda/de/ci/exam/project/Flight.java
-
32src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java
-
12src/test/java/hs/fulda/de/ci/exam/project/AccountTest.java
-
158src/test/java/hs/fulda/de/ci/exam/project/AdminTest.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); |
||||
|
} |
||||
} |
} |
@ -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; |
||||
|
} |
||||
|
} |
@ -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 ""; |
||||
|
} |
||||
|
} |
@ -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 ""; |
||||
|
} |
||||
|
} |
@ -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()); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue