From 49707917925641e0431edb1158a2ce2654c3fdb1 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Mon, 25 May 2020 17:31:48 +0200 Subject: [PATCH] added MotionSensorService tests --- .../service/MotionSensorServiceTests.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorServiceTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorServiceTests.java new file mode 100644 index 0000000..12c5451 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorServiceTests.java @@ -0,0 +1,40 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; +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.security.test.context.support.WithMockUser; + +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +@DisplayName("MotionSensorService test") +public class MotionSensorServiceTests { + @InjectMocks private MotionSensorService service; + + @Mock private SensorSocketEndpoint sensorSocketEndpoint; + @Mock private DeviceService deviceService; + @Mock private MotionSensorRepository motionSensorRepository; + + @Test + public void testUpdateDetectionFromMotionSensor() { + MotionSensor sensor = new MotionSensor(); + sensor.setId(1L); + User user = new User(); + user.setId(1L); + when(deviceService.saveAsOwner(sensor, "user")).thenReturn(sensor); + when(motionSensorRepository.findUser(sensor.getId())).thenReturn(user); + doNothing().when(sensorSocketEndpoint).queueDeviceUpdate(sensor, user, false, null, false); + MotionSensor returned = service.updateDetectionFromMotionSensor(sensor, true, "user"); + assertThat(returned).isEqualTo(sensor); + assertTrue(returned.isDetected()); + } +}