Code smells fix
This commit is contained in:
parent
d63ec73127
commit
2f9585bd20
21 changed files with 89 additions and 42 deletions
|
@ -70,7 +70,7 @@ public class ButtonDimmerController
|
|||
|
||||
deviceService.saveAllAsOwner(buttonDimmer.getOutputs(), principal.getName());
|
||||
|
||||
return buttonDimmer.getOutputs();
|
||||
return buttonDimmer.getDimmables();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
|
|
@ -96,8 +96,8 @@ public abstract class InputDeviceConnectionController<
|
|||
* @return the list of output devices attached to the input device of id inputId
|
||||
* @throws NotFoundException if inputId or outputId are not valid
|
||||
*/
|
||||
protected Set<? extends OutputDevice> addOutput(
|
||||
Long inputId, List<Long> outputs, String username) throws NotFoundException {
|
||||
protected Set<OutputDevice> addOutput(Long inputId, List<Long> outputs, String username)
|
||||
throws NotFoundException {
|
||||
final Connection pair = checkConnectionIDs(inputId, outputs, username);
|
||||
|
||||
for (final O o : pair.getOutputs()) {
|
||||
|
@ -116,8 +116,8 @@ public abstract class InputDeviceConnectionController<
|
|||
* @return the list of output devices attached to the input device of id inputId
|
||||
* @throws NotFoundException if inputId or outputId are not valid
|
||||
*/
|
||||
protected Set<? extends OutputDevice> removeOutput(
|
||||
Long inputId, List<Long> outputs, String username) throws NotFoundException {
|
||||
protected Set<OutputDevice> removeOutput(Long inputId, List<Long> outputs, String username)
|
||||
throws NotFoundException {
|
||||
final Connection pair = checkConnectionIDs(inputId, outputs, username);
|
||||
|
||||
for (final O o : pair.getOutputs()) {
|
||||
|
|
|
@ -62,7 +62,7 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
|
|||
dimmer.setLightIntensity(bd.getIntensity());
|
||||
deviceService.saveAllAsOwner(dimmer.getOutputs(), principal.getName());
|
||||
|
||||
return dimmer.getOutputs();
|
||||
return dimmer.getDimmables();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
|
|
@ -77,7 +77,7 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
|
|||
}
|
||||
|
||||
deviceService.saveAsOwner(s, principal.getName());
|
||||
return deviceService.saveAllAsOwner(s.getOutputs(), principal.getName());
|
||||
return deviceService.saveAllAsOwner(s.getSwitchables(), principal.getName());
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Triggerable;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
@ -8,5 +9,5 @@ import lombok.Setter;
|
|||
public abstract class TriggerDTO {
|
||||
@NotNull @Getter @Setter private long deviceId;
|
||||
|
||||
public abstract Trigger<?> toModel();
|
||||
public abstract Trigger<? extends Triggerable> toModel();
|
||||
}
|
||||
|
|
|
@ -18,14 +18,14 @@ public class ButtonDimmer extends Dimmer {
|
|||
|
||||
/** Increases the current intensity level of the dimmable light by DIM_INCREMENT */
|
||||
public void increaseIntensity() {
|
||||
for (Dimmable dl : getOutputs()) {
|
||||
for (Dimmable dl : getDimmables()) {
|
||||
dl.setIntensity(dl.getIntensity() + DIM_INCREMENT);
|
||||
}
|
||||
}
|
||||
|
||||
/** Decreases the current intensity level of the dimmable light by DIM_INCREMENT */
|
||||
public void decreaseIntensity() {
|
||||
for (Dimmable dl : getOutputs()) {
|
||||
for (Dimmable dl : getDimmables()) {
|
||||
dl.setIntensity(dl.getIntensity() - DIM_INCREMENT);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import javax.persistence.*;
|
||||
import lombok.Data;
|
||||
|
@ -43,4 +44,21 @@ public class ConfirmationToken {
|
|||
confirmToken = UUID.randomUUID().toString();
|
||||
resetPassword = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ConfirmationToken that = (ConfirmationToken) o;
|
||||
return resetPassword == that.resetPassword
|
||||
&& Objects.equals(id, that.id)
|
||||
&& confirmToken.equals(that.confirmToken)
|
||||
&& createdDate.equals(that.createdDate)
|
||||
&& Objects.equals(user, that.user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, confirmToken, createdDate, user, resetPassword);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ public class Dimmable extends Switchable implements RangeTriggerable {
|
|||
@ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH)
|
||||
@GsonExclude
|
||||
@SocketGsonExclude
|
||||
@EqualsAndHashCode.Exclude
|
||||
@Getter
|
||||
@Setter
|
||||
private Set<Dimmer> dimmers = new HashSet<>();
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
|
||||
/** Represents a generic dimmer input device */
|
||||
@Entity
|
||||
|
@ -20,6 +21,7 @@ public abstract class Dimmer extends InputDevice implements Connectable<Dimmable
|
|||
@GsonExclude
|
||||
@SocketGsonExclude
|
||||
@EqualsAndHashCode.Exclude
|
||||
@Getter
|
||||
@JoinTable(
|
||||
name = "dimmer_dimmable",
|
||||
joinColumns = @JoinColumn(name = "dimmer_id"),
|
||||
|
@ -32,8 +34,8 @@ public abstract class Dimmer extends InputDevice implements Connectable<Dimmable
|
|||
* @return duh
|
||||
*/
|
||||
@Override
|
||||
public Set<Dimmable> getOutputs() {
|
||||
return this.dimmables;
|
||||
public Set<OutputDevice> getOutputs() {
|
||||
return Set.copyOf(this.dimmables);
|
||||
}
|
||||
|
||||
/** Add a light to be controller by this dimmer */
|
||||
|
@ -44,10 +46,10 @@ public abstract class Dimmer extends InputDevice implements Connectable<Dimmable
|
|||
public void connect(Dimmable output, boolean connect) {
|
||||
if (connect) {
|
||||
output.getDimmers().add(this);
|
||||
getOutputs().add(output);
|
||||
dimmables.add(output);
|
||||
} else {
|
||||
output.getDimmers().remove(this);
|
||||
getOutputs().remove(output);
|
||||
dimmables.remove(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ public abstract class InputDevice extends Device {
|
|||
super(kind, FlowType.INPUT);
|
||||
}
|
||||
|
||||
public Set<? extends OutputDevice> getOutputs() {
|
||||
public Set<OutputDevice> getOutputs() {
|
||||
return Set.of();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ public class KnobDimmer extends Dimmer implements RangeTriggerable {
|
|||
*/
|
||||
public void setLightIntensity(int intensity) {
|
||||
this.intensity = intensity;
|
||||
for (Dimmable dl : getOutputs()) {
|
||||
for (Dimmable dl : getDimmables()) {
|
||||
dl.setIntensity(intensity);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
|
||||
/** A switch input device */
|
||||
@Entity
|
||||
|
@ -22,6 +23,7 @@ public class Switch extends InputDevice implements BooleanTriggerable, Connectab
|
|||
@GsonExclude
|
||||
@SocketGsonExclude
|
||||
@EqualsAndHashCode.Exclude
|
||||
@Getter
|
||||
@JoinTable(
|
||||
name = "switch_switchable",
|
||||
joinColumns = @JoinColumn(name = "switch_id"),
|
||||
|
@ -64,17 +66,17 @@ public class Switch extends InputDevice implements BooleanTriggerable, Connectab
|
|||
}
|
||||
|
||||
@Override
|
||||
public Set<Switchable> getOutputs() {
|
||||
return switchables;
|
||||
public Set<OutputDevice> getOutputs() {
|
||||
return Set.copyOf(switchables);
|
||||
}
|
||||
|
||||
public void connect(Switchable output, boolean connect) {
|
||||
if (connect) {
|
||||
output.getSwitches().add(this);
|
||||
getOutputs().add(output);
|
||||
switchables.add(output);
|
||||
} else {
|
||||
output.getSwitches().remove(this);
|
||||
getOutputs().remove(output);
|
||||
switchables.remove(output);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
|||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
|
||||
|
@ -55,4 +56,18 @@ public abstract class Switchable extends OutputDevice {
|
|||
newState.setOn(isOn());
|
||||
return newState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
Switchable that = (Switchable) o;
|
||||
return isOn() == that.isOn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), isOn());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,15 +21,15 @@ public class AutomationService {
|
|||
this.conditionRepository = conditionRepository;
|
||||
}
|
||||
|
||||
public List<Trigger<?>> findTriggersByDeviceId(Long deviceId) {
|
||||
return triggerRepository.findAllByDeviceId(deviceId);
|
||||
public void findTriggersByDeviceId(Long deviceId, List<Trigger<?>> toPut) {
|
||||
toPut.addAll(triggerRepository.findAllByDeviceId(deviceId));
|
||||
}
|
||||
|
||||
public Automation findByVerifiedId(Long automationId) {
|
||||
return automationRepository.findById(automationId).orElseThrow();
|
||||
}
|
||||
|
||||
public List<Condition<?>> findAllConditionsByAutomationId(Long automationId) {
|
||||
return conditionRepository.findAllByAutomationId(automationId);
|
||||
public void findAllConditionsByAutomationId(Long automationId, List<Condition<?>> toPut) {
|
||||
toPut.addAll(conditionRepository.findAllByAutomationId(automationId));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
|||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
@ -52,7 +53,8 @@ public class DeviceService {
|
|||
public void triggerTriggers(Device device, final String username) {
|
||||
|
||||
final long deviceId = device.getId();
|
||||
final List<Trigger<?>> triggers = automationService.findTriggersByDeviceId(deviceId);
|
||||
final List<Trigger<?>> triggers = new ArrayList<>();
|
||||
automationService.findTriggersByDeviceId(deviceId, triggers);
|
||||
|
||||
triggers.stream()
|
||||
.filter(Trigger::triggered)
|
||||
|
@ -61,8 +63,9 @@ public class DeviceService {
|
|||
.distinct()
|
||||
.filter(
|
||||
a -> {
|
||||
final List<Condition<?>> conditions =
|
||||
automationService.findAllConditionsByAutomationId(a.getId());
|
||||
final List<Condition<?>> conditions = new ArrayList<>();
|
||||
automationService.findAllConditionsByAutomationId(
|
||||
a.getId(), conditions);
|
||||
if (conditions.isEmpty()) return true;
|
||||
return conditions.stream().allMatch(Condition::triggered);
|
||||
})
|
||||
|
|
|
@ -98,7 +98,7 @@ public class SwitchControllerTests {
|
|||
ArrayList<Switchable> helper = new ArrayList<Switchable>();
|
||||
helper.add(light);
|
||||
helper.add(light2);
|
||||
when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName()))
|
||||
when(deviceService.saveAllAsOwner(aSwitch.getSwitchables(), principal.getName()))
|
||||
.thenReturn(helper);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
|
@ -123,7 +123,7 @@ public class SwitchControllerTests {
|
|||
ArrayList<Switchable> helper = new ArrayList<Switchable>();
|
||||
helper.add(light);
|
||||
helper.add(light2);
|
||||
when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName()))
|
||||
when(deviceService.saveAllAsOwner(aSwitch.getSwitchables(), principal.getName()))
|
||||
.thenReturn(helper);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
|
@ -148,7 +148,7 @@ public class SwitchControllerTests {
|
|||
ArrayList<Switchable> helper = new ArrayList<Switchable>();
|
||||
helper.add(light);
|
||||
helper.add(light2);
|
||||
when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName()))
|
||||
when(deviceService.saveAllAsOwner(aSwitch.getSwitchables(), principal.getName()))
|
||||
.thenReturn(helper);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
|
|
|
@ -39,7 +39,7 @@ public class ButtonDimmerTests {
|
|||
@DisplayName(" increase the intensity ")
|
||||
public void increase() {
|
||||
buttonDimmer.increaseIntensity();
|
||||
for (Dimmable dl : buttonDimmer.getOutputs()) {
|
||||
for (Dimmable dl : buttonDimmer.getDimmables()) {
|
||||
assertTrue(dl.getIntensity() > 10);
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class ButtonDimmerTests {
|
|||
@DisplayName(" decrease the intensity ")
|
||||
public void decrease() {
|
||||
buttonDimmer.decreaseIntensity();
|
||||
for (Dimmable dl : buttonDimmer.getOutputs()) {
|
||||
for (Dimmable dl : buttonDimmer.getDimmables()) {
|
||||
assertTrue(dl.getIntensity() < 10);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,12 +34,13 @@ public class DimmerTests {
|
|||
@DisplayName("connect off")
|
||||
public void connectOff() {
|
||||
DimmableLight d = new DimmableLight();
|
||||
d.setId(35L);
|
||||
d.getDimmers().add(this.dimmer);
|
||||
dimmer.getOutputs().add(d);
|
||||
dimmer.getDimmables().add(d);
|
||||
dimmer.connect(d, false);
|
||||
|
||||
assertFalse(d.getDimmers().contains((this.dimmer)));
|
||||
|
||||
assertFalse((this.dimmer.getOutputs().contains(d)));
|
||||
assertFalse(this.dimmer.getOutputs().contains(d));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ public class KnobDimmerTests {
|
|||
@DisplayName(" set the intensity ")
|
||||
public void increase() {
|
||||
knobDimmer.setLightIntensity(30);
|
||||
for (Dimmable dl : knobDimmer.getOutputs()) {
|
||||
for (Dimmable dl : knobDimmer.getDimmables()) {
|
||||
assertEquals(30, dl.getIntensity());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,9 @@ public class SwitchTests {
|
|||
RegularLight regularLight = new RegularLight();
|
||||
DimmableLight dimmableLight = new DimmableLight();
|
||||
SmartPlug smartPlug = new SmartPlug();
|
||||
this.aSwitch.getOutputs().add(regularLight);
|
||||
this.aSwitch.getOutputs().add(dimmableLight);
|
||||
this.aSwitch.getOutputs().add(smartPlug);
|
||||
this.aSwitch.getSwitchables().add(regularLight);
|
||||
this.aSwitch.getSwitchables().add(dimmableLight);
|
||||
this.aSwitch.getSwitchables().add(smartPlug);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -80,7 +80,7 @@ public class SwitchTests {
|
|||
@DisplayName("Checks that toggling on sets all elements of the Set on as well")
|
||||
public void toggleEffctOnSet() {
|
||||
aSwitch.toggle();
|
||||
for (final Switchable s : aSwitch.getOutputs()) {
|
||||
for (final Switchable s : aSwitch.getSwitchables()) {
|
||||
assertTrue(s.isOn());
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ public class SwitchTests {
|
|||
public void toggleOffEffectOnElementes() {
|
||||
aSwitch.setOn(true);
|
||||
aSwitch.toggle();
|
||||
for (final Switchable s : aSwitch.getOutputs()) {
|
||||
for (final Switchable s : aSwitch.getSwitchables()) {
|
||||
assertFalse(s.isOn());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,8 +98,12 @@ public class DeviceServiceTests {
|
|||
|
||||
a.getScenes().add(sp);
|
||||
|
||||
when(automationService.findTriggersByDeviceId(1L)).thenReturn(List.of(b));
|
||||
when(automationService.findAllConditionsByAutomationId(5L)).thenReturn(List.of(c));
|
||||
doAnswer(i -> ((List<Trigger<?>>) i.getArgument(1)).add(b))
|
||||
.when(automationService)
|
||||
.findTriggersByDeviceId(eq(1L), any());
|
||||
doAnswer(i -> ((List<Condition<?>>) i.getArgument(1)).add(c))
|
||||
.when(automationService)
|
||||
.findAllConditionsByAutomationId(eq(5L), any());
|
||||
when(automationService.findByVerifiedId(5L)).thenReturn(a);
|
||||
when(sceneService.findByValidatedId(4L)).thenReturn(s);
|
||||
|
||||
|
|
Loading…
Reference in a new issue