motion sensor tests
This commit is contained in:
parent
af93063cf4
commit
9616638245
2 changed files with 116 additions and 0 deletions
|
@ -1,9 +1,14 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class GenericDeviceSaveRequest {
|
public class GenericDeviceSaveRequest {
|
||||||
/**
|
/**
|
||||||
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
||||||
|
|
|
@ -0,0 +1,111 @@
|
||||||
|
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.service.DeviceService;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@DisplayName("The motion sensor controller")
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
@WithMockUser(username = "user")
|
||||||
|
public class MotionSensorControllerTests {
|
||||||
|
@InjectMocks private MotionSensorController motionSensorController;
|
||||||
|
|
||||||
|
@Mock private DeviceService deviceService;
|
||||||
|
|
||||||
|
@Mock private MotionSensorService motionSensorService;
|
||||||
|
|
||||||
|
@Mock private MotionSensorRepository motionSensorRepository;
|
||||||
|
|
||||||
|
|
||||||
|
@Mock private Principal mockPrincipal;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkMotionSensorAgainstRequest(final MotionSensor toCheck, final GenericDeviceSaveRequest request) {
|
||||||
|
assertThat(toCheck).isNotNull();
|
||||||
|
assertThat(toCheck.getName()).isEqualTo(request.getName());
|
||||||
|
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName("when creating should return the same object")
|
||||||
|
@Test
|
||||||
|
@SneakyThrows(NotFoundException.class)
|
||||||
|
public void testCreate() {
|
||||||
|
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
|
||||||
|
when(deviceService.saveAsOwner(any(MotionSensor.class), eq("user")))
|
||||||
|
.thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
|
final GenericDeviceSaveRequest toSend =
|
||||||
|
new GenericDeviceSaveRequest(42L, "Test sensor");
|
||||||
|
final MotionSensor created = motionSensorController.create(toSend, mockPrincipal);
|
||||||
|
|
||||||
|
checkMotionSensorAgainstRequest(created, toSend);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 returned = motionSensorController.updateDetection(1L, false, mockPrincipal);
|
||||||
|
assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId());
|
||||||
|
assertThat(returned.getName()).isEqualTo(toSend.getName());
|
||||||
|
assertFalse(returned.isDetected());
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName("when deleting an existing id should succeed")
|
||||||
|
@Test
|
||||||
|
@SneakyThrows(NotFoundException.class)
|
||||||
|
public void testDelete() {
|
||||||
|
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
|
Assertions.assertDoesNotThrow(() -> motionSensorController.delete(42L, mockPrincipal));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue