From 886f43bf62de875cc356a33e9333afb91d468433 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Sat, 25 Apr 2020 14:38:47 +0200 Subject: [PATCH 1/2] small fix to thermostat controller --- .../smarthut/controller/ThermostatController.java | 2 +- .../sanmarinoes/smarthut/models/Thermostat.java | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java index 670df76..4cffe89 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java @@ -25,6 +25,7 @@ public class ThermostatController { private Thermostat save(Thermostat newT, ThermostatSaveRequest t, final Principal principal) { newT.setTargetTemperature(t.getTargetTemperature()); + thermostatService.populateMeasuredTemperature(newT); newT.setId(t.getId()); newT.setName(t.getName()); newT.setRoomId(t.getRoomId()); @@ -32,7 +33,6 @@ public class ThermostatController { newT.setOn(t.isTurnOn()); newT = deviceService.saveAsOwner(newT, principal.getName()); - thermostatService.populateMeasuredTemperature(newT); return newT; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java index 2dd3772..bb4bef1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java @@ -1,13 +1,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; import com.google.gson.annotations.SerializedName; import java.math.BigDecimal; -import java.util.HashSet; -import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.OneToMany; import javax.persistence.Transient; import javax.validation.constraints.NotNull; @@ -38,6 +34,10 @@ public class Thermostat extends Switchable implements BooleanTriggerable { BigDecimal measured = this.getMeasuredTemperature(); BigDecimal target = this.getTargetTemperature(); + System.out.println("measured is"); + // System.out.println(measured.getClass().getName()); + System.out.println("target is"); + System.out.println(target.getClass().getName()); BigDecimal delta = target.subtract(measured); if (delta.abs().doubleValue() < 0.25) { @@ -85,9 +85,9 @@ public class Thermostat extends Switchable implements BooleanTriggerable { @Column private boolean useExternalSensors = false; - @OneToMany(mappedBy = "scene", orphanRemoval = true) - @GsonExclude - private Set> triggers = new HashSet<>(); + // @OneToMany(mappedBy = "scene", orphanRemoval = true) + // @GsonExclude + // private Set> triggers = new HashSet<>() /** Creates a thermostat with a temperature sensor and its initial OFF state */ public Thermostat() { From 1b22534f40edeb6b5e4494152891ee3915a66aeb Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Sun, 10 May 2020 15:17:27 +0200 Subject: [PATCH 2/2] room and thermostat tests --- .../sa4/sanmarinoes/smarthut/RoomTests.java | 81 +++++++++++++ .../sanmarinoes/smarthut/ThermostatTests.java | 107 ++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RoomTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RoomTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RoomTests.java new file mode 100644 index 0000000..44aac75 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RoomTests.java @@ -0,0 +1,81 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import javax.swing.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("Room test") +public class RoomTests { + private Room room; + + @BeforeEach + private void createRoom() { + room = new Room(); + } + + @Test + @DisplayName("test id") + public void testId() { + room.setId(42l); + assertEquals(42l, room.getId()); + } + + @Test + @DisplayName("test userId") + public void testUserId() { + room.setUserId(42l); + assertEquals(42l, room.getUserId()); + } + + @Test + @DisplayName("test name") + public void testName() { + room.setName("alpaca"); + assertEquals("alpaca", room.getName()); + } + + @Test + @DisplayName("test image") + public void testImage() { + room.setImage("realFakeImage.png"); + assertEquals("realFakeImage.png", room.getImage()); + } + + @Test + @DisplayName("test toString()") + public void testToString() { + room.setId(1l); + room.setName("alpaca"); + assertEquals("Room{id=1, name='alpaca'}", room.toString()); + } + + @Test + @DisplayName("test devices") + public void testDevices() { + room.getDevices().add(new DimmableLight()); + assertEquals(1, room.getDevices().size()); + } + + @Test + @DisplayName("test user") + public void testUser() { + User user = new User(); + user.setId(34l); + room.setUser(user); + assertEquals(34l, room.getUser().getId()); + } + + @Test + @DisplayName("test Icon") + public void testIcon() { + // ImageIcon image = new ImageIcon("file","description"); + room.setIcon(null); + assertEquals(null, room.getIcon()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatTests.java new file mode 100644 index 0000000..ecc3d9b --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatTests.java @@ -0,0 +1,107 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; +import java.math.BigDecimal; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("Thermostat tests") +public class ThermostatTests { + private Thermostat thermostat; + + @BeforeEach + public void createThermostat() { + this.thermostat = new Thermostat(); + } + + @Test + @DisplayName("test isOn()") + public void inOnTest() { + assertFalse(thermostat.isOn()); + } + + @Test + @DisplayName("test setOn(true) ") + public void setOnTestTrue() { + thermostat.setOn(true); + assertTrue(thermostat.isOn()); + } + + @Test + @DisplayName("test setOn(false) ") + public void setOnTestFalse() { + thermostat.setOn(false); + assertFalse(thermostat.isOn()); + } + + @Test + @DisplayName("test compute measured is null") + public void computeMeasureNull() { + this.thermostat.setMeasuredTemperature(null); + thermostat.setOn(true); + assertEquals(Thermostat.Mode.IDLE, thermostat.getMode()); + } + + @Test + @DisplayName("test compute |measured-target|<0.25") + public void computeMeasureIdle() { + this.thermostat.setMeasuredTemperature(new BigDecimal(1)); + this.thermostat.setTargetTemperature(new BigDecimal(0.9)); + thermostat.setOn(true); + assertEquals(Thermostat.Mode.IDLE, thermostat.getMode()); + } + + @Test + @DisplayName("test compute heating") + public void computeMeasureHeating() { + this.thermostat.setMeasuredTemperature(new BigDecimal(1)); + this.thermostat.setTargetTemperature(new BigDecimal(2)); + thermostat.setOn(true); + assertEquals(Thermostat.Mode.HEATING, thermostat.getMode()); + } + + @Test + @DisplayName("test compute cooling") + public void computeMeasureCooling() { + this.thermostat.setMeasuredTemperature(new BigDecimal(10)); + this.thermostat.setTargetTemperature(new BigDecimal(5)); + thermostat.setOn(true); + assertEquals(Thermostat.Mode.COOLING, thermostat.getMode()); + } + + @Test + @DisplayName("test external sensor") + public void testExternalSensor() { + thermostat.setUseExternalSensors(true); + assertTrue(thermostat.isUseExternalSensors()); + } + + @Test + @DisplayName("test internal sensor temperature") + public void testInternalSensorTemperature() { + thermostat.setInternalSensorTemperature(new BigDecimal(42)); + assertEquals(new BigDecimal(42), thermostat.getInternalSensorTemperature()); + } + + @Test + @DisplayName("test triggerState") + public void testTriggerState() { + assertFalse(thermostat.readTriggerState()); + } + + // for obvious reasons I am not gonna check all the possible combinations of toString() + @Test + @DisplayName("test to string") + public void testToString() { + thermostat.setMeasuredTemperature(new BigDecimal(1)); + thermostat.setTargetTemperature(new BigDecimal(1)); + assertEquals( + "Thermostat{targetTemperature=1, internalSensorTemperature=17.0, mode=OFF, measuredTemperature=1, useExternalSensors=false}", + thermostat.toString()); + } +}