Merge branch 'tests' into 'dev'

Code smells fix

See merge request sa4-2020/the-sanmarinoes/backend!171
This commit is contained in:
Claudio Maggioni 2020-05-23 23:23:43 +02:00
commit 3cfe4cb1e8
11 changed files with 30 additions and 25 deletions

View file

@ -4,6 +4,7 @@ import java.util.Date;
import java.util.UUID; import java.util.UUID;
import javax.persistence.*; import javax.persistence.*;
import lombok.Data; import lombok.Data;
import lombok.NonNull;
@Data @Data
@Entity @Entity
@ -26,6 +27,7 @@ public class ConfirmationToken {
} }
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@NonNull
private Date createdDate; private Date createdDate;
@OneToOne(targetEntity = User.class, fetch = FetchType.EAGER) @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER)

View file

@ -9,6 +9,7 @@ import lombok.EqualsAndHashCode;
/** A device that can be turned either on or off */ /** A device that can be turned either on or off */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
@Inheritance(strategy = InheritanceType.JOINED) @Inheritance(strategy = InheritanceType.JOINED)
public abstract class Switchable extends OutputDevice { public abstract class Switchable extends OutputDevice {

View file

@ -1,9 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service; package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.TriggerRepository;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -12,13 +9,16 @@ import org.springframework.stereotype.Component;
public class AutomationService { public class AutomationService {
private final AutomationRepository automationRepository; private final AutomationRepository automationRepository;
private final TriggerRepository<Trigger<?>> triggerRepository; private final TriggerRepository<Trigger<?>> triggerRepository;
private final ConditionRepository<Condition<?>> conditionRepository;
@Autowired @Autowired
public AutomationService( public AutomationService(
AutomationRepository automationRepository, AutomationRepository automationRepository,
TriggerRepository<Trigger<?>> triggerRepository) { TriggerRepository<Trigger<?>> triggerRepository,
ConditionRepository<Condition<?>> conditionRepository) {
this.automationRepository = automationRepository; this.automationRepository = automationRepository;
this.triggerRepository = triggerRepository; this.triggerRepository = triggerRepository;
this.conditionRepository = conditionRepository;
} }
public List<Trigger<?>> findTriggersByDeviceId(Long deviceId) { public List<Trigger<?>> findTriggersByDeviceId(Long deviceId) {
@ -28,4 +28,8 @@ public class AutomationService {
public Automation findByVerifiedId(Long automationId) { public Automation findByVerifiedId(Long automationId) {
return automationRepository.findById(automationId).orElseThrow(); return automationRepository.findById(automationId).orElseThrow();
} }
public List<Condition<?>> findAllConditionsByAutomationId(Long automationId) {
return conditionRepository.findAllByAutomationId(automationId);
}
} }

View file

@ -22,7 +22,6 @@ public class DeviceService {
private final EagerUserRepository userRepository; private final EagerUserRepository userRepository;
private final DevicePopulationService devicePopulationService; private final DevicePopulationService devicePopulationService;
private final DevicePropagationService devicePropagationService; private final DevicePropagationService devicePropagationService;
private final ConditionRepository<Condition<?>> conditionRepository;
@Autowired @Autowired
public DeviceService( public DeviceService(
@ -32,8 +31,7 @@ public class DeviceService {
AutomationService automationService, AutomationService automationService,
EagerUserRepository userRepository, EagerUserRepository userRepository,
DevicePopulationService devicePopulationService, DevicePopulationService devicePopulationService,
DevicePropagationService devicePropagationService, DevicePropagationService devicePropagationService) {
ConditionRepository<Condition<?>> conditionRepository) {
this.deviceRepository = deviceRepository; this.deviceRepository = deviceRepository;
this.sceneService = sceneService; this.sceneService = sceneService;
this.roomRepository = roomRepository; this.roomRepository = roomRepository;
@ -41,7 +39,6 @@ public class DeviceService {
this.userRepository = userRepository; this.userRepository = userRepository;
this.devicePopulationService = devicePopulationService; this.devicePopulationService = devicePopulationService;
this.devicePropagationService = devicePropagationService; this.devicePropagationService = devicePropagationService;
this.conditionRepository = conditionRepository;
} }
public void throwIfRoomNotOwned(Long roomId, String username) throws NotFoundException { public void throwIfRoomNotOwned(Long roomId, String username) throws NotFoundException {
@ -65,7 +62,7 @@ public class DeviceService {
.filter( .filter(
a -> { a -> {
final List<Condition<?>> conditions = final List<Condition<?>> conditions =
conditionRepository.findAllByAutomationId(a.getId()); automationService.findAllConditionsByAutomationId(a.getId());
if (conditions.isEmpty()) return true; if (conditions.isEmpty()) return true;
return conditions.stream().allMatch(Condition::triggered); return conditions.stream().allMatch(Condition::triggered);
}) })

View file

@ -15,6 +15,7 @@ import java.security.Principal;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
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;
@ -73,7 +74,7 @@ public class ButtonDimmerControllerTests {
doNothing().when(service).deleteByIdAsOwner(eq(42L), eq("user")); doNothing().when(service).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
controller.delete(42L, principal); Assertions.assertDoesNotThrow(() -> controller.delete(42L, principal));
} }
@Test @Test

View file

@ -15,6 +15,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal; import java.security.Principal;
import java.util.Optional; import java.util.Optional;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
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;
@ -91,11 +92,10 @@ public class CurtainsControllerTests {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
controller.delete(42L, principal); Assertions.assertDoesNotThrow(() -> controller.delete(42L, principal));
} }
@Test @Test
@SneakyThrows({NotFoundException.class, DuplicateStateException.class})
public void testSceneBinding() { public void testSceneBinding() {
when(principal.getName()).thenReturn("user"); when(principal.getName()).thenReturn("user");
Curtains curtains = new Curtains(); Curtains curtains = new Curtains();
@ -107,7 +107,7 @@ public class CurtainsControllerTests {
State s = curtains.cloneState(); State s = curtains.cloneState();
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene)); when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0); when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
controller.sceneBinding(24L, 1L, principal); Assertions.assertDoesNotThrow(() -> controller.sceneBinding(24L, 1L, principal));
} }
@Test @Test

View file

@ -43,7 +43,7 @@ public class GuestControllerTests {
@Test @Test
public void findAllEmptyTest() { public void findAllEmptyTest() {
List<UserResponse> l = guestController.findAll(); List<UserResponse> l = guestController.findAll();
assertThat(l.isEmpty()); assertThat(l).isEmpty();
} }
@DisplayName( @DisplayName(
@ -85,7 +85,7 @@ public class GuestControllerTests {
public void findHostsEmptyTest() { public void findHostsEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(user); when(userRepository.findByUsername("user")).thenReturn(user);
assertThat(guestController.findHosts(mockPrincipal).isEmpty()); assertThat(guestController.findHosts(mockPrincipal)).isEmpty();
} }
@DisplayName("Check that the guest list is empty") @DisplayName("Check that the guest list is empty")
@ -93,7 +93,7 @@ public class GuestControllerTests {
public void findGuestsEmptyTest() { public void findGuestsEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(user); when(userRepository.findByUsername("user")).thenReturn(user);
assertThat(guestController.findGuests(mockPrincipal).isEmpty()); assertThat(guestController.findGuests(mockPrincipal)).isEmpty();
} }
@Test @Test

View file

@ -15,6 +15,7 @@ import java.security.Principal;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
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;
@ -73,7 +74,7 @@ public class KnobDimmerControllerTests {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
controller.delete(42L, principal); Assertions.assertDoesNotThrow(() -> controller.delete(42L, principal));
} }
@Test @Test

View file

@ -16,6 +16,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal; import java.security.Principal;
import java.util.Optional; import java.util.Optional;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
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;
@ -96,11 +97,10 @@ public class SecurityCameraControllerTests {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
controller.delete(42L, principal); Assertions.assertDoesNotThrow(() -> controller.delete(42L, principal));
} }
@Test @Test
@SneakyThrows({NotFoundException.class, DuplicateStateException.class})
public void testSceneBinding() { public void testSceneBinding() {
when(principal.getName()).thenReturn("user"); when(principal.getName()).thenReturn("user");
SecurityCamera camera = new SecurityCamera(); SecurityCamera camera = new SecurityCamera();
@ -114,7 +114,7 @@ public class SecurityCameraControllerTests {
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene)); when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0); when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
// when(stateRepository.save(eq(state))).thenReturn(state); // when(stateRepository.save(eq(state))).thenReturn(state);
controller.sceneBinding(24L, 1L, principal); Assertions.assertDoesNotThrow(() -> controller.sceneBinding(24L, 1L, principal));
} }
@Test @Test

View file

@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
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;
@ -77,7 +78,7 @@ public class SwitchControllerTests {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
controller.deleteById(42L, principal); Assertions.assertDoesNotThrow(() -> controller.deleteById(42L, principal));
} }
@Test @Test

View file

@ -33,8 +33,6 @@ public class DeviceServiceTests {
@Mock private DevicePropagationService devicePropagationService; @Mock private DevicePropagationService devicePropagationService;
@Mock private ConditionRepository<Condition<?>> conditionRepository;
@InjectMocks private DeviceService deviceService; @InjectMocks private DeviceService deviceService;
@Test @Test
@ -101,7 +99,7 @@ public class DeviceServiceTests {
a.getScenes().add(sp); a.getScenes().add(sp);
when(automationService.findTriggersByDeviceId(1L)).thenReturn(List.of(b)); when(automationService.findTriggersByDeviceId(1L)).thenReturn(List.of(b));
when(conditionRepository.findAllByAutomationId(5L)).thenReturn(List.of(c)); when(automationService.findAllConditionsByAutomationId(5L)).thenReturn(List.of(c));
when(automationService.findByVerifiedId(5L)).thenReturn(a); when(automationService.findByVerifiedId(5L)).thenReturn(a);
when(sceneService.findByValidatedId(4L)).thenReturn(s); when(sceneService.findByValidatedId(4L)).thenReturn(s);