Merge branch 'tests' into 'dev'

Tests

See merge request sa4-2020/the-sanmarinoes/backend!208
This commit is contained in:
Claudio Maggioni 2020-05-27 22:30:40 +02:00
commit 2708c438ba
3 changed files with 66 additions and 9 deletions

View file

@ -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 */

View file

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

View file

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