From 69aaf9920f131b161ce2a90121e02c75b9ce6d34 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 27 May 2020 11:14:19 +0200 Subject: [PATCH] Tests --- .../smarthut/service/SensorService.java | 38 +++++++---- .../smarthut/service/SensorServiceTests.java | 66 +++++++++++++++++++ 2 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorServiceTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorService.java index 359220b..527b0a9 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorService.java @@ -4,6 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SensorRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.Random; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -11,24 +12,39 @@ import org.springframework.stereotype.Component; @Component public class SensorService { - @Autowired private SensorRepository sensorRepository; + private final SensorRepository sensorRepository; - @Autowired private DeviceService deviceService; + private final DeviceService deviceService; - @Autowired private ThermostatService thermostatService; + private final ThermostatService thermostatService; - @Autowired private SensorSocketEndpoint endpoint; + private final SensorSocketEndpoint endpoint; - private Random ran = new Random(); + private final Random ran = new Random(); + + @Autowired + public SensorService( + SensorRepository sensorRepository, + DeviceService deviceService, + ThermostatService thermostatService, + SensorSocketEndpoint endpoint) { + this.sensorRepository = sensorRepository; + this.deviceService = deviceService; + this.thermostatService = thermostatService; + this.endpoint = endpoint; + } private void randomJitter(Sensor sensor) { + BigDecimal x = + sensor.getTypical() + .subtract( + sensor.getError() + .divide(BigDecimal.valueOf(2), RoundingMode.CEILING)) + .add( + BigDecimal.valueOf( + ran.nextDouble() * sensor.getError().doubleValue() * 2)); - double x; - - x = - (ran.nextInt(sensor.getTypical().intValue()) + sensor.getError().intValue()) * 0.975 - + 1; - updateValueFromSensor(sensor, BigDecimal.valueOf(x)); + updateValueFromSensor(sensor, x); } public void sensorFakeUpdate() { diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorServiceTests.java new file mode 100644 index 0000000..adfd511 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorServiceTests.java @@ -0,0 +1,66 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.service; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SensorRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; +import java.math.BigDecimal; +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.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.security.test.context.support.WithMockUser; + +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class SensorServiceTests { + + @InjectMocks private SensorService sensorService; + + @Mock private DeviceService deviceService; + + @Mock private SensorSocketEndpoint endpoint; + + @Mock private SensorRepository sensorRepository; + + @Mock private ThermostatService thermostatService; + + @Test + public void testRandomJitter() { + final Sensor s = new Sensor(); + s.setTypical(BigDecimal.ZERO); + s.setError(BigDecimal.ONE); + s.setId(42L); + final User u = new User(); + u.setUsername("user"); + doNothing().when(thermostatService).updateStates(); + when(deviceService.saveAsOwner(s, "user")).thenReturn(s); + when(sensorRepository.findUser(42L)).thenReturn(u); + when(sensorRepository.findAll()).thenReturn(List.of(s)); + doNothing().when(endpoint).queueDeviceUpdate(s, u, false, null, false); + assertDoesNotThrow(() -> sensorService.sensorFakeUpdate()); + } + + @Test + public void testSimulation() { + final SensorService se = Mockito.spy(sensorService); + final Sensor s = new Sensor(); + s.setError(BigDecimal.ONE); + s.setTypical(BigDecimal.ZERO); + doReturn(s).when(se).update(s); + se.updateSimulationFromSensor(s, null, null); + assertThat(s.getError()).isEqualTo(BigDecimal.ONE); + assertThat(s.getTypical()).isEqualTo(BigDecimal.ZERO); + + se.updateSimulationFromSensor(s, BigDecimal.ZERO, BigDecimal.ONE); + assertThat(s.getError()).isEqualTo(BigDecimal.ZERO); + assertThat(s.getTypical()).isEqualTo(BigDecimal.ONE); + } +}