Finished tests on DevicePropagationService

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-21 21:46:29 +02:00
parent ec0abbbe18
commit 58a8b939fa
3 changed files with 114 additions and 20 deletions

View file

@ -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

View file

@ -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());
}
}

View file

@ -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);
}
}