|
| 1 | +package delft; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.*; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | +import static org.mockito.ArgumentMatchers.*; |
| 6 | +import static org.mockito.Mockito.*; |
| 7 | + |
| 8 | +import java.time.LocalDate; |
| 9 | +import java.util.*; |
| 10 | +import java.util.stream.*; |
| 11 | +import org.junit.jupiter.api.*; |
| 12 | +import org.mockito.*; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 16 | + |
| 17 | + |
| 18 | +@ExtendWith(MockitoExtension.class) |
| 19 | +class SoftWhereTests { |
| 20 | + |
| 21 | + //Methods have setup inside them, since I cannot use @BeforeEach and am |
| 22 | + //not sure if if I write a separate method to setup this will work, so yeah |
| 23 | + //there is a ton of code duplication to make sure grading will |
| 24 | + //work as expected. I also need to not have any dependency between tests, |
| 25 | + //so I cannot depend on one test making the setup. I will also not risk |
| 26 | + //putting setup into lines 26-32 without a method because I have no clue |
| 27 | + //what your grading system does and if it would work. |
| 28 | + //I have just chosen the safest way. |
| 29 | + |
| 30 | + @Mock |
| 31 | + ReservationRepository reservationRepository; |
| 32 | + |
| 33 | + @Mock |
| 34 | + TripRepository tripRepository; |
| 35 | + |
| 36 | + SoftWhere softwhere; |
| 37 | + Person person1, person2, person3, person4, person5, person6; |
| 38 | + |
| 39 | + Trip trip1, trip2, trip3; |
| 40 | + Info info1, info2, info3; |
| 41 | + |
| 42 | + Reservation res1, res2, res3, res4; |
| 43 | + |
| 44 | + //test when it is possible to register for the given trip |
| 45 | + @Test |
| 46 | + void testPossibleToRegister() { |
| 47 | + MockitoAnnotations.openMocks(this); |
| 48 | + |
| 49 | + |
| 50 | + softwhere = new SoftWhere(tripRepository, reservationRepository); |
| 51 | + |
| 52 | + person1 = new Person("fname1", "mname1", "lname1", LocalDate.of(1998, 2, 5)); |
| 53 | + person4 = new Person("fname4", "mname4", "lname4", LocalDate.of(1995, 11, 17)); |
| 54 | + person5 = new Person("fname5", "mname5", "lname5", LocalDate.of(1978, 1, 1)); |
| 55 | + info2 = new Info(LocalDate.of(2021, 7, 1), LocalDate.of(2021, 7, 15), Destination.MALTA); |
| 56 | + trip2 = new Trip("Trip to Malta", info2, 2050, 3); |
| 57 | + res4 = new Reservation(trip2, new ArrayList<>(List.of(person4, person5))); |
| 58 | + |
| 59 | + when(tripRepository.getTripById(2L)).thenReturn(trip2); |
| 60 | + when(reservationRepository.getAllReservationsByTrip(trip2)).thenReturn(new ArrayList<>(List.of(res4))); |
| 61 | + |
| 62 | + List<Person> people = new ArrayList<>(List.of(person1)); |
| 63 | + |
| 64 | + assertTrue(softwhere.makeReservation(2L, people)); |
| 65 | + |
| 66 | + verify(tripRepository, times(1)).getTripById(2L); |
| 67 | + verify(reservationRepository, times(1)).getAllReservationsByTrip(trip2); |
| 68 | + verify(reservationRepository, times(1)).save(new Reservation(trip2, people)); |
| 69 | + |
| 70 | + verifyNoMoreInteractions(tripRepository); |
| 71 | + verifyNoMoreInteractions(reservationRepository); |
| 72 | + } |
| 73 | + |
| 74 | + //test when it is impossible to make a reservation |
| 75 | + //as there are not enough places left |
| 76 | + @Test |
| 77 | + void testNotEnoughPlacesLeft() { |
| 78 | + reservationRepository = mock(ReservationRepository.class); |
| 79 | + tripRepository = mock(TripRepository.class); |
| 80 | + |
| 81 | + softwhere = new SoftWhere(tripRepository, reservationRepository); |
| 82 | + |
| 83 | + person3 = new Person("fname3", "mname3", "lname3", LocalDate.of(1998, 11, 17)); |
| 84 | + person4 = new Person("fname4", "mname4", "lname4", LocalDate.of(1995, 11, 17)); |
| 85 | + |
| 86 | + person1 = new Person("fname1", "mname1", "lname1", LocalDate.of(1998, 2, 5)); |
| 87 | + person2 = new Person("fname2", "mname2", "lname2", LocalDate.of(2001, 4, 3)); |
| 88 | + person5 = new Person("fname5", "mname5", "lname5", LocalDate.of(1978, 1, 1)); |
| 89 | + person6 = new Person("fname6", "mname6", "lname6", LocalDate.of(2004, 10, 4)); |
| 90 | + info1 = new Info(LocalDate.of(2021, 6, 24), LocalDate.of(2021, 6, 30), Destination.GREECE); |
| 91 | + trip1 = new Trip("Trip to Greece", info1, 1999, 5); |
| 92 | + res1 = new Reservation(trip1, new ArrayList<>(List.of(person1, person2))); |
| 93 | + res2 = new Reservation(trip1, new ArrayList<>(List.of(person5, person6))); |
| 94 | + |
| 95 | + when(tripRepository.getTripById(1L)).thenReturn(trip1); |
| 96 | + when(reservationRepository.getAllReservationsByTrip(trip1)).thenReturn(new ArrayList<>(List.of(res1, res2))); |
| 97 | + |
| 98 | + List<Person> people = new ArrayList<>(List.of(person3, person4)); |
| 99 | + |
| 100 | + assertFalse(softwhere.makeReservation(1L, people)); |
| 101 | + |
| 102 | + verify(tripRepository, times(1)).getTripById(1L); |
| 103 | + verify(reservationRepository, times(1)).getAllReservationsByTrip(trip1); |
| 104 | + |
| 105 | + verifyNoMoreInteractions(tripRepository); |
| 106 | + verifyNoMoreInteractions(reservationRepository); |
| 107 | + } |
| 108 | + |
| 109 | + //test when the wanted trip does not exist |
| 110 | + @Test |
| 111 | + void testNonexistentTrip() { |
| 112 | + reservationRepository = mock(ReservationRepository.class); |
| 113 | + tripRepository = mock(TripRepository.class); |
| 114 | + |
| 115 | + softwhere = new SoftWhere(tripRepository, reservationRepository); |
| 116 | + |
| 117 | + person3 = new Person("fname3", "mname3", "lname3", LocalDate.of(1998, 11, 17)); |
| 118 | + |
| 119 | + when(tripRepository.getTripById(4L)).thenThrow(new ElementNotFoundException()); |
| 120 | + |
| 121 | + List<Person> people = new ArrayList<>(List.of(person3)); |
| 122 | + |
| 123 | + assertFalse(softwhere.makeReservation(4L, people)); |
| 124 | + |
| 125 | + verify(tripRepository, times(1)).getTripById(4L); |
| 126 | + |
| 127 | + verifyNoMoreInteractions(tripRepository); |
| 128 | + verifyNoMoreInteractions(reservationRepository); |
| 129 | + } |
| 130 | +} |
| 131 | + |
0 commit comments