diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java index 878bfe3..a014e79 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java @@ -44,8 +44,6 @@ public class SensorController { newSensor.setName(s.getName()); newSensor.setRoomId(s.getRoomId()); newSensor.setValue(s.getValue()); - // newSensor.setError(s.getError()); - // newSensor.setTypical(s.getTypical()); return deviceService.saveAsOwner(newSensor, principal.getName()); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java index 7aee6c0..02c61d1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java @@ -87,10 +87,6 @@ public class Sensor extends InputDevice implements RangeTriggerable { this.typical = typical; } - public void setTypicalNull() { - this.typical = null; - } - public Sensor() { super("sensor"); } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java index 484564f..c56537e 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java @@ -108,4 +108,25 @@ public class SensorControllerTests { assertThat(sensorController.updateValue(1L, BigDecimal.valueOf(30), mockPrincipal)) .isSameAs(s); } + + @Test + public void testUpdateSimulation() throws NotFoundException { + final Sensor s = new Sensor(); + when(sensorRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(s)); + when(sensorRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty()); + when(sensorService.updateSimulationFromSensor( + s, BigDecimal.valueOf(30), BigDecimal.valueOf(10))) + .thenAnswer( + i -> { + s.setError(i.getArgument(1)); + s.setTypical(i.getArgument(2)); + return s; + }); + assertThatThrownBy(() -> sensorController.updateValue(2L, BigDecimal.ZERO, mockPrincipal)) + .isInstanceOf(NotFoundException.class); + assertThat( + sensorController.updateSimulation( + 1L, BigDecimal.valueOf(30), BigDecimal.valueOf(10), mockPrincipal)) + .isSameAs(s); + } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatControllerTests.java index 2f23934..41dfd0d 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatControllerTests.java @@ -63,6 +63,13 @@ public class ThermostatControllerTests { assertThat(toCheck.isUseExternalSensors()).isEqualTo(request.isUseExternalSensors()); assertThat(toCheck.getName()).isEqualTo(request.getName()); assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId()); + if (request.getErr() != null) { + assertThat(toCheck.getErr()).isEqualTo(request.getErr()); + } + + if (request.getTypical() != null) { + assertThat(toCheck.getTypical()).isEqualTo(request.getTypical()); + } } @Test @@ -83,6 +90,8 @@ public class ThermostatControllerTests { toSend.setTargetTemperature(new BigDecimal(40)); toSend.setName("Thermostat Test"); toSend.setUseExternalSensors(true); + toSend.setErr(BigDecimal.valueOf(10)); + toSend.setTypical(BigDecimal.valueOf(15)); final Thermostat thermostat = thermostatController.create(toSend, mockPrincipal); diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SensorTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SensorTests.java index 0aa1089..b747a9a 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SensorTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SensorTests.java @@ -26,6 +26,22 @@ public class SensorTests { assertEquals(SensorType.LIGHT, sensor.getSensor()); } + @Test + @DisplayName("get and set err") + public void getAndSetError() { + sensor.setError(BigDecimal.valueOf(10)); + + assertEquals(BigDecimal.valueOf(10), sensor.getError()); + } + + @Test + @DisplayName("get and set typical") + public void getAndSetTypical() { + sensor.setTypical(BigDecimal.valueOf(10)); + + assertEquals(BigDecimal.valueOf(10), sensor.getTypical()); + } + @Test @DisplayName("get and set value") public void getAndSetValue() { diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatTests.java index e189131..30a23ea 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatTests.java @@ -2,6 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import static org.junit.jupiter.api.Assertions.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat.Mode; import java.math.BigDecimal; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -85,12 +86,34 @@ public class ThermostatTests { assertEquals(new BigDecimal(42), thermostat.getInternalSensorTemperature()); } + @Test + @DisplayName("test err ") + public void errTest() { + thermostat.setErr(BigDecimal.valueOf(10)); + assertEquals(BigDecimal.valueOf(10), thermostat.getErr()); + } + + @Test + @DisplayName("test typical ") + public void typicalTest() { + thermostat.setTypical(BigDecimal.valueOf(10)); + assertEquals(BigDecimal.valueOf(10), thermostat.getTypical()); + } + @Test @DisplayName("test triggerState") public void testTriggerState() { assertFalse(thermostat.readTriggerState()); } + @Test + @DisplayName("test triggerState") + public void testTriggerStateTrue() { + thermostat.setMode(Mode.COOLING); + + assertTrue(thermostat.readTriggerState()); + } + // for obvious reasons I am not gonna check all the possible combinations of toString() @Test @DisplayName("test to string") diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneServiceTests.java index b42f263..2925b5a 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneServiceTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneServiceTests.java @@ -1,11 +1,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -22,6 +24,8 @@ public class SceneServiceTests { @Mock private SceneRepository sceneRepository; + @Mock private StateRepository stateRepository; + @Mock private DevicePropagationService deviceService; @Mock private DevicePopulationService devicePopulationService; @@ -56,4 +60,20 @@ public class SceneServiceTests { assertThat(sceneService.applyAsGuest(s, "user", 42L)).containsExactly(d); assertThat(d.getIntensity()).isEqualTo(40); } + + @Test + public void testCopyStates() { + + final State state = new DimmableState(); + + final Scene sceneFrom = new Scene(); + sceneFrom.getStates().add(state); + final Scene sceneTo = new Scene(); + + when(stateRepository.save(any(State.class))).thenReturn(state); + + List s = sceneService.copyStates(sceneTo, sceneFrom); + + assertEquals(s.get(0), state); + } }