From 55cea5aca272379751ca6b886e8c7ae0f9f58495 Mon Sep 17 00:00:00 2001 From: omenem Date: Tue, 26 May 2020 17:15:21 +0200 Subject: [PATCH] solved NullPoinyer exception --- .../smarthut/service/MotionSensorService.java | 2 +- .../MotionSensorControllerTests.java | 18 +++++------------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorService.java index f4288ee..9f369a0 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorService.java @@ -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); diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorControllerTests.java index 748bcdf..a104862 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorControllerTests.java @@ -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")