Tests on ThermostatService
This commit is contained in:
parent
84767b1793
commit
733b4639fa
2 changed files with 82 additions and 4 deletions
|
@ -14,13 +14,25 @@ import org.springframework.stereotype.Component;
|
||||||
@Component
|
@Component
|
||||||
public class ThermostatService {
|
public class ThermostatService {
|
||||||
|
|
||||||
@Autowired private SensorSocketEndpoint endpoint;
|
private final SensorSocketEndpoint endpoint;
|
||||||
|
|
||||||
@Autowired private DeviceService deviceService;
|
private final DeviceService deviceService;
|
||||||
|
|
||||||
@Autowired private ThermostatPopulationService thermostatPopulationService;
|
private final ThermostatPopulationService thermostatPopulationService;
|
||||||
|
|
||||||
@Autowired private ThermostatRepository thermostatRepository;
|
private final ThermostatRepository thermostatRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public ThermostatService(
|
||||||
|
SensorSocketEndpoint endpoint,
|
||||||
|
DeviceService deviceService,
|
||||||
|
ThermostatPopulationService thermostatPopulationService,
|
||||||
|
ThermostatRepository thermostatRepository) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
this.deviceService = deviceService;
|
||||||
|
this.thermostatPopulationService = thermostatPopulationService;
|
||||||
|
this.thermostatRepository = thermostatRepository;
|
||||||
|
}
|
||||||
|
|
||||||
private void randomJitter(Thermostat thermostat) {
|
private void randomJitter(Thermostat thermostat) {
|
||||||
updateValueForThermostat(
|
updateValueForThermostat(
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository;
|
||||||
|
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.Assertions;
|
||||||
|
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 ThermostatServiceTests {
|
||||||
|
|
||||||
|
@InjectMocks private ThermostatService thermostatService;
|
||||||
|
|
||||||
|
@Mock private ThermostatRepository thermostatRepository;
|
||||||
|
|
||||||
|
@Mock private DeviceService deviceService;
|
||||||
|
|
||||||
|
@Mock private ThermostatPopulationService thermostatPopulationService;
|
||||||
|
|
||||||
|
@Mock private SensorSocketEndpoint endpoint;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFakeUpdateAll() {
|
||||||
|
when(deviceService.saveAsOwner(any(), any())).thenAnswer(i -> i.getArgument(0));
|
||||||
|
when(thermostatRepository.findUser(1L)).thenReturn(new User());
|
||||||
|
|
||||||
|
Thermostat t = new Thermostat();
|
||||||
|
t.setOn(true);
|
||||||
|
t.setId(1L);
|
||||||
|
t.setTargetTemperature(BigDecimal.valueOf(17.0));
|
||||||
|
|
||||||
|
when(thermostatRepository.findAll()).thenReturn(List.of(t));
|
||||||
|
doAnswer(
|
||||||
|
i -> {
|
||||||
|
((Thermostat) i.getArgument(0))
|
||||||
|
.setMeasuredTemperature(BigDecimal.valueOf(18.0));
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.when(thermostatPopulationService)
|
||||||
|
.populateMeasuredTemperature(t);
|
||||||
|
|
||||||
|
doNothing().when(endpoint).queueDeviceUpdate(any(), any(), eq(false), eq(null), eq(false));
|
||||||
|
|
||||||
|
Assertions.assertDoesNotThrow(() -> thermostatService.fakeUpdateAll());
|
||||||
|
assertThat(t.getMode()).isNotEqualTo(Thermostat.Mode.OFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAll() {
|
||||||
|
final Thermostat t = new Thermostat();
|
||||||
|
doNothing().when(thermostatPopulationService).populateMeasuredTemperature(t);
|
||||||
|
when(thermostatRepository.findAllByUsername("user")).thenReturn(List.of(t));
|
||||||
|
assertThat(thermostatService.findAll("user")).containsExactly(t);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue