solved NullPoinyer exception

This commit is contained in:
omenem 2020-05-26 17:15:21 +02:00
parent 82d111613f
commit 55cea5aca2
2 changed files with 6 additions and 14 deletions

View file

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

View file

@ -1,7 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.assertNotNull;
import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -9,10 +9,8 @@ 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.error.NotFoundException;
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.User;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.MotionSensorService; 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 lombok.SneakyThrows; import lombok.SneakyThrows;
@ -42,8 +40,6 @@ public class MotionSensorControllerTests {
@Mock private MotionSensorRepository motionSensorRepository; @Mock private MotionSensorRepository motionSensorRepository;
@Mock private SensorSocketEndpoint sensorSocketEndpoint;
@Mock private Principal mockPrincipal; @Mock private Principal mockPrincipal;
@BeforeEach @BeforeEach
@ -88,20 +84,16 @@ public class MotionSensorControllerTests {
when(motionSensorRepository.findByIdAndUsername(anyLong(), any(String.class))) when(motionSensorRepository.findByIdAndUsername(anyLong(), any(String.class)))
.thenReturn(Optional.of(motionSensor)); .thenReturn(Optional.of(motionSensor));
when(deviceService.saveAsOwner(any(MotionSensor.class), any(String.class))) when(motionSensorService.updateDetectionFromMotionSensor(
.thenReturn(motionSensor); any(MotionSensor.class), anyBoolean(), anyString()))
.thenAnswer(i -> i.getArguments()[0]);
doNothing()
.when(sensorSocketEndpoint)
.queueDeviceUpdate(
any(MotionSensor.class), any(User.class), eq(false), eq(null), eq(false));
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
MotionSensor returned = motionSensorController.updateDetection(1L, false, mockPrincipal); MotionSensor returned = motionSensorController.updateDetection(1L, false, mockPrincipal);
assertFalse(returned.isDetected()); assertNotNull(returned);
} }
@DisplayName("when deleting an existing id should succeed") @DisplayName("when deleting an existing id should succeed")