From 420fed7f88ea1ba8a6aa9a7520f3133b45a95832 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 22 May 2020 13:18:30 +0200 Subject: [PATCH 1/9] 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/9] 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/9] 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/9] 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/9] 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 84767b1793d47810ddc5f4d8be1bc78ea4bb39a9 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 23 May 2020 14:53:10 +0200 Subject: [PATCH 6/9] Tests on UserAccountController --- .../controller/ButtonDimmerController.java | 4 +- .../controller/UserAccountController.java | 7 +- .../dto/InitPasswordResetRequest.java | 4 + .../smarthut/dto/PasswordResetRequest.java | 4 + .../UserAccountControllerTests.java | 120 +++++++++++++++++- 5 files changed, 131 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 5e3966c..330c31a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -21,8 +21,8 @@ import org.springframework.web.bind.annotation.*; public class ButtonDimmerController extends InputDeviceConnectionController { - private DeviceService deviceService; - private ButtonDimmerRepository buttonDimmerRepository; + private final DeviceService deviceService; + private final ButtonDimmerRepository buttonDimmerRepository; @Autowired protected ButtonDimmerController( diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java index f458f62..a0fe8d1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java @@ -126,11 +126,8 @@ public class UserAccountController { throw new UserNotFoundException(); } - ConfirmationToken token; - do { - token = new ConfirmationToken(toReset); - token.setResetPassword(true); - } while (confirmationTokenRepository.findByConfirmToken(token.getConfirmToken()) != null); + ConfirmationToken token = new ConfirmationToken(toReset); + token.setResetPassword(true); // Delete existing email password reset tokens confirmationTokenRepository.deleteByUserAndResetPassword(toReset, true); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java index dca0d79..4148e18 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java @@ -3,10 +3,14 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; import javax.validation.constraints.Email; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.Pattern; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; /** DTO for password reset request */ @Data +@AllArgsConstructor +@NoArgsConstructor public class InitPasswordResetRequest { /** * The user's email (validated according to criteria used in >input type="email"<> diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java index 3d9ad7e..e533c49 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java @@ -3,10 +3,14 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; /** DTO for password reset request */ @Data +@NoArgsConstructor +@AllArgsConstructor public class PasswordResetRequest { @NotNull private String confirmationToken; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountControllerTests.java index eeb11c7..3269550 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountControllerTests.java @@ -1,17 +1,33 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; -import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.EmailConfigurationService; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.InitPasswordResetRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.PasswordResetRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserRegistrationRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateRegistrationException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.EmailTokenNotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationToken; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.EmailSenderService; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.test.context.support.WithMockUser; @ExtendWith(MockitoExtension.class) @@ -22,6 +38,14 @@ public class UserAccountControllerTests { @Mock private UserRepository userRepository; + @Mock private EmailSenderService emailSenderService; + + @Mock private EmailConfigurationService emailConfigurationService; + + @Mock private ConfirmationTokenRepository confirmationTokenRepository; + + @Mock private BCryptPasswordEncoder passwordEncoder; + @Test public void testRegisterUser() { final UserRegistrationRequest registrationRequest = new UserRegistrationRequest(); @@ -42,4 +66,98 @@ public class UserAccountControllerTests { Assertions.assertThatThrownBy(() -> userAccountController.registerUser(registrationRequest)) .isInstanceOf(DuplicateRegistrationException.class); } + + @Test + public void testResetPassword() throws EmailTokenNotFoundException { + final User u = new User(); + final ConfirmationToken c = new ConfirmationToken(u); + c.setResetPassword(false); + + when(userRepository.save(u)).thenReturn(u); + + when(confirmationTokenRepository.findByConfirmToken("token")).thenReturn(c); + when(confirmationTokenRepository.findByConfirmToken("token2")).thenReturn(null); + doNothing().when(confirmationTokenRepository).delete(c); + when(passwordEncoder.encode("password")).thenReturn("encoded"); + + assertThatThrownBy( + () -> + userAccountController.resetPassword( + new PasswordResetRequest("token2", "password"))) + .isInstanceOf(EmailTokenNotFoundException.class); + + assertThatThrownBy( + () -> + userAccountController.resetPassword( + new PasswordResetRequest("token", "password"))) + .isInstanceOf(EmailTokenNotFoundException.class); + + c.setResetPassword(true); + + userAccountController.resetPassword(new PasswordResetRequest("token", "password")); + + assertThat(u.getPassword()).isEqualTo("encoded"); + } + + @Test + public void testInitResetPassword() throws UserNotFoundException { + when(emailConfigurationService.getResetPasswordSubject()).thenReturn("password reset"); + when(emailConfigurationService.getResetPassword()).thenReturn("reset-password"); + when(emailConfigurationService.getResetPasswordPath()).thenReturn("path"); + + final User u = new User(); + u.setEmail("info@theshell.com"); + + boolean[] done = new boolean[1]; + + doAnswer( + i -> { + final SimpleMailMessage m = i.getArgument(0); + assertThat(m.getSubject()).isEqualTo("password reset"); + assertThat(m.getText()).startsWith("reset-password path"); + done[0] = true; + return null; + }) + .when(emailSenderService) + .sendEmail(any()); + + doNothing().when(confirmationTokenRepository).deleteByUserAndResetPassword(u, true); + when(confirmationTokenRepository.save(any())).thenAnswer(i -> i.getArgument(0)); + + when(userRepository.findByEmailIgnoreCase("info@theshell.ch")).thenReturn(u); + when(userRepository.findByEmailIgnoreCase("info@vimtok.com")).thenReturn(null); + + assertThatThrownBy( + () -> + userAccountController.initResetPassword( + new InitPasswordResetRequest("info@vimtok.com"))) + .isInstanceOf(UserNotFoundException.class); + userAccountController.initResetPassword(new InitPasswordResetRequest("info@theshell.ch")); + + assertThat(done[0]).isTrue(); + } + + @Test + public void testConfirmUserAccount() throws EmailTokenNotFoundException, IOException { + final User u = new User(); + final ConfirmationToken c = new ConfirmationToken(u); + c.setResetPassword(true); + + when(confirmationTokenRepository.findByConfirmToken("token")).thenReturn(null); + when(confirmationTokenRepository.findByConfirmToken(c.getConfirmToken())).thenReturn(c); + when(userRepository.save(u)).thenReturn(u); + final HttpServletResponse r = new MockHttpServletResponse(); + + assertThatThrownBy(() -> userAccountController.confirmUserAccount("token", r)) + .isInstanceOf(EmailTokenNotFoundException.class); + assertThatThrownBy(() -> userAccountController.confirmUserAccount(c.getConfirmToken(), r)) + .isInstanceOf(EmailTokenNotFoundException.class); + + c.setResetPassword(false); + + when(emailConfigurationService.getRegistrationRedirect()).thenReturn("malusa.html"); + + userAccountController.confirmUserAccount(c.getConfirmToken(), r); + assertThat(u.isEnabled()).isTrue(); + } } From 733b4639fa334f14e347273c589bc80414cfd306 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 23 May 2020 15:18:48 +0200 Subject: [PATCH 7/9] Tests on ThermostatService --- .../smarthut/service/ThermostatService.java | 20 ++++-- .../service/ThermostatServiceTests.java | 66 +++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatServiceTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java index 5eeec6c..9065826 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java @@ -14,13 +14,25 @@ import org.springframework.stereotype.Component; @Component public class ThermostatService { - @Autowired private SensorSocketEndpoint endpoint; + private final SensorSocketEndpoint endpoint; - @Autowired private DeviceService deviceService; + private final DeviceService deviceService; - @Autowired private ThermostatPopulationService thermostatPopulationService; + private final ThermostatPopulationService thermostatPopulationService; - @Autowired private ThermostatRepository thermostatRepository; + private final ThermostatRepository thermostatRepository; + + @Autowired + public ThermostatService( + SensorSocketEndpoint endpoint, + DeviceService deviceService, + ThermostatPopulationService thermostatPopulationService, + ThermostatRepository thermostatRepository) { + this.endpoint = endpoint; + this.deviceService = deviceService; + this.thermostatPopulationService = thermostatPopulationService; + this.thermostatRepository = thermostatRepository; + } private void randomJitter(Thermostat thermostat) { updateValueForThermostat( diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatServiceTests.java new file mode 100644 index 0000000..05a0447 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatServiceTests.java @@ -0,0 +1,66 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; +import java.math.BigDecimal; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class ThermostatServiceTests { + + @InjectMocks private ThermostatService thermostatService; + + @Mock private ThermostatRepository thermostatRepository; + + @Mock private DeviceService deviceService; + + @Mock private ThermostatPopulationService thermostatPopulationService; + + @Mock private SensorSocketEndpoint endpoint; + + @Test + public void testFakeUpdateAll() { + when(deviceService.saveAsOwner(any(), any())).thenAnswer(i -> i.getArgument(0)); + when(thermostatRepository.findUser(1L)).thenReturn(new User()); + + Thermostat t = new Thermostat(); + t.setOn(true); + t.setId(1L); + t.setTargetTemperature(BigDecimal.valueOf(17.0)); + + when(thermostatRepository.findAll()).thenReturn(List.of(t)); + doAnswer( + i -> { + ((Thermostat) i.getArgument(0)) + .setMeasuredTemperature(BigDecimal.valueOf(18.0)); + return null; + }) + .when(thermostatPopulationService) + .populateMeasuredTemperature(t); + + doNothing().when(endpoint).queueDeviceUpdate(any(), any(), eq(false), eq(null), eq(false)); + + Assertions.assertDoesNotThrow(() -> thermostatService.fakeUpdateAll()); + assertThat(t.getMode()).isNotEqualTo(Thermostat.Mode.OFF); + } + + @Test + public void testFindAll() { + final Thermostat t = new Thermostat(); + doNothing().when(thermostatPopulationService).populateMeasuredTemperature(t); + when(thermostatRepository.findAllByUsername("user")).thenReturn(List.of(t)); + assertThat(thermostatService.findAll("user")).containsExactly(t); + } +} From a8c76c03e0cafabd2f2317c1ea4af9da5eccb767 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 23 May 2020 15:58:09 +0200 Subject: [PATCH 8/9] Tests on SceneController --- .../controller/SceneControllerTests.java | 78 ++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java index 33f7331..0134642 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.when; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SceneService; import java.security.Principal; import java.util.List; import java.util.Optional; @@ -33,12 +34,72 @@ public class SceneControllerTests { @Mock private StateRepository stateStateRepository; + @Mock private SceneService sceneService; + private final User u; + private final User h; + public SceneControllerTests() { u = new User(); u.setName("user"); u.setId(1L); + + h = new User(); + h.setId(2L); + h.setUsername("host"); + + u.getHosts().add(h); + h.getGuests().add(u); + } + + @Test + public void testCopy() throws NotFoundException { + final Scene scene = new Scene(); + final Scene copyFrom = new Scene(); + + when(mockPrincipal.getName()).thenReturn("user"); + when(sceneRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(scene)); + when(sceneRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty()); + + when(sceneRepository.findByIdAndUsername(10L, "user")).thenReturn(Optional.of(copyFrom)); + when(sceneRepository.findByIdAndUsername(20L, "user")).thenReturn(Optional.empty()); + + assertThatThrownBy(() -> sceneController.copy(2L, 10L, mockPrincipal)) + .isInstanceOf(NotFoundException.class); + assertThatThrownBy(() -> sceneController.copy(1L, 20L, mockPrincipal)) + .isInstanceOf(NotFoundException.class); + + when(sceneService.copyStates(scene, copyFrom)).thenReturn(List.of()); + + assertThat(sceneController.copy(1L, 10L, mockPrincipal)).isEmpty(); + } + + @Test + public void testApply() throws NotFoundException { + final Scene s = new Scene(); + when(mockPrincipal.getName()).thenReturn("user"); + when(sceneRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(s)); + when(sceneRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty()); + when(sceneService.apply(s, "user", false)).thenReturn(List.of()); + + assertThatThrownBy(() -> sceneController.apply(2L, mockPrincipal, null)) + .isInstanceOf(NotFoundException.class); + assertThat(sceneController.apply(1L, mockPrincipal, null)).isEmpty(); + + when(userRepository.findByUsername("user")).thenReturn(u); + when(userRepository.findById(2L)).thenReturn(Optional.of(h)); + + when(sceneRepository.findByIdAndUserIdAndGuestAccessEnabled(1L, 2L, true)) + .thenReturn(Optional.of(s)); + when(sceneRepository.findByIdAndUserIdAndGuestAccessEnabled(2L, 2L, true)) + .thenReturn(Optional.empty()); + + when(sceneService.applyAsGuest(s, "user", 2L)).thenReturn(List.of()); + + assertThatThrownBy(() -> sceneController.apply(2L, mockPrincipal, 2L)) + .isInstanceOf(NotFoundException.class); + assertThat(sceneController.apply(1L, mockPrincipal, 2L)).isEmpty(); } @Test @@ -46,10 +107,14 @@ public class SceneControllerTests { when(mockPrincipal.getName()).thenReturn("user"); when(sceneRepository.findByUsername("user")).thenReturn(List.of()); assertThat(sceneController.findAll(mockPrincipal, null)).isEmpty(); + + when(userRepository.findByUsername("user")).thenReturn(u); + when(userRepository.findById(2L)).thenReturn(Optional.of(h)); + assertThat(sceneController.findAll(mockPrincipal, 2L)).isEmpty(); } private void equalToRequest(Scene created, SceneSaveRequest a) { - assertThat(created.getName()).isEqualTo(a.getName()); + if (a.getName() != null) assertThat(created.getName()).isEqualTo(a.getName()); assertThat(created.getUserId()).isEqualTo(1L); assertThat(created.getIcon()).isEqualTo(a.getIcon()); assertThat(created.isGuestAccessEnabled()).isEqualTo(a.isGuestAccessEnabled()); @@ -85,6 +150,11 @@ public class SceneControllerTests { assertThat(created.getId()).isEqualTo(42L); equalToRequest(created, a); + a.setName(null); + created = sceneController.update(a.getId(), a, mockPrincipal); + assertThat(created.getId()).isEqualTo(42L); + equalToRequest(created, a); + a.setId(43L); assertThatThrownBy(() -> sceneController.update(a.getId(), a, mockPrincipal)) .isInstanceOf(NotFoundException.class); @@ -96,4 +166,10 @@ public class SceneControllerTests { doNothing().when(sceneRepository).deleteById(42L); Assertions.assertDoesNotThrow(() -> sceneController.deleteById(42L)); } + + @Test + public void testGetStates() { + when(stateStateRepository.findBySceneId(1L)).thenReturn(List.of()); + assertThat(sceneController.getStates(1L)).isEmpty(); + } } From 6abedac3ccbe9e6b3e532be73f996f5d63de958d Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 23 May 2020 17:50:39 +0200 Subject: [PATCH 9/9] 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")