From ae10998c9861e9d164961a4916720f8a5a658d88 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 20 May 2020 16:39:44 +0200 Subject: [PATCH] Some tests on DevicePropagationService --- .../service/DevicePropagationService.java | 2 +- .../DevicePropagationServiceTests.java | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationService.java index 7fa0adb..3f2ed0e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationService.java @@ -138,7 +138,7 @@ public class DevicePropagationService { * @param username the username of the owner of that device * @param causedByTrigger if true, send the update to the owner as well */ - private void propagateUpdateAsOwner(Device device, String username, boolean causedByTrigger) { + void propagateUpdateAsOwner(Device device, String username, boolean causedByTrigger) { final User user = userRepository.findByUsername(username); final Set guests = user.getGuests(); // make sure we're broadcasting from host 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 897515c..e26f69e 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 @@ -87,4 +87,43 @@ public class DevicePropagationServiceTests { assertThat(done[0]).isEqualTo(2); } + + @Test + public void testRenameIfDuplicate() { + when(deviceRepository.findDuplicates("Device", "user")).thenReturn(2); + when(deviceRepository.findDuplicates("Device (new)", "user")).thenReturn(1); + when(deviceRepository.findDuplicates("New Device", "user")).thenReturn(1); + when(deviceRepository.findDuplicates("New Device (new)", "user")).thenReturn(0); + + Device d = new RegularLight(); + d.setName("Device"); + d.setId(42L); + + devicePropagationService.renameIfDuplicate(d, "user"); + + assertThat(d.getName()).isEqualTo("Device (new)"); + + d.setName("New Device"); + d.setId(0L); + + devicePropagationService.renameIfDuplicate(d, "user"); + + assertThat(d.getName()).isEqualTo("New Device (new)"); + } + + @Test + public void testSaveAllAsOwner() { + final DevicePropagationService dps = Mockito.spy(devicePropagationService); + final Device d = new RegularLight(); + final List dl = List.of(d); + doNothing().when(dps).renameIfDuplicate(d, "user"); + when(deviceRepository.saveAll(dl)).thenReturn(dl); + doNothing().when(dps).propagateUpdateAsOwner(d, "user", false); + doNothing().when(dps).propagateUpdateAsOwner(d, "user", true); + + assertThat(dps.saveAllAsOwner(dl, "user")).containsExactly(d); + assertThat(dps.saveAllAsOwner(dl, "user", true, true)).containsExactly(d); + assertThat(dps.saveAllAsOwner(dl, "user", true, false)).containsExactly(d); + assertThat(dps.saveAllAsOwner(dl, "user", false, true)).containsExactly(d); + } }