wip
This commit is contained in:
parent
9616638245
commit
82d111613f
1 changed files with 27 additions and 19 deletions
|
@ -2,23 +2,19 @@ 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.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
import static org.mockito.ArgumentMatchers.*;
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.CurtainsRepository;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
||||||
import java.math.BigDecimal;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.MotionSensorService;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.MotionSensorService;
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
@ -37,6 +33,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
@WithMockUser(username = "user")
|
@WithMockUser(username = "user")
|
||||||
public class MotionSensorControllerTests {
|
public class MotionSensorControllerTests {
|
||||||
|
|
||||||
@InjectMocks private MotionSensorController motionSensorController;
|
@InjectMocks private MotionSensorController motionSensorController;
|
||||||
|
|
||||||
@Mock private DeviceService deviceService;
|
@Mock private DeviceService deviceService;
|
||||||
|
@ -45,6 +42,7 @@ public class MotionSensorControllerTests {
|
||||||
|
|
||||||
@Mock private MotionSensorRepository motionSensorRepository;
|
@Mock private MotionSensorRepository motionSensorRepository;
|
||||||
|
|
||||||
|
@Mock private SensorSocketEndpoint sensorSocketEndpoint;
|
||||||
|
|
||||||
@Mock private Principal mockPrincipal;
|
@Mock private Principal mockPrincipal;
|
||||||
|
|
||||||
|
@ -53,7 +51,8 @@ public class MotionSensorControllerTests {
|
||||||
when(mockPrincipal.getName()).thenReturn("user");
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkMotionSensorAgainstRequest(final MotionSensor toCheck, final GenericDeviceSaveRequest request) {
|
private void checkMotionSensorAgainstRequest(
|
||||||
|
final MotionSensor toCheck, final GenericDeviceSaveRequest request) {
|
||||||
assertThat(toCheck).isNotNull();
|
assertThat(toCheck).isNotNull();
|
||||||
assertThat(toCheck.getName()).isEqualTo(request.getName());
|
assertThat(toCheck.getName()).isEqualTo(request.getName());
|
||||||
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
|
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
|
||||||
|
@ -70,8 +69,7 @@ public class MotionSensorControllerTests {
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
final GenericDeviceSaveRequest toSend =
|
final GenericDeviceSaveRequest toSend = new GenericDeviceSaveRequest(42L, "Test sensor");
|
||||||
new GenericDeviceSaveRequest(42L, "Test sensor");
|
|
||||||
final MotionSensor created = motionSensorController.create(toSend, mockPrincipal);
|
final MotionSensor created = motionSensorController.create(toSend, mockPrincipal);
|
||||||
|
|
||||||
checkMotionSensorAgainstRequest(created, toSend);
|
checkMotionSensorAgainstRequest(created, toSend);
|
||||||
|
@ -80,20 +78,29 @@ public class MotionSensorControllerTests {
|
||||||
@Test
|
@Test
|
||||||
@SneakyThrows(NotFoundException.class)
|
@SneakyThrows(NotFoundException.class)
|
||||||
public void testUpdate() {
|
public void testUpdate() {
|
||||||
when(mockPrincipal.getName()).thenReturn("user");
|
|
||||||
when(motionSensorService.updateDetectionFromMotionSensor(any(MotionSensor.class), eq(true), eq("user"))).thenAnswer(i -> i.getArguments()[0]);
|
|
||||||
MotionSensor motionSensor = new MotionSensor();
|
MotionSensor motionSensor = new MotionSensor();
|
||||||
motionSensor.setId(1L);
|
motionSensor.setId(1L);
|
||||||
motionSensor.setName("name");
|
motionSensor.setName("name");
|
||||||
motionSensor.setRoomId(42L);
|
motionSensor.setRoomId(42L);
|
||||||
when(deviceService.saveAsOwner(any(MotionSensor.class), eq("user"))).thenReturn(motionSensor);
|
motionSensor.setDetected(true);
|
||||||
GenericDeviceSaveRequest toSend = new GenericDeviceSaveRequest();
|
|
||||||
toSend.setRoomId(42L);
|
when(motionSensorRepository.findByIdAndUsername(anyLong(), any(String.class)))
|
||||||
toSend.setName("name");
|
.thenReturn(Optional.of(motionSensor));
|
||||||
when(motionSensorRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(motionSensor));
|
|
||||||
|
when(deviceService.saveAsOwner(any(MotionSensor.class), any(String.class)))
|
||||||
|
.thenReturn(motionSensor);
|
||||||
|
|
||||||
|
doNothing()
|
||||||
|
.when(sensorSocketEndpoint)
|
||||||
|
.queueDeviceUpdate(
|
||||||
|
any(MotionSensor.class), any(User.class), eq(false), eq(null), eq(false));
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
MotionSensor returned = motionSensorController.updateDetection(1L, false, mockPrincipal);
|
MotionSensor returned = motionSensorController.updateDetection(1L, false, mockPrincipal);
|
||||||
assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId());
|
|
||||||
assertThat(returned.getName()).isEqualTo(toSend.getName());
|
|
||||||
assertFalse(returned.isDetected());
|
assertFalse(returned.isDetected());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,6 +108,7 @@ public class MotionSensorControllerTests {
|
||||||
@Test
|
@Test
|
||||||
@SneakyThrows(NotFoundException.class)
|
@SneakyThrows(NotFoundException.class)
|
||||||
public void testDelete() {
|
public void testDelete() {
|
||||||
|
|
||||||
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
|
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
|
||||||
|
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
|
Loading…
Reference in a new issue