Merge branch '75-backend-unit-testing-matteo' into 'dev'

Resolve "backend-unit-testing"

Closes #75

See merge request sa4-2020/the-sanmarinoes/backend!141
This commit is contained in:
Matteo Omenetti 2020-05-17 11:56:14 +02:00
commit 18e5f74b90
17 changed files with 740 additions and 2 deletions

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
/**

View file

@ -2,6 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.Max;
@ -22,7 +23,7 @@ public class Dimmable extends Switchable implements RangeTriggerable {
@SocketGsonExclude
@Getter
@Setter
private Set<Dimmer> dimmers;
private Set<Dimmer> dimmers = new HashSet<>();
/** The light intensity value. Goes from 0 (off) to 100 (on) */
@Column(nullable = false)

View file

@ -0,0 +1,38 @@
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.DimmableState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Dimmable State Tests")
public class DimmableStateTests {
private DimmableState dimmableState;
@BeforeEach
public void createDimmableState() {
dimmableState = new DimmableState();
}
@Test
@DisplayName("get and set intensity")
public void getAndSetIntensity() {
this.dimmableState.setIntensity(20);
assertEquals(20, this.dimmableState.getIntensity());
}
@Test
@DisplayName("apply")
public void apply() {
DimmableLight d = new DimmableLight();
d.setIntensity(45);
this.dimmableState.setDevice(d);
this.dimmableState.setIntensity(30);
this.dimmableState.apply();
assertEquals(30, d.getIntensity());
}
}

View file

@ -0,0 +1,44 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Dimmer Tests")
public class DimmerTests {
private KnobDimmer dimmer;
@BeforeEach
public void createDimmer() {
dimmer = new KnobDimmer();
}
@Test
@DisplayName("connect true")
public void connectTrue() {
DimmableLight d = new DimmableLight();
dimmer.connect(d, true);
assertTrue(d.getDimmers().contains((this.dimmer)));
assertTrue((this.dimmer.getOutputs().contains(d)));
}
@Test
@DisplayName("connect off")
public void connectOff() {
DimmableLight d = new DimmableLight();
d.getDimmers().add(this.dimmer);
dimmer.getOutputs().add(d);
dimmer.connect(d, false);
assertFalse(d.getDimmers().contains((this.dimmer)));
assertFalse((this.dimmer.getOutputs().contains(d)));
}
}

View file

@ -0,0 +1,26 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Motion Sensor Tests")
public class MotionSensorTests {
private MotionSensor motionSensor;
@BeforeEach
public void createMotionSensor() {
motionSensor = new MotionSensor();
}
@Test
@DisplayName("set and get detected")
public void setAndGetDetected() {
this.motionSensor.setDetected(true);
assertTrue(this.motionSensor.isDetected());
}
}

View file

@ -0,0 +1,61 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Operator;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("RAnge Condition Tests")
public class RangeConditionTests {
private RangeCondition rangeCondition;
@BeforeEach
public void creteRangeCondition() {
this.rangeCondition = new RangeCondition();
}
@Test
@DisplayName("set and get operator")
public void setAndGetOperator() {
rangeCondition.setOperator(Operator.EQUAL);
assertEquals(Operator.EQUAL, rangeCondition.getOperator());
}
@Test
@DisplayName("set and get range")
public void setAndGetRange() {
rangeCondition.setRange(20.5);
assertEquals(20.5, rangeCondition.getRange());
}
@Test
@DisplayName("triggered")
public void triggered() {
DimmableLight d = new DimmableLight();
d.setIntensity(40);
rangeCondition.setDevice(d);
rangeCondition.setRange(45D);
rangeCondition.setOperator(Operator.EQUAL);
assertFalse(rangeCondition.triggered());
rangeCondition.setOperator(Operator.LESS);
assertTrue(rangeCondition.triggered());
rangeCondition.setOperator(Operator.GREATER);
assertFalse(rangeCondition.triggered());
rangeCondition.setOperator(Operator.LESS_EQUAL);
assertTrue(rangeCondition.triggered());
rangeCondition.setOperator(Operator.GREATER_EQUAL);
assertFalse(rangeCondition.triggered());
}
}

View file

@ -0,0 +1,63 @@
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.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Operator;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Range Trigger Tests")
public class RangeTriggerTests {
private RangeTrigger rangeTrigger;
@BeforeEach
public void createRangeTrigger() {
this.rangeTrigger = new RangeTrigger();
}
@Test
@DisplayName("set and get operator")
public void setAndGetOperator() {
rangeTrigger.setOperator(Operator.EQUAL);
assertEquals(Operator.EQUAL, rangeTrigger.getOperator());
}
@Test
@DisplayName("set and get range")
public void setAndGetRange() {
rangeTrigger.setRange(20.5);
assertEquals(20.5, rangeTrigger.getRange());
}
@Test
@DisplayName("triggered")
public void triggered() {
DimmableLight d = new DimmableLight();
d.setIntensity(40);
rangeTrigger.setDevice(d);
rangeTrigger.setRange(45D);
rangeTrigger.setOperator(Operator.EQUAL);
assertFalse(rangeTrigger.triggered());
rangeTrigger.setOperator(Operator.LESS);
assertTrue(rangeTrigger.triggered());
rangeTrigger.setOperator(Operator.GREATER);
assertFalse(rangeTrigger.triggered());
rangeTrigger.setOperator(Operator.LESS_EQUAL);
assertTrue(rangeTrigger.triggered());
rangeTrigger.setOperator(Operator.GREATER_EQUAL);
assertFalse(rangeTrigger.triggered());
}
}

