Merge branch '90-dimmablestatecontroller-tests' of lab.si.usi.ch:sa4-2020/the-sanmarinoes/backend into 90-dimmablestatecontroller-tests

This commit is contained in:
Jacob Salvi 2020-05-25 17:35:25 +02:00
commit 99c33cdfa5
48 changed files with 1080 additions and 132 deletions

View file

@ -18,9 +18,15 @@ import org.springframework.web.filter.OncePerRequestFilter;
@Component
public class JWTRequestFilter extends OncePerRequestFilter {
@Autowired private JWTUserDetailsService jwtUserDetailsService;
private final JWTUserDetailsService jwtUserDetailsService;
private final JWTTokenUtils jwtTokenUtils;
@Autowired private JWTTokenUtils jwtTokenUtils;
@Autowired
public JWTRequestFilter(
JWTUserDetailsService jwtUserDetailsService, JWTTokenUtils jwtTokenUtils) {
this.jwtUserDetailsService = jwtUserDetailsService;
this.jwtTokenUtils = jwtTokenUtils;
}
@Override
protected void doFilterInternal(

View file

@ -10,8 +10,8 @@ import java.security.Principal;
public abstract class GuestEnabledController<T extends Device> {
private UserRepository userRepository;
private DeviceRepository<T> deviceRepository;
private final UserRepository userRepository;
private final DeviceRepository<T> deviceRepository;
public GuestEnabledController(
final UserRepository userRepository, final DeviceRepository<T> deviceRepository) {

View file

@ -19,10 +19,10 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/regularLight")
public class RegularLightController extends GuestEnabledController<RegularLight> {
private RegularLightRepository regularLightRepository;
private SceneRepository sceneRepository;
private StateRepository<State> stateRepository;
private DeviceService deviceService;
private final RegularLightRepository regularLightRepository;
private final SceneRepository sceneRepository;
private final StateRepository<State> stateRepository;
private final DeviceService deviceService;
@Autowired
public RegularLightController(

View file

@ -38,6 +38,10 @@ public class ScenePriority {
return id;
}
public void setId(long id) {
this.id = id;
}
public Integer getPriority() {
return priority;
}

View file

@ -0,0 +1,15 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class CameraConfigurationServiceTests {
@Test
public void test() {
final CameraConfigurationService c = new CameraConfigurationService();
c.setVideoUrl("cose");
assertThat(c.getVideoUrl()).isEqualTo("cose");
}
}

View file

@ -0,0 +1,104 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionOrTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanConditionRepository;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
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;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@DisplayName("BooleanConditionController tests")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class BooleanConditionControllerTests {
@InjectMocks private BooleanConditionController controller;
@Mock private BooleanConditionRepository repository;
@Mock private Principal principal;
@Test
public void testCreate() {
BooleanCondition condition = new BooleanCondition();
condition.setOn(true);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
when(repository.save(any())).thenAnswer(i -> i.getArgument(0));
BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest();
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOn(false);
BooleanCondition returned = controller.create(toSend);
assertThat(returned.isOn()).isEqualTo(toSend.isOn());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
BooleanCondition condition = new BooleanCondition();
condition.setOn(true);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
when(repository.save(condition)).thenReturn(condition);
BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest();
toSend.setId(34L);
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOn(false);
when(repository.findById(34L)).thenReturn(Optional.of(condition));
BooleanCondition returned = controller.update(toSend);
assertThat(returned.isOn()).isEqualTo(toSend.isOn());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
public void testGetAll() {
BooleanCondition condition1 = new BooleanCondition();
condition1.setAutomationId(1L);
BooleanCondition condition2 = new BooleanCondition();
condition2.setAutomationId(1L);
BooleanCondition condition3 = new BooleanCondition();
condition3.setAutomationId(1L);
ArrayList<BooleanCondition> list = new ArrayList<>();
list.add(condition1);
list.add(condition2);
list.add(condition3);
when(repository.findAllByAutomationId(42L)).thenReturn(list);
List<BooleanCondition> returned = controller.getAll(42L);
assertThat(returned).contains(condition1);
assertThat(returned).contains(condition2);
assertThat(returned).contains(condition3);
assertThat(returned.size()).isEqualTo(3);
}
@Test
public void testDelete() {
doNothing().when(repository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
}

View file

@ -0,0 +1,104 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionOrTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTriggerRepository;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
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;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@DisplayName("BooleanTriggerController tests")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class BooleanTriggerControllerTests {
@InjectMocks private BooleanTriggerController controller;
@Mock private BooleanTriggerRepository repository;
@Mock private Principal principal;
@Test
public void testCreate() {
BooleanTrigger condition = new BooleanTrigger();
condition.setOn(true);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
when(repository.save(any())).thenAnswer(i -> i.getArgument(0));
BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest();
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOn(false);
BooleanTrigger returned = controller.create(toSend);
assertThat(returned.isOn()).isEqualTo(toSend.isOn());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
BooleanTrigger condition = new BooleanTrigger();
condition.setOn(true);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
when(repository.save(condition)).thenReturn(condition);
BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest();
toSend.setId(34L);
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOn(false);
when(repository.findById(34L)).thenReturn(Optional.of(condition));
BooleanTrigger returned = controller.update(toSend);
assertThat(returned.isOn()).isEqualTo(toSend.isOn());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
public void testGetAll() {
BooleanTrigger condition1 = new BooleanTrigger();
condition1.setAutomationId(1L);
BooleanTrigger condition2 = new BooleanTrigger();
condition2.setAutomationId(1L);
BooleanTrigger condition3 = new BooleanTrigger();
condition3.setAutomationId(1L);
ArrayList<BooleanTrigger> list = new ArrayList<>();
list.add(condition1);
list.add(condition2);
list.add(condition3);
when(repository.findAllByAutomationId(42L)).thenReturn(list);
List<BooleanTrigger> returned = controller.getAll(42L);
assertThat(returned).contains(condition1);
assertThat(returned).contains(condition2);
assertThat(returned).contains(condition3);
assertThat(returned.size()).isEqualTo(3);
}
@Test
public void testDelete() {
doNothing().when(repository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
}

View file

@ -0,0 +1,171 @@
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.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
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;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class DimmableLightControllerTests {
@InjectMocks private DimmableLightController dimmableLightController;
@Mock private DimmableLightRepository dimmableLightRepository;
@Mock private SceneRepository sceneRepository;
@Mock private StateRepository<State> stateRepository;
@Mock private DeviceService deviceService;
@Mock private Principal mockPrincipal;
@Mock private UserRepository userRepository;
@Mock private DeviceRepository<DimmableLight> deviceRepository;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
}
private void checkDimmableLightAgainstRequest(
final DimmableLight toCheck, final DimmableSaveRequest request) {
assertThat(toCheck).isNotNull();
assertThat(toCheck.getIntensity()).isEqualTo(request.getIntensity());
assertThat(toCheck.getName()).isEqualTo(request.getName());
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
}
@Test
@DisplayName("when creating should return the same object")
@SneakyThrows(NotFoundException.class)
public void testCreate() {
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
when(deviceService.saveAsOwner(any(DimmableLight.class), eq("user")))
.thenAnswer(i -> i.getArguments()[0]);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
final DimmableSaveRequest toSend = new DimmableSaveRequest();
toSend.setName("rl");
toSend.setRoomId(20L);
toSend.setIntensity(35);
final DimmableLight regularLight = dimmableLightController.create(toSend, mockPrincipal);
checkDimmableLightAgainstRequest(regularLight, toSend);
}
@Test
@DisplayName("when updating should return the same object")
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
final DimmableSaveRequest toSend = new DimmableSaveRequest();
toSend.setId(50L);
toSend.setName("rl");
toSend.setRoomId(20L);
toSend.setIntensity(35);
final DimmableLight toUpdate = new DimmableLight();
toUpdate.setName("OOO");
toUpdate.setRoomId(40L);
toSend.setId(50L);
toUpdate.setOn(false);
when(dimmableLightRepository.findByIdAndUserId(anyLong(), anyLong()))
.thenReturn(java.util.Optional.of(toUpdate));
when(deviceService.saveAsGuest(any(DimmableLight.class), eq("user"), anyLong()))
.thenAnswer(i -> i.getArguments()[0]);
User guest = new User();
User host = new User();
host.getGuests().add(guest);
guest.getHosts().add(host);
when(userRepository.findById(20L)).thenReturn(Optional.of(host));
when(userRepository.findByUsername("user")).thenReturn(guest);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
final DimmableLight dimmableLight =
dimmableLightController.update(toSend, mockPrincipal, 20L);
checkDimmableLightAgainstRequest(dimmableLight, toSend);
}
@Test
@DisplayName("an existing id should succeed")
@SneakyThrows(NotFoundException.class)
public void testDelete() {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> dimmableLightController.delete(42L, mockPrincipal));
}
@Test
public void testSceneBinding() {
DimmableLight dimmableLight = new DimmableLight();
when(dimmableLightRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(dimmableLight));
Scene scene = new Scene();
scene.setId(1L);
DimmableState state = new DimmableState();
state.setSceneId(1L);
State s = dimmableLight.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
Assertions.assertDoesNotThrow(
() -> dimmableLightController.sceneBinding(24L, 1L, mockPrincipal));
}
@Test
public void testSceneBinding2() {
when(mockPrincipal.getName()).thenReturn("user");
DimmableLight dimmableLight = new DimmableLight();
when(dimmableLightRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(dimmableLight));
Scene scene = new Scene();
scene.setId(1L);
DimmableState state = new DimmableState();
state.setSceneId(1L);
State s = dimmableLight.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2);
assertThatThrownBy(() -> dimmableLightController.sceneBinding(24L, 1L, mockPrincipal))
.isInstanceOf(DuplicateStateException.class);
}
}

View file

@ -0,0 +1,200 @@
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.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
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;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class RegularLightControllerTests {
@InjectMocks private RegularLightController regularLightController;
@Mock private RegularLightRepository regularLightRepository;
@Mock private DeviceService deviceService;
@Mock private Principal mockPrincipal;
@Mock private UserRepository userRepository;
@Mock private SceneRepository sceneRepository;
@Mock private StateRepository<State> stateRepository;
private void checkRegularLightAgainstRequest(
final RegularLight toCheck, final SwitchableSaveRequest request) {
assertThat(toCheck).isNotNull();
assertThat(toCheck.isOn()).isEqualTo(request.isOn());
assertThat(toCheck.getName()).isEqualTo(request.getName());
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
}
@Test
public void testGetAll() {
when(regularLightRepository.findAll()).thenReturn(List.of());
assertThat(regularLightController.findAll().isEmpty());
}
@Test
public void testGet() throws NotFoundException {
RegularLight r = new RegularLight();
when(regularLightRepository.findById(1L)).thenReturn(Optional.of(r));
when(regularLightRepository.findById(2L)).thenReturn(Optional.empty());
assertThat(regularLightController.findById(1L)).isSameAs(r);
assertThatThrownBy(() -> regularLightController.findById(2L))
.isInstanceOf(NotFoundException.class);
}
@Test
@DisplayName("when creating should return the same object")
@SneakyThrows(NotFoundException.class)
public void testCreate() {
when(mockPrincipal.getName()).thenReturn("user");
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
when(deviceService.saveAsOwner(any(RegularLight.class), eq("user")))
.thenAnswer(i -> i.getArguments()[0]);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
toSend.setName("rl");
toSend.setRoomId(20L);
toSend.setOn(true);
final RegularLight regularLight = regularLightController.create(toSend, mockPrincipal);
checkRegularLightAgainstRequest(regularLight, toSend);
}
@Test
@DisplayName("when updating should return the same object")
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
when(mockPrincipal.getName()).thenReturn("user");
final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
toSend.setName("rl");
toSend.setRoomId(20L);
toSend.setOn(true);
final RegularLight toUpdate = new RegularLight();
toUpdate.setName("OOO");
toUpdate.setRoomId(40L);
toUpdate.setOn(false);
when(regularLightRepository.findByIdAndUsername(50L, "user"))
.thenReturn(Optional.of(toUpdate));
when(regularLightRepository.findByIdAndUsername(60L, "user")).thenReturn(Optional.empty());
when(deviceService.saveAsOwner(toUpdate, "user")).thenReturn(toUpdate);
toSend.setId(60L);
assertThatThrownBy(() -> regularLightController.update(toSend, mockPrincipal, null))
.isInstanceOf(NotFoundException.class);
toSend.setId(50L);
assertThat(regularLightController.update(toSend, mockPrincipal, null)).isEqualTo(toUpdate);
toUpdate.setName("OOO");
toUpdate.setRoomId(40L);
toSend.setId(50L);
toUpdate.setOn(false);
when(regularLightRepository.findByIdAndUserId(anyLong(), anyLong()))
.thenReturn(java.util.Optional.of(toUpdate));
when(deviceService.saveAsGuest(any(RegularLight.class), eq("user"), anyLong()))
.thenAnswer(i -> i.getArguments()[0]);
User guest = new User();
User host = new User();
host.getGuests().add(guest);
guest.getHosts().add(host);
when(userRepository.findById(20L)).thenReturn(Optional.of(host));
when(userRepository.findByUsername("user")).thenReturn(guest);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
final RegularLight regularLight = regularLightController.update(toSend, mockPrincipal, 20L);
checkRegularLightAgainstRequest(regularLight, toSend);
}
@Test
@DisplayName("an existing id should succeed")
@SneakyThrows(NotFoundException.class)
public void testDelete() {
when(mockPrincipal.getName()).thenReturn("user");
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> regularLightController.delete(42L, mockPrincipal));
}
@Test
public void testSceneBinding() {
when(mockPrincipal.getName()).thenReturn("user");
RegularLight regularLight = new RegularLight();
when(regularLightRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(regularLight));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = regularLight.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
Assertions.assertDoesNotThrow(
() -> regularLightController.sceneBinding(24L, 1L, mockPrincipal));
}
@Test
public void testSceneBinding2() {
when(mockPrincipal.getName()).thenReturn("user");
when(mockPrincipal.getName()).thenReturn("user");
RegularLight regularLight = new RegularLight();
when(regularLightRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(regularLight));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = regularLight.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2);
assertThatThrownBy(() -> regularLightController.sceneBinding(24L, 1L, mockPrincipal))
.isInstanceOf(DuplicateStateException.class);
}
}

View file

@ -0,0 +1,107 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ScenePrioritySaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository;
import java.util.List;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
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;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class ScenePriorityControllerTests {
@InjectMocks private ScenePriorityController scenePriorityController;
@Mock private ScenePriorityRepository scenePriorityRepository;
@Test
@DisplayName("test for get all should return empty")
public void testGetAll() throws NotFoundException {
when(scenePriorityRepository.findAllByAutomationId(anyLong())).thenReturn(List.of());
assertThat(scenePriorityController.getByAutomationId(20)).isEmpty();
}
private void checkScenePriorityAgainstRequest(
final ScenePriority toCheck, final ScenePrioritySaveRequest request) {
assertThat(toCheck).isNotNull();
assertThat(toCheck.getAutomationId()).isEqualTo(request.getAutomationId());
assertThat(toCheck.getPriority()).isEqualTo(request.getPriority());
assertThat(toCheck.getSceneId()).isEqualTo(request.getSceneId());
}
@Test
@DisplayName("Test for create")
public void create() {
ScenePrioritySaveRequest saveRequest = new ScenePrioritySaveRequest();
saveRequest.setAutomationId(20L);
saveRequest.setPriority(40);
saveRequest.setSceneId(10L);
when(scenePriorityRepository.save(any(ScenePriority.class)))
.thenAnswer(i -> i.getArguments()[0]);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
ScenePriority scenePriority = scenePriorityController.create(saveRequest);
checkScenePriorityAgainstRequest(scenePriority, saveRequest);
}
@Test
@DisplayName("Test for update")
@SneakyThrows(NotFoundException.class)
public void update() {
ScenePrioritySaveRequest saveRequest = new ScenePrioritySaveRequest();
saveRequest.setAutomationId(20L);
saveRequest.setPriority(40);
saveRequest.setSceneId(10L);
ScenePriority toUpdate = new ScenePriority();
when(scenePriorityRepository.findById(anyLong()))
.thenReturn(java.util.Optional.of(toUpdate));
when(scenePriorityRepository.save(any(ScenePriority.class)))
.thenAnswer(i -> i.getArguments()[0]);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
ScenePriority scenePriority = scenePriorityController.update(saveRequest);
checkScenePriorityAgainstRequest(scenePriority, saveRequest);
}
@Test
@DisplayName("an existing id should succeed")
public void testDelete() {
doNothing().when(scenePriorityRepository).deleteBySceneId(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> scenePriorityController.delete(42L));
}
}

View file

@ -1,6 +1,7 @@
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.doNothing;
import static org.mockito.Mockito.when;
@ -8,9 +9,12 @@ import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SensorRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SensorService;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@ -35,6 +39,10 @@ public class SensorControllerTests {
@Mock private Principal mockPrincipal;
@Mock private SensorRepository sensorRepository;
@Mock private SensorService sensorService;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
@ -78,4 +86,21 @@ public class SensorControllerTests {
Assertions.assertDoesNotThrow(() -> sensorController.deleteById(42L, mockPrincipal));
}
@Test
public void testUpdateValue() throws NotFoundException {
final Sensor s = new Sensor();
when(sensorRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(s));
when(sensorRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty());
when(sensorService.updateValueFromSensor(s, BigDecimal.valueOf(30)))
.thenAnswer(
i -> {
s.setValue(i.getArgument(1));
return s;
});
assertThatThrownBy(() -> sensorController.updateValue(2L, BigDecimal.ZERO, mockPrincipal))
.isInstanceOf(NotFoundException.class);
assertThat(sensorController.updateValue(1L, BigDecimal.valueOf(30), mockPrincipal))
.isSameAs(s);
}
}

View file

@ -1,6 +1,7 @@
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.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
@ -8,16 +9,20 @@ import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatPopulationService;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@ -128,4 +133,37 @@ public class ThermostatControllerTests {
Assertions.assertDoesNotThrow(() -> thermostatController.deleteById(42L, mockPrincipal));
}
@Test
public void testSceneBinding() {
Thermostat thermostat = new Thermostat();
when(thermostatRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(thermostat));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = thermostat.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
Assertions.assertDoesNotThrow(
() -> thermostatController.sceneBinding(24L, 1L, mockPrincipal));
}
@Test
public void testSceneBinding2() {
when(mockPrincipal.getName()).thenReturn("user");
Thermostat thermostat = new Thermostat();
when(thermostatRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(thermostat));
Scene scene = new Scene();
scene.setId(1L);
SwitchableState state = new SwitchableState();
state.setSceneId(1L);
State s = thermostat.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2);
assertThatThrownBy(() -> thermostatController.sceneBinding(24L, 1L, mockPrincipal))
.isInstanceOf(DuplicateStateException.class);
}
}

View file

@ -1,43 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
import static org.junit.jupiter.api.Assertions.assertEquals;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Scene Priority Tests")
public class ScenePriorityTests {
private ScenePriority scenePriority;
@BeforeEach
public void scenePriorityCreate() {
this.scenePriority = new ScenePriority();
}
@Test
@DisplayName("get and set automation id")
public void getAndSetAutomationId() {
scenePriority.setAutomationId(20L);
assertEquals(20, scenePriority.getAutomationId());
}
@Test
@DisplayName("get and set scene id")
public void getAndSetSceneId() {
scenePriority.setSceneId(20L);
assertEquals(20, scenePriority.getSceneId());
}
@Test
@DisplayName("get and set priority")
public void getAndSetPriority() {
scenePriority.setPriority(20);
assertEquals(20, scenePriority.getPriority());
}
}

View file

@ -1,9 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,10 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Switch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,10 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Switch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,10 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,9 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationToken;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import java.util.Date;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Curtains;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;

View file

@ -1,9 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -35,4 +33,13 @@ public class DimmableStateTests {
this.dimmableState.apply();
assertEquals(30, d.getIntensity());
}
@Test
@DisplayName("apply")
public void copy() {
this.dimmableState.setIntensity(30);
DimmableState copy = this.dimmableState.copy();
assertEquals(30, copy.getIntensity());
}
}

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;

View file

@ -1,10 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -0,0 +1,14 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class InputDeviceTests {
@Test
public void test() {
final InputDevice motionSensor = new MotionSensor();
assertThat(motionSensor.getOutputs()).isEmpty();
}
}

View file

@ -1,10 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
@ -44,4 +41,10 @@ public class KnobDimmerTests {
}
}
}
@Test
public void testReadTriggerState() {
this.knobDimmer.setLightIntensity(1);
assertEquals(1, this.knobDimmer.readTriggerState());
}
}

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -22,5 +21,6 @@ public class MotionSensorTests {
public void setAndGetDetected() {
this.motionSensor.setDetected(true);
assertTrue(this.motionSensor.isDetected());
assertTrue(this.motionSensor.readTriggerState());
}
}

