Merge branch 'dev' into '75-backend-unit-testing-matteo'

# Conflicts:
#   src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java
This commit is contained in:
Matteo Omenetti 2020-05-17 10:20:16 +02:00
commit 5f692db5f7
50 changed files with 291 additions and 687 deletions

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionSaveRequest;
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;
@ -29,7 +29,7 @@ public class BooleanConditionController {
return booleanConditionRepository.findAllByAutomationId(automationId);
}
private BooleanCondition save(BooleanCondition newRL, BooleanConditionSaveRequest s) {
private BooleanCondition save(BooleanCondition newRL, BooleanConditionOrTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId());
newRL.setOn(s.isOn());
@ -39,13 +39,13 @@ public class BooleanConditionController {
@PostMapping
public BooleanCondition create(
@Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) {
@Valid @RequestBody BooleanConditionOrTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new BooleanCondition(), booleanTriggerSaveRequest);
}
@PutMapping
public BooleanCondition update(
@Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest)
@Valid @RequestBody BooleanConditionOrTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException {
return save(
booleanConditionRepository

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanTriggerSaveRequest;
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;
@ -29,7 +29,7 @@ public class BooleanTriggerController {
return booleanTriggerRepository.findAllByAutomationId(automationId);
}
private BooleanTrigger save(BooleanTrigger newRL, BooleanTriggerSaveRequest s) {
private BooleanTrigger save(BooleanTrigger newRL, BooleanConditionOrTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId());
newRL.setOn(s.isOn());
@ -39,13 +39,13 @@ public class BooleanTriggerController {
@PostMapping
public BooleanTrigger create(
@Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) {
@Valid @RequestBody BooleanConditionOrTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new BooleanTrigger(), booleanTriggerSaveRequest);
}
@PutMapping
public BooleanTrigger update(
@Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest)
@Valid @RequestBody BooleanConditionOrTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException {
return save(
booleanTriggerRepository

View file

@ -18,7 +18,7 @@ public class CurtainsController {
@Autowired private DeviceService deviceService;
@Autowired private CurtainsRepository curtainsService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository;
@Autowired private StateRepository<State> stateRepository;
private Curtains save(Curtains newRL, DimmableSaveRequest s, final Principal principal) {
newRL.setName(s.getName());
@ -53,7 +53,7 @@ public class CurtainsController {
}
@PostMapping("/{id}/state")
public State<? extends Dimmable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -63,7 +63,7 @@ public class CurtainsController {
curtainsService
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Dimmable> s = c.cloneState();
State s = c.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -18,7 +18,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
private final DimmableLightRepository dimmableLightRepository;
private final SceneRepository sceneRepository;
private final StateRepository<State<?>> stateRepository;
private final StateRepository<State> stateRepository;
private final DeviceService deviceService;
@Autowired
@ -26,7 +26,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
UserRepository userRepository,
DimmableLightRepository dimmableLightRepository,
SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository,
StateRepository<State> stateRepository,
DeviceService deviceService) {
super(userRepository, dimmableLightRepository);
this.dimmableLightRepository = dimmableLightRepository;
@ -87,7 +87,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
* necessary to specify the query in the mapping
*/
@PostMapping("/{id}/state")
public State<? extends Dimmable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -97,7 +97,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
dimmableLightRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Dimmable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -17,9 +17,9 @@ public class DimmableStateController {
@Autowired private DimmableStateRepository dimmableStateRepository;
@PutMapping
public DimmableState<?> update(@Valid @RequestBody DimmableStateSaveRequest ss)
public DimmableState update(@Valid @RequestBody DimmableStateSaveRequest ss)
throws NotFoundException {
final DimmableState<?> initial =
final DimmableState initial =
dimmableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
initial.setIntensity(ss.getIntensity());
dimmableStateRepository.save(initial);

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionOrTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeConditionRepository;
@ -29,7 +29,7 @@ public class RangeConditionController {
return rangeConditionRepository.findAllByAutomationId(automationId);
}
private RangeCondition save(RangeCondition newRL, RangeConditionSaveRequest s) {
private RangeCondition save(RangeCondition newRL, RangeConditionOrTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId());
newRL.setOperator(s.getOperator());
@ -40,13 +40,13 @@ public class RangeConditionController {
@PostMapping
public RangeCondition create(
@Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) {
@Valid @RequestBody RangeConditionOrTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new RangeCondition(), booleanTriggerSaveRequest);
}
@PutMapping
public RangeCondition update(
@Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest)
@Valid @RequestBody RangeConditionOrTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException {
return save(
rangeConditionRepository

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionOrTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTriggerRepository;
@ -29,7 +29,7 @@ public class RangeTriggerController {
return rangeTriggerRepository.findAllByAutomationId(automationId);
}
private RangeTrigger save(RangeTrigger newRL, RangeTriggerSaveRequest s) {
private RangeTrigger save(RangeTrigger newRL, RangeConditionOrTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId());
newRL.setOperator(s.getOperator());
@ -40,13 +40,13 @@ public class RangeTriggerController {
@PostMapping
public RangeTrigger create(
@Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) {
@Valid @RequestBody RangeConditionOrTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new RangeTrigger(), booleanTriggerSaveRequest);
}
@PutMapping
public RangeTrigger update(
@Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest)
@Valid @RequestBody RangeConditionOrTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException {
return save(
rangeTriggerRepository

View file

@ -29,7 +29,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
private RegularLightRepository regularLightRepository;
private SceneRepository sceneRepository;
private StateRepository<State<?>> stateRepository;
private StateRepository<State> stateRepository;
private DeviceService deviceService;
@Autowired
@ -37,7 +37,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
UserRepository userRepository,
RegularLightRepository regularLightRepository,
SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository,
StateRepository<State> stateRepository,
DeviceService deviceService) {
super(userRepository, regularLightRepository);
this.regularLightRepository = regularLightRepository;
@ -98,7 +98,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
}
@PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -107,7 +107,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
regularLightRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -22,7 +22,7 @@ public class SceneController {
@Autowired private SceneRepository sceneRepository;
@Autowired private UserRepository userRepository;
@Autowired private SceneService sceneService;
@Autowired private StateRepository<State<?>> stateRepository;
@Autowired private StateRepository<State> stateRepository;
@GetMapping
public List<Scene> findAll(
@ -78,7 +78,7 @@ public class SceneController {
}
@PostMapping("/{id}/copyFrom/{copyId}")
public @ResponseBody List<State<?>> copy(
public @ResponseBody List<State> copy(
@PathVariable("id") long id,
@PathVariable("copyId") long copyId,
final Principal principal)
@ -126,8 +126,8 @@ public class SceneController {
* id).
*/
@GetMapping(path = "/{sceneId}/states")
public List<State<?>> getDevices(@PathVariable("sceneId") long sceneId) {
Iterable<State<?>> states = stateRepository.findBySceneId(sceneId);
public List<State> getStates(@PathVariable("sceneId") long sceneId) {
Iterable<State> states = stateRepository.findBySceneId(sceneId);
return toList(states);
}
}

View file

@ -26,7 +26,7 @@ public class SecurityCameraController {
private final DeviceService deviceService;
private final SecurityCameraRepository securityCameraService;
private final SceneRepository sceneRepository;
private final StateRepository<State<?>> stateRepository;
private final StateRepository<State> stateRepository;
private final CameraConfigurationService cameraConfigurationService;
@Autowired
@ -34,7 +34,7 @@ public class SecurityCameraController {
DeviceService deviceService,
SecurityCameraRepository securityCameraService,
SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository,
StateRepository<State> stateRepository,
CameraConfigurationService cameraConfigurationService) {
this.deviceService = deviceService;
this.securityCameraService = securityCameraService;
@ -80,7 +80,7 @@ public class SecurityCameraController {
}
@PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -90,7 +90,7 @@ public class SecurityCameraController {
securityCameraService
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -19,7 +19,7 @@ public class SmartPlugController {
@Autowired private DeviceService deviceService;
@Autowired private SmartPlugRepository smartPlugRepository;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository;
@Autowired private StateRepository<State> stateRepository;
private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp, final Principal principal) {
newSP.setOn(sp.isOn());
@ -67,7 +67,7 @@ public class SmartPlugController {
}
@PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -77,7 +77,7 @@ public class SmartPlugController {
smartPlugRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -22,9 +22,9 @@ public class SwitchableStateController {
@Autowired private SwitchableStateRepository switchableStateRepository;
@PutMapping
public SwitchableState<?> update(@Valid @RequestBody SwitchableStateSaveRequest ss)
public SwitchableState update(@Valid @RequestBody SwitchableStateSaveRequest ss)
throws NotFoundException {
final SwitchableState<?> initial =
final SwitchableState initial =
switchableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
initial.setOn(ss.isOn());
switchableStateRepository.save(initial);

View file

@ -21,7 +21,7 @@ public class ThermostatController {
@Autowired private ThermostatRepository thermostatRepository;
@Autowired private ThermostatPopulationService thermostatService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository;
@Autowired private StateRepository<State> stateRepository;
private Thermostat save(Thermostat newT, ThermostatSaveRequest t, final Principal principal) {
newT.setTargetTemperature(t.getTargetTemperature());
@ -63,7 +63,7 @@ public class ThermostatController {
}
@PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -73,7 +73,7 @@ public class ThermostatController {
thermostatRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -158,7 +158,7 @@ public class UserAccountController {
final ConfirmationToken token =
confirmationTokenRepository.findByConfirmToken(resetRequest.getConfirmationToken());
if (token == null || !token.getResetPassword()) {
if (token == null || !token.isResetPassword()) {
throw new EmailTokenNotFoundException();
}
@ -186,7 +186,7 @@ public class UserAccountController {
final ConfirmationToken token =
confirmationTokenRepository.findByConfirmToken(confirmationToken);
if (token != null && !token.getResetPassword()) {
if (token != null && !token.isResetPassword()) {
token.getUser().setEnabled(true);
userRepository.save(token.getUser());
response.sendRedirect(emailConfig.getRegistrationRedirect());

View file

@ -1,14 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Condition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.List;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
@ -36,7 +28,7 @@ public class AutomationFastUpdateRequest {
}
public static class RangeTriggerDTO extends TriggerDTO {
@NotNull RangeTrigger.Operator operator;
@NotNull Operator operator;
@NotNull double range;
@Override
@ -85,7 +77,7 @@ public class AutomationFastUpdateRequest {
public static class RangeConditionDTO extends ConditionDTO {
@NotNull RangeCondition.Operator operator;
@NotNull Operator operator;
@NotNull double range;
@Override

View file

@ -4,7 +4,7 @@ import javax.validation.constraints.NotNull;
import lombok.Data;
@Data
public class BooleanTriggerSaveRequest {
public class BooleanConditionOrTriggerSaveRequest {
private long id;

View file

@ -1,42 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotNull;
public class BooleanConditionSaveRequest {
@NotNull private long id;
@NotNull private Long deviceId;
@NotNull private Long automationId;
@NotNull private boolean on;
public long getId() {
return id;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getAutomationId() {
return automationId;
}
public void setAutomationId(Long automationId) {
this.automationId = automationId;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
}

View file

@ -1,11 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Operator;
import javax.validation.constraints.NotNull;
import lombok.Data;
@Data
public class RangeTriggerSaveRequest {
public class RangeConditionOrTriggerSaveRequest {
private long id;
@ -13,7 +13,7 @@ public class RangeTriggerSaveRequest {
@NotNull private Long automationId;
@NotNull private RangeTrigger.Operator operator;
@NotNull private Operator operator;
@NotNull private double range;
}

View file

@ -1,54 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition.Operator;
import javax.validation.constraints.NotNull;
public class RangeConditionSaveRequest {
@NotNull private long id;
@NotNull private Long deviceId;
@NotNull private Long automationId;
@NotNull private RangeCondition.Operator operator;
@NotNull private double range;
public long getId() {
return id;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getAutomationId() {
return automationId;
}
public void setAutomationId(Long automationId) {
this.automationId = automationId;
}
public Operator getOperator() {
return operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
public double getRange() {
return range;
}
public void setRange(double range) {
this.range = range;
}
}

View file

@ -5,9 +5,11 @@ import java.math.BigDecimal;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class SensorSaveRequest {
/** The type of this sensor */
@NotNull

View file

@ -6,8 +6,9 @@ import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import lombok.Data;
@Data
@Entity
public class Automation {
@ -22,7 +23,6 @@ public class Automation {
@GsonExclude
private User user;
@NotNull
@Column(name = "user_id", nullable = false)
@GsonExclude
private Long userId;
@ -36,49 +36,5 @@ public class Automation {
@OneToMany(mappedBy = "automation", orphanRemoval = true, cascade = CascadeType.REMOVE)
private Set<Condition<?>> conditions = new HashSet<>();
@NotNull @NotEmpty private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set<ScenePriority> getScenes() {
return scenes;
}
public Set<Trigger<?>> getTriggers() {
return triggers;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Set<Condition<?>> getConditions() {
return conditions;
}
@NotEmpty private String name;
}

View file

@ -2,10 +2,14 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
@Entity
public class BooleanCondition extends Condition<BooleanTriggerable> {
@Getter
@Setter
@Column(name = "switchable_on")
private boolean on;
@ -13,14 +17,6 @@ public class BooleanCondition extends Condition<BooleanTriggerable> {
super("booleanCondition");
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
@Override
public boolean triggered() {
return this.getDevice().readTriggerState() == isOn();

View file

@ -2,29 +2,21 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
@Entity
public class BooleanTrigger extends Trigger<BooleanTriggerable> {
@Column(name = "switchable_on")
@Getter
@Setter
private boolean on;
public BooleanTrigger() {
super("booleanTrigger");
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public boolean check(boolean on) {
return this.on == on;
}
@Override
public boolean triggered() {
return getDevice().readTriggerState() == isOn();

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
/**

View file

@ -10,7 +10,9 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;
import lombok.Data;
@Data
@Entity
public abstract class Condition<D> {
@ -52,42 +54,6 @@ public abstract class Condition<D> {
public abstract boolean triggered();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public D getDevice() {
return device;
}
public void setDevice(D device) {
this.device = device;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Automation getAutomation() {
return automation;
}
public void setAutomation(Automation automation) {
this.automation = automation;
}
public Long getAutomationId() {
return automationId;
}
public Condition<D> setAutomationId(Long automationId) {
this.automationId = automationId;
return this;

View file

@ -12,7 +12,9 @@ import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import lombok.Data;
@Data
@Entity
public class ConfirmationToken {
@ -40,47 +42,4 @@ public class ConfirmationToken {
confirmToken = UUID.randomUUID().toString();
resetPassword = false;
}
/** Constructor for hibernate reflective stuff things whatever */
public ConfirmationToken() {}
public Long getId() {
return id;
}
public String getConfirmToken() {
return confirmToken;
}
public Date getCreatedDate() {
return (Date) createdDate.clone();
}
public User getUser() {
return user;
}
public void setId(Long id) {
this.id = id;
}
public void setConfirmToken(String confirmToken) {
this.confirmToken = confirmToken;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = (Date) createdDate.clone();
}
public void setUser(User user) {
this.user = user;
}
public boolean getResetPassword() {
return resetPassword;
}
public void setResetPassword(boolean resetPassword) {
this.resetPassword = resetPassword;
}
}

View file

@ -6,9 +6,11 @@ import com.google.gson.annotations.SerializedName;
import io.swagger.annotations.ApiModelProperty;
import java.util.Set;
import javax.persistence.*;
import lombok.Data;
/** Generic abstraction for a smart home device */
@Entity
@Data
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Device {
@ -65,7 +67,7 @@ public abstract class Device {
@OneToMany(mappedBy = "device", orphanRemoval = true)
@GsonExclude
@SocketGsonExclude
private Set<State<?>> states;
private Set<State> states;
@Transient @GsonExclude private Long fromHostId = null;
@ -73,88 +75,8 @@ public abstract class Device {
@Transient @GsonExclude private boolean deleted = false;
public Device(String kind, FlowType flowType) {
protected Device(String kind, FlowType flowType) {
this.kind = kind;
this.flowType = flowType;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public Set<Trigger<? extends Device>> getTriggers() {
return triggers;
}
public void setTriggers(Set<Trigger<? extends Device>> triggers) {
this.triggers = triggers;
}
public Long getRoomId() {
return roomId;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKind() {
return kind;
}
public FlowType getFlowType() {
return flowType;
}
public Set<State<?>> getStates() {
return states;
}
public void setStates(Set<State<?>> states) {
this.states = states;
}
public Long getFromHostId() {
return fromHostId;
}
public void setFromHostId(Long fromHostId) {
this.fromHostId = fromHostId;
}
public boolean isFromGuest() {
return fromGuest;
}
public void setFromGuest(boolean fromGuest) {
this.fromGuest = fromGuest;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}

View file

@ -7,7 +7,8 @@ import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@ -23,20 +24,17 @@ public class Dimmable extends Switchable implements RangeTriggerable {
private Set<Dimmer> dimmers = new HashSet<>();
/** The light intensity value. Goes from 0 (off) to 100 (on) */
@NotNull
@Column(nullable = false)
@Min(0)
@Max(100)
@Getter
private Integer intensity = 0;
@NotNull
@Column(nullable = false)
@Getter
@Setter
private Integer oldIntensity = 100;
public Integer getIntensity() {
return intensity;
}
/**
* Sets the intensity to a certain level. Out of bound values are corrected to the respective
* extremums. An intensity level of 0 turns the light off, but keeps the old intensity level
@ -66,19 +64,13 @@ public class Dimmable extends Switchable implements RangeTriggerable {
intensity = on ? oldIntensity : 0;
}
public Set<Dimmer> getDimmers() {
return this.dimmers;
}
public void readStateAndSet(DimmableState<? extends Dimmable> state) {
public void readStateAndSet(DimmableState state) {
setIntensity(state.getIntensity());
}
@Override
public State<? extends Dimmable> cloneState() {
final DimmableState<Dimmable> newState = new DimmableState<>();
public State cloneState() {
final DimmableState newState = new DimmableState();
newState.setDeviceId(getId());
newState.setDevice(this);
newState.setIntensity(getIntensity());
return newState;
}

View file

@ -3,32 +3,32 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import lombok.Getter;
import lombok.Setter;
/** Represent a state for an IDimmable device */
@Entity
public class DimmableState<T extends Dimmable> extends State<T> {
public class DimmableState extends State {
public void setDevice(Dimmable device) {
setInnerDevice(device);
}
/** The light intensity value. Goes from 0 (off) to 100 (on) */
@Min(0)
@Max(100)
@Getter
@Setter
private int intensity = 0;
public int getIntensity() {
return intensity;
}
public void setIntensity(int dimAmount) {
this.intensity = dimAmount;
}
@Override
public void apply() {
getDevice().readStateAndSet(this);
((Dimmable) getDevice()).readStateAndSet(this);
}
@Override
protected State<T> copy() {
final DimmableState<T> d = new DimmableState<>();
protected DimmableState copy() {
final DimmableState d = new DimmableState();
d.setIntensity(intensity);
return d;
}

View file

@ -1,3 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface DimmableStateRepository extends StateRepository<DimmableState<?>> {}
public interface DimmableStateRepository extends StateRepository<DimmableState> {}

View file

@ -10,7 +10,7 @@ import javax.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Dimmer extends InputDevice implements Connectable<Dimmable> {
public Dimmer(String kind) {
protected Dimmer(String kind) {
super(kind);
}

View file

@ -2,22 +2,18 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
/** Represents a motion sensor device */
@Entity
public class MotionSensor extends InputDevice implements BooleanTriggerable {
@Getter
@Setter
@Column(nullable = false)
private boolean detected;
public boolean isDetected() {
return detected;
}
public void setDetected(boolean detected) {
this.detected = detected;
}
public MotionSensor() {
super("motionSensor");
}

View file

@ -0,0 +1,33 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.SerializedName;
public enum Operator {
@SerializedName("EQUAL")
EQUAL,
@SerializedName("LESS")
LESS,
@SerializedName("GREATER")
GREATER,
@SerializedName("LESS_EQUAL")
LESS_EQUAL,
@SerializedName("GREATER_EQUAL")
GREATER_EQUAL;
boolean checkAgainst(double value, double range) {
switch (this) {
case EQUAL:
return value == range;
case LESS:
return value < range;
case GREATER:
return value > range;
case GREATER_EQUAL:
return value >= range;
case LESS_EQUAL:
return value <= range;
default:
throw new IllegalStateException();
}
}
}

View file

@ -14,10 +14,10 @@ public abstract class OutputDevice extends Device {
}
/**
* Creates a State<?> object initialized to point at this device and with values copied from
* this device's state
* Creates a State object initialized to point at this device and with values copied from this
* device's state
*
* @return a new State<?> object
* @return a new State object
*/
public abstract State<? extends OutputDevice> cloneState();
public abstract State cloneState();
}

View file

@ -1,10 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger.Operator;
import com.google.gson.annotations.SerializedName;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
@Entity
public class RangeCondition extends Condition<RangeTriggerable> {
@ -13,57 +12,18 @@ public class RangeCondition extends Condition<RangeTriggerable> {
super("rangeCondition");
}
public enum Operator {
@SerializedName("EQUAL")
EQUAL,
@SerializedName("LESS")
LESS,
@SerializedName("GREATER")
GREATER,
@SerializedName("LESS_EQUAL")
LESS_EQUAL,
@SerializedName("GREATER_EQUAL")
GREATER_EQUAL
}
@Getter
@Setter
@Column(nullable = false)
private RangeCondition.Operator operator;
private Operator operator;
@NotNull
@Getter
@Setter
@Column(nullable = false)
private double range;
public RangeCondition.Operator getOperator() {
return operator;
}
public void setOperator(RangeCondition.Operator operator) {
this.operator = operator;
}
public double getRange() {
return range;
}
public void setRange(Double range) {
this.range = range;
}
@Override
public boolean triggered() {
double value = getDevice().readTriggerState();
switch (operator) {
case EQUAL:
return value == range;
case LESS:
return value < range;
case GREATER:
return value > range;
case GREATER_EQUAL:
return value >= range;
case LESS_EQUAL:
return value <= range;
}
throw new IllegalStateException();
return operator.checkAgainst(getDevice().readTriggerState(), range);
}
}

View file

@ -1,8 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.SerializedName;
import javax.persistence.Column;
import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
@Entity
public class RangeTrigger extends Trigger<RangeTriggerable> {
@ -11,56 +12,18 @@ public class RangeTrigger extends Trigger<RangeTriggerable> {
super("rangeTrigger");
}
@Override
public boolean triggered() {
double value = getDevice().readTriggerState();
switch (operator) {
case EQUAL:
return value == range;
case LESS:
return value < range;
case GREATER:
return value > range;
case GREATER_EQUAL:
return value >= range;
case LESS_EQUAL:
return value <= range;
}
throw new IllegalStateException();
}
public enum Operator {
@SerializedName("EQUAL")
EQUAL,
@SerializedName("LESS")
LESS,
@SerializedName("GREATER")
GREATER,
@SerializedName("LESS_EQUAL")
LESS_EQUAL,
@SerializedName("GREATER_EQUAL")
GREATER_EQUAL
}
@Getter
@Setter
@Column(nullable = false)
private Operator operator;
@Getter
@Setter
@Column(nullable = false)
private double range;
public Operator getOperator() {
return operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
public double getRange() {
return range;
}
public void setRange(Double range) {
this.range = range;
@Override
public boolean triggered() {
return operator.checkAgainst(getDevice().readTriggerState(), range);
}
}

View file

@ -2,7 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
/** Represents a standard non-dimmable light */
@Entity
@ -10,22 +11,12 @@ public class RegularLight extends Switchable implements BooleanTriggerable {
/** Whether the light is on or not */
@Column(name = "light_on", nullable = false)
@NotNull
@Getter
@Setter
boolean on;
public RegularLight() {
super("regularLight");
this.on = false;
}
@Override
public boolean isOn() {
return on;
}
@Override
public void setOn(boolean on) {
this.on = on;
}
@Override

View file

@ -5,7 +5,6 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
/** Represents a room in the house owned by the user */
@Entity
@ -42,12 +41,10 @@ public class Room {
* User that owns the house this room is in as a foreign key id. To use when updating and
* inserting from a REST call.
*/
@NotNull
@Column(name = "user_id", nullable = false)
private Long userId;
/** The user given name of this room (e.g. 'Master bedroom') */
@NotNull
@Column(nullable = false)
private String name;

View file

@ -5,12 +5,13 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import lombok.Data;
/**
* Represent a collection of state changes to devices even in different rooms but belonging to the
* same user
*/
@Data
@Entity
public class Scene {
@ -25,80 +26,21 @@ public class Scene {
@GsonExclude
private User user;
@NotNull
@Column(name = "user_id", nullable = false)
@GsonExclude
private Long userId;
@OneToMany(mappedBy = "scene", orphanRemoval = true)
@GsonExclude
private Set<State<?>> states = new HashSet<>();
private Set<State> states = new HashSet<>();
/** The user given name of this room (e.g. 'Master bedroom') */
@NotNull
@Column(nullable = false)
private String name;
@Column(nullable = false)
@NotNull
private Icon icon;
/** Determines whether a guest can access this scene */
@Column private boolean guestAccessEnabled;
public Icon getIcon() {
return icon;
}
public void setIcon(Icon icon) {
this.icon = icon;
}
public boolean isGuestAccessEnabled() {
return guestAccessEnabled;
}
public void setGuestAccessEnabled(boolean guestAccessEnabled) {
this.guestAccessEnabled = guestAccessEnabled;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Set<State<?>> getStates() {
return states;
}
public void setStates(Set<State<?>> states) {
this.states = states;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
}

View file

@ -11,7 +11,6 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PreRemove;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@Entity
public class ScenePriority {
@ -30,7 +29,6 @@ public class ScenePriority {
@Column(name = "automation_id", nullable = false)
private Long automationId;
@NotNull
@Min(0)
@Column(nullable = false)
private Integer priority;

View file

@ -2,7 +2,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
@Entity
public class SecurityCamera extends Switchable implements BooleanTriggerable {
@ -13,7 +12,6 @@ public class SecurityCamera extends Switchable implements BooleanTriggerable {
}
@Column(name = "camera_on", nullable = false)
@NotNull
private boolean on;
@Column(name = "video", nullable = false)

View file

@ -3,7 +3,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
/** A smart plug that can be turned either on or off */
@Entity
@ -14,7 +13,6 @@ public class SmartPlug extends Switchable implements BooleanTriggerable {
/** The total amount of power that the smart plug has consumed represented in W/h */
@Column(precision = 13, scale = 3)
@NotNull
private BigDecimal totalConsumption = BigDecimal.ZERO;
/** Whether the smart plug is on */

View file

@ -3,7 +3,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
/**
* Represents instructions on how to change the state of a particular device. Many states (plus
@ -12,95 +13,66 @@ import javax.validation.constraints.NotNull;
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"device_id", "scene_id"})})
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class State<D extends OutputDevice> {
public abstract class State {
@ManyToOne(targetEntity = OutputDevice.class)
@JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude
@Getter
private OutputDevice device;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true)
@Getter
private long id;
@ManyToOne(targetEntity = OutputDevice.class)
@JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude
private D device;
/**
* The device this state belongs in, as a foreign key id. To use when updating and inserting
* from a REST call.
*/
@Column(name = "device_id", nullable = false)
@NotNull
@Getter
@Setter
private Long deviceId;
@ManyToOne
@JoinColumn(name = "scene_id", updatable = false, insertable = false)
@GsonExclude
@Getter
@Setter
private Scene scene;
@Column(name = "scene_id", nullable = false)
@NotNull
@Getter
@Setter
private Long sceneId;
protected void setInnerDevice(OutputDevice device) {
this.device = device;
}
/** Sets the state of the connected device to the state represented by this object. */
public abstract void apply();
/** Creates a perfect copy of this state, except for the id field and the sceneId/scene */
protected abstract State<D> copy();
protected abstract State copy();
public State<D> copyToSceneId(Long sceneId) {
final State<D> s = copy();
public State copyToSceneId(Long sceneId) {
final State s = copy();
s.setDeviceId(this.deviceId);
s.device = this.device;
s.setSceneId(sceneId);
s.setScene(this.scene);
return s;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public D getDevice() {
return device;
}
public void setDevice(D device) {
this.device = device;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Scene getScene() {
return scene;
}
public void setScene(Scene scene) {
this.scene = scene;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
@PreRemove
public void removeDeviceAndScene() {
this.setScene(null);
this.setSceneId(null);
this.setDevice(null);
this.setScene(null);
this.setDeviceId(null);
this.device = null;
}
}

View file

@ -5,7 +5,7 @@ import javax.transaction.Transactional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface StateRepository<T extends State<?>> extends CrudRepository<T, Long> {
public interface StateRepository<T extends State> extends CrudRepository<T, Long> {
@Transactional
void deleteAllBySceneId(long roomId);

View file

@ -45,15 +45,13 @@ public abstract class Switchable extends OutputDevice {
return inputs;
}
public void readStateAndSet(SwitchableState<? extends Switchable> state) {
public void readStateAndSet(SwitchableState state) {
setOn(state.isOn());
}
@Override
public State<? extends Switchable> cloneState() {
final SwitchableState<Switchable> newState = new SwitchableState<>();
public State cloneState() {
final SwitchableState newState = new SwitchableState();
newState.setDeviceId(getId());
newState.setDevice(this);
newState.setOn(isOn());
return newState;
}

View file

@ -2,30 +2,30 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
/** Represents a state for a Switchable device */
@Entity
public class SwitchableState<T extends Switchable> extends State<T> {
public class SwitchableState extends State {
public void setDevice(Switchable device) {
setInnerDevice(device);
}
@Column(name = "switchable_on")
@Getter
@Setter
private boolean on;
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
@Override
public void apply() {
getDevice().readStateAndSet(this);
((Switchable) getDevice()).readStateAndSet(this);
}
@Override
protected State<T> copy() {
final SwitchableState<T> d = new SwitchableState<>();
protected SwitchableState copy() {
final SwitchableState d = new SwitchableState();
d.setOn(on);
return d;
}

View file

@ -1,3 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface SwitchableStateRepository extends StateRepository<SwitchableState<?>> {}
public interface SwitchableStateRepository extends StateRepository<SwitchableState> {}

View file

@ -5,7 +5,6 @@ import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
/** A thermostat capable of controlling cooling and heating. */
@Entity
@ -72,7 +71,7 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
Sensor.TYPICAL_VALUES.get(Sensor.SensorType.TEMPERATURE);
/** State of this thermostat */
@Column @NotNull private Thermostat.Mode mode;
@Column private Thermostat.Mode mode;
@Transient private BigDecimal measuredTemperature;

View file

@ -11,7 +11,7 @@ public class SceneService {
private final DevicePopulationService devicePopulationService;
private final DevicePropagationService devicePropagationService;
private final StateRepository<State<?>> stateRepository;
private final StateRepository<State> stateRepository;
private final SceneRepository sceneRepository;
public Scene findByValidatedId(Long id) {
@ -22,7 +22,7 @@ public class SceneService {
public SceneService(
DevicePopulationService devicePopulationService,
DevicePropagationService devicePropagationService,
StateRepository<State<?>> stateRepository,
StateRepository<State> stateRepository,
SceneRepository sceneRepository) {
this.devicePopulationService = devicePopulationService;
this.devicePropagationService = devicePropagationService;
@ -33,7 +33,7 @@ public class SceneService {
private List<Device> copyStatesToDevices(Scene fromScene) {
final List<Device> updated = new ArrayList<>(fromScene.getStates().size());
for (final State<?> s : fromScene.getStates()) {
for (final State s : fromScene.getStates()) {
s.apply();
updated.add(s.getDevice());
}
@ -54,9 +54,9 @@ public class SceneService {
return updated;
}
public List<State<?>> copyStates(Scene to, Scene from) {
final ArrayList<State<?>> states = new ArrayList<>();
for (final State<?> s : from.getStates()) {
public List<State> copyStates(Scene to, Scene from) {
final ArrayList<State> states = new ArrayList<>();
for (final State s : from.getStates()) {
states.add(stateRepository.save(s.copyToSceneId(to.getId())));
}
return states;

View file

@ -0,0 +1,77 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
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.service.DeviceService;
import java.math.BigDecimal;
import java.security.Principal;
import lombok.SneakyThrows;
import org.junit.jupiter.api.*;
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("The sensor controller")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class SensorControllerTests {
@InjectMocks private SensorController sensorController;
@Mock private DeviceService deviceService;
@Mock private Principal mockPrincipal;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
}
private void checkSensorAgainstRequest(final Sensor toCheck, final SensorSaveRequest request) {
assertThat(toCheck).isNotNull();
assertThat(toCheck.getSensor()).isEqualTo(request.getSensor());
assertThat(toCheck.getValue()).isEqualTo(request.getValue());
assertThat(toCheck.getName()).isEqualTo(request.getName());
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
}
@DisplayName("when creating should return the same object")
@Test
@SneakyThrows(NotFoundException.class)
public void testCreate() {
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
when(deviceService.saveAsOwner(any(Sensor.class), eq("user")))
.thenAnswer(i -> i.getArguments()[0]);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
final SensorSaveRequest toSend =
new SensorSaveRequest(
Sensor.SensorType.TEMPERATURE, BigDecimal.ZERO, 42L, "Test sensor");
final Sensor created = sensorController.create(toSend, mockPrincipal);
checkSensorAgainstRequest(created, toSend);
}
@DisplayName("when deleting an existant id should succeed")
@Test
@SneakyThrows(NotFoundException.class)
public void testDelete() {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
sensorController.deleteById(42L, mockPrincipal);
}
}