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 b9bb68a..aec1231 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 @@ -1,6 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java index 45ad962..7f2829f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java @@ -1,6 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertTrue; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; 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 index b3971b8..0d2b24e 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmerTests.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +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.KnobDimmer; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java index 8eb7f74..2a77c47 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java @@ -1,6 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; 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 4c46d70..28e2a29 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 @@ -1,8 +1,6 @@ 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 static org.junit.jupiter.api.Assertions.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Operator; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java index c4f4694..17ec2e4 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RoomTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RoomTests.java index 781473b..e6fd907 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RoomTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RoomTests.java @@ -6,7 +6,6 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; -import javax.swing.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; 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 index a9ed512..857f64e 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SecurityCameraTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SecurityCameraTests.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SecurityCamera; import org.junit.jupiter.api.BeforeEach; 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 index 0326d70..4cc53c8 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SensorTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SensorTests.java @@ -1,6 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor.SensorType; 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 index 921979e..a586128 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartPlugTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartPlugTests.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SmartPlug; import java.math.BigDecimal; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java index 9f937d3..7b1738d 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableSaveRequestTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableSaveRequestTests.java index ec37aec..816c588 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableSaveRequestTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableSaveRequestTests.java @@ -1,8 +1,6 @@ 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 static org.junit.jupiter.api.Assertions.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateSaveRequestTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateSaveRequestTests.java index d3619cf..7a3be15 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateSaveRequestTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchableStateSaveRequestTests.java @@ -1,8 +1,6 @@ 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 static org.junit.jupiter.api.Assertions.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableStateSaveRequest; import org.junit.jupiter.api.BeforeEach; 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 737053d..d95d01c 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 @@ -1,6 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertTrue; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatSaveRequestTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatSaveRequestTests.java index dac79f2..90dbc3f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatSaveRequestTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatSaveRequestTests.java @@ -1,7 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.assertFalse; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatSaveRequest; import java.math.BigDecimal; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatTests.java index ecc3d9b..abff3be 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ThermostatTests.java @@ -1,8 +1,6 @@ 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 static org.junit.jupiter.api.Assertions.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; import java.math.BigDecimal; 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 index 3790f3a..eeced96 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/TriggerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/TriggerTests.java @@ -1,6 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger; 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 index 0ac7d71..554783b 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UtilsTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/UtilsTests.java @@ -1,7 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; import static org.assertj.core.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.when; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; 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 1805aba..b0e3a68 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 @@ -2,16 +2,20 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import java.security.Principal; import java.util.List; import java.util.Optional; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java index 2ae72f2..c86a1ec 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java @@ -3,7 +3,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; import static org.springframework.test.util.AssertionErrors.fail; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest; 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 index 6ef3158..56b15b5 100644 --- 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 @@ -2,7 +2,8 @@ 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 static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; @@ -11,7 +12,9 @@ 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.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; 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 new file mode 100644 index 0000000..897515c --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationServiceTests.java @@ -0,0 +1,90 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +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.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class DevicePropagationServiceTests { + @InjectMocks private DevicePropagationService devicePropagationService; + + @Mock private SensorSocketEndpoint endpoint; + + @Mock private DeviceRepository deviceRepository; + + @Mock private EagerUserRepository userRepository; + + @Test + public void testPropagateUpdateAsGuest() { + Device toPropagate = new SecurityCamera(); + + User host = new User(); + host.setName("host"); + host.setId(1L); + + User guest = new User(); + guest.setName("guest"); + guest.setId(2L); + + User guest2 = new User(); + guest.setName("guest2"); + guest.setId(3L); + + guest.getHosts().add(host); + host.getGuests().add(guest); + guest2.getHosts().add(host); + host.getGuests().add(guest2); + + doNothing().when(endpoint).queueDeviceUpdate(toPropagate, host, true, null, false); + + boolean[] done = new boolean[1]; + + doAnswer(i -> done[0] = true) + .when(endpoint) + .queueDeviceUpdate(toPropagate, guest2, false, host.getId(), false); + + devicePropagationService.propagateUpdateAsGuest(toPropagate, host, guest); + assertThat(done[0]).isTrue(); + } + + @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); + + int[] done = new int[1]; + + final DevicePropagationService devicePropagationService1 = + Mockito.spy(devicePropagationService); + doAnswer(i -> done[0]++) + .when(devicePropagationService1) + .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); + + assertThat(done[0]).isEqualTo(2); + } +}