View file

@ -0,0 +1,21 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class OperatorTests {
@Test
public void testOperators() {
assertThat(Operator.EQUAL.checkAgainst(30, 30)).isTrue();
assertThat(Operator.EQUAL.checkAgainst(20, 30)).isFalse();
assertThat(Operator.LESS.checkAgainst(20, 30)).isTrue();
assertThat(Operator.LESS.checkAgainst(40, 30)).isFalse();
assertThat(Operator.LESS_EQUAL.checkAgainst(30, 30)).isTrue();
assertThat(Operator.LESS_EQUAL.checkAgainst(40, 30)).isFalse();
assertThat(Operator.GREATER.checkAgainst(40, 30)).isTrue();
assertThat(Operator.GREATER.checkAgainst(30, 30)).isFalse();
assertThat(Operator.GREATER_EQUAL.checkAgainst(30, 30)).isTrue();
assertThat(Operator.GREATER_EQUAL.checkAgainst(20, 30)).isFalse();
}
}

View file

@ -1,10 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,10 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,9 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,11 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -0,0 +1,87 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Scene Priority Tests")
public class ScenePriorityTests {
private ScenePriority scenePriority;
@BeforeEach
public void scenePriorityCreate() {
this.scenePriority = new ScenePriority();
}
@Test
@DisplayName("get and set automation id")
public void getAndSetAutomationId() {
scenePriority.setAutomationId(20L);
assertEquals(20, scenePriority.getAutomationId());
}
@Test
@DisplayName("get and set id")
public void getAndSetId() {
scenePriority.setId(20L);
assertEquals(20, scenePriority.getId());
}
@Test
@DisplayName("get and set scene id")
public void getAndSetSceneId() {
scenePriority.setSceneId(20L);
assertEquals(20, scenePriority.getSceneId());
}
@Test
@DisplayName("get and set priority")
public void getAndSetPriority() {
scenePriority.setPriority(20);
assertEquals(20, scenePriority.getPriority());
}
@Test
@DisplayName("get and set automation")
public void getAndSetAutomation() {
Automation a = new Automation();
scenePriority.setAutomation(a);
assertEquals(a, scenePriority.getAutomation());
}
@Test
@DisplayName("get and set scene")
public void getAndSetScene() {
Scene s = new Scene();
scenePriority.setScene(s);
assertEquals(s, scenePriority.getScene());
}
@Test
@DisplayName("get and set scene")
public void testRemove() {
Scene s = new Scene();
scenePriority.setScene(s);
scenePriority.setSceneId(20L);
Automation a = new Automation();
scenePriority.setAutomation(a);
scenePriority.setAutomationId(20L);
scenePriority.preRemove();
assertNull(scenePriority.getAutomation());
assertNull(scenePriority.getAutomationId());
assertNull(scenePriority.getScene());
assertNull(scenePriority.getSceneId());
}
}

