This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-27 11:14:19 +02:00
parent ec93ff22cc
commit 69aaf9920f
2 changed files with 93 additions and 11 deletions

View file

@ -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() {

View file

@ -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);
}
}