View file

@ -0,0 +1,43 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.assertEquals;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Scene Priority Tests")
public class ScenePriorityTests {
private ScenePriority scenePriority;
@BeforeEach
public void scenePriorityCreate() {
this.scenePriority = new ScenePriority();
}
@Test
@DisplayName("get and set automation id")
public void getAndSetAutomationId() {
scenePriority.setAutomationId(20L);
assertEquals(20, scenePriority.getAutomationId());
}
@Test
@DisplayName("get and set scene id")
public void getAndSetSceneId() {
scenePriority.setSceneId(20L);
assertEquals(20, scenePriority.getSceneId());
}
@Test
@DisplayName("get and set priority")
public void getAndSetPriority() {
scenePriority.setPriority(20);
assertEquals(20, scenePriority.getPriority());
}
}

View file

@ -0,0 +1,52 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Scene Tests")
public class SceneTests {
private Scene scene;
@BeforeEach
public void createScene() {
this.scene = new Scene();
}
@Test
@DisplayName("get and set id")
public void getAndSetId() {
scene.setId(20L);
assertEquals(20, scene.getId());
}
@Test
@DisplayName("get and set user id")
public void getAndSetUserId() {
scene.setUserId(20L);
assertEquals(20, scene.getUserId());
}
@Test
@DisplayName("get and set name")
public void getAndSetName() {
scene.setName("ciao mamma");
assertEquals("ciao mamma", scene.getName());
}
@Test
@DisplayName("get access enabled")
public void accessEnabled() {
scene.setGuestAccessEnabled(true);
assertTrue(scene.isGuestAccessEnabled());
}
}

View file

@ -0,0 +1,44 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SecurityCamera;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Security Camera Tests")
public class SecurityCameraTests {
private SecurityCamera securityCamera;
@BeforeEach
public void createSecurityCamera() {
securityCamera = new SecurityCamera();
}
@Test
@DisplayName("get and set Path")
public void getAndSetPath() {
securityCamera.setPath("ciao mamma");
assertEquals("ciao mamma", securityCamera.getPath());
}
@Test
@DisplayName("get and set On")
public void getAndSetOn() {
securityCamera.setOn(true);
assertTrue(securityCamera.isOn());
}
@Test
@DisplayName("trigger state")
public void triggerState() {
securityCamera.setOn(true);
assertTrue(securityCamera.readTriggerState());
}
}

View file

@ -0,0 +1,46 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor.SensorType;
import java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Sensor Tests")
public class SensorTests {
Sensor sensor;
@BeforeEach
public void createSensor() {
this.sensor = new Sensor();
}
@Test
@DisplayName("get and set sensor")
public void getAndSetSensor() {
sensor.setSensor(SensorType.LIGHT);
assertEquals(SensorType.LIGHT, sensor.getSensor());
}
@Test
@DisplayName("get and set value")
public void getAndSetValue() {
sensor.setValue(new BigDecimal(40));
assertEquals(new BigDecimal(40), sensor.getValue());
}
@Test
@DisplayName("to String")
public void toStringTest() {
sensor.setValue(new BigDecimal(40));
sensor.setSensor(SensorType.LIGHT);
assertEquals("Sensor{value=40, sensor=LIGHT}", sensor.toString());
}
}

View file

@ -0,0 +1,43 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SmartPlug;
import java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("SmartPlug Tests")
public class SmartPlugTests {
private SmartPlug smartPlug;
@BeforeEach
public void createSmartPlug() {
this.smartPlug = new SmartPlug();
}
@Test
@DisplayName("set and get on")
public void testOn() {
smartPlug.setOn(true);
assertTrue(smartPlug.isOn());
}
@Test
@DisplayName("read trigger state")
public void readTriggerState() {
smartPlug.setOn(true);
assertTrue(smartPlug.readTriggerState());
}
@Test
@DisplayName("reset total consumption")
public void reset() {
assertEquals(new BigDecimal(0), smartPlug.getTotalConsumption());
}
}

View file