View file

@ -1,9 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,9 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach;
@ -32,7 +31,7 @@ public class SensorTests {
public void getAndSetValue() {
sensor.setValue(new BigDecimal(40));
assertEquals(new BigDecimal(40), sensor.getValue());
assertEquals(40.0, sensor.readTriggerState());
}
@Test

View file

@ -1,9 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
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;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;

View file

@ -1,10 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -48,4 +46,44 @@ public class StateTests {
this.state.setSceneId(50L);
assertEquals(50, this.state.getSceneId());
}
@Test
@DisplayName("copy to scene id")
public void copyToSceneId() {
this.state.setSceneId(50L);
Scene s = new Scene();
this.state.setScene(s);
this.state.setDeviceId(30L);
DimmableLight d = new DimmableLight();
this.state.setDevice(d);
State stat = this.state.copyToSceneId(10L);
assertEquals(10, stat.getSceneId());
assertEquals(30, stat.getDeviceId());
assertEquals(s, stat.getScene());
assertEquals(d, stat.getDevice());
}
@Test
@DisplayName("preremove")
public void preRemove() {
this.state.setSceneId(50L);
Scene s = new Scene();
this.state.setScene(s);
this.state.setDeviceId(30L);
DimmableLight d = new DimmableLight();
this.state.setDevice(d);
state.removeDeviceAndScene();
assertNull(state.getSceneId());
assertNull(state.getDeviceId());
assertNull(state.getScene());
assertNull(state.getDevice());
}
}

