added ThermostatPopulationServiceTests

This commit is contained in:
Jacob Salvi 2020-05-25 16:30:31 +02:00
parent 82900b1d02
commit e7def27d22
2 changed files with 47 additions and 1 deletions

View file

@ -17,7 +17,7 @@ import org.springframework.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user") @WithMockUser(username = "user")
@DisplayName("SwitchableState controller test") @DisplayName("AutomationService test")
public class AutomationServiceTests { public class AutomationServiceTests {
@InjectMocks private AutomationService service; @InjectMocks private AutomationService service;

View file

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