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

# Conflicts:
#   src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserResponseTests.java
This commit is contained in:
Jacob Salvi 2020-05-17 15:28:14 +02:00
commit 0151467b2e
23 changed files with 1057 additions and 57 deletions

View file

@ -6,35 +6,37 @@ import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Data
public class AutomationFastUpdateRequest {
public abstract static class TriggerDTO {
@NotNull public long deviceId;
@NotNull @Getter @Setter private long deviceId;
public abstract Trigger<?> toModel();
}
public static class BooleanTriggerDTO extends TriggerDTO {
@NotNull public boolean on;
@NotNull @Getter @Setter private boolean on;
@Override
public Trigger<?> toModel() {
BooleanTrigger t = new BooleanTrigger();
t.setDeviceId(this.deviceId);
t.setDeviceId(this.getDeviceId());
t.setOn(this.on);
return t;
}
}
public static class RangeTriggerDTO extends TriggerDTO {
@NotNull Operator operator;
@NotNull double range;
@NotNull @Getter @Setter private Operator operator;
@NotNull @Getter @Setter private double range;
@Override
public Trigger<?> toModel() {
RangeTrigger t = new RangeTrigger();
t.setDeviceId(this.deviceId);
t.setDeviceId(this.getDeviceId());
t.setOperator(this.operator);
t.setRange(this.range);
return t;
@ -42,11 +44,11 @@ public class AutomationFastUpdateRequest {
}
public static class ScenePriorityDTO {
@NotNull public long sceneId;
@NotNull @Getter @Setter private long sceneId;
@NotNull
@Min(0)
public Integer priority;
private @Getter @Setter Integer priority;
public ScenePriority toModel() {
ScenePriority s = new ScenePriority();
@ -57,19 +59,19 @@ public class AutomationFastUpdateRequest {
}
public abstract static class ConditionDTO {
@NotNull public long deviceId;
@NotNull @Getter @Setter private long deviceId;
public abstract Condition<?> toModel();
}
public static class BooleanConditionDTO extends ConditionDTO {
@NotNull public boolean on;
@NotNull @Getter @Setter private boolean on;
@Override
public Condition<?> toModel() {
BooleanCondition t = new BooleanCondition();
t.setDeviceId(this.deviceId);
t.setDeviceId(this.getDeviceId());
t.setOn(this.on);
return t;
}
@ -77,13 +79,13 @@ public class AutomationFastUpdateRequest {
public static class RangeConditionDTO extends ConditionDTO {
@NotNull Operator operator;
@NotNull double range;
@NotNull @Getter @Setter private Operator operator;
@NotNull @Getter @Setter private double range;
@Override
public Condition<?> toModel() {
RangeCondition t = new RangeCondition();
t.setDeviceId(this.deviceId);
t.setDeviceId(this.getDeviceId());
t.setOperator(this.operator);
t.setRange(this.range);
return t;
@ -92,13 +94,13 @@ public class AutomationFastUpdateRequest {
public static class ThermostatConditionDTO extends ConditionDTO {
@NotNull ThermostatCondition.Operator operator;
@NotNull private Thermostat.Mode mode;
@NotNull @Getter @Setter private ThermostatCondition.Operator operator;
@NotNull @Getter @Setter private Thermostat.Mode mode;
@Override
public Condition<?> toModel() {
ThermostatCondition t = new ThermostatCondition();
t.setDeviceId(this.deviceId);
t.setDeviceId(this.getDeviceId());
t.setOperator(this.operator);
t.setMode(this.mode);
return t;

View file

@ -5,9 +5,11 @@ import java.math.BigDecimal;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class SensorSaveRequest {
/** The type of this sensor */
@NotNull

View file

@ -1,13 +1,12 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
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 javax.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ThermostatConditionSaveRequest {
@NotNull private long id;
@NotNull private Long deviceId;
@ -17,40 +16,4 @@ public class ThermostatConditionSaveRequest {
@NotNull private ThermostatCondition.Operator operator;
@NotNull private Thermostat.Mode mode;
public long getId() {
return id;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getAutomationId() {
return automationId;
}
public void setAutomationId(Long automationId) {
this.automationId = automationId;
}
public Operator getOperator() {
return operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
public Mode getMode() {
return mode;
}
public void setMode(Mode mode) {
this.mode = mode;
}
}

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,97 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationFastUpdateRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Automation Update DTO")
public class AutomationFastUpdateRequestTest {
AutomationFastUpdateRequest.BooleanTriggerDTO booleanTriggerDTO;
AutomationFastUpdateRequest.RangeTriggerDTO rangeTriggerDTO;
AutomationFastUpdateRequest.ScenePriorityDTO scenePriorityDTO;
AutomationFastUpdateRequest.BooleanConditionDTO booleanConditionDTO;
AutomationFastUpdateRequest.RangeConditionDTO rangeConditionDTO;
AutomationFastUpdateRequest.ThermostatConditionDTO thermostatConditionDTO;
@Test
@DisplayName(" checking boolean trigger ")
public void booleanTriggerDTOTest() {
booleanTriggerDTO = new AutomationFastUpdateRequest.BooleanTriggerDTO();
booleanTriggerDTO.setOn(true);
booleanTriggerDTO.setDeviceId(42);
BooleanTrigger booleanTrigger = (BooleanTrigger) booleanTriggerDTO.toModel();
assertEquals(booleanTrigger.isOn(), booleanTriggerDTO.isOn());
assertEquals(booleanTrigger.getDeviceId(), booleanTriggerDTO.getDeviceId());
}
@Test
@DisplayName(" checking range trigger ")
public void rangeTriggerDTOTest() {
rangeTriggerDTO = new AutomationFastUpdateRequest.RangeTriggerDTO();
rangeTriggerDTO.setOperator(Operator.EQUAL);
rangeTriggerDTO.setDeviceId(420);
rangeTriggerDTO.setRange(12);
RangeTrigger rangeTrigger = (RangeTrigger) rangeTriggerDTO.toModel();
assertEquals(rangeTrigger.getOperator(), rangeTriggerDTO.getOperator());
assertEquals(rangeTrigger.getRange(), rangeTriggerDTO.getRange());
assertEquals(rangeTrigger.getDeviceId(), rangeTriggerDTO.getDeviceId());
}
@Test
@DisplayName(" checking scene priority ")
public void scenePriorityDTOTest() {
scenePriorityDTO = new AutomationFastUpdateRequest.ScenePriorityDTO();
scenePriorityDTO.setPriority(67);
scenePriorityDTO.setSceneId(21);
ScenePriority scenePriority = scenePriorityDTO.toModel();
assertEquals(scenePriority.getPriority(), scenePriorityDTO.getPriority());
assertEquals(scenePriority.getSceneId(), scenePriorityDTO.getSceneId());
}
@Test
@DisplayName(" checking boolean condition ")
public void booleanConditionDTOTest() {
booleanConditionDTO = new AutomationFastUpdateRequest.BooleanConditionDTO();
booleanConditionDTO.setOn(true);
booleanConditionDTO.setDeviceId(17);
BooleanCondition booleanCondition = (BooleanCondition) booleanConditionDTO.toModel();
assertEquals(booleanCondition.isOn(), booleanConditionDTO.isOn());
assertEquals(booleanCondition.getDeviceId(), booleanConditionDTO.getDeviceId());
}
@Test
@DisplayName(" checking range condition ")
public void rangeConditionDTOTest() {
rangeConditionDTO = new AutomationFastUpdateRequest.RangeConditionDTO();
rangeConditionDTO.setOperator(Operator.LESS);
rangeConditionDTO.setRange(82.01);
rangeConditionDTO.setDeviceId(13);
RangeCondition rangeCondition = (RangeCondition) rangeConditionDTO.toModel();
assertEquals(rangeCondition.getOperator(), rangeConditionDTO.getOperator());
assertEquals(rangeCondition.getRange(), rangeConditionDTO.getRange());
assertEquals(rangeCondition.getDeviceId(), rangeConditionDTO.getDeviceId());
}
@Test
@DisplayName(" checking thermostat condition ")
public void thermostatConditionDTOTest() {
thermostatConditionDTO = new AutomationFastUpdateRequest.ThermostatConditionDTO();
thermostatConditionDTO.setDeviceId(25);
thermostatConditionDTO.setOperator(ThermostatCondition.Operator.EQUAL);
thermostatConditionDTO.setMode(Thermostat.Mode.HEATING);
ThermostatCondition thermostatCondition =
(ThermostatCondition) thermostatConditionDTO.toModel();
assertEquals(thermostatCondition.getMode(), thermostatConditionDTO.getMode());
assertEquals(thermostatCondition.getOperator(), thermostatConditionDTO.getOperator());
assertEquals(thermostatCondition.getDeviceId(), thermostatConditionDTO.getDeviceId());
}
}

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,72 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.assertEquals;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Exception tests")
public class ExceptionTests {
@Test
public void testBadData() {
try {
throw new BadDataException("message");
} catch (BadDataException e) {
assertEquals(e.getMessage(), "message");
}
}
@Test
public void testDuplicateRegistration() {
try {
throw new DuplicateRegistrationException();
} catch (DuplicateRegistrationException e) {
assertEquals(e.getMessage(), "Email or username already belonging to another user");
}
}
@Test
public void testDuplicateState() {
try {
throw new DuplicateStateException();
} catch (DuplicateStateException e) {
assertEquals(
e.getMessage(),
"Cannot create state since it has already been created for this scene and this device");
}
}
@Test
public void testEmailTokenNotFound() {
try {
throw new EmailTokenNotFoundException();
} catch (EmailTokenNotFoundException e) {
assertEquals(e.getMessage(), "Email verification token not found in DB");
}
}
@Test
public void testNotFound() {
try {
throw new NotFoundException();
} catch (NotFoundException e) {
assertEquals(e.getMessage(), "Not found");
}
try {
throw new NotFoundException("message");
} catch (NotFoundException e) {
assertEquals(e.getMessage(), "message not found");
}
}
@Test
public void testUserNotFound() {
try {
throw new UserNotFoundException();
} catch (UserNotFoundException e) {
assertEquals(e.getMessage(), "No user found with given email");
}
}
}

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

View file

@ -0,0 +1,48 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
public class UtilsTests {
@Test
public void testToList() {
List<String> hormannTitles = List.of("Prof.", "Dr.", "Kai (spiritual leader)");
assertThat(Utils.toList(hormannTitles))
.containsExactly(hormannTitles.get(0), hormannTitles.get(1), hormannTitles.get(2));
}
@Test
public void testReturnIfGuest() {
Principal principal = Mockito.mock(Principal.class);
UserRepository userRepository = Mockito.mock(UserRepository.class);
User host = new User();
User guest = new User();
host.getGuests().add(guest);
when(userRepository.findById(1L)).thenReturn(Optional.of(host));
when(userRepository.findById(2L)).thenReturn(Optional.empty());
when(userRepository.findByUsername("user")).thenReturn(guest);
when(principal.getName()).thenReturn("user");
try {
assertThat(Utils.returnIfGuest(userRepository, "toReturn", 1L, principal))
.isEqualTo("toReturn");
} catch (NotFoundException e) {
fail(e.getMessage());
}
assertThatThrownBy(() -> Utils.returnIfGuest(userRepository, "toReturn", 2L, principal))
.isInstanceOf(NotFoundException.class);
}
}

View file

@ -0,0 +1,77 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.math.BigDecimal;
import java.security.Principal;
import lombok.SneakyThrows;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@DisplayName("The sensor controller")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class SensorControllerTests {
@InjectMocks private SensorController sensorController;
@Mock private DeviceService deviceService;
@Mock private Principal mockPrincipal;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
}
private void checkSensorAgainstRequest(final Sensor toCheck, final SensorSaveRequest request) {
assertThat(toCheck).isNotNull();
assertThat(toCheck.getSensor()).isEqualTo(request.getSensor());
assertThat(toCheck.getValue()).isEqualTo(request.getValue());
assertThat(toCheck.getName()).isEqualTo(request.getName());
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
}
@DisplayName("when creating should return the same object")
@Test
@SneakyThrows(NotFoundException.class)
public void testCreate() {
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
when(deviceService.saveAsOwner(any(Sensor.class), eq("user")))
.thenAnswer(i -> i.getArguments()[0]);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
final SensorSaveRequest toSend =
new SensorSaveRequest(
Sensor.SensorType.TEMPERATURE, BigDecimal.ZERO, 42L, "Test sensor");
final Sensor created = sensorController.create(toSend, mockPrincipal);
checkSensorAgainstRequest(created, toSend);
}
@DisplayName("when deleting an existant id should succeed")
@Test
@SneakyThrows(NotFoundException.class)
public void testDelete() {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
sensorController.deleteById(42L, mockPrincipal);
}
}