View file

@ -1,9 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.assertj.core.api.Assertions.assertThat;
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;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -11,7 +11,8 @@ import org.junit.jupiter.api.Test;
@DisplayName("A switch")
public class SwitchTests {
Switch aSwitch;
private Switch aSwitch;
private SmartPlug smartPlug;
@BeforeEach
public void createNewSwitch() {
@ -19,10 +20,11 @@ public class SwitchTests {
this.aSwitch = new Switch();
RegularLight regularLight = new RegularLight();
DimmableLight dimmableLight = new DimmableLight();
SmartPlug smartPlug = new SmartPlug();
smartPlug = new SmartPlug();
this.aSwitch.getSwitchables().add(regularLight);
this.aSwitch.getSwitchables().add(dimmableLight);
this.aSwitch.getSwitchables().add(smartPlug);
this.aSwitch.connect(smartPlug, true);
assertThat(this.aSwitch.getOutputs()).containsAll(this.aSwitch.getSwitchables());
}
@Test
@ -94,4 +96,10 @@ public class SwitchTests {
assertFalse(s.isOn());
}
}
@Test
public void testDisconnect() {
this.aSwitch.connect(smartPlug, false);
assertFalse(this.aSwitch.getOutputs().contains(smartPlug));
}
}

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -15,6 +14,7 @@ public class SwitchableStateTests {
@BeforeEach
public void createSwitchableState() {
switchableState = new SwitchableState();
switchableState.setDevice(new RegularLight());
}
@Test
@ -23,5 +23,10 @@ public class SwitchableStateTests {
switchableState.setOn(true);
assertTrue(switchableState.isOn());
switchableState.apply();
assertTrue(((Switchable) switchableState.getDevice()).isOn());
assertTrue(switchableState.copy().isOn());
}
}

