Fixed some code smells

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-21 22:03:38 +02:00
parent 58a8b939fa
commit 1a289f6800
21 changed files with 55 additions and 13 deletions

View file

@ -2,10 +2,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class BooleanCondition extends Condition<BooleanTriggerable> { public class BooleanCondition extends Condition<BooleanTriggerable> {
@Getter @Getter

View file

@ -17,6 +17,14 @@ public class ConfirmationToken {
@Column(name = "confirmation_token", unique = true) @Column(name = "confirmation_token", unique = true)
private String confirmToken; private String confirmToken;
public Date getCreatedDate() {
return new Date(createdDate.getTime());
}
public void setCreatedDate(Date createdDate) {
this.createdDate = new Date(createdDate.getTime());
}
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
private Date createdDate; private Date createdDate;

View file

@ -7,10 +7,12 @@ import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Dimmable extends Switchable implements RangeTriggerable { public class Dimmable extends Switchable implements RangeTriggerable {
@ -70,6 +72,7 @@ public class Dimmable extends Switchable implements RangeTriggerable {
setIntensity(state.getIntensity()); setIntensity(state.getIntensity());
} }
@Override
public State cloneState() { public State cloneState() {
final DimmableState newState = new DimmableState(); final DimmableState newState = new DimmableState();
newState.setDeviceId(getId()); newState.setDeviceId(getId());

View file

@ -5,9 +5,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import lombok.EqualsAndHashCode;
/** Represents a generic dimmer input device */ /** Represents a generic dimmer input device */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Dimmer extends InputDevice implements Connectable<Dimmable> { public abstract class Dimmer extends InputDevice implements Connectable<Dimmable> {
protected Dimmer(String kind) { protected Dimmer(String kind) {
@ -17,6 +19,7 @@ public abstract class Dimmer extends InputDevice implements Connectable<Dimmable
@ManyToMany(cascade = CascadeType.DETACH) @ManyToMany(cascade = CascadeType.DETACH)
@GsonExclude @GsonExclude
@SocketGsonExclude @SocketGsonExclude
@EqualsAndHashCode.Exclude
@JoinTable( @JoinTable(
name = "dimmer_dimmable", name = "dimmer_dimmable",
joinColumns = @JoinColumn(name = "dimmer_id"), joinColumns = @JoinColumn(name = "dimmer_id"),

View file

@ -2,12 +2,14 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
/** /**
* Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity * Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity
* value, like a knob) * value, like a knob)
*/ */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class KnobDimmer extends Dimmer implements RangeTriggerable { public class KnobDimmer extends Dimmer implements RangeTriggerable {
@Column private int intensity = 0; @Column private int intensity = 0;

View file

@ -2,11 +2,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
/** Represents a motion sensor device */ /** Represents a motion sensor device */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class MotionSensor extends InputDevice implements BooleanTriggerable { public class MotionSensor extends InputDevice implements BooleanTriggerable {
@Getter @Getter

View file

@ -2,10 +2,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class RangeCondition extends Condition<RangeTriggerable> { public class RangeCondition extends Condition<RangeTriggerable> {
public RangeCondition() { public RangeCondition() {

View file

@ -2,11 +2,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
/** Represents a standard non-dimmable light */ /** Represents a standard non-dimmable light */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class RegularLight extends Switchable implements BooleanTriggerable { public class RegularLight extends Switchable implements BooleanTriggerable {
/** Whether the light is on or not */ /** Whether the light is on or not */

View file

@ -2,8 +2,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class SecurityCamera extends Switchable implements BooleanTriggerable { public class SecurityCamera extends Switchable implements BooleanTriggerable {
public SecurityCamera() { public SecurityCamera() {

View file

@ -7,9 +7,11 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType; import javax.persistence.EnumType;
import javax.persistence.Enumerated; import javax.persistence.Enumerated;
import lombok.EqualsAndHashCode;
/** A sensor input device that measures a quantity in a continuous scale (e.g. temperature) */ /** A sensor input device that measures a quantity in a continuous scale (e.g. temperature) */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class Sensor extends InputDevice implements RangeTriggerable { public class Sensor extends InputDevice implements RangeTriggerable {
public static final Map<SensorType, BigDecimal> TYPICAL_VALUES = public static final Map<SensorType, BigDecimal> TYPICAL_VALUES =

View file

@ -3,9 +3,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.math.BigDecimal; import java.math.BigDecimal;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
/** A smart plug that can be turned either on or off */ /** A smart plug that can be turned either on or off */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class SmartPlug extends Switchable implements BooleanTriggerable { public class SmartPlug extends Switchable implements BooleanTriggerable {
/** The average consumption of an active plug when on in Watt */ /** The average consumption of an active plug when on in Watt */

View file

@ -5,9 +5,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import lombok.EqualsAndHashCode;
/** A switch input device */ /** A switch input device */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class Switch extends InputDevice implements BooleanTriggerable, Connectable<Switchable> { public class Switch extends InputDevice implements BooleanTriggerable, Connectable<Switchable> {
@ManyToMany( @ManyToMany(

View file

@ -5,9 +5,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import lombok.EqualsAndHashCode;
/** A device that can be turned either on or off */ /** A device that can be turned either on or off */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
@Inheritance(strategy = InheritanceType.JOINED) @Inheritance(strategy = InheritanceType.JOINED)
public abstract class Switchable extends OutputDevice { public abstract class Switchable extends OutputDevice {

View file

@ -5,9 +5,11 @@ import java.math.BigDecimal;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Transient; import javax.persistence.Transient;
import lombok.EqualsAndHashCode;
/** A thermostat capable of controlling cooling and heating. */ /** A thermostat capable of controlling cooling and heating. */
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class Thermostat extends Switchable implements BooleanTriggerable { public class Thermostat extends Switchable implements BooleanTriggerable {
@Override @Override

View file

@ -3,8 +3,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
@Entity @Entity
@EqualsAndHashCode(callSuper = true)
public class ThermostatCondition extends Condition<Thermostat> { public class ThermostatCondition extends Condition<Thermostat> {
public ThermostatCondition() { public ThermostatCondition() {

View file

@ -66,7 +66,7 @@ public class DeviceService {
a -> { a -> {
final List<Condition<?>> conditions = final List<Condition<?>> conditions =
conditionRepository.findAllByAutomationId(a.getId()); conditionRepository.findAllByAutomationId(a.getId());
if (conditions.size() == 0) return true; if (conditions.isEmpty()) return true;
return conditions.stream().allMatch(Condition::triggered); return conditions.stream().allMatch(Condition::triggered);
}) })
.map(Automation::getScenes) .map(Automation::getScenes)

View file

@ -15,6 +15,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal; import java.security.Principal;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -186,6 +187,6 @@ public class AutomationControllerTests {
old.setId(42L); old.setId(42L);
old.setName("Old Name"); old.setName("Old Name");
doNothing().when(automationRepository).deleteById(42L); doNothing().when(automationRepository).deleteById(42L);
automationController.delete(42L); Assertions.assertDoesNotThrow(() -> automationController.delete(42L));
} }
} }

View file

@ -12,6 +12,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal; import java.security.Principal;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -93,6 +94,6 @@ public class SceneControllerTests {
public void testDelete() { public void testDelete() {
doNothing().when(stateStateRepository).deleteAllBySceneId(42L); doNothing().when(stateStateRepository).deleteAllBySceneId(42L);
doNothing().when(sceneRepository).deleteById(42L); doNothing().when(sceneRepository).deleteById(42L);
sceneController.deleteById(42L); Assertions.assertDoesNotThrow(() -> sceneController.deleteById(42L));
} }
} }

View file

@ -14,6 +14,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal; import java.security.Principal;
import java.util.Optional; import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -88,6 +89,6 @@ public class SmartPlugControllerTests {
public void testDelete() throws NotFoundException { public void testDelete() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
doNothing().when(deviceService).deleteByIdAsOwner(42L, "user"); doNothing().when(deviceService).deleteByIdAsOwner(42L, "user");
smartPlugController.deleteById(42L, mockPrincipal); Assertions.assertDoesNotThrow(() -> smartPlugController.deleteById(42L, mockPrincipal));
} }
} }

View file

@ -12,7 +12,7 @@ public class ExceptionTests {
try { try {
throw new BadDataException("message"); throw new BadDataException("message");
} catch (BadDataException e) { } catch (BadDataException e) {
assertEquals(e.getMessage(), "message"); assertEquals("message", e.getMessage());
} }
} }
@ -21,7 +21,7 @@ public class ExceptionTests {
try { try {
throw new DuplicateRegistrationException(); throw new DuplicateRegistrationException();
} catch (DuplicateRegistrationException e) { } catch (DuplicateRegistrationException e) {
assertEquals(e.getMessage(), "Email or username already belonging to another user"); assertEquals("Email or username already belonging to another user", e.getMessage());
} }
} }
@ -31,8 +31,8 @@ public class ExceptionTests {
throw new DuplicateStateException(); throw new DuplicateStateException();
} catch (DuplicateStateException e) { } catch (DuplicateStateException e) {
assertEquals( assertEquals(
e.getMessage(), "Cannot create state since it has already been created for this scene and this device",
"Cannot create state since it has already been created for this scene and this device"); e.getMessage());
} }
} }
@ -41,7 +41,7 @@ public class ExceptionTests {
try { try {
throw new EmailTokenNotFoundException(); throw new EmailTokenNotFoundException();
} catch (EmailTokenNotFoundException e) { } catch (EmailTokenNotFoundException e) {
assertEquals(e.getMessage(), "Email verification token not found in DB"); assertEquals("Email verification token not found in DB", e.getMessage());
} }
} }
@ -50,13 +50,13 @@ public class ExceptionTests {
try { try {
throw new NotFoundException(); throw new NotFoundException();
} catch (NotFoundException e) { } catch (NotFoundException e) {
assertEquals(e.getMessage(), "Not found"); assertEquals("Not found", e.getMessage());
} }
try { try {
throw new NotFoundException("message"); throw new NotFoundException("message");
} catch (NotFoundException e) { } catch (NotFoundException e) {
assertEquals(e.getMessage(), "message not found"); assertEquals("message not found", e.getMessage());
} }
} }
@ -65,7 +65,7 @@ public class ExceptionTests {
try { try {
throw new UserNotFoundException(); throw new UserNotFoundException();
} catch (UserNotFoundException e) { } catch (UserNotFoundException e) {
assertEquals(e.getMessage(), "No user found with given email"); assertEquals("No user found with given email", e.getMessage());
} }
} }
} }

View file

@ -8,6 +8,7 @@ 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.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -151,7 +152,7 @@ public class DeviceServiceTests {
@Test @Test
public void populateComputedFields() { public void populateComputedFields() {
doNothing().when(devicePopulationService).populateComputedFields(List.of()); doNothing().when(devicePopulationService).populateComputedFields(List.of());
deviceService.populateComputedFields(List.of()); Assertions.assertDoesNotThrow(() -> deviceService.populateComputedFields(List.of()));
} }
@Test @Test