Started tests on UserAccountController

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-21 23:32:48 +02:00
parent 1a289f6800
commit 37af69c7a7
4 changed files with 48 additions and 6 deletions

View file

@ -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);

View file

@ -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"),

View file

@ -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<Switch> inputs = new HashSet<>();
protected Switchable(String kind) {

View file

@ -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);
}
}