Merge branch 'tests' into 'dev'

Code smells fix

See merge request sa4-2020/the-sanmarinoes/backend!174
This commit is contained in:
Claudio Maggioni 2020-05-24 12:18:53 +02:00
commit 784574086b
21 changed files with 89 additions and 42 deletions

View file

@ -70,7 +70,7 @@ public class ButtonDimmerController
deviceService.saveAllAsOwner(buttonDimmer.getOutputs(), principal.getName()); deviceService.saveAllAsOwner(buttonDimmer.getOutputs(), principal.getName());
return buttonDimmer.getOutputs(); return buttonDimmer.getDimmables();
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View file

@ -96,8 +96,8 @@ public abstract class InputDeviceConnectionController<
* @return the list of output devices attached to the input device of id inputId * @return the list of output devices attached to the input device of id inputId
* @throws NotFoundException if inputId or outputId are not valid * @throws NotFoundException if inputId or outputId are not valid
*/ */
protected Set<? extends OutputDevice> addOutput( protected Set<OutputDevice> addOutput(Long inputId, List<Long> outputs, String username)
Long inputId, List<Long> outputs, String username) throws NotFoundException { throws NotFoundException {
final Connection pair = checkConnectionIDs(inputId, outputs, username); final Connection pair = checkConnectionIDs(inputId, outputs, username);
for (final O o : pair.getOutputs()) { 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 * @return the list of output devices attached to the input device of id inputId
* @throws NotFoundException if inputId or outputId are not valid * @throws NotFoundException if inputId or outputId are not valid
*/ */
protected Set<? extends OutputDevice> removeOutput( protected Set<OutputDevice> removeOutput(Long inputId, List<Long> outputs, String username)
Long inputId, List<Long> outputs, String username) throws NotFoundException { throws NotFoundException {
final Connection pair = checkConnectionIDs(inputId, outputs, username); final Connection pair = checkConnectionIDs(inputId, outputs, username);
for (final O o : pair.getOutputs()) { for (final O o : pair.getOutputs()) {

View file

@ -62,7 +62,7 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
dimmer.setLightIntensity(bd.getIntensity()); dimmer.setLightIntensity(bd.getIntensity());
deviceService.saveAllAsOwner(dimmer.getOutputs(), principal.getName()); deviceService.saveAllAsOwner(dimmer.getOutputs(), principal.getName());
return dimmer.getOutputs(); return dimmer.getDimmables();
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View file

@ -77,7 +77,7 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
} }
deviceService.saveAsOwner(s, principal.getName()); deviceService.saveAsOwner(s, principal.getName());
return deviceService.saveAllAsOwner(s.getOutputs(), principal.getName()); return deviceService.saveAllAsOwner(s.getSwitchables(), principal.getName());
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation; 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.Trigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Triggerable;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -8,5 +9,5 @@ import lombok.Setter;
public abstract class TriggerDTO { public abstract class TriggerDTO {
@NotNull @Getter @Setter private long deviceId; @NotNull @Getter @Setter private long deviceId;
public abstract Trigger<?> toModel(); public abstract Trigger<? extends Triggerable> toModel();
} }

View file

@ -18,14 +18,14 @@ public class ButtonDimmer extends Dimmer {
/** Increases the current intensity level of the dimmable light by DIM_INCREMENT */ /** Increases the current intensity level of the dimmable light by DIM_INCREMENT */
public void increaseIntensity() { public void increaseIntensity() {
for (Dimmable dl : getOutputs()) { for (Dimmable dl : getDimmables()) {
dl.setIntensity(dl.getIntensity() + DIM_INCREMENT); dl.setIntensity(dl.getIntensity() + DIM_INCREMENT);
} }
} }
/** Decreases the current intensity level of the dimmable light by DIM_INCREMENT */ /** Decreases the current intensity level of the dimmable light by DIM_INCREMENT */
public void decreaseIntensity() { public void decreaseIntensity() {
for (Dimmable dl : getOutputs()) { for (Dimmable dl : getDimmables()) {
dl.setIntensity(dl.getIntensity() - DIM_INCREMENT); dl.setIntensity(dl.getIntensity() - DIM_INCREMENT);
} }
} }

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.Date; import java.util.Date;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import javax.persistence.*; import javax.persistence.*;
import lombok.Data; import lombok.Data;
@ -43,4 +44,21 @@ public class ConfirmationToken {
confirmToken = UUID.randomUUID().toString(); confirmToken = UUID.randomUUID().toString();
resetPassword = false; 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);
}
} }

View file

@ -23,6 +23,7 @@ public class Dimmable extends Switchable implements RangeTriggerable {
@ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH) @ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH)
@GsonExclude @GsonExclude
@SocketGsonExclude @SocketGsonExclude
@EqualsAndHashCode.Exclude
@Getter @Getter
@Setter @Setter
private Set<Dimmer> dimmers = new HashSet<>(); private Set<Dimmer> dimmers = new HashSet<>();

View file

@ -6,6 +6,7 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter;
/** Represents a generic dimmer input device */ /** Represents a generic dimmer input device */
@Entity @Entity
@ -20,6 +21,7 @@ public abstract class Dimmer extends InputDevice implements Connectable<Dimmable
@GsonExclude @GsonExclude
@SocketGsonExclude @SocketGsonExclude
@EqualsAndHashCode.Exclude @EqualsAndHashCode.Exclude
@Getter
@JoinTable( @JoinTable(
name = "dimmer_dimmable", name = "dimmer_dimmable",
joinColumns = @JoinColumn(name = "dimmer_id"), joinColumns = @JoinColumn(name = "dimmer_id"),
@ -32,8 +34,8 @@ public abstract class Dimmer extends InputDevice implements Connectable<Dimmable
* @return duh * @return duh
*/ */
@Override @Override
public Set<Dimmable> getOutputs() { public Set<OutputDevice> getOutputs() {
return this.dimmables; return Set.copyOf(this.dimmables);
} }
/** Add a light to be controller by this dimmer */ /** 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) { public void connect(Dimmable output, boolean connect) {
if (connect) { if (connect) {
output.getDimmers().add(this); output.getDimmers().add(this);
getOutputs().add(output); dimmables.add(output);
} else { } else {
output.getDimmers().remove(this); output.getDimmers().remove(this);
getOutputs().remove(output); dimmables.remove(output);
} }
} }
} }

View file

@ -16,7 +16,7 @@ public abstract class InputDevice extends Device {
super(kind, FlowType.INPUT); super(kind, FlowType.INPUT);
} }
public Set<? extends OutputDevice> getOutputs() { public Set<OutputDevice> getOutputs() {
return Set.of(); return Set.of();
} }
} }

View file

@ -25,7 +25,7 @@ public class KnobDimmer extends Dimmer implements RangeTriggerable {
*/ */
public void setLightIntensity(int intensity) { public void setLightIntensity(int intensity) {
this.intensity = intensity; this.intensity = intensity;
for (Dimmable dl : getOutputs()) { for (Dimmable dl : getDimmables()) {
dl.setIntensity(intensity); dl.setIntensity(intensity);
} }
} }

View file

@ -6,6 +6,7 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter;
/** A switch input device */ /** A switch input device */
@Entity @Entity
@ -22,6 +23,7 @@ public class Switch extends InputDevice implements BooleanTriggerable, Connectab
@GsonExclude @GsonExclude
@SocketGsonExclude @SocketGsonExclude
@EqualsAndHashCode.Exclude @EqualsAndHashCode.Exclude
@Getter
@JoinTable( @JoinTable(
name = "switch_switchable", name = "switch_switchable",
joinColumns = @JoinColumn(name = "switch_id"), joinColumns = @JoinColumn(name = "switch_id"),
@ -64,17 +66,17 @@ public class Switch extends InputDevice implements BooleanTriggerable, Connectab
} }
@Override @Override
public Set<Switchable> getOutputs() { public Set<OutputDevice> getOutputs() {
return switchables; return Set.copyOf(switchables);
} }
public void connect(Switchable output, boolean connect) { public void connect(Switchable output, boolean connect) {
if (connect) { if (connect) {
output.getSwitches().add(this); output.getSwitches().add(this);
getOutputs().add(output); switchables.add(output);
} else { } else {
output.getSwitches().remove(this); output.getSwitches().remove(this);
getOutputs().remove(output); switchables.remove(output);
} }
} }

View file

@ -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.GsonExclude;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
@ -55,4 +56,18 @@ public abstract class Switchable extends OutputDevice {
newState.setOn(isOn()); newState.setOn(isOn());
return newState; 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());
}
} }

View file

@ -21,15 +21,15 @@ public class AutomationService {
this.conditionRepository = conditionRepository; this.conditionRepository = conditionRepository;
} }
public List<Trigger<?>> findTriggersByDeviceId(Long deviceId) { public void findTriggersByDeviceId(Long deviceId, List<Trigger<?>> toPut) {
return triggerRepository.findAllByDeviceId(deviceId); toPut.addAll(triggerRepository.findAllByDeviceId(deviceId));
} }
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) { public void findAllConditionsByAutomationId(Long automationId, List<Condition<?>> toPut) {
return conditionRepository.findAllByAutomationId(automationId); toPut.addAll(conditionRepository.findAllByAutomationId(automationId));
} }
} }

