diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java index 2cf74db..6d1850f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java @@ -17,7 +17,7 @@ import org.springframework.security.test.context.support.WithMockUser; @ExtendWith(MockitoExtension.class) @WithMockUser(username = "user") -@DisplayName("SwitchableState controller test") +@DisplayName("AutomationService test") public class AutomationServiceTests { @InjectMocks private AutomationService service; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationServiceTests.java new file mode 100644 index 0000000..a48b59b --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationServiceTests.java @@ -0,0 +1,46 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.service; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import java.math.BigDecimal; +import java.util.Optional; +import org.junit.jupiter.api.DisplayName; +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; +import org.springframework.security.test.context.support.WithMockUser; + +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +@DisplayName("ThermostatPopulationService test") +public class ThermostatPopulationServiceTests { + @InjectMocks private ThermostatPopulationService service; + + @Mock ThermostatRepository repository; + + @Test + public void testPopulateMeasuredTemperatureIf() { + Thermostat thermostat = new Thermostat(); + thermostat.setRoomId(0L); + thermostat.setUseExternalSensors(true); + when(repository.getAverageTemperature( + thermostat.getRoomId(), Sensor.SensorType.TEMPERATURE)) + .thenReturn(Optional.of(new BigDecimal(17))); + service.populateMeasuredTemperature(thermostat); + assertThat(thermostat.getMeasuredTemperature()).isEqualTo(new BigDecimal(17)); + } + + @Test + public void testPopulateMeasuredTemperatureElse() { + Thermostat thermostat = new Thermostat(); + thermostat.setRoomId(0L); + thermostat.setUseExternalSensors(false); + thermostat.setInternalSensorTemperature(new BigDecimal(19)); + service.populateMeasuredTemperature(thermostat); + assertThat(thermostat.getMeasuredTemperature()).isEqualTo(new BigDecimal(19)); + } +}