test for security camera

This commit is contained in:
Jacob Salvi 2020-05-23 18:20:34 +02:00
parent 2450a11b6e
commit cae11d44ad
3 changed files with 138 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,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;