From 58a8b939fa2e5a2b134452940bb398a83933bb61 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Thu, 21 May 2020 21:46:29 +0200 Subject: [PATCH 1/2] Finished tests on DevicePropagationService --- .../dto/SwitchOperationRequestTests.java | 4 +- .../ThermostatConditionSaveRequestTests.java | 10 +- .../DevicePropagationServiceTests.java | 120 ++++++++++++++++-- 3 files changed, 114 insertions(+), 20 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchOperationRequestTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchOperationRequestTests.java index 74fafc3..dac05c8 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchOperationRequestTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchOperationRequestTests.java @@ -18,8 +18,8 @@ public class SwitchOperationRequestTests { @Test @DisplayName("test setId") public void testSetId() { - request.setId(42l); - assertEquals(42l, request.getId()); + request.setId(42L); + assertEquals(42L, request.getId()); } @Test diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequestTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequestTests.java index 51b4fd2..9283e6f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequestTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequestTests.java @@ -21,15 +21,15 @@ public class ThermostatConditionSaveRequestTests { @Test @DisplayName("test setDeviceId") public void testSetDeviceId() { - this.saveRequest.setDeviceId(42l); - assertEquals(42l, saveRequest.getDeviceId()); + this.saveRequest.setDeviceId(42L); + assertEquals(42L, saveRequest.getDeviceId()); } @Test @DisplayName("test setAutomationId") public void testSetAutomationId() { - this.saveRequest.setAutomationId(42l); - assertEquals(42l, saveRequest.getAutomationId()); + this.saveRequest.setAutomationId(42L); + assertEquals(42L, saveRequest.getAutomationId()); } @Test @@ -56,6 +56,6 @@ public class ThermostatConditionSaveRequestTests { @Test @DisplayName("test getId") public void testGetId() { - assertEquals(0l, saveRequest.getId()); + assertEquals(0L, saveRequest.getId()); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationServiceTests.java index e26f69e..47de5e1 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationServiceTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationServiceTests.java @@ -1,12 +1,15 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; import java.util.List; import java.util.Optional; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -24,6 +27,24 @@ public class DevicePropagationServiceTests { @Mock private EagerUserRepository userRepository; + private User host; + private User guest; + + private void initHostGuest() { + host = new User(); + host.setName("host"); + host.setUsername("host"); + host.setId(1L); + + guest = new User(); + guest.setName("guest"); + guest.setUsername("guest"); + guest.setId(2L); + + guest.getHosts().add(host); + host.getGuests().add(guest); + } + @Test public void testPropagateUpdateAsGuest() { Device toPropagate = new SecurityCamera(); @@ -59,16 +80,9 @@ public class DevicePropagationServiceTests { @Test public void saveAllAsGuestSceneApplication() { - User host = new User(); - host.setName("host"); - host.setId(1L); - - User guest = new User(); - guest.setName("guest"); - guest.setId(2L); - - guest.getHosts().add(host); - host.getGuests().add(guest); + initHostGuest(); + when(userRepository.findById(1L)).thenReturn(Optional.of(host)); + when(userRepository.findByUsername("guest")).thenReturn(guest); int[] done = new int[1]; @@ -79,9 +93,6 @@ public class DevicePropagationServiceTests { .propagateUpdateAsGuest(any(), eq(host), eq(guest)); when(deviceRepository.saveAll(any())).thenAnswer(i -> i.getArguments()[0]); - when(userRepository.findById(1L)).thenReturn(Optional.of(host)); - when(userRepository.findByUsername("guest")).thenReturn(guest); - devicePropagationService1.saveAllAsGuestSceneApplication( List.of(new SecurityCamera(), new ButtonDimmer()), "guest", 1L); @@ -126,4 +137,87 @@ public class DevicePropagationServiceTests { assertThat(dps.saveAllAsOwner(dl, "user", true, false)).containsExactly(d); assertThat(dps.saveAllAsOwner(dl, "user", false, true)).containsExactly(d); } + + @Test + public void testSaveAllAsGuest() { + final DevicePropagationService dps = Mockito.spy(devicePropagationService); + + initHostGuest(); + when(userRepository.findById(1L)).thenReturn(Optional.of(host)); + when(userRepository.findByUsername("guest")).thenReturn(guest); + when(userRepository.findById(42L)).thenReturn(Optional.empty()); + + final User phonyGuest = new User(); + phonyGuest.setName("phonyguest"); + + when(userRepository.findByUsername("phonyguest")).thenReturn(phonyGuest); + + final Device d = new ButtonDimmer(); + + doNothing().when(dps).renameIfDuplicate(d, "host"); + + assertThatThrownBy(() -> dps.saveAsGuest(d, "phonyguest", 1L)) + .isInstanceOf(NotFoundException.class); + assertThatThrownBy(() -> dps.saveAsGuest(d, "guest", 42L)) + .isInstanceOf(NotFoundException.class); + + when(deviceRepository.save(d)).thenReturn(d); + doNothing().when(dps).propagateUpdateAsGuest(d, host, guest); + + Assertions.assertDoesNotThrow(() -> dps.saveAsGuest(d, "guest", 1L)); + } + + @Test + public void testSaveAsOwner() { + final DevicePropagationService dps = Mockito.spy(devicePropagationService); + final Device d = new ButtonDimmer(); + doNothing().when(dps).renameIfDuplicate(d, "user"); + doNothing().when(dps).propagateUpdateAsOwner(d, "user", false); + when(deviceRepository.save(d)).thenReturn(d); + assertThat(dps.saveAsOwner(d, "user")).isSameAs(d); + } + + @Test + public void testDeleteByIdAsOwner() { + initHostGuest(); + + final Device d = new ButtonDimmer(); + + boolean[] done = new boolean[1]; + when(userRepository.findByUsername("host")).thenReturn(host); + when(deviceRepository.findByIdAndUsername(42L, "host")).thenReturn(Optional.of(d)); + when(deviceRepository.findByIdAndUsername(43L, "host")).thenReturn(Optional.empty()); + doAnswer(i -> done[0] = true).when(deviceRepository).delete(d); + doNothing().when(endpoint).queueDeviceUpdate(d, guest, false, host.getId(), true); + + assertThatThrownBy(() -> devicePropagationService.deleteByIdAsOwner(43L, "host")) + .isInstanceOf(NotFoundException.class); + Assertions.assertDoesNotThrow( + () -> devicePropagationService.deleteByIdAsOwner(42L, "host")); + + assertThat(done[0]).isTrue(); + } + + @Test + public void testPropagateUpdateAsOwner() { + initHostGuest(); + when(userRepository.findByUsername("host")).thenReturn(host); + + final Device d = new ButtonDimmer(); + + int[] counter = new int[1]; + + doAnswer(i -> counter[0]++) + .when(endpoint) + .queueDeviceUpdate(d, guest, false, host.getId(), false); + doAnswer(i -> counter[0]++) + .when(endpoint) + .queueDeviceUpdate(d, host, false, host.getId(), false); + + devicePropagationService.propagateUpdateAsOwner(d, "host", false); + assertThat(counter[0]).isEqualTo(1); + counter[0] = 0; + devicePropagationService.propagateUpdateAsOwner(d, "host", true); + assertThat(counter[0]).isEqualTo(2); + } } From 1a289f6800ddf039e0777045c21a9ae393d8fbe7 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Thu, 21 May 2020 22:03:38 +0200 Subject: [PATCH 2/2] Fixed some code smells --- .../smarthut/models/BooleanCondition.java | 2 ++ .../smarthut/models/ConfirmationToken.java | 8 ++++++++ .../sanmarinoes/smarthut/models/Dimmable.java | 3 +++ .../sa4/sanmarinoes/smarthut/models/Dimmer.java | 3 +++ .../sanmarinoes/smarthut/models/KnobDimmer.java | 2 ++ .../smarthut/models/MotionSensor.java | 2 ++ .../smarthut/models/RangeCondition.java | 2 ++ .../smarthut/models/RegularLight.java | 2 ++ .../smarthut/models/SecurityCamera.java | 2 ++ .../sa4/sanmarinoes/smarthut/models/Sensor.java | 2 ++ .../sanmarinoes/smarthut/models/SmartPlug.java | 2 ++ .../sa4/sanmarinoes/smarthut/models/Switch.java | 2 ++ .../sanmarinoes/smarthut/models/Switchable.java | 2 ++ .../sanmarinoes/smarthut/models/Thermostat.java | 2 ++ .../smarthut/models/ThermostatCondition.java | 2 ++ .../smarthut/service/DeviceService.java | 2 +- .../controller/AutomationControllerTests.java | 3 ++- .../controller/SceneControllerTests.java | 3 ++- .../controller/SmartPlugControllerTests.java | 3 ++- .../smarthut/error/ExceptionTests.java | 16 ++++++++-------- .../smarthut/service/DeviceServiceTests.java | 3 ++- 21 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java index 4097c26..30d47ae 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java @@ -2,10 +2,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import javax.persistence.Column; import javax.persistence.Entity; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @Entity +@EqualsAndHashCode(callSuper = true) public class BooleanCondition extends Condition { @Getter diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java index 5271a83..5f61bc9 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java @@ -17,6 +17,14 @@ public class ConfirmationToken { @Column(name = "confirmation_token", unique = true) private String confirmToken; + public Date getCreatedDate() { + return new Date(createdDate.getTime()); + } + + public void setCreatedDate(Date createdDate) { + this.createdDate = new Date(createdDate.getTime()); + } + @Temporal(TemporalType.TIMESTAMP) private Date createdDate; 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 dd4c6c8..95f8e0d 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 @@ -7,10 +7,12 @@ import java.util.Set; import javax.persistence.*; import javax.validation.constraints.Max; import javax.validation.constraints.Min; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @Entity +@EqualsAndHashCode(callSuper = true) @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Dimmable extends Switchable implements RangeTriggerable { @@ -70,6 +72,7 @@ public class Dimmable extends Switchable implements RangeTriggerable { setIntensity(state.getIntensity()); } + @Override public State cloneState() { final DimmableState newState = new DimmableState(); newState.setDeviceId(getId()); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java index 109e13e..205ddfd 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java @@ -5,9 +5,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import java.util.HashSet; import java.util.Set; import javax.persistence.*; +import lombok.EqualsAndHashCode; /** Represents a generic dimmer input device */ @Entity +@EqualsAndHashCode(callSuper = true) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class Dimmer extends InputDevice implements Connectable { protected Dimmer(String kind) { @@ -17,6 +19,7 @@ public abstract class Dimmer extends InputDevice implements Connectable { public RangeCondition() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java index feac0fb..1ef8412 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java @@ -2,11 +2,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import javax.persistence.Column; import javax.persistence.Entity; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; /** Represents a standard non-dimmable light */ @Entity +@EqualsAndHashCode(callSuper = true) public class RegularLight extends Switchable implements BooleanTriggerable { /** Whether the light is on or not */ diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java index ccb1224..b0afc82 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java @@ -2,8 +2,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import javax.persistence.Column; import javax.persistence.Entity; +import lombok.EqualsAndHashCode; @Entity +@EqualsAndHashCode(callSuper = true) public class SecurityCamera extends Switchable implements BooleanTriggerable { public SecurityCamera() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java index 58aa7f9..f4d444a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java @@ -7,9 +7,11 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; +import lombok.EqualsAndHashCode; /** A sensor input device that measures a quantity in a continuous scale (e.g. temperature) */ @Entity +@EqualsAndHashCode(callSuper = true) public class Sensor extends InputDevice implements RangeTriggerable { public static final Map TYPICAL_VALUES = diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java index b69e3e7..11d964b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java @@ -3,9 +3,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Entity; +import lombok.EqualsAndHashCode; /** A smart plug that can be turned either on or off */ @Entity +@EqualsAndHashCode(callSuper = true) public class SmartPlug extends Switchable implements BooleanTriggerable { /** The average consumption of an active plug when on in Watt */ diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index 359ecad..d67bfad 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -5,9 +5,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import java.util.HashSet; import java.util.Set; import javax.persistence.*; +import lombok.EqualsAndHashCode; /** A switch input device */ @Entity +@EqualsAndHashCode(callSuper = true) public class Switch extends InputDevice implements BooleanTriggerable, Connectable { @ManyToMany( diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java index f2d2c44..b327cf8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java @@ -5,9 +5,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import java.util.HashSet; import java.util.Set; import javax.persistence.*; +import lombok.EqualsAndHashCode; /** A device that can be turned either on or off */ @Entity +@EqualsAndHashCode(callSuper = true) @Inheritance(strategy = InheritanceType.JOINED) public abstract class Switchable extends OutputDevice { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java index a2a71a1..229f7f6 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java @@ -5,9 +5,11 @@ import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Transient; +import lombok.EqualsAndHashCode; /** A thermostat capable of controlling cooling and heating. */ @Entity +@EqualsAndHashCode(callSuper = true) public class Thermostat extends Switchable implements BooleanTriggerable { @Override diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java index 2bb65d1..c621c9b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java @@ -3,8 +3,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import com.google.gson.annotations.SerializedName; import javax.persistence.Column; import javax.persistence.Entity; +import lombok.EqualsAndHashCode; @Entity +@EqualsAndHashCode(callSuper = true) public class ThermostatCondition extends Condition { public ThermostatCondition() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java index c28ad1f..07adb5a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java @@ -66,7 +66,7 @@ public class DeviceService { a -> { final List> conditions = conditionRepository.findAllByAutomationId(a.getId()); - if (conditions.size() == 0) return true; + if (conditions.isEmpty()) return true; return conditions.stream().allMatch(Condition::triggered); }) .map(Automation::getScenes) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java index 452ffc7..39d8193 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java @@ -15,6 +15,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.security.Principal; import java.util.List; import java.util.Optional; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -186,6 +187,6 @@ public class AutomationControllerTests { old.setId(42L); old.setName("Old Name"); doNothing().when(automationRepository).deleteById(42L); - automationController.delete(42L); + Assertions.assertDoesNotThrow(() -> automationController.delete(42L)); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java index a1deaa0..33f7331 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java @@ -12,6 +12,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.security.Principal; import java.util.List; import java.util.Optional; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -93,6 +94,6 @@ public class SceneControllerTests { public void testDelete() { doNothing().when(stateStateRepository).deleteAllBySceneId(42L); doNothing().when(sceneRepository).deleteById(42L); - sceneController.deleteById(42L); + Assertions.assertDoesNotThrow(() -> sceneController.deleteById(42L)); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java index ddb4cc8..3907f3d 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java @@ -14,6 +14,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; import java.util.Optional; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -88,6 +89,6 @@ public class SmartPlugControllerTests { public void testDelete() throws NotFoundException { when(mockPrincipal.getName()).thenReturn("user"); doNothing().when(deviceService).deleteByIdAsOwner(42L, "user"); - smartPlugController.deleteById(42L, mockPrincipal); + Assertions.assertDoesNotThrow(() -> smartPlugController.deleteById(42L, mockPrincipal)); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/ExceptionTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/ExceptionTests.java index adf0a52..bf26000 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/ExceptionTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/ExceptionTests.java @@ -12,7 +12,7 @@ public class ExceptionTests { try { throw new BadDataException("message"); } catch (BadDataException e) { - assertEquals(e.getMessage(), "message"); + assertEquals("message", e.getMessage()); } } @@ -21,7 +21,7 @@ public class ExceptionTests { try { throw new DuplicateRegistrationException(); } catch (DuplicateRegistrationException e) { - assertEquals(e.getMessage(), "Email or username already belonging to another user"); + assertEquals("Email or username already belonging to another user", e.getMessage()); } } @@ -31,8 +31,8 @@ public class ExceptionTests { throw new DuplicateStateException(); } catch (DuplicateStateException e) { assertEquals( - e.getMessage(), - "Cannot create state since it has already been created for this scene and this device"); + "Cannot create state since it has already been created for this scene and this device", + e.getMessage()); } } @@ -41,7 +41,7 @@ public class ExceptionTests { try { throw new EmailTokenNotFoundException(); } catch (EmailTokenNotFoundException e) { - assertEquals(e.getMessage(), "Email verification token not found in DB"); + assertEquals("Email verification token not found in DB", e.getMessage()); } } @@ -50,13 +50,13 @@ public class ExceptionTests { try { throw new NotFoundException(); } catch (NotFoundException e) { - assertEquals(e.getMessage(), "Not found"); + assertEquals("Not found", e.getMessage()); } try { throw new NotFoundException("message"); } catch (NotFoundException e) { - assertEquals(e.getMessage(), "message not found"); + assertEquals("message not found", e.getMessage()); } } @@ -65,7 +65,7 @@ public class ExceptionTests { try { throw new UserNotFoundException(); } catch (UserNotFoundException e) { - assertEquals(e.getMessage(), "No user found with given email"); + assertEquals("No user found with given email", e.getMessage()); } } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceServiceTests.java index 37db737..cc61142 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceServiceTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceServiceTests.java @@ -8,6 +8,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.List; import java.util.Optional; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -151,7 +152,7 @@ public class DeviceServiceTests { @Test public void populateComputedFields() { doNothing().when(devicePopulationService).populateComputedFields(List.of()); - deviceService.populateComputedFields(List.of()); + Assertions.assertDoesNotThrow(() -> deviceService.populateComputedFields(List.of())); } @Test