Merge branch '82-securitycameracontrollertests' into 'dev'

Resolve "securityCameraControllerTests"

See merge request sa4-2020/the-sanmarinoes/backend!170
This commit is contained in:
Claudio Maggioni 2020-05-23 22:51:34 +02:00
commit e7d69457e4
5 changed files with 318 additions and 2 deletions

View file

@ -28,7 +28,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("KnobDimmer controller test")
@DisplayName("ButtonDimmer controller test")
public class ButtonDimmerControllerTests {
@InjectMocks private ButtonDimmerController controller;

View file

@ -0,0 +1,128 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows;
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.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("Curtains controller test")
public class CurtainsControllerTests {
@InjectMocks CurtainsController controller;
@Mock private DeviceService deviceService;
@Mock private CurtainsRepository curtainsService;
@Mock private SceneRepository sceneRepository;
@Mock private StateRepository<State> stateRepository;
@Mock Principal principal;
@Test
@SneakyThrows(NotFoundException.class)
public void testCreate() {
when(principal.getName()).thenReturn("user");
Curtains curtains = new Curtains();
curtains.setIntensity(50);
curtains.setName("name");
curtains.setRoomId(42L);
when(deviceService.saveAsOwner(any(Curtains.class), eq("user"))).thenReturn(curtains);
doNothing().when(deviceService).throwIfRoomNotOwned(42L, "user");
DimmableSaveRequest toSend = new DimmableSaveRequest();
toSend.setRoomId(42L);
toSend.setIntensity(50);
toSend.setName("name");
Curtains returned = controller.create(toSend, principal);
assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId());
assertThat(returned.getName()).isEqualTo(toSend.getName());
assertTrue(returned.isOn());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
when(principal.getName()).thenReturn("user");
Curtains curtains = new Curtains();
curtains.setId(1L);
curtains.setIntensity(50);
curtains.setName("name");
curtains.setRoomId(42L);
when(deviceService.saveAsOwner(any(Curtains.class), eq("user"))).thenReturn(curtains);
DimmableSaveRequest toSend = new DimmableSaveRequest();
toSend.setId(1L);
toSend.setRoomId(42L);
toSend.setIntensity(50);
toSend.setName("name");
when(curtainsService.findById(1L)).thenReturn(Optional.of(curtains));
Curtains returned = controller.update(toSend, principal);
assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId());
assertThat(returned.getName()).isEqualTo(toSend.getName());
assertTrue(returned.isOn());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testDelete() {
when(principal.getName()).thenReturn("user");
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
controller.delete(42L, principal);
}
@Test
@SneakyThrows({NotFoundException.class, DuplicateStateException.class})
public void testSceneBinding() {
when(principal.getName()).thenReturn("user");
Curtains curtains = new Curtains();
when(curtainsService.findByIdAndUsername(24L, "user")).thenReturn(Optional.of(curtains));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = curtains.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
controller.sceneBinding(24L, 1L, principal);
}
@Test
public void testSceneBinding2() {
when(principal.getName()).thenReturn("user");
Curtains curtains = new Curtains();
when(curtainsService.findByIdAndUsername(24L, "user")).thenReturn(Optional.of(curtains));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = curtains.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2);
assertThatThrownBy(() -> controller.sceneBinding(24L, 1L, principal))
.isInstanceOf(DuplicateStateException.class);
}
}

View file

@ -1,13 +1,19 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
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 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.models.EagerUserRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -89,4 +95,50 @@ public class GuestControllerTests {
when(userRepository.findByUsername("user")).thenReturn(user);
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));
}
}

View file

@ -0,0 +1,136 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.CameraConfigurationService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows;
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.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("SecurityCamera controller test")
public class SecurityCameraControllerTests {
@InjectMocks private SecurityCameraController controller;
@Mock Principal principal;
@Mock private DeviceService deviceService;
@Mock private SecurityCameraRepository securityCameraService;
@Mock private SceneRepository sceneRepository;
@Mock private StateRepository<State> stateRepository;
@Mock private CameraConfigurationService cameraConfigurationService;
@Test
@SneakyThrows(NotFoundException.class)
public void testCreate() {
when(principal.getName()).thenReturn("user");
SecurityCamera camera = new SecurityCamera();
camera.setOn(true);
camera.setName("name");
camera.setRoomId(42L);
when(deviceService.saveAsOwner(any(SecurityCamera.class), eq("user"))).thenReturn(camera);
SwitchableSaveRequest toSend = new SwitchableSaveRequest();
toSend.setOn(true);
toSend.setRoomId(42L);
toSend.setName("name");
when(cameraConfigurationService.getVideoUrl()).thenReturn("url");
doNothing().when(deviceService).throwIfRoomNotOwned(42L, "user");
SecurityCamera returned = controller.create(toSend, principal);
assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId());
assertThat(returned.getName()).isEqualTo(toSend.getName());
assertTrue(returned.isOn());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
when(principal.getName()).thenReturn("user");
SecurityCamera camera = new SecurityCamera();
camera.setOn(true);
camera.setName("name");
camera.setRoomId(42L);
SwitchableSaveRequest toSend = new SwitchableSaveRequest();
toSend.setId(1L);
toSend.setOn(true);
toSend.setRoomId(42L);
toSend.setName("name");
when(securityCameraService.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(camera));
when(deviceService.saveAsOwner(any(SecurityCamera.class), eq("user"))).thenReturn(camera);
when(cameraConfigurationService.getVideoUrl()).thenReturn("url");
SecurityCamera returned = controller.update(toSend, principal);
assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId());
assertThat(returned.getName()).isEqualTo(toSend.getName());
assertTrue(returned.isOn());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testDelete() {
when(principal.getName()).thenReturn("user");
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
controller.delete(42L, principal);
}
@Test
@SneakyThrows({NotFoundException.class, DuplicateStateException.class})
public void testSceneBinding() {
when(principal.getName()).thenReturn("user");
SecurityCamera camera = new SecurityCamera();
when(securityCameraService.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(camera));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = camera.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
// when(stateRepository.save(eq(state))).thenReturn(state);
controller.sceneBinding(24L, 1L, principal);
}
@Test
public void testSceneBinding2() {
when(principal.getName()).thenReturn("user");
SecurityCamera camera = new SecurityCamera();
when(securityCameraService.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(camera));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = camera.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2);
assertThatThrownBy(() -> controller.sceneBinding(24L, 1L, principal))
.isInstanceOf(DuplicateStateException.class);
}
}

View file

@ -32,7 +32,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("KnobDimmer controller test")
@DisplayName("SwitchController controller test")
public class SwitchControllerTests {
@InjectMocks private SwitchController controller;