From 3ed6102b9fd60c2ad588eefdd9b838fbd339a179 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 13 May 2020 11:57:31 +0200 Subject: [PATCH 01/13] Added example tests for SensorController --- .../smarthut/dto/SensorSaveRequest.java | 2 + .../controller/SensorControllerTests.java | 77 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java index 8566bde..bd86cd3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java @@ -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 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 new file mode 100644 index 0000000..6ef3158 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java @@ -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); + } +} From 368530de97160059bf9aedcda9cf5a7e64b1e9b2 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Thu, 14 May 2020 19:00:07 +0200 Subject: [PATCH 02/13] automation dto should be tested --- .../dto/AutomationFastUpdateRequest.java | 12 +-- .../AutomationFastUpdateRequestTest.java | 97 +++++++++++++++++++ 2 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java index 4217761..ada8906 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java @@ -28,8 +28,8 @@ public class AutomationFastUpdateRequest { } public static class RangeTriggerDTO extends TriggerDTO { - @NotNull Operator operator; - @NotNull double range; + @NotNull public Operator operator; + @NotNull public double range; @Override public Trigger toModel() { @@ -77,8 +77,8 @@ public class AutomationFastUpdateRequest { public static class RangeConditionDTO extends ConditionDTO { - @NotNull Operator operator; - @NotNull double range; + @NotNull public Operator operator; + @NotNull public double range; @Override public Condition toModel() { @@ -92,8 +92,8 @@ public class AutomationFastUpdateRequest { public static class ThermostatConditionDTO extends ConditionDTO { - @NotNull ThermostatCondition.Operator operator; - @NotNull private Thermostat.Mode mode; + @NotNull public ThermostatCondition.Operator operator; + @NotNull public Thermostat.Mode mode; @Override public Condition toModel() { diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java new file mode 100644 index 0000000..70e1f99 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java @@ -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.on = true; + booleanTriggerDTO.deviceId = 42; + BooleanTrigger booleanTrigger = (BooleanTrigger) booleanTriggerDTO.toModel(); + assertEquals(booleanTrigger.isOn(), booleanTriggerDTO.on); + assertEquals(booleanTrigger.getDeviceId(), booleanTriggerDTO.deviceId); + } + + @Test + @DisplayName(" checking range trigger ") + public void rangeTriggerDTOTest() { + rangeTriggerDTO = new AutomationFastUpdateRequest.RangeTriggerDTO(); + rangeTriggerDTO.operator = Operator.EQUAL; + rangeTriggerDTO.deviceId = 420; + rangeTriggerDTO.range = 12; + + RangeTrigger rangeTrigger = (RangeTrigger) rangeTriggerDTO.toModel(); + assertEquals(rangeTrigger.getOperator(), rangeTriggerDTO.operator); + assertEquals(rangeTrigger.getRange(), rangeTriggerDTO.range); + assertEquals(rangeTrigger.getDeviceId(), rangeTriggerDTO.deviceId); + } + + @Test + @DisplayName(" checking scene priority ") + public void scenePriorityDTOTest() { + scenePriorityDTO = new AutomationFastUpdateRequest.ScenePriorityDTO(); + scenePriorityDTO.priority = 67; + scenePriorityDTO.sceneId = 21; + + ScenePriority scenePriority = scenePriorityDTO.toModel(); + assertEquals(scenePriority.getPriority(), scenePriorityDTO.priority); + assertEquals(scenePriority.getSceneId(), scenePriorityDTO.priority); + } + + @Test + @DisplayName(" checking boolean condition ") + public void booleanConditionDTOTest() { + booleanConditionDTO = new AutomationFastUpdateRequest.BooleanConditionDTO(); + booleanConditionDTO.on = true; + booleanConditionDTO.deviceId = 17; + + BooleanCondition booleanCondition = (BooleanCondition) booleanConditionDTO.toModel(); + assertEquals(booleanCondition.isOn(), booleanConditionDTO.on); + assertEquals(booleanCondition.getDeviceId(), booleanConditionDTO.deviceId); + } + + @Test + @DisplayName(" checking range condition ") + public void rangeConditionDTOTest() { + rangeConditionDTO = new AutomationFastUpdateRequest.RangeConditionDTO(); + rangeConditionDTO.operator = Operator.LESS; + rangeConditionDTO.range = 82.01; + rangeConditionDTO.deviceId = 13; + + RangeCondition rangeCondition = (RangeCondition) rangeConditionDTO.toModel(); + assertEquals(rangeCondition.getOperator(), rangeConditionDTO.operator); + assertEquals(rangeCondition.getRange(), rangeConditionDTO.range); + assertEquals(rangeCondition.getDeviceId(), rangeConditionDTO.deviceId); + } + + @Test + @DisplayName(" checking thermostat condition ") + public void thermostatConditionDTOTest() { + thermostatConditionDTO = new AutomationFastUpdateRequest.ThermostatConditionDTO(); + thermostatConditionDTO.deviceId = 25; + thermostatConditionDTO.operator = ThermostatCondition.Operator.EQUAL; + thermostatConditionDTO.mode = Thermostat.Mode.HEATING; + + ThermostatCondition thermostatCondition = + (ThermostatCondition) thermostatConditionDTO.toModel(); + assertEquals(thermostatCondition.getMode(), thermostatConditionDTO.mode); + assertEquals(thermostatCondition.getOperator(), thermostatConditionDTO.operator); + assertEquals(thermostatCondition.getDeviceId(), thermostatConditionDTO.deviceId); + } +} From 3dcfa584f070e6ab11a182fb073983611f306fa6 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Thu, 14 May 2020 21:02:49 +0200 Subject: [PATCH 03/13] made some fields private --- .../dto/AutomationFastUpdateRequest.java | 14 ++++++----- .../AutomationFastUpdateRequestTest.java | 24 +++++++++---------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java index ada8906..f086fef 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java @@ -8,6 +8,8 @@ import javax.validation.constraints.NotNull; import lombok.Data; @Data +@Getter +@Setter public class AutomationFastUpdateRequest { public abstract static class TriggerDTO { @NotNull public long deviceId; @@ -28,8 +30,8 @@ public class AutomationFastUpdateRequest { } public static class RangeTriggerDTO extends TriggerDTO { - @NotNull public Operator operator; - @NotNull public double range; + @NotNull private Operator operator; + @NotNull private double range; @Override public Trigger toModel() { @@ -77,8 +79,8 @@ public class AutomationFastUpdateRequest { public static class RangeConditionDTO extends ConditionDTO { - @NotNull public Operator operator; - @NotNull public double range; + @NotNull private Operator operator; + @NotNull private double range; @Override public Condition toModel() { @@ -92,8 +94,8 @@ public class AutomationFastUpdateRequest { public static class ThermostatConditionDTO extends ConditionDTO { - @NotNull public ThermostatCondition.Operator operator; - @NotNull public Thermostat.Mode mode; + @NotNull private ThermostatCondition.Operator operator; + @NotNull private Thermostat.Mode mode; @Override public Condition toModel() { diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java index 70e1f99..013f067 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java @@ -32,13 +32,13 @@ public class AutomationFastUpdateRequestTest { @DisplayName(" checking range trigger ") public void rangeTriggerDTOTest() { rangeTriggerDTO = new AutomationFastUpdateRequest.RangeTriggerDTO(); - rangeTriggerDTO.operator = Operator.EQUAL; + rangeTriggerDTO.setOperator(Operator.EQUAL); rangeTriggerDTO.deviceId = 420; - rangeTriggerDTO.range = 12; + rangeTriggerDTO.setRange(12); RangeTrigger rangeTrigger = (RangeTrigger) rangeTriggerDTO.toModel(); - assertEquals(rangeTrigger.getOperator(), rangeTriggerDTO.operator); - assertEquals(rangeTrigger.getRange(), rangeTriggerDTO.range); + assertEquals(rangeTrigger.getOperator(), rangeTriggerDTO.getOperator()); + assertEquals(rangeTrigger.getRange(), rangeTriggerDTO.getRange()); assertEquals(rangeTrigger.getDeviceId(), rangeTriggerDTO.deviceId); } @@ -70,13 +70,13 @@ public class AutomationFastUpdateRequestTest { @DisplayName(" checking range condition ") public void rangeConditionDTOTest() { rangeConditionDTO = new AutomationFastUpdateRequest.RangeConditionDTO(); - rangeConditionDTO.operator = Operator.LESS; - rangeConditionDTO.range = 82.01; + rangeConditionDTO.setOperator(Operator.LESS); + rangeConditionDTO.setRange(82.01); rangeConditionDTO.deviceId = 13; RangeCondition rangeCondition = (RangeCondition) rangeConditionDTO.toModel(); - assertEquals(rangeCondition.getOperator(), rangeConditionDTO.operator); - assertEquals(rangeCondition.getRange(), rangeConditionDTO.range); + assertEquals(rangeCondition.getOperator(), rangeConditionDTO.getOperator()); + assertEquals(rangeCondition.getRange(), rangeConditionDTO.getRange()); assertEquals(rangeCondition.getDeviceId(), rangeConditionDTO.deviceId); } @@ -85,13 +85,13 @@ public class AutomationFastUpdateRequestTest { public void thermostatConditionDTOTest() { thermostatConditionDTO = new AutomationFastUpdateRequest.ThermostatConditionDTO(); thermostatConditionDTO.deviceId = 25; - thermostatConditionDTO.operator = ThermostatCondition.Operator.EQUAL; - thermostatConditionDTO.mode = Thermostat.Mode.HEATING; + thermostatConditionDTO.setOperator(ThermostatCondition.Operator.EQUAL); + thermostatConditionDTO.setMode(Thermostat.Mode.HEATING); ThermostatCondition thermostatCondition = (ThermostatCondition) thermostatConditionDTO.toModel(); - assertEquals(thermostatCondition.getMode(), thermostatConditionDTO.mode); - assertEquals(thermostatCondition.getOperator(), thermostatConditionDTO.operator); + assertEquals(thermostatCondition.getMode(), thermostatConditionDTO.getMode()); + assertEquals(thermostatCondition.getOperator(), thermostatConditionDTO.getOperator()); assertEquals(thermostatCondition.getDeviceId(), thermostatConditionDTO.deviceId); } } From 3d9ddb5ffdee6be7b9a445817e0936ed0ac00266 Mon Sep 17 00:00:00 2001 From: omenem Date: Fri, 15 May 2020 18:52:57 +0200 Subject: [PATCH 04/13] some tests --- .../smarthut/DimmableStateTests.java | 38 ++++++++++++ .../sa4/sanmarinoes/smarthut/DimmerTests.java | 44 +++++++++++++ .../smarthut/MotionSensorTests.java | 26 ++++++++ .../smarthut/RangeConditionTests.java | 61 ++++++++++++++++++ .../smarthut/RangeTriggerTests.java | 62 +++++++++++++++++++ .../smarthut/ScenePriorityTests.java | 43 +++++++++++++ .../sa4/sanmarinoes/smarthut/SceneTests.java | 52 ++++++++++++++++ .../smarthut/SecurityCameraTests.java | 44 +++++++++++++ .../sa4/sanmarinoes/smarthut/SensorTests.java | 46 ++++++++++++++ .../sa4/sanmarinoes/smarthut/StateTests.java | 58 +++++++++++++++++ 10 files changed, 474 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmerTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/MotionSensorTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeConditionTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeTriggerTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ScenePriorityTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SceneTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SecurityCameraTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SensorTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java new file mode 100644 index 0000000..87bd181 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java @@ -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, this.dimmableState.getDevice().getIntensity()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmerTests.java new file mode 100644 index 0000000..b3971b8 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmerTests.java @@ -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))); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/MotionSensorTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/MotionSensorTests.java new file mode 100644 index 0000000..9a547e0 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/MotionSensorTests.java @@ -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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeConditionTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeConditionTests.java new file mode 100644 index 0000000..354d6da --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeConditionTests.java @@ -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.RangeCondition; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition.Operator; +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(RangeCondition.Operator.EQUAL); + + assertEquals(RangeCondition.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(RangeCondition.Operator.EQUAL); + assertFalse(rangeCondition.triggered()); + + rangeCondition.setOperator(RangeCondition.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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeTriggerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeTriggerTests.java new file mode 100644 index 0000000..6dda510 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeTriggerTests.java @@ -0,0 +1,62 @@ +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.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(RangeTrigger.Operator.EQUAL); + + assertEquals(RangeTrigger.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(RangeTrigger.Operator.EQUAL); + assertFalse(rangeTrigger.triggered()); + + rangeTrigger.setOperator(RangeTrigger.Operator.LESS); + assertTrue(rangeTrigger.triggered()); + + rangeTrigger.setOperator(RangeTrigger.Operator.GREATER); + assertFalse(rangeTrigger.triggered()); + + rangeTrigger.setOperator(RangeTrigger.Operator.LESS_EQUAL); + assertTrue(rangeTrigger.triggered()); + + rangeTrigger.setOperator(RangeTrigger.Operator.GREATER_EQUAL); + assertFalse(rangeTrigger.triggered()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ScenePriorityTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ScenePriorityTests.java new file mode 100644 index 0000000..6d27fbf --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ScenePriorityTests.java @@ -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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SceneTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SceneTests.java new file mode 100644 index 0000000..28c6f84 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SceneTests.java @@ -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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SecurityCameraTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SecurityCameraTests.java new file mode 100644 index 0000000..a9ed512 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SecurityCameraTests.java @@ -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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SensorTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SensorTests.java new file mode 100644 index 0000000..0326d70 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SensorTests.java @@ -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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java new file mode 100644 index 0000000..85dc695 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java @@ -0,0 +1,58 @@ +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 id") + public void getAndSetId() { + this.state.setId(20); + assertEquals(20, this.state.getId()); + } + + @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()); + } +} From 7020f0d692a0f18d3de0db4d59d26100dba140ba Mon Sep 17 00:00:00 2001 From: omenem Date: Fri, 15 May 2020 19:01:28 +0200 Subject: [PATCH 05/13] fix --- .../ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java index bc093e0..e72cbf6 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java @@ -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; @@ -19,7 +20,7 @@ public class Dimmable extends Switchable implements RangeTriggerable { @ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH) @GsonExclude @SocketGsonExclude - private Set dimmers; + private Set dimmers = new HashSet<>(); /** The light intensity value. Goes from 0 (off) to 100 (on) */ @NotNull From 3f568c77d32b3ac395c3c07b4337e95f578b5584 Mon Sep 17 00:00:00 2001 From: omenem Date: Sat, 16 May 2020 18:00:28 +0200 Subject: [PATCH 06/13] more tests --- .../sanmarinoes/smarthut/SmartPlugTests.java | 43 +++++++++++++++++++ .../smarthut/SwitchableStateTests.java | 28 ++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartPlugTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartPlugTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartPlugTests.java new file mode 100644 index 0000000..921979e --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartPlugTests.java @@ -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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateTests.java new file mode 100644 index 0000000..c89e04f --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateTests.java @@ -0,0 +1,28 @@ +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.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()); + } +} From 198be1f66860b8db3f01a146edd6be0e8928bdda Mon Sep 17 00:00:00 2001 From: omenem Date: Sun, 17 May 2020 10:16:12 +0200 Subject: [PATCH 07/13] more tests --- .../smarthut/ThermostatConditionTests.java | 53 ++++++++++++ .../sanmarinoes/smarthut/TriggerTests.java | 64 ++++++++++++++ .../sa4/sanmarinoes/smarthut/UserTests.java | 83 +++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatConditionTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/TriggerTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatConditionTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatConditionTests.java new file mode 100644 index 0000000..d19ce69 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatConditionTests.java @@ -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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/TriggerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/TriggerTests.java new file mode 100644 index 0000000..3790f3a --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/TriggerTests.java @@ -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()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserTests.java new file mode 100644 index 0000000..1d16b16 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserTests.java @@ -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)); + } +} From 0ce6e84380b80104eb3848687d81dc75162ea1bd Mon Sep 17 00:00:00 2001 From: omenem Date: Sun, 17 May 2020 11:26:49 +0200 Subject: [PATCH 08/13] fix --- .../sanmarinoes/smarthut/DimmableStateTests.java | 4 ++-- .../sanmarinoes/smarthut/RangeConditionTests.java | 10 +++++----- .../sanmarinoes/smarthut/RangeTriggerTests.java | 15 ++++++++------- .../smarthut/SwitchableStateTests.java | 5 ++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java index 87bd181..7dbfe35 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java @@ -11,11 +11,11 @@ import org.junit.jupiter.api.Test; @DisplayName("Dimmable State Tests") public class DimmableStateTests { - private DimmableState dimmableState; + private DimmableState dimmableState; @BeforeEach public void createDimmableState() { - dimmableState = new DimmableState<>(); + dimmableState = new DimmableState(); } @Test diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeConditionTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeConditionTests.java index 354d6da..1d7772e 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeConditionTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeConditionTests.java @@ -3,8 +3,8 @@ 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 ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition.Operator; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -22,9 +22,9 @@ public class RangeConditionTests { @Test @DisplayName("set and get operator") public void setAndGetOperator() { - rangeCondition.setOperator(RangeCondition.Operator.EQUAL); + rangeCondition.setOperator(Operator.EQUAL); - assertEquals(RangeCondition.Operator.EQUAL, rangeCondition.getOperator()); + assertEquals(Operator.EQUAL, rangeCondition.getOperator()); } @Test @@ -43,10 +43,10 @@ public class RangeConditionTests { rangeCondition.setDevice(d); rangeCondition.setRange(45D); - rangeCondition.setOperator(RangeCondition.Operator.EQUAL); + rangeCondition.setOperator(Operator.EQUAL); assertFalse(rangeCondition.triggered()); - rangeCondition.setOperator(RangeCondition.Operator.LESS); + rangeCondition.setOperator(Operator.LESS); assertTrue(rangeCondition.triggered()); rangeCondition.setOperator(Operator.GREATER); diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeTriggerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeTriggerTests.java index 6dda510..4c46d70 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeTriggerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RangeTriggerTests.java @@ -5,6 +5,7 @@ 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; @@ -23,9 +24,9 @@ public class RangeTriggerTests { @Test @DisplayName("set and get operator") public void setAndGetOperator() { - rangeTrigger.setOperator(RangeTrigger.Operator.EQUAL); + rangeTrigger.setOperator(Operator.EQUAL); - assertEquals(RangeTrigger.Operator.EQUAL, rangeTrigger.getOperator()); + assertEquals(Operator.EQUAL, rangeTrigger.getOperator()); } @Test @@ -44,19 +45,19 @@ public class RangeTriggerTests { rangeTrigger.setDevice(d); rangeTrigger.setRange(45D); - rangeTrigger.setOperator(RangeTrigger.Operator.EQUAL); + rangeTrigger.setOperator(Operator.EQUAL); assertFalse(rangeTrigger.triggered()); - rangeTrigger.setOperator(RangeTrigger.Operator.LESS); + rangeTrigger.setOperator(Operator.LESS); assertTrue(rangeTrigger.triggered()); - rangeTrigger.setOperator(RangeTrigger.Operator.GREATER); + rangeTrigger.setOperator(Operator.GREATER); assertFalse(rangeTrigger.triggered()); - rangeTrigger.setOperator(RangeTrigger.Operator.LESS_EQUAL); + rangeTrigger.setOperator(Operator.LESS_EQUAL); assertTrue(rangeTrigger.triggered()); - rangeTrigger.setOperator(RangeTrigger.Operator.GREATER_EQUAL); + rangeTrigger.setOperator(Operator.GREATER_EQUAL); assertFalse(rangeTrigger.triggered()); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateTests.java index c89e04f..737053d 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateTests.java @@ -2,7 +2,6 @@ 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.SwitchableState; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -11,11 +10,11 @@ import org.junit.jupiter.api.Test; @DisplayName("Switchable State Tests") public class SwitchableStateTests { - private SwitchableState switchableState; + private SwitchableState switchableState; @BeforeEach public void createSwitchableState() { - switchableState = new SwitchableState<>(); + switchableState = new SwitchableState(); } @Test From 940c072711d494a286d2776b6ee7ec931528ccaa Mon Sep 17 00:00:00 2001 From: omenem Date: Sun, 17 May 2020 11:33:30 +0200 Subject: [PATCH 09/13] fix --- .../usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java | 2 +- .../java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java index 7dbfe35..3c19612 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableStateTests.java @@ -33,6 +33,6 @@ public class DimmableStateTests { this.dimmableState.setDevice(d); this.dimmableState.setIntensity(30); this.dimmableState.apply(); - assertEquals(30, this.dimmableState.getDevice().getIntensity()); + assertEquals(30, d.getIntensity()); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java index 85dc695..5dd87fa 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java @@ -12,11 +12,11 @@ import org.junit.jupiter.api.Test; @DisplayName("Curtains tests") public class StateTests { - private DimmableState state; + private DimmableState state; @BeforeEach public void createState() { - this.state = new DimmableState<>(); + this.state = new DimmableState(); } @Test From c5b3a57faa10560cb69fc5305ae1f72be3f4c8fe Mon Sep 17 00:00:00 2001 From: omenem Date: Sun, 17 May 2020 11:52:37 +0200 Subject: [PATCH 10/13] fix --- .../ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java index 5dd87fa..3c5ef86 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/StateTests.java @@ -19,13 +19,6 @@ public class StateTests { this.state = new DimmableState(); } - @Test - @DisplayName("get and set id") - public void getAndSetId() { - this.state.setId(20); - assertEquals(20, this.state.getId()); - } - @Test @DisplayName("get and set device") public void getAndSetDevice() { From bc7b30f3eae67af3f2a7b01e9438bdf4cf35c49f Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 17 May 2020 12:49:40 +0200 Subject: [PATCH 11/13] Fixed lombok in AutomationFastUpdateRequest --- .../dto/AutomationFastUpdateRequest.java | 38 +++++++++---------- .../AutomationFastUpdateRequestTest.java | 36 +++++++++--------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java index f086fef..667645e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java @@ -6,37 +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 -@Getter -@Setter 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 private Operator operator; - @NotNull private 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; @@ -44,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(); @@ -59,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; } @@ -79,13 +79,13 @@ public class AutomationFastUpdateRequest { public static class RangeConditionDTO extends ConditionDTO { - @NotNull private Operator operator; - @NotNull private 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; @@ -94,13 +94,13 @@ public class AutomationFastUpdateRequest { public static class ThermostatConditionDTO extends ConditionDTO { - @NotNull private 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; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java index 013f067..4f10fc6 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AutomationFastUpdateRequestTest.java @@ -21,11 +21,11 @@ public class AutomationFastUpdateRequestTest { @DisplayName(" checking boolean trigger ") public void booleanTriggerDTOTest() { booleanTriggerDTO = new AutomationFastUpdateRequest.BooleanTriggerDTO(); - booleanTriggerDTO.on = true; - booleanTriggerDTO.deviceId = 42; + booleanTriggerDTO.setOn(true); + booleanTriggerDTO.setDeviceId(42); BooleanTrigger booleanTrigger = (BooleanTrigger) booleanTriggerDTO.toModel(); - assertEquals(booleanTrigger.isOn(), booleanTriggerDTO.on); - assertEquals(booleanTrigger.getDeviceId(), booleanTriggerDTO.deviceId); + assertEquals(booleanTrigger.isOn(), booleanTriggerDTO.isOn()); + assertEquals(booleanTrigger.getDeviceId(), booleanTriggerDTO.getDeviceId()); } @Test @@ -33,37 +33,37 @@ public class AutomationFastUpdateRequestTest { public void rangeTriggerDTOTest() { rangeTriggerDTO = new AutomationFastUpdateRequest.RangeTriggerDTO(); rangeTriggerDTO.setOperator(Operator.EQUAL); - rangeTriggerDTO.deviceId = 420; + 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.deviceId); + assertEquals(rangeTrigger.getDeviceId(), rangeTriggerDTO.getDeviceId()); } @Test @DisplayName(" checking scene priority ") public void scenePriorityDTOTest() { scenePriorityDTO = new AutomationFastUpdateRequest.ScenePriorityDTO(); - scenePriorityDTO.priority = 67; - scenePriorityDTO.sceneId = 21; + scenePriorityDTO.setPriority(67); + scenePriorityDTO.setSceneId(21); ScenePriority scenePriority = scenePriorityDTO.toModel(); - assertEquals(scenePriority.getPriority(), scenePriorityDTO.priority); - assertEquals(scenePriority.getSceneId(), scenePriorityDTO.priority); + assertEquals(scenePriority.getPriority(), scenePriorityDTO.getPriority()); + assertEquals(scenePriority.getSceneId(), scenePriorityDTO.getSceneId()); } @Test @DisplayName(" checking boolean condition ") public void booleanConditionDTOTest() { booleanConditionDTO = new AutomationFastUpdateRequest.BooleanConditionDTO(); - booleanConditionDTO.on = true; - booleanConditionDTO.deviceId = 17; + booleanConditionDTO.setOn(true); + booleanConditionDTO.setDeviceId(17); BooleanCondition booleanCondition = (BooleanCondition) booleanConditionDTO.toModel(); - assertEquals(booleanCondition.isOn(), booleanConditionDTO.on); - assertEquals(booleanCondition.getDeviceId(), booleanConditionDTO.deviceId); + assertEquals(booleanCondition.isOn(), booleanConditionDTO.isOn()); + assertEquals(booleanCondition.getDeviceId(), booleanConditionDTO.getDeviceId()); } @Test @@ -72,19 +72,19 @@ public class AutomationFastUpdateRequestTest { rangeConditionDTO = new AutomationFastUpdateRequest.RangeConditionDTO(); rangeConditionDTO.setOperator(Operator.LESS); rangeConditionDTO.setRange(82.01); - rangeConditionDTO.deviceId = 13; + rangeConditionDTO.setDeviceId(13); RangeCondition rangeCondition = (RangeCondition) rangeConditionDTO.toModel(); assertEquals(rangeCondition.getOperator(), rangeConditionDTO.getOperator()); assertEquals(rangeCondition.getRange(), rangeConditionDTO.getRange()); - assertEquals(rangeCondition.getDeviceId(), rangeConditionDTO.deviceId); + assertEquals(rangeCondition.getDeviceId(), rangeConditionDTO.getDeviceId()); } @Test @DisplayName(" checking thermostat condition ") public void thermostatConditionDTOTest() { thermostatConditionDTO = new AutomationFastUpdateRequest.ThermostatConditionDTO(); - thermostatConditionDTO.deviceId = 25; + thermostatConditionDTO.setDeviceId(25); thermostatConditionDTO.setOperator(ThermostatCondition.Operator.EQUAL); thermostatConditionDTO.setMode(Thermostat.Mode.HEATING); @@ -92,6 +92,6 @@ public class AutomationFastUpdateRequestTest { (ThermostatCondition) thermostatConditionDTO.toModel(); assertEquals(thermostatCondition.getMode(), thermostatConditionDTO.getMode()); assertEquals(thermostatCondition.getOperator(), thermostatConditionDTO.getOperator()); - assertEquals(thermostatCondition.getDeviceId(), thermostatConditionDTO.deviceId); + assertEquals(thermostatCondition.getDeviceId(), thermostatConditionDTO.getDeviceId()); } } From 4e35aa4a7588ac50ec5648a8788b5a23ef87e2f5 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 17 May 2020 13:26:51 +0200 Subject: [PATCH 12/13] Tests on exception classes --- .../dto/ThermostatConditionSaveRequest.java | 41 +---------- .../sanmarinoes/smarthut/ExceptionTests.java | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+), 39 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ExceptionTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequest.java index 8c56f2d..c45b482 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequest.java @@ -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; - } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ExceptionTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ExceptionTests.java new file mode 100644 index 0000000..07ff076 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ExceptionTests.java @@ -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"); + } + } +} From eeec7269660fc1725cc324fa215b4fb0374325d7 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 17 May 2020 13:56:54 +0200 Subject: [PATCH 13/13] Tests on Utils and UserResponse --- .../smarthut/UserResponseTests.java | 21 ++++++++ .../sa4/sanmarinoes/smarthut/UtilsTests.java | 48 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserResponseTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UtilsTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserResponseTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserResponseTests.java new file mode 100644 index 0000000..010976c --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UserResponseTests.java @@ -0,0 +1,21 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserResponse; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import org.junit.jupiter.api.Test; + +public class UserResponseTests { + @Test + public void testUserResponse() { + User u = new User(); + u.setEmail("email@example.com"); + u.setId(42L); + u.setUsername("username"); + UserResponse us = UserResponse.fromUser(u); + assertEquals(us.getName(), u.getName()); + assertEquals(us.getId(), u.getId()); + assertEquals(us.getUsername(), u.getUsername()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UtilsTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UtilsTests.java new file mode 100644 index 0000000..0ac7d71 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UtilsTests.java @@ -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 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); + } +}