diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerControllerTests.java index d3fb238..39f5b16 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerControllerTests.java @@ -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; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraControllerTests.java new file mode 100644 index 0000000..5e2d429 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraControllerTests.java @@ -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 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); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchControllerTests.java index ea8378a..f0c985f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchControllerTests.java @@ -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;