diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java index 9bc0753..f172f9a 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java @@ -14,7 +14,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.security.test.context.support.WithMockUser; @@ -43,60 +42,55 @@ public class GuestControllerTests { @Test public void findAllEmptyTest() { when(mockPrincipal.getName()).thenReturn("user"); - Mockito.doReturn(List.of()).when(userRepository.findByUsername("user")); + when(userRepository.findByUsername("user")).thenReturn(user); List l = guestController.findAll(); assertThat(l.isEmpty()); } - @DisplayName("Check that the list contains the elements added") + @DisplayName( + "Check that the list contains the elements added and that hosts and guests are returned properly") @Test public void findAllTest() { - User host = new User(); - User guest = new User(); + User host1 = new User(); + User host2 = new User(); + User guest1 = new User(); + User guest2 = new User(); when(mockPrincipal.getName()).thenReturn("user"); when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user); - Mockito.doReturn(List.of(host, guest)).when(userRepository.findByUsername("user")); - assertThat(guestController.findAll()).isSameAs(List.of(host, guest)); + when(userRepository.findAll()).thenReturn(List.of(host1, host2, guest1, guest2)); + assertThat(guestController.findAll()) + .isSameAs( + List.of( + UserResponse.fromUser(host1), + UserResponse.fromUser(host2), + UserResponse.fromUser(guest1), + UserResponse.fromUser(guest2))); + + when(guestController.findHosts(mockPrincipal)) + .thenReturn(List.of(UserResponse.fromUser(host1), UserResponse.fromUser(host2))); + assertThat(guestController.findHosts(mockPrincipal)) + .isSameAs(List.of(UserResponse.fromUser(host1), UserResponse.fromUser(host2))); + + when(guestController.findGuests(mockPrincipal)) + .thenReturn(List.of(UserResponse.fromUser(guest1), UserResponse.fromUser(guest2))); + assertThat(guestController.findGuests(mockPrincipal)) + .isSameAs(List.of(UserResponse.fromUser(guest1), UserResponse.fromUser(guest2))); } @DisplayName("Check that the host list is empty") @Test public void findHostsEmptyTest() { when(mockPrincipal.getName()).thenReturn("user"); - Mockito.doReturn(List.of()).when(userRepository.findByUsername("user")); + when(userRepository.findByUsername("user")).thenReturn(user); assertThat(guestController.findHosts(mockPrincipal).isEmpty()); } - @DisplayName("Check that the host list contains the hosts") - @Test - public void findHostsTest() { - User host1 = new User(); - User host2 = new User(); - - when(mockPrincipal.getName()).thenReturn("user"); - when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user); - Mockito.doReturn(List.of(host1, host2)).when(userRepository.findByUsername("user")); - assertThat(guestController.findHosts(mockPrincipal)).isSameAs(List.of(host1, host2)); - } - @DisplayName("Check that the guest list is empty") @Test public void findGuestsEmptyTest() { when(mockPrincipal.getName()).thenReturn("user"); - Mockito.doReturn(List.of()).when(userRepository.findByUsername("user")); + when(userRepository.findByUsername("user")).thenReturn(user); assertThat(guestController.findGuests(mockPrincipal).isEmpty()); } - - @DisplayName("Check that the guest list contains the guests") - @Test - public void findGuestsTest() { - User guest1 = new User(); - User guest2 = new User(); - - when(mockPrincipal.getName()).thenReturn("user"); - when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user); - Mockito.doReturn(List.of(guest1, guest2)).when(userRepository.findByUsername("user")); - assertThat(guestController.findGuests(mockPrincipal)).isSameAs(List.of(guest1, guest2)); - } }