@ -0,0 +1,51 @@
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.DimmableState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Curtains tests")
public class StateTests {
private DimmableState state;
@BeforeEach
public void createState() {
this.state = new DimmableState();
}
@Test
@DisplayName("get and set device")
public void getAndSetDevice() {
DimmableLight d = new DimmableLight();
this.state.setDevice(d);
assertEquals(d, this.state.getDevice());
}
@Test
@DisplayName("get and set device id")
public void getAndSetDeviceId() {
this.state.setDeviceId(30L);
assertEquals(30, this.state.getDeviceId());
}
@Test
@DisplayName("get and set scene")
public void getAndSetScene() {
Scene s = new Scene();
this.state.setScene(s);
assertEquals(s, this.state.getScene());
}
@Test
@DisplayName("get and set sceneId")
public void getAndSetSceneId() {
this.state.setSceneId(50L);
assertEquals(50, this.state.getSceneId());
}
}

View file

@ -0,0 +1,27 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Switchable State Tests")
public class SwitchableStateTests {
private SwitchableState switchableState;
@BeforeEach
public void createSwitchableState() {
switchableState = new SwitchableState();
}
@Test
@DisplayName("is on")
public void isOn() {
switchableState.setOn(true);
assertTrue(switchableState.isOn());
}
}

View file

@ -0,0 +1,53 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat.Mode;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition.Operator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("ThermostatCondition Tests")
public class ThermostatConditionTests {
private ThermostatCondition thermostatCondition;
@BeforeEach
public void createThermostatCondtion() {
this.thermostatCondition = new ThermostatCondition();
}
@Test
@DisplayName("get and set mode")
public void getAndSetMode() {
thermostatCondition.setMode(Thermostat.Mode.IDLE);
assertEquals(Thermostat.Mode.IDLE, thermostatCondition.getMode());
}
@Test
@DisplayName("get and set operator")
public void getAndSeOperator() {
thermostatCondition.setOperator(Operator.EQUAL);
assertEquals(Operator.EQUAL, thermostatCondition.getOperator());
}
@Test
@DisplayName("get and set operator")
public void triggered() {
thermostatCondition.setMode(Thermostat.Mode.IDLE);
thermostatCondition.setOperator(Operator.EQUAL);
Thermostat t = new Thermostat();
t.setMode(Mode.IDLE);
thermostatCondition.setDevice(t);
assertTrue(thermostatCondition.triggered());
thermostatCondition.setOperator(Operator.NOTEQUAL);
assertFalse(thermostatCondition.triggered());
}
}

View file

@ -0,0 +1,64 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Trigger Tests")
public class TriggerTests {
private BooleanTrigger booleanTrigger;
@BeforeEach
public void createBooleanTrigger() {
booleanTrigger = new BooleanTrigger();
}
@Test
@DisplayName("get Kind")
public void getKind() {
assertEquals("booleanTrigger", booleanTrigger.getKind());
}
@Test
@DisplayName("get and set id")
public void getAndSetId() {
booleanTrigger.setId(20);
assertEquals(20, booleanTrigger.getId());
}
@Test
@DisplayName("get and set device")
public void getAndSetDevice() {
RegularLight r = new RegularLight();
booleanTrigger.setDevice(r);
assertEquals(r, booleanTrigger.getDevice());
}
@Test
@DisplayName("get and set device id")
public void getAndSetDeviceId() {
booleanTrigger.setDeviceId(20L);
assertEquals(20, booleanTrigger.getDeviceId());
}
@Test
@DisplayName("get and set automation")
public void getAndSetAutomation() {
Automation r = new Automation();
booleanTrigger.setAutomation(r);
assertEquals(r, booleanTrigger.getAutomation());
}
@Test
@DisplayName("get and set automation id")
public void getAndSetAutomationId() {
booleanTrigger.setAutomationId(20L);
assertEquals(20, booleanTrigger.getAutomationId());
}
}

View file

@ -0,0 +1,83 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName(" USer Tests")
public class UserTests {
private User user;
@BeforeEach
public void createUser() {
user = new User();
}
@Test
@DisplayName("get and set id")
public void getAndSetId() {
user.setId(20l);
assertEquals(20, user.getId());
}
@Test
@DisplayName("get and set id")
public void getAndSetName() {
user.setName("Paolo Bitta");
assertEquals("Paolo Bitta", user.getName());
}
@Test
@DisplayName("get and set id")
public void getAndSetUsername() {
user.setUsername("PaulB");
assertEquals("PaulB", user.getUsername());
}
@Test
@DisplayName("get and set email")
public void getAndSetEmail() {
user.setEmail("paolo.bitta@gmail.com");
assertEquals("paolo.bitta@gmail.com", user.getEmail());
}
@Test
@DisplayName("get and set password")
public void getAndSetPassword() {
user.setPassword("cameraCaffe");
assertEquals("cameraCaffe", user.getPassword());
}
@Test
@DisplayName("get and set enabled")
public void getAndSetEnabled() {
user.setEnabled(true);
assertTrue(user.getEnabled());
}
@Test
@DisplayName("get and set Cameraenabled")
public void getAndSeCameraEnabled() {
user.setCameraEnabled(true);
assertTrue(user.isCameraEnabled());
}
@Test
@DisplayName("equals")
public void eq() {
assertFalse(user.equals(null));
assertTrue(user.equals(user));
}
}