From 420fed7f88ea1ba8a6aa9a7520f3133b45a95832 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 22 May 2020 13:18:30 +0200 Subject: [PATCH 1/6] findall and findhosts should be tested in guestcontroller --- .../controller/GuestControllerTests.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java 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 new file mode 100644 index 0000000..e24721a --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java @@ -0,0 +1,81 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserResponse; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.EagerUserRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import java.security.Principal; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +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; + +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class GuestControllerTests { + @InjectMocks private GuestController guestController; + + @Mock private Principal mockPrincipal; + @Mock private EagerUserRepository userRepository; + + private final User user; + + public GuestControllerTests() { + user = new User(); + user.setName("user"); + user.setId(12L); + } + + @BeforeEach + public void setup() { + guestController = new GuestController(); + } + + @DisplayName("Check that the list is empty when we have no hosts/guests") + @Test + public void findAllEmptyTest() { + when(mockPrincipal.getName()).thenReturn("user"); + Mockito.doReturn(List.of()).when(userRepository.findByUsername("user")); + List l = guestController.findAll(); + assertThat(l.isEmpty()); + } + + @DisplayName("Check that the list contains the elements added") + @Test + public void findAllTest() { + User host = new User(); + User guest = 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)); + } + + @DisplayName("Check that we get empty host list") + @Test + public void findHostsEmptyTest() { + when(mockPrincipal.getName()).thenReturn("user2"); + Mockito.doReturn(List.of()).when(userRepository.findByUsername("user2")); + 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)); + } +} From 67b2c3ae4f1fde8ea7c097f8fa46c8ac1d0c681f Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 22 May 2020 13:23:14 +0200 Subject: [PATCH 2/6] findguests should be tested and imports should be fixed --- .../controller/GuestControllerTests.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) 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 e24721a..9bc0753 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 @@ -16,6 +16,7 @@ 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; @ExtendWith(MockitoExtension.class) @WithMockUser(username = "user") @@ -59,11 +60,11 @@ public class GuestControllerTests { assertThat(guestController.findAll()).isSameAs(List.of(host, guest)); } - @DisplayName("Check that we get empty host list") + @DisplayName("Check that the host list is empty") @Test public void findHostsEmptyTest() { - when(mockPrincipal.getName()).thenReturn("user2"); - Mockito.doReturn(List.of()).when(userRepository.findByUsername("user2")); + when(mockPrincipal.getName()).thenReturn("user"); + Mockito.doReturn(List.of()).when(userRepository.findByUsername("user")); assertThat(guestController.findHosts(mockPrincipal).isEmpty()); } @@ -78,4 +79,24 @@ public class GuestControllerTests { 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")); + 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)); + } } From 64007d1b5ce8d41db4ded5828e4979797cb35026 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 22 May 2020 14:29:17 +0200 Subject: [PATCH 3/6] tests done so far should be fixed --- .../controller/GuestControllerTests.java | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) 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)); - } } From b1343bbd401058deb8b52f62e6562613eb29beac Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 22 May 2020 14:35:36 +0200 Subject: [PATCH 4/6] trying to fix nullpointerexception --- .../smarthut/controller/GuestControllerTests.java | 6 ------ 1 file changed, 6 deletions(-) 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 f172f9a..8619374 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 @@ -8,7 +8,6 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.EagerUserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import java.security.Principal; import java.util.List; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -33,11 +32,6 @@ public class GuestControllerTests { user.setId(12L); } - @BeforeEach - public void setup() { - guestController = new GuestController(); - } - @DisplayName("Check that the list is empty when we have no hosts/guests") @Test public void findAllEmptyTest() { From 5c2ee4c55f0be96ccd6445a791232bb2934ed95a Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 22 May 2020 14:44:59 +0200 Subject: [PATCH 5/6] assertion error should be fixed --- .../smarthut/controller/GuestControllerTests.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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 8619374..c116667 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 @@ -50,16 +50,15 @@ public class GuestControllerTests { User guest1 = new User(); User guest2 = new User(); + user.addHost(host1); + user.addHost(host2); + user.addGuest(guest1); + user.addGuest(guest2); + when(mockPrincipal.getName()).thenReturn("user"); when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user); 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))); + assertThat(guestController.findAll()).isSameAs(List.of(host1, host2, guest1, guest2)); when(guestController.findHosts(mockPrincipal)) .thenReturn(List.of(UserResponse.fromUser(host1), UserResponse.fromUser(host2))); From 6abedac3ccbe9e6b3e532be73f996f5d63de958d Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 23 May 2020 17:50:39 +0200 Subject: [PATCH 6/6] It wasn't, but now it is --- .../controller/GuestControllerTests.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) 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 c116667..26af2b6 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 @@ -8,6 +8,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.EagerUserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import java.security.Principal; import java.util.List; +import java.util.stream.Collectors; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -35,8 +36,6 @@ public class GuestControllerTests { @DisplayName("Check that the list is empty when we have no hosts/guests") @Test public void findAllEmptyTest() { - when(mockPrincipal.getName()).thenReturn("user"); - when(userRepository.findByUsername("user")).thenReturn(user); List l = guestController.findAll(); assertThat(l.isEmpty()); } @@ -46,9 +45,13 @@ public class GuestControllerTests { @Test public void findAllTest() { User host1 = new User(); + host1.setId(2L); User host2 = new User(); + host2.setId(3L); User guest1 = new User(); + guest1.setId(4L); User guest2 = new User(); + guest2.setId(5L); user.addHost(host1); user.addHost(host2); @@ -58,17 +61,17 @@ public class GuestControllerTests { when(mockPrincipal.getName()).thenReturn("user"); when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user); when(userRepository.findAll()).thenReturn(List.of(host1, host2, guest1, guest2)); - assertThat(guestController.findAll()).isSameAs(List.of(host1, host2, guest1, guest2)); + assertThat(guestController.findAll()) + .containsAll( + List.of(host1, host2, guest1, guest2) + .stream() + .map(UserResponse::fromUser) + .collect(Collectors.toList())); - 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))); + .containsExactly(UserResponse.fromUser(host1), UserResponse.fromUser(host2)); assertThat(guestController.findGuests(mockPrincipal)) - .isSameAs(List.of(UserResponse.fromUser(guest1), UserResponse.fromUser(guest2))); + .containsExactly(UserResponse.fromUser(guest1), UserResponse.fromUser(guest2)); } @DisplayName("Check that the host list is empty")