added a couple of tests to guestcontrollertests

This commit is contained in:
Jacob Salvi 2020-05-23 19:46:35 +02:00
parent 5dd4dfec2c
commit 6dedf59332

View file

@ -1,13 +1,19 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestPermissionsRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestsUpdateRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserResponse; 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.EagerUserRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import java.security.Principal; import java.security.Principal;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -89,4 +95,50 @@ public class GuestControllerTests {
when(userRepository.findByUsername("user")).thenReturn(user); when(userRepository.findByUsername("user")).thenReturn(user);
assertThat(guestController.findGuests(mockPrincipal).isEmpty()); assertThat(guestController.findGuests(mockPrincipal).isEmpty());
} }
@Test
public void testUpdatePermission() {
GuestPermissionsRequest permission = new GuestPermissionsRequest();
permission.setCameraEnabled(true);
when(mockPrincipal.getName()).thenReturn("user");
User user = new User();
user.setCameraEnabled(true);
when(userRepository.findByUsername("user")).thenReturn(user);
when(userRepository.save(user)).thenReturn(user);
assertTrue(guestController.updatePermissions(permission, mockPrincipal).isCameraEnabled());
}
@Test
public void testSetGuests() {
when(mockPrincipal.getName()).thenReturn("user");
ArrayList<User> users = new ArrayList<>();
User user1 = new User();
user1.setId(1L);
user1.getGuests().add(new User());
user1.getGuests().add(new User());
user1.getGuests().add(new User());
User user2 = new User();
user2.setId(2L);
User user3 = new User();
user3.setId(3L);
users.add(user1);
users.add(user2);
users.add(user3);
ArrayList<Long> ids = new ArrayList<>();
ids.add(1L);
ids.add(2L);
ids.add(3L);
GuestsUpdateRequest request = new GuestsUpdateRequest();
request.setIds(ids);
when(userRepository.findAllById(request.getIds())).thenReturn(users);
when(userRepository.findByUsername("user")).thenReturn(user1);
when(userRepository.saveAll(Set.copyOf(user1.getGuests()))).thenReturn(null);
when(userRepository.save(any(User.class))).thenReturn(null);
when(userRepository.saveAll(users)).thenReturn(users);
List<User> returned = guestController.setGuests(request, mockPrincipal);
assertThat(returned.size()).isEqualTo(3);
assertTrue(returned.contains(user1));
assertTrue(returned.contains(user2));
assertTrue(returned.contains(user3));
}
} }