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 javax.persistence.*;
import lombok.Data;
import lombok.NonNull;
@Data
@Entity
@ -26,6 +27,7 @@ public class ConfirmationToken {
}
@Temporal(TemporalType.TIMESTAMP)
@NonNull
private Date createdDate;
@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 */
@Entity
@EqualsAndHashCode(callSuper = true)
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Switchable extends OutputDevice {

View file

@ -1,9 +1,6 @@
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.AutomationRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.TriggerRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -12,13 +9,16 @@ import org.springframework.stereotype.Component;
public class AutomationService {
private final AutomationRepository automationRepository;
private final TriggerRepository<Trigger<?>> triggerRepository;
private final ConditionRepository<Condition<?>> conditionRepository;
@Autowired
public AutomationService(
AutomationRepository automationRepository,
TriggerRepository<Trigger<?>> triggerRepository) {
TriggerRepository<Trigger<?>> triggerRepository,
ConditionRepository<Condition<?>> conditionRepository) {
this.automationRepository = automationRepository;
this.triggerRepository = triggerRepository;
this.conditionRepository = conditionRepository;
}
public List<Trigger<?>> findTriggersByDeviceId(Long deviceId) {
@ -28,4 +28,8 @@ public class AutomationService {
public Automation findByVerifiedId(Long automationId) {
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 DevicePopulationService devicePopulationService;
private final DevicePropagationService devicePropagationService;
private final ConditionRepository<Condition<?>> conditionRepository;
@Autowired
public DeviceService(
@ -32,8 +31,7 @@ public class DeviceService {
AutomationService automationService,
EagerUserRepository userRepository,
DevicePopulationService devicePopulationService,
DevicePropagationService devicePropagationService,
ConditionRepository<Condition<?>> conditionRepository) {
DevicePropagationService devicePropagationService) {
this.deviceRepository = deviceRepository;
this.sceneService = sceneService;
this.roomRepository = roomRepository;
@ -41,7 +39,6 @@ public class DeviceService {
this.userRepository = userRepository;
this.devicePopulationService = devicePopulationService;
this.devicePropagationService = devicePropagationService;
this.conditionRepository = conditionRepository;
}
public void throwIfRoomNotOwned(Long roomId, String username) throws NotFoundException {
@ -65,7 +62,7 @@ public class DeviceService {
.filter(
a -> {
final List<Condition<?>> conditions =
conditionRepository.findAllByAutomationId(a.getId());
automationService.findAllConditionsByAutomationId(a.getId());
if (conditions.isEmpty()) return true;
return conditions.stream().allMatch(Condition::triggered);
})

View file

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

View file

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

View file

@ -43,7 +43,7 @@ public class GuestControllerTests {
@Test
public void findAllEmptyTest() {
List<UserResponse> l = guestController.findAll();
assertThat(l.isEmpty());
assertThat(l).isEmpty();
}
@DisplayName(
@ -85,7 +85,7 @@ public class GuestControllerTests {
public void findHostsEmptyTest() {
when(mockPrincipal.getName()).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")
@ -93,7 +93,7 @@ public class GuestControllerTests {
public void findGuestsEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(user);
assertThat(guestController.findGuests(mockPrincipal).isEmpty());
assertThat(guestController.findGuests(mockPrincipal)).isEmpty();
}
@Test

View file

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

View file

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

View file

@ -19,6 +19,7 @@ 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;
@ -77,7 +78,7 @@ public class SwitchControllerTests {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
controller.deleteById(42L, principal);
Assertions.assertDoesNotThrow(() -> controller.deleteById(42L, principal));
}
@Test

View file

@ -33,8 +33,6 @@ public class DeviceServiceTests {
@Mock private DevicePropagationService devicePropagationService;
@Mock private ConditionRepository<Condition<?>> conditionRepository;
@InjectMocks private DeviceService deviceService;
@Test
@ -101,7 +99,7 @@ public class DeviceServiceTests {
a.getScenes().add(sp);
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(sceneService.findByValidatedId(4L)).thenReturn(s);