View file

@ -1,10 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat.Mode;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition.Operator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
@ -49,5 +47,13 @@ public class ThermostatConditionTests {
thermostatCondition.setOperator(Operator.NOTEQUAL);
assertFalse(thermostatCondition.triggered());
t.setMode(Mode.COOLING);
thermostatCondition.setOperator(Operator.EQUAL);
assertFalse(thermostatCondition.triggered());
thermostatCondition.setOperator(Operator.NOTEQUAL);
assertTrue(thermostatCondition.triggered());
}
}

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
import java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;

View file

@ -1,10 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -61,4 +59,11 @@ public class TriggerTests {
booleanTrigger.setAutomationId(20L);
assertEquals(20, booleanTrigger.getAutomationId());
}
@Test
public void testRemove() {
booleanTrigger.setDeviceId(30L);
booleanTrigger.removeDeviceAndScene();
assertNull(booleanTrigger.getDeviceId());
}
}

View file

@ -1,8 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.model;
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import static org.junit.jupiter.api.Assertions.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

View file

@ -17,6 +17,7 @@ import org.springframework.security.test.context.support.WithMockUser;
@WithMockUser(username = "user")
@DisplayName("DevicePopulationService test")
public class DevicePopulationServiceTests {
@InjectMocks private DevicePopulationService service;
@Mock private ThermostatPopulationService populationService;

View file

@ -0,0 +1,34 @@
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.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@ExtendWith({MockitoExtension.class})
public class JWTUserDetailsServiceTests {
@InjectMocks private JWTUserDetailsService jwtUserDetailsService;
@Mock private UserRepository userRepository;
@Test
public void testLoadByUsername() {
final User u = new User();
u.setUsername("username");
u.setPassword("password");
when(userRepository.findByUsername("username")).thenReturn(u);
assertThatThrownBy(() -> jwtUserDetailsService.loadUserByUsername("username"))
.isInstanceOf(UsernameNotFoundException.class);
u.setEnabled(true);
assertThat(jwtUserDetailsService.loadUserByUsername("username")).isNotNull();
}
}

View file

@ -10,6 +10,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
import java.math.BigDecimal;
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;
@ -63,4 +64,14 @@ public class ThermostatServiceTests {
when(thermostatRepository.findAllByUsername("user")).thenReturn(List.of(t));
assertThat(thermostatService.findAll("user")).containsExactly(t);
}
@Test
public void testFindById() {
final Thermostat t = new Thermostat();
when(thermostatRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty());
when(thermostatRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(t));
doNothing().when(thermostatPopulationService).populateMeasuredTemperature(t);
assertThat(thermostatService.findById(2L, "user")).isEmpty();
assertThat(thermostatService.findById(1L, "user")).isNotEmpty();
}
}

View file

@ -43,5 +43,15 @@ public class UtilsTests {
assertThatThrownBy(() -> Utils.returnIfGuest(userRepository, "toReturn", 2L, principal))
.isInstanceOf(NotFoundException.class);
host.getGuests().clear();
assertThatThrownBy(
() ->
assertThat(
Utils.returnIfGuest(
userRepository, "toReturn", 1L, principal))
.isEqualTo("toReturn"))
.isInstanceOf(NotFoundException.class);
}
}