Merge branch '81-guestcontroller-test' into 'dev'
Resolve "GuestController test" Closes #81 See merge request sa4-2020/the-sanmarinoes/backend!161
This commit is contained in:
commit
1dc37cca4b
1 changed files with 92 additions and 0 deletions
|
@ -0,0 +1,92 @@
|
||||||
|
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 java.util.stream.Collectors;
|
||||||
|
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.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName("Check that the list is empty when we have no hosts/guests")
|
||||||
|
@Test
|
||||||
|
public void findAllEmptyTest() {
|
||||||
|
List<UserResponse> l = guestController.findAll();
|
||||||
|
assertThat(l.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName(
|
||||||
|
"Check that the list contains the elements added and that hosts and guests are returned properly")
|
||||||
|
@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);
|
||||||
|
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())
|
||||||
|
.containsAll(
|
||||||
|
List.of(host1, host2, guest1, guest2)
|
||||||
|
.stream()
|
||||||
|
.map(UserResponse::fromUser)
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
|
||||||
|
assertThat(guestController.findHosts(mockPrincipal))
|
||||||
|
.containsExactly(UserResponse.fromUser(host1), UserResponse.fromUser(host2));
|
||||||
|
assertThat(guestController.findGuests(mockPrincipal))
|
||||||
|
.containsExactly(UserResponse.fromUser(guest1), UserResponse.fromUser(guest2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName("Check that the host list is empty")
|
||||||
|
@Test
|
||||||
|
public void findHostsEmptyTest() {
|
||||||
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
|
when(userRepository.findByUsername("user")).thenReturn(user);
|
||||||
|
assertThat(guestController.findHosts(mockPrincipal).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName("Check that the guest list is empty")
|
||||||
|
@Test
|
||||||
|
public void findGuestsEmptyTest() {
|
||||||
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
|
when(userRepository.findByUsername("user")).thenReturn(user);
|
||||||
|
assertThat(guestController.findGuests(mockPrincipal).isEmpty());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue