Merge branch 'tests' into 'dev'

Some tests on DevicePropagationService

See merge request sa4-2020/the-sanmarinoes/backend!159
This commit is contained in:
Claudio Maggioni 2020-05-20 16:40:03 +02:00
commit ec0abbbe18
2 changed files with 40 additions and 1 deletions

View file

@ -138,7 +138,7 @@ public class DevicePropagationService {
* @param username the username of the owner of that device * @param username the username of the owner of that device
* @param causedByTrigger if true, send the update to the owner as well * @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 User user = userRepository.findByUsername(username);
final Set<User> guests = user.getGuests(); final Set<User> guests = user.getGuests();
// make sure we're broadcasting from host // make sure we're broadcasting from host

View file

@ -87,4 +87,43 @@ public class DevicePropagationServiceTests {
assertThat(done[0]).isEqualTo(2); 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<Device> 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);
}
} }