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(
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

@ -1,7 +1,7 @@
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.assertNotNull;
import static org.mockito.ArgumentMatchers.*;
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.models.MotionSensor;
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.MotionSensorService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows;
@ -42,8 +40,6 @@ public class MotionSensorControllerTests {
@Mock private MotionSensorRepository motionSensorRepository;
@Mock private SensorSocketEndpoint sensorSocketEndpoint;
@Mock private Principal mockPrincipal;
@BeforeEach
@ -88,20 +84,16 @@ public class MotionSensorControllerTests {
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));
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);
assertFalse(returned.isDetected());
assertNotNull(returned);
}
@DisplayName("when deleting an existing id should succeed")