Finished tests on DevicePropagationService
This commit is contained in:
parent
ec0abbbe18
commit
58a8b939fa
3 changed files with 114 additions and 20 deletions
|
@ -18,8 +18,8 @@ public class SwitchOperationRequestTests {
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("test setId")
|
@DisplayName("test setId")
|
||||||
public void testSetId() {
|
public void testSetId() {
|
||||||
request.setId(42l);
|
request.setId(42L);
|
||||||
assertEquals(42l, request.getId());
|
assertEquals(42L, request.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -21,15 +21,15 @@ public class ThermostatConditionSaveRequestTests {
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("test setDeviceId")
|
@DisplayName("test setDeviceId")
|
||||||
public void testSetDeviceId() {
|
public void testSetDeviceId() {
|
||||||
this.saveRequest.setDeviceId(42l);
|
this.saveRequest.setDeviceId(42L);
|
||||||
assertEquals(42l, saveRequest.getDeviceId());
|
assertEquals(42L, saveRequest.getDeviceId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("test setAutomationId")
|
@DisplayName("test setAutomationId")
|
||||||
public void testSetAutomationId() {
|
public void testSetAutomationId() {
|
||||||
this.saveRequest.setAutomationId(42l);
|
this.saveRequest.setAutomationId(42L);
|
||||||
assertEquals(42l, saveRequest.getAutomationId());
|
assertEquals(42L, saveRequest.getAutomationId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -56,6 +56,6 @@ public class ThermostatConditionSaveRequestTests {
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("test getId")
|
@DisplayName("test getId")
|
||||||
public void testGetId() {
|
public void testGetId() {
|
||||||
assertEquals(0l, saveRequest.getId());
|
assertEquals(0L, saveRequest.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.mockito.Mockito.*;
|
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.models.*;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
|
@ -24,6 +27,24 @@ public class DevicePropagationServiceTests {
|
||||||
|
|
||||||
@Mock private EagerUserRepository userRepository;
|
@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
|
@Test
|
||||||
public void testPropagateUpdateAsGuest() {
|
public void testPropagateUpdateAsGuest() {
|
||||||
Device toPropagate = new SecurityCamera();
|
Device toPropagate = new SecurityCamera();
|
||||||
|
@ -59,16 +80,9 @@ public class DevicePropagationServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void saveAllAsGuestSceneApplication() {
|
public void saveAllAsGuestSceneApplication() {
|
||||||
User host = new User();
|
initHostGuest();
|
||||||
host.setName("host");
|
when(userRepository.findById(1L)).thenReturn(Optional.of(host));
|
||||||
host.setId(1L);
|
when(userRepository.findByUsername("guest")).thenReturn(guest);
|
||||||
|
|
||||||
User guest = new User();
|
|
||||||
guest.setName("guest");
|
|
||||||
guest.setId(2L);
|
|
||||||
|
|
||||||
guest.getHosts().add(host);
|
|
||||||
host.getGuests().add(guest);
|
|
||||||
|
|
||||||
int[] done = new int[1];
|
int[] done = new int[1];
|
||||||
|
|
||||||
|
@ -79,9 +93,6 @@ public class DevicePropagationServiceTests {
|
||||||
.propagateUpdateAsGuest(any(), eq(host), eq(guest));
|
.propagateUpdateAsGuest(any(), eq(host), eq(guest));
|
||||||
when(deviceRepository.saveAll(any())).thenAnswer(i -> i.getArguments()[0]);
|
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(
|
devicePropagationService1.saveAllAsGuestSceneApplication(
|
||||||
List.of(new SecurityCamera(), new ButtonDimmer()), "guest", 1L);
|
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", true, false)).containsExactly(d);
|
||||||
assertThat(dps.saveAllAsOwner(dl, "user", false, true)).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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue