From 7371999cc70ff2bc7d48aa26a78fc2483fa55914 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 27 May 2020 22:29:59 +0200 Subject: [PATCH] Tests --- .../smarthut/scheduled/UpdateTasks.java | 34 ++++++++++++---- .../smarthut/scheduled/UpdateTasksTests.java | 39 +++++++++++++++++++ .../smarthut/service/DeviceServiceTests.java | 2 +- 3 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasksTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasks.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasks.java index ec0f43f..2cb8d01 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasks.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasks.java @@ -20,19 +20,37 @@ import org.springframework.stereotype.Component; @Component public class UpdateTasks { - @Autowired private SensorRepository sensorRepository; + private final SensorRepository sensorRepository; - @Autowired private MotionSensorRepository motionSensorRepository; + private final MotionSensorRepository motionSensorRepository; - @Autowired private SmartPlugRepository smartPlugRepository; + private final SmartPlugRepository smartPlugRepository; - @Autowired private SensorService sensorService; + private final SensorService sensorService; - @Autowired private ThermostatService thermostatService; + private final ThermostatService thermostatService; - @Autowired private MotionSensorService motionSensorService; + private final MotionSensorService motionSensorService; - @Autowired private SensorSocketEndpoint sensorSocketEndpoint; + private final SensorSocketEndpoint sensorSocketEndpoint; + + @Autowired + public UpdateTasks( + SensorRepository sensorRepository, + MotionSensorRepository motionSensorRepository, + SmartPlugRepository smartPlugRepository, + SensorService sensorService, + ThermostatService thermostatService, + MotionSensorService motionSensorService, + SensorSocketEndpoint sensorSocketEndpoint) { + this.sensorRepository = sensorRepository; + this.motionSensorRepository = motionSensorRepository; + this.smartPlugRepository = smartPlugRepository; + this.sensorService = sensorService; + this.thermostatService = thermostatService; + this.motionSensorService = motionSensorService; + this.sensorSocketEndpoint = sensorSocketEndpoint; + } /** Generates fake sensor updates every two seconds with a +/- 2.5% error */ @Scheduled(fixedRate = 2000) @@ -78,7 +96,7 @@ public class UpdateTasks { c.forEach( s -> sensorSocketEndpoint.queueDeviceUpdate( - s, sensorRepository.findUser(s.getId()), false, null, false)); + s, smartPlugRepository.findUser(s.getId()), false, null, false)); } /** Sends device updates through sensor socket in batch every one second */ diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasksTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasksTests.java new file mode 100644 index 0000000..bf511c3 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasksTests.java @@ -0,0 +1,39 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.scheduled; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SmartPlug; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SmartPlugRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; +import java.util.List; +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; + +@ExtendWith(MockitoExtension.class) +public class UpdateTasksTests { + + @InjectMocks private UpdateTasks updateTasks; + + @Mock private SmartPlugRepository smartPlugRepository; + + @Mock private SensorSocketEndpoint sensorSocketEndpoint; + + @Test + public void testSmartPlugConsumptionFakeUpdate() { + final User u = new User(); + final SmartPlug s = new SmartPlug(); + s.setId(20L); + doReturn(u).when(smartPlugRepository).findUser(20L); + doNothing() + .when(smartPlugRepository) + .updateTotalConsumption(SmartPlug.AVERAGE_CONSUMPTION_KW); + when(smartPlugRepository.findByOn(true)).thenReturn(List.of(s)); + doNothing().when(sensorSocketEndpoint).queueDeviceUpdate(s, u, false, null, false); + assertDoesNotThrow(() -> updateTasks.smartPlugConsumptionFakeUpdate()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceServiceTests.java index ce7e5b9..f357ed3 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceServiceTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceServiceTests.java @@ -174,7 +174,7 @@ public class DeviceServiceTests { currentDeviceService.saveAllAsOwner(devices, "user", true, false); assertThat(count[0]).isEqualTo(0); - currentDeviceService.saveAllAsOwner(devices, "user", false, false); + currentDeviceService.saveAllAsOwner(devices, "user"); assertThat(count[0]).isEqualTo(2); }