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.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
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.models.CurtainsRepository;
|
||||
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.Sensor;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||
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.util.Optional;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.MotionSensorService;
|
||||
import lombok.SneakyThrows;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
@ -37,6 +33,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
|||
@ExtendWith(MockitoExtension.class)
|
||||
@WithMockUser(username = "user")
|
||||
public class MotionSensorControllerTests {
|
||||
|
||||
@InjectMocks private MotionSensorController motionSensorController;
|
||||
|
||||
@Mock private DeviceService deviceService;
|
||||
|
@ -45,6 +42,7 @@ public class MotionSensorControllerTests {
|
|||
|
||||
@Mock private MotionSensorRepository motionSensorRepository;
|
||||
|
||||
@Mock private SensorSocketEndpoint sensorSocketEndpoint;
|
||||
|
||||
@Mock private Principal mockPrincipal;
|
||||
|
||||
|
@ -53,7 +51,8 @@ public class MotionSensorControllerTests {
|
|||
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.getName()).isEqualTo(request.getName());
|
||||
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
|
||||
|
@ -70,8 +69,7 @@ public class MotionSensorControllerTests {
|
|||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
|
||||
final GenericDeviceSaveRequest toSend =
|
||||
new GenericDeviceSaveRequest(42L, "Test sensor");
|
||||
final GenericDeviceSaveRequest toSend = new GenericDeviceSaveRequest(42L, "Test sensor");
|
||||
final MotionSensor created = motionSensorController.create(toSend, mockPrincipal);
|
||||
|
||||
checkMotionSensorAgainstRequest(created, toSend);
|
||||
|
@ -80,20 +78,29 @@ public class MotionSensorControllerTests {
|
|||
@Test
|
||||
@SneakyThrows(NotFoundException.class)
|
||||
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.setId(1L);
|
||||
motionSensor.setName("name");
|
||||
motionSensor.setRoomId(42L);
|
||||
when(deviceService.saveAsOwner(any(MotionSensor.class), eq("user"))).thenReturn(motionSensor);
|
||||
GenericDeviceSaveRequest toSend = new GenericDeviceSaveRequest();
|
||||
toSend.setRoomId(42L);
|
||||
toSend.setName("name");
|
||||
when(motionSensorRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(motionSensor));
|
||||
motionSensor.setDetected(true);
|
||||
|
||||
when(motionSensorRepository.findByIdAndUsername(anyLong(), any(String.class)))
|
||||
.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);
|
||||
assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId());
|
||||
assertThat(returned.getName()).isEqualTo(toSend.getName());
|
||||
|
||||
assertFalse(returned.isDetected());
|
||||
}
|
||||
|
||||
|
@ -101,6 +108,7 @@ public class MotionSensorControllerTests {
|
|||
@Test
|
||||
@SneakyThrows(NotFoundException.class)
|
||||
public void testDelete() {
|
||||
|
||||
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
|
Loading…
Reference in a new issue