Merge branch '85-motionsensorcontroller-test' into 'dev'

Resolve "motionSensorController-test"

Closes #85

See merge request sa4-2020/the-sanmarinoes/backend!192
This commit is contained in:
Matteo Omenetti 2020-05-26 17:18:52 +02:00
commit 92ef722ec0
3 changed files with 117 additions and 1 deletions

View file

@ -1,9 +1,14 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GenericDeviceSaveRequest {
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from

View file

@ -23,7 +23,7 @@ public class MotionSensorService {
public MotionSensor updateDetectionFromMotionSensor(
MotionSensor sensor, boolean detected, String username) {
sensor.setDetected(detected);
final MotionSensor toReturn = deviceService.saveAsOwner(sensor, username);
MotionSensor toReturn = deviceService.saveAsOwner(sensor, username);
sensorSocketEndpoint.queueDeviceUpdate(
sensor, motionSensorRepository.findUser(sensor.getId()), false, null, false);

View file

@ -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.assertNotNull;
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.error.NotFoundException;
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.service.DeviceService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.MotionSensorService;
import java.security.Principal;
import java.util.Optional;
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() {
MotionSensor motionSensor = new MotionSensor();
motionSensor.setId(1L);
motionSensor.setName("name");
motionSensor.setRoomId(42L);
motionSensor.setDetected(true);
when(motionSensorRepository.findByIdAndUsername(anyLong(), any(String.class)))
.thenReturn(Optional.of(motionSensor));
when(motionSensorService.updateDetectionFromMotionSensor(
any(MotionSensor.class), anyBoolean(), anyString()))
.thenAnswer(i -> i.getArguments()[0]);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
MotionSensor returned = motionSensorController.updateDetection(1L, false, mockPrincipal);
assertNotNull(returned);
}
@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));
}
}