From 37af69c7a7db90a2af0682bfabd075dcf7e79cd4 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Thu, 21 May 2020 23:32:48 +0200 Subject: [PATCH] Started tests on UserAccountController --- .../controller/UserAccountController.java | 6 +-- .../sanmarinoes/smarthut/models/Switch.java | 1 + .../smarthut/models/Switchable.java | 2 +- .../UserAccountControllerTests.java | 45 +++++++++++++++++++ 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountControllerTests.java 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 b1648ed..f458f62 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 @@ -101,11 +101,7 @@ public class UserAccountController { toSave.setEmail(registrationData.getEmail()); userRepository.save(toSave); - ConfirmationToken token; - do { - token = new ConfirmationToken(toSave); - } while (confirmationTokenRepository.findByConfirmToken(token.getConfirmToken()) - != null); + ConfirmationToken token = new ConfirmationToken(toSave); confirmationTokenRepository.save(token); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index d67bfad..ebc0113 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -21,6 +21,7 @@ public class Switch extends InputDevice implements BooleanTriggerable, Connectab }) @GsonExclude @SocketGsonExclude + @EqualsAndHashCode.Exclude @JoinTable( name = "switch_switchable", joinColumns = @JoinColumn(name = "switch_id"), diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java index b327cf8..d976758 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java @@ -9,7 +9,6 @@ import lombok.EqualsAndHashCode; /** A device that can be turned either on or off */ @Entity -@EqualsAndHashCode(callSuper = true) @Inheritance(strategy = InheritanceType.JOINED) public abstract class Switchable extends OutputDevice { @@ -23,6 +22,7 @@ public abstract class Switchable extends OutputDevice { }) @GsonExclude @SocketGsonExclude + @EqualsAndHashCode.Exclude private Set inputs = new HashSet<>(); protected Switchable(String kind) { 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 new file mode 100644 index 0000000..eeb11c7 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountControllerTests.java @@ -0,0 +1,45 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.mockito.Mockito.when; + +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.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; +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.security.test.context.support.WithMockUser; + +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class UserAccountControllerTests { + + @InjectMocks private UserAccountController userAccountController; + + @Mock private UserRepository userRepository; + + @Test + public void testRegisterUser() { + final UserRegistrationRequest registrationRequest = new UserRegistrationRequest(); + registrationRequest.setEmail("info@theshell.ch"); + registrationRequest.setUsername("username"); + + when(userRepository.findByEmailIgnoreCase("info@theshell.ch")).thenReturn(null); + when(userRepository.findByEmailIgnoreCase("info@vimtok.com")).thenReturn(new User()); + when(userRepository.findByUsername("username")).thenReturn(new User()); + when(userRepository.findByUsername("simoneriva")).thenReturn(null); + + Assertions.assertThatThrownBy(() -> userAccountController.registerUser(registrationRequest)) + .isInstanceOf(DuplicateRegistrationException.class); + + registrationRequest.setUsername("simoneriva"); + registrationRequest.setEmail("info@vimtok.com"); + + Assertions.assertThatThrownBy(() -> userAccountController.registerUser(registrationRequest)) + .isInstanceOf(DuplicateRegistrationException.class); + } +}