diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java index 5eeec6c..9065826 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java @@ -14,13 +14,25 @@ import org.springframework.stereotype.Component; @Component 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) { updateValueForThermostat( diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatServiceTests.java new file mode 100644 index 0000000..05a0447 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatServiceTests.java @@ -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); + } +}