View file

@ -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.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -52,7 +53,8 @@ public class DeviceService {
public void triggerTriggers(Device device, final String username) { public void triggerTriggers(Device device, final String username) {
final long deviceId = device.getId(); final long deviceId = device.getId();
final List<Trigger<?>> triggers = automationService.findTriggersByDeviceId(deviceId); final List<Trigger<?>> triggers = new ArrayList<>();
automationService.findTriggersByDeviceId(deviceId, triggers);
triggers.stream() triggers.stream()
.filter(Trigger::triggered) .filter(Trigger::triggered)
@ -61,8 +63,9 @@ public class DeviceService {
.distinct() .distinct()
.filter( .filter(
a -> { a -> {
final List<Condition<?>> conditions = final List<Condition<?>> conditions = new ArrayList<>();
automationService.findAllConditionsByAutomationId(a.getId()); automationService.findAllConditionsByAutomationId(
a.getId(), conditions);
if (conditions.isEmpty()) return true; if (conditions.isEmpty()) return true;
return conditions.stream().allMatch(Condition::triggered); return conditions.stream().allMatch(Condition::triggered);
}) })

View file

@ -98,7 +98,7 @@ public class SwitchControllerTests {
ArrayList<Switchable> helper = new ArrayList<Switchable>(); ArrayList<Switchable> helper = new ArrayList<Switchable>();
helper.add(light); helper.add(light);
helper.add(light2); helper.add(light2);
when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName())) when(deviceService.saveAllAsOwner(aSwitch.getSwitchables(), principal.getName()))
.thenReturn(helper); .thenReturn(helper);
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
@ -123,7 +123,7 @@ public class SwitchControllerTests {
ArrayList<Switchable> helper = new ArrayList<Switchable>(); ArrayList<Switchable> helper = new ArrayList<Switchable>();
helper.add(light); helper.add(light);
helper.add(light2); helper.add(light2);
when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName())) when(deviceService.saveAllAsOwner(aSwitch.getSwitchables(), principal.getName()))
.thenReturn(helper); .thenReturn(helper);
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
@ -148,7 +148,7 @@ public class SwitchControllerTests {
ArrayList<Switchable> helper = new ArrayList<Switchable>(); ArrayList<Switchable> helper = new ArrayList<Switchable>();
helper.add(light); helper.add(light);
helper.add(light2); helper.add(light2);
when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName())) when(deviceService.saveAllAsOwner(aSwitch.getSwitchables(), principal.getName()))
.thenReturn(helper); .thenReturn(helper);
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

View file

@ -39,7 +39,7 @@ public class ButtonDimmerTests {
@DisplayName(" increase the intensity ") @DisplayName(" increase the intensity ")
public void increase() { public void increase() {
buttonDimmer.increaseIntensity(); buttonDimmer.increaseIntensity();
for (Dimmable dl : buttonDimmer.getOutputs()) { for (Dimmable dl : buttonDimmer.getDimmables()) {
assertTrue(dl.getIntensity() > 10); assertTrue(dl.getIntensity() > 10);
} }
} }
@ -48,7 +48,7 @@ public class ButtonDimmerTests {
@DisplayName(" decrease the intensity ") @DisplayName(" decrease the intensity ")
public void decrease() { public void decrease() {
buttonDimmer.decreaseIntensity(); buttonDimmer.decreaseIntensity();
for (Dimmable dl : buttonDimmer.getOutputs()) { for (Dimmable dl : buttonDimmer.getDimmables()) {
assertTrue(dl.getIntensity() < 10); assertTrue(dl.getIntensity() < 10);
} }
} }

View file

@ -34,12 +34,13 @@ public class DimmerTests {
@DisplayName("connect off") @DisplayName("connect off")
public void connectOff() { public void connectOff() {
DimmableLight d = new DimmableLight(); DimmableLight d = new DimmableLight();
d.setId(35L);
d.getDimmers().add(this.dimmer); d.getDimmers().add(this.dimmer);
dimmer.getOutputs().add(d); dimmer.getDimmables().add(d);
dimmer.connect(d, false); dimmer.connect(d, false);
assertFalse(d.getDimmers().contains((this.dimmer))); assertFalse(d.getDimmers().contains((this.dimmer)));
assertFalse((this.dimmer.getOutputs().contains(d))); assertFalse(this.dimmer.getOutputs().contains(d));
} }
} }

View file

@ -39,7 +39,7 @@ public class KnobDimmerTests {
@DisplayName(" set the intensity ") @DisplayName(" set the intensity ")
public void increase() { public void increase() {
knobDimmer.setLightIntensity(30); knobDimmer.setLightIntensity(30);
for (Dimmable dl : knobDimmer.getOutputs()) { for (Dimmable dl : knobDimmer.getDimmables()) {
assertEquals(30, dl.getIntensity()); assertEquals(30, dl.getIntensity());
} }
} }

View file

@ -20,9 +20,9 @@ public class SwitchTests {
RegularLight regularLight = new RegularLight(); RegularLight regularLight = new RegularLight();
DimmableLight dimmableLight = new DimmableLight(); DimmableLight dimmableLight = new DimmableLight();
SmartPlug smartPlug = new SmartPlug(); SmartPlug smartPlug = new SmartPlug();
this.aSwitch.getOutputs().add(regularLight); this.aSwitch.getSwitchables().add(regularLight);
this.aSwitch.getOutputs().add(dimmableLight); this.aSwitch.getSwitchables().add(dimmableLight);
this.aSwitch.getOutputs().add(smartPlug); this.aSwitch.getSwitchables().add(smartPlug);
} }
@Test @Test
@ -80,7 +80,7 @@ public class SwitchTests {
@DisplayName("Checks that toggling on sets all elements of the Set on as well") @DisplayName("Checks that toggling on sets all elements of the Set on as well")
public void toggleEffctOnSet() { public void toggleEffctOnSet() {
aSwitch.toggle(); aSwitch.toggle();
for (final Switchable s : aSwitch.getOutputs()) { for (final Switchable s : aSwitch.getSwitchables()) {
assertTrue(s.isOn()); assertTrue(s.isOn());
} }
} }
@ -90,7 +90,7 @@ public class SwitchTests {
public void toggleOffEffectOnElementes() { public void toggleOffEffectOnElementes() {
aSwitch.setOn(true); aSwitch.setOn(true);
aSwitch.toggle(); aSwitch.toggle();
for (final Switchable s : aSwitch.getOutputs()) { for (final Switchable s : aSwitch.getSwitchables()) {
assertFalse(s.isOn()); assertFalse(s.isOn());
} }
} }

View file

@ -98,8 +98,12 @@ public class DeviceServiceTests {
a.getScenes().add(sp); a.getScenes().add(sp);
when(automationService.findTriggersByDeviceId(1L)).thenReturn(List.of(b)); doAnswer(i -> ((List<Trigger<?>>) i.getArgument(1)).add(b))
when(automationService.findAllConditionsByAutomationId(5L)).thenReturn(List.of(c)); .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(automationService.findByVerifiedId(5L)).thenReturn(a);
when(sceneService.findByValidatedId(4L)).thenReturn(s); when(sceneService.findByValidatedId(4L)).thenReturn(s);