findall and findhosts should be tested in guestcontroller
This commit is contained in:
parent
7a8524c15e
commit
420fed7f88
1 changed files with 81 additions and 0 deletions
|
@ -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<UserResponse> 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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue