From 0fd8d966d9cfb1d247832ab290a669a04a7daf2b Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Wed, 28 Jan 2026 00:11:12 +0100 Subject: [PATCH 1/9] updated version of junit, assertj, mockito to latest version --- pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 543c1aa50..7d8ef717a 100644 --- a/pom.xml +++ b/pom.xml @@ -16,19 +16,19 @@ org.junit.jupiter junit-jupiter - 5.11.4 + 6.0.1 test org.assertj assertj-core - 3.27.3 + 3.27.6 test org.mockito mockito-junit-jupiter - 5.15.2 + 5.21.0 test From 85966ae9e8aa92607e95b4416522c98eb61b2fb6 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Sat, 7 Feb 2026 18:43:31 +0100 Subject: [PATCH 2/9] created BookingSystemTest to verify functionality in BookingSystem, added test to verify happy case --- .../java/com/example/BookingSystemTest.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/test/java/com/example/BookingSystemTest.java diff --git a/src/test/java/com/example/BookingSystemTest.java b/src/test/java/com/example/BookingSystemTest.java new file mode 100644 index 000000000..bcf739cc7 --- /dev/null +++ b/src/test/java/com/example/BookingSystemTest.java @@ -0,0 +1,49 @@ +package com.example; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import java.time.LocalDateTime; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +class BookingSystemTest { + + @Mock + private TimeProvider timeProvider; + + @Mock + private RoomRepository roomRepository; + + @Mock + private NotificationService notificationService; + + @Mock + private Room room; + + private BookingSystem bookingSystem; + + @BeforeEach + void preconditions() { + room = new Room("101", "101"); + + Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + Mockito.when(roomRepository.findById("101")).thenReturn(Optional.of(room)); + //Preconditions + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + } + + @Test + void bookingSystemTest() + { + bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); + } + + + +} \ No newline at end of file From 536996d3a5c157d7ddca8b73ed2d5626373bb678 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Sun, 8 Feb 2026 21:30:55 +0100 Subject: [PATCH 3/9] added unhappy testcase if startTime/endTime/room is null added unhappy testcase for startTime is before current time --- .../java/com/example/BookingSystemTest.java | 60 ++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/example/BookingSystemTest.java b/src/test/java/com/example/BookingSystemTest.java index bcf739cc7..e11a9798e 100644 --- a/src/test/java/com/example/BookingSystemTest.java +++ b/src/test/java/com/example/BookingSystemTest.java @@ -1,5 +1,6 @@ package com.example; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -9,8 +10,6 @@ import java.time.LocalDateTime; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.*; - @ExtendWith(MockitoExtension.class) class BookingSystemTest { @@ -28,22 +27,69 @@ class BookingSystemTest { private BookingSystem bookingSystem; - @BeforeEach - void preconditions() { + + @Test + void bookingSystemTestHappyCase() + { + //Preconditions room = new Room("101", "101"); Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); Mockito.when(roomRepository.findById("101")).thenReturn(Optional.of(room)); + + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + //Execution + bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); + } + + @Test + void bookingSystemTestUnhappyCaseStartTimeIsNull(){ + //Preconditions + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + //Execution + Assertions.assertThrows(IllegalArgumentException.class, () -> bookingSystem.bookRoom("101", null, LocalDateTime.now().plusHours(1))); + } + @Test + void bookingSystemTestUnhappyCaseEndTimeIsNull(){ + + //Preconditions + + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + //Execution + Assertions.assertThrows(IllegalArgumentException.class, () -> bookingSystem.bookRoom("101", LocalDateTime.now(), null)); + + } @Test - void bookingSystemTest() - { - bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); + void bookingSystemTestUnhappyCaseRoomIsNull(){ + + //Preconditions + + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + //Execution + Assertions.assertThrows(IllegalArgumentException.class, () -> bookingSystem.bookRoom(null, LocalDateTime.now(), LocalDateTime.now().plusHours(1))); + } + @Test + void bookingSystemTestUnhappyCaseStartTimeIsBeforeCurrentTime() + { + //Preconditions + room = new Room("101", "101"); + + Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + //Execution + Assertions.assertThrows(IllegalArgumentException.class, () ->bookingSystem.bookRoom("101", LocalDateTime.now().minusHours(1), LocalDateTime.now().plusHours(1))); + } } \ No newline at end of file From b2938a378035b9c23d32f7171a1376b23cbc952e Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Sun, 8 Feb 2026 22:04:18 +0100 Subject: [PATCH 4/9] added unhappy testcase for endTime is before startTime --- src/test/java/com/example/BookingSystemTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/com/example/BookingSystemTest.java b/src/test/java/com/example/BookingSystemTest.java index e11a9798e..1161ee39c 100644 --- a/src/test/java/com/example/BookingSystemTest.java +++ b/src/test/java/com/example/BookingSystemTest.java @@ -90,6 +90,21 @@ void bookingSystemTestUnhappyCaseStartTimeIsBeforeCurrentTime() //Execution Assertions.assertThrows(IllegalArgumentException.class, () ->bookingSystem.bookRoom("101", LocalDateTime.now().minusHours(1), LocalDateTime.now().plusHours(1))); } + @Test + void bookingSystemTestUnhappyCaseEndtimeIsBeforeStartTime() + { + //Preconditions + room = new Room("101", "101"); + + Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + + + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + //Execution + Assertions.assertThrows(IllegalArgumentException.class, () ->bookingSystem.bookRoom("101", LocalDateTime.now().plusHours(2), LocalDateTime.now().plusHours(1))); + } + } \ No newline at end of file From f3ff25c8e45f261a33f0adce1ba10360de3ef4da Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Sun, 8 Feb 2026 23:22:37 +0100 Subject: [PATCH 5/9] added unhappy textcase for bookRoom when room is not available and available --- .../java/com/example/BookingSystemTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/test/java/com/example/BookingSystemTest.java b/src/test/java/com/example/BookingSystemTest.java index 1161ee39c..b05090e94 100644 --- a/src/test/java/com/example/BookingSystemTest.java +++ b/src/test/java/com/example/BookingSystemTest.java @@ -10,6 +10,8 @@ import java.time.LocalDateTime; import java.util.Optional; +import static org.mockito.ArgumentMatchers.any; + @ExtendWith(MockitoExtension.class) class BookingSystemTest { @@ -104,7 +106,56 @@ void bookingSystemTestUnhappyCaseEndtimeIsBeforeStartTime() //Execution Assertions.assertThrows(IllegalArgumentException.class, () ->bookingSystem.bookRoom("101", LocalDateTime.now().plusHours(2), LocalDateTime.now().plusHours(1))); } + @Test + void bookingSystemTestUnhappyCaseRoomDoesNotExist() + { + //Preconditions + + Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + Mockito.when(roomRepository.findById("101")).thenReturn(Optional.empty()); + + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + //Execution + Assertions.assertThrows(IllegalArgumentException.class, () ->bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1))); + } + @Test + void bookRoom_roomNotAvailable_returnsFalse_andDoesNotSave() { + + + Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + + Room room = Mockito.mock(Room.class); + Mockito.when(roomRepository.findById("101")).thenReturn(Optional.of(room)); + Mockito.when(room.isAvailable(any(), any())).thenReturn(false); + BookingSystem bookingSystem = + new BookingSystem(timeProvider, roomRepository, notificationService); + boolean result = bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); + + + Assertions.assertFalse(result); + + } + @Test + void bookRoom_roomAvailable_returnsTrue_andSaves() { + + + Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + + Room room = Mockito.mock(Room.class); + Mockito.when(roomRepository.findById("101")).thenReturn(Optional.of(room)); + Mockito.when(room.isAvailable(any(), any())).thenReturn(true); + + BookingSystem bookingSystem = + new BookingSystem(timeProvider, roomRepository, notificationService); + + + boolean result = bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); + + + Assertions.assertTrue(result); + } } \ No newline at end of file From b484b673b352513a7bb40dc0cd941076097e27ee Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 18:21:35 +0100 Subject: [PATCH 6/9] Added try catch on testmethods to verify exception messages --- .../java/com/example/BookingSystemTest.java | 72 ++++++++++++++++--- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/example/BookingSystemTest.java b/src/test/java/com/example/BookingSystemTest.java index b05090e94..43256df98 100644 --- a/src/test/java/com/example/BookingSystemTest.java +++ b/src/test/java/com/example/BookingSystemTest.java @@ -8,6 +8,7 @@ import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; import static org.mockito.ArgumentMatchers.any; @@ -86,11 +87,14 @@ void bookingSystemTestUnhappyCaseStartTimeIsBeforeCurrentTime() Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + try{ + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + bookingSystem.bookRoom("101", LocalDateTime.now().plusHours(-1), LocalDateTime.now().plusHours(2)); + } catch (IllegalArgumentException e) { + Assertions.assertEquals(e.getMessage(), "Kan inte boka tid i dåtid"); + } - bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); - //Execution - Assertions.assertThrows(IllegalArgumentException.class, () ->bookingSystem.bookRoom("101", LocalDateTime.now().minusHours(1), LocalDateTime.now().plusHours(1))); } @Test void bookingSystemTestUnhappyCaseEndtimeIsBeforeStartTime() @@ -100,11 +104,14 @@ void bookingSystemTestUnhappyCaseEndtimeIsBeforeStartTime() Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + try { + bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + bookingSystem.bookRoom("101", LocalDateTime.now().plusHours(2), LocalDateTime.now().plusHours(1)); + //Execution - bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); - - //Execution - Assertions.assertThrows(IllegalArgumentException.class, () ->bookingSystem.bookRoom("101", LocalDateTime.now().plusHours(2), LocalDateTime.now().plusHours(1))); + } catch (IllegalArgumentException e) { + Assertions.assertEquals(e.getMessage(), "Sluttid måste vara efter starttid"); + } } @Test void bookingSystemTestUnhappyCaseRoomDoesNotExist() @@ -136,7 +143,7 @@ void bookRoom_roomNotAvailable_returnsFalse_andDoesNotSave() { boolean result = bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); - Assertions.assertFalse(result); + Assertions.assertFalse(true); } @Test @@ -149,13 +156,56 @@ void bookRoom_roomAvailable_returnsTrue_andSaves() { Mockito.when(roomRepository.findById("101")).thenReturn(Optional.of(room)); Mockito.when(room.isAvailable(any(), any())).thenReturn(true); - BookingSystem bookingSystem = - new BookingSystem(timeProvider, roomRepository, notificationService); + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); boolean result = bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); - Assertions.assertTrue(result); + Assertions.assertFalse(result); + } + @Test + void getAvailableRooms_startTimeIsNull_throwsException() { + try { + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + bookingSystem.getAvailableRooms(null, LocalDateTime.now().plusHours(1)); + }catch (IllegalArgumentException e) { + Assertions.assertEquals(e.getMessage(), "Måste ange både start- och sluttid"); + } + } + @Test + void getAvailableRooms_endTimeIsNull_throwsException() { + try { + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + bookingSystem.getAvailableRooms(LocalDateTime.now(), null); + }catch (IllegalArgumentException e) { + Assertions.assertEquals(e.getMessage(), "Måste ange både start- och sluttid"); + } + } + @Test + void getAvailableRooms_endBeforeStart_throwsException() { + try { + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + bookingSystem.getAvailableRooms(LocalDateTime.now().plusHours(1), LocalDateTime.now()); + }catch (IllegalArgumentException e) { + Assertions.assertEquals(e.getMessage(), "Sluttid måste vara efter starttid"); + } + } + @Test + void getAvailableRooms_returnsOnlyRoomsThatAreAvailable() { + + Room availableRoom = Mockito.mock(Room.class); + Room unavailableRoom = Mockito.mock(Room.class); + + Mockito.when(roomRepository.findAll()).thenReturn(List.of(availableRoom, unavailableRoom)); + Mockito.when(availableRoom.isAvailable(LocalDateTime.now(), LocalDateTime.now().plusHours(1))).thenReturn(true); + Mockito.when(unavailableRoom.isAvailable(LocalDateTime.now(), LocalDateTime.now().plusHours(1))).thenReturn(false); + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + List result = bookingSystem.getAvailableRooms(LocalDateTime.now(), LocalDateTime.now().plusHours(1)); + + Assertions.assertTrue(result.contains(availableRoom)); + Assertions.assertFalse(result.contains(unavailableRoom)); + } } \ No newline at end of file From 1a8fbadb6c4111cdc9cb5979674071a58367a8fb Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 19:04:54 +0100 Subject: [PATCH 7/9] added happy testcase for only fetching rooms that are available for getAvailableRooms method --- .../java/com/example/BookingSystemTest.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/example/BookingSystemTest.java b/src/test/java/com/example/BookingSystemTest.java index 43256df98..ba4198fa8 100644 --- a/src/test/java/com/example/BookingSystemTest.java +++ b/src/test/java/com/example/BookingSystemTest.java @@ -36,14 +36,16 @@ void bookingSystemTestHappyCase() { //Preconditions room = new Room("101", "101"); + LocalDateTime start = LocalDateTime.of(2026, 1, 1, 12, 0); + LocalDateTime end = start.plusHours(1); - Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + Mockito.when(timeProvider.getCurrentTime()).thenReturn(start); Mockito.when(roomRepository.findById("101")).thenReturn(Optional.of(room)); bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); //Execution - bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); + bookingSystem.bookRoom("101", start, end); } @Test @@ -54,7 +56,7 @@ void bookingSystemTestUnhappyCaseStartTimeIsNull(){ bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); //Execution - Assertions.assertThrows(IllegalArgumentException.class, () -> bookingSystem.bookRoom("101", null, LocalDateTime.now().plusHours(1))); + Assertions.assertThrows(IllegalArgumentException.class, () -> bookingSystem.bookRoom("101", null, LocalDateTime.now())); } @Test @@ -82,14 +84,16 @@ void bookingSystemTestUnhappyCaseRoomIsNull(){ @Test void bookingSystemTestUnhappyCaseStartTimeIsBeforeCurrentTime() { + LocalDateTime start = LocalDateTime.of(2026, 1, 1, 12, 0); + LocalDateTime end = start.plusHours(1); //Preconditions room = new Room("101", "101"); - Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + Mockito.when(timeProvider.getCurrentTime()).thenReturn(start); try{ bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); - bookingSystem.bookRoom("101", LocalDateTime.now().plusHours(-1), LocalDateTime.now().plusHours(2)); + bookingSystem.bookRoom("101", start.minusHours(1), end); } catch (IllegalArgumentException e) { Assertions.assertEquals(e.getMessage(), "Kan inte boka tid i dåtid"); } @@ -99,14 +103,16 @@ void bookingSystemTestUnhappyCaseStartTimeIsBeforeCurrentTime() @Test void bookingSystemTestUnhappyCaseEndtimeIsBeforeStartTime() { + LocalDateTime start = LocalDateTime.of(2026, 1, 1, 12, 0); + LocalDateTime end = start.plusHours(-1); //Preconditions room = new Room("101", "101"); - Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); + Mockito.when(timeProvider.getCurrentTime()).thenReturn(start); try { bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); - bookingSystem.bookRoom("101", LocalDateTime.now().plusHours(2), LocalDateTime.now().plusHours(1)); + bookingSystem.bookRoom("101", start, end); //Execution } catch (IllegalArgumentException e) { @@ -143,7 +149,7 @@ void bookRoom_roomNotAvailable_returnsFalse_andDoesNotSave() { boolean result = bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); - Assertions.assertFalse(true); + Assertions.assertFalse(result); } @Test @@ -162,50 +168,59 @@ void bookRoom_roomAvailable_returnsTrue_andSaves() { boolean result = bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1)); - Assertions.assertFalse(result); + Assertions.assertTrue(result); } @Test void getAvailableRooms_startTimeIsNull_throwsException() { + LocalDateTime start = null; + LocalDateTime end = LocalDateTime.of(2026, 1, 1, 12, 0); try { BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); - bookingSystem.getAvailableRooms(null, LocalDateTime.now().plusHours(1)); + bookingSystem.getAvailableRooms(start, end); }catch (IllegalArgumentException e) { Assertions.assertEquals(e.getMessage(), "Måste ange både start- och sluttid"); } } @Test void getAvailableRooms_endTimeIsNull_throwsException() { + LocalDateTime start = LocalDateTime.of(2026, 1, 1, 12, 0); + LocalDateTime end = null; try { BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); - bookingSystem.getAvailableRooms(LocalDateTime.now(), null); + bookingSystem.getAvailableRooms(start, end); }catch (IllegalArgumentException e) { Assertions.assertEquals(e.getMessage(), "Måste ange både start- och sluttid"); } } @Test void getAvailableRooms_endBeforeStart_throwsException() { + LocalDateTime start = LocalDateTime.of(2026, 1, 1, 12, 0); + LocalDateTime end = start.plusHours(-1); try { BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); - bookingSystem.getAvailableRooms(LocalDateTime.now().plusHours(1), LocalDateTime.now()); + bookingSystem.getAvailableRooms(start, end); }catch (IllegalArgumentException e) { Assertions.assertEquals(e.getMessage(), "Sluttid måste vara efter starttid"); } } @Test void getAvailableRooms_returnsOnlyRoomsThatAreAvailable() { + LocalDateTime start = LocalDateTime.of(2026, 1, 1, 12, 0); + LocalDateTime end = start.plusHours(1); Room availableRoom = Mockito.mock(Room.class); Room unavailableRoom = Mockito.mock(Room.class); Mockito.when(roomRepository.findAll()).thenReturn(List.of(availableRoom, unavailableRoom)); - Mockito.when(availableRoom.isAvailable(LocalDateTime.now(), LocalDateTime.now().plusHours(1))).thenReturn(true); - Mockito.when(unavailableRoom.isAvailable(LocalDateTime.now(), LocalDateTime.now().plusHours(1))).thenReturn(false); + + Mockito.when(availableRoom.isAvailable(start, end)).thenReturn(true); + Mockito.when(unavailableRoom.isAvailable(start, end)).thenReturn(false); + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); - List result = bookingSystem.getAvailableRooms(LocalDateTime.now(), LocalDateTime.now().plusHours(1)); + List result = bookingSystem.getAvailableRooms(start, end); Assertions.assertTrue(result.contains(availableRoom)); Assertions.assertFalse(result.contains(unavailableRoom)); - } } \ No newline at end of file From 9353eebeb1b980988a6d1fb6d996331afc52e45b Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 19:56:06 +0100 Subject: [PATCH 8/9] added cancelBooking testcases bookingIdIsNull + bookingNotFound --- .../java/com/example/BookingSystemTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/com/example/BookingSystemTest.java b/src/test/java/com/example/BookingSystemTest.java index ba4198fa8..2d8b45278 100644 --- a/src/test/java/com/example/BookingSystemTest.java +++ b/src/test/java/com/example/BookingSystemTest.java @@ -223,4 +223,28 @@ void getAvailableRooms_returnsOnlyRoomsThatAreAvailable() { Assertions.assertTrue(result.contains(availableRoom)); Assertions.assertFalse(result.contains(unavailableRoom)); } + @Test + void cancelBooking_bookingIdIsNull_throwsException() { + + try { + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + bookingSystem.cancelBooking(null); + }catch (IllegalArgumentException e) { + Assertions.assertEquals(e.getMessage(), "Boknings-id kan inte vara null"); + } + } + @Test + void cancelBooking_bookingNotFound_returnsFalse() { + + Room room = Mockito.mock(Room.class); + Mockito.when(room.hasBooking("1")).thenReturn(false); + Mockito.when(roomRepository.findAll()).thenReturn(List.of(room)); + + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + boolean result = bookingSystem.cancelBooking("1"); + + Assertions.assertFalse(result); + } + } \ No newline at end of file From 0334b4837627a1fbd349f9987a452c20ac367819 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 20:17:22 +0100 Subject: [PATCH 9/9] added test case for booking already started + succesful cancellation --- .../java/com/example/BookingSystemTest.java | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/example/BookingSystemTest.java b/src/test/java/com/example/BookingSystemTest.java index 2d8b45278..c28e209ee 100644 --- a/src/test/java/com/example/BookingSystemTest.java +++ b/src/test/java/com/example/BookingSystemTest.java @@ -133,7 +133,7 @@ void bookingSystemTestUnhappyCaseRoomDoesNotExist() Assertions.assertThrows(IllegalArgumentException.class, () ->bookingSystem.bookRoom("101", LocalDateTime.now(), LocalDateTime.now().plusHours(1))); } @Test - void bookRoom_roomNotAvailable_returnsFalse_andDoesNotSave() { + void bookRoom_roomNotAvailable_returnsFalse() { Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); @@ -153,7 +153,7 @@ void bookRoom_roomNotAvailable_returnsFalse_andDoesNotSave() { } @Test - void bookRoom_roomAvailable_returnsTrue_andSaves() { + void bookRoom_roomAvailable_returnsTrue() { Mockito.when(timeProvider.getCurrentTime()).thenReturn(LocalDateTime.now()); @@ -243,8 +243,47 @@ void cancelBooking_bookingNotFound_returnsFalse() { BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); boolean result = bookingSystem.cancelBooking("1"); - + Assertions.assertFalse(result); } + @Test + void cancelBooking_bookingAlreadyStarted_throwsException(){ + LocalDateTime start = LocalDateTime.of(2026, 1, 1, 12, 0); + Mockito.when(timeProvider.getCurrentTime()).thenReturn(start); + Room room = Mockito.mock(Room.class); + Booking booking = Mockito.mock(Booking.class); + Mockito.when(roomRepository.findAll()).thenReturn(List.of(room)); + Mockito.when(room.hasBooking("1")).thenReturn(true); + Mockito.when(room.getBooking("1")).thenReturn(booking); + Mockito.when(booking.getStartTime()).thenReturn(start.plusHours(-1)); + + try { + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + bookingSystem.cancelBooking("1"); + }catch (IllegalStateException e) { + Assertions.assertEquals(e.getMessage(), "Kan inte avboka påbörjad eller avslutad bokning"); + } + + } + @Test + void cancelBooking_success_returnsTrue() { + // Arrange + LocalDateTime start = LocalDateTime.of(2026, 1, 1, 12, 0); + Mockito.when(timeProvider.getCurrentTime()).thenReturn(start); + Room room = Mockito.mock(Room.class); + Booking booking = Mockito.mock(Booking.class); + + Mockito.when(roomRepository.findAll()).thenReturn(List.of(room)); + Mockito.when(room.hasBooking("1")).thenReturn(true); + Mockito.when(room.getBooking("1")).thenReturn(booking); + Mockito.when(booking.getStartTime()).thenReturn(start.plusHours(1)); + + BookingSystem bookingSystem = new BookingSystem(timeProvider, roomRepository, notificationService); + + boolean result = bookingSystem.cancelBooking("1"); + + Assertions.assertTrue(result); + } + } \ No newline at end of file