Merge branch 'sonar-fix' into 'dev'

Sonarqube fixes

See merge request sa4-2020/the-sanmarinoes/backend!120
This commit is contained in:
Claudio Maggioni 2020-05-08 23:18:45 +02:00
commit 774bbc5ab9
13 changed files with 23 additions and 28 deletions

View file

@ -64,8 +64,8 @@ public class CurtainsController {
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Dimmable> s = c.cloneState(); State<? extends Dimmable> s = c.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
throw new DuplicateStateException(); throw new DuplicateStateException();
return stateRepository.save(s); return stateRepository.save(s);

View file

@ -5,6 +5,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.BadDataException;
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.Device; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository;
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;
@ -40,11 +41,12 @@ public class DeviceController {
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
// check if roomId is valid // check if roomId is valid
final Room r =
roomRepository roomRepository
.findByIdAndUsername(deviceSaveRequest.getRoomId(), principal.getName()) .findByIdAndUsername(deviceSaveRequest.getRoomId(), principal.getName())
.orElseThrow(() -> new BadDataException("roomId is not a valid room id")); .orElseThrow(() -> new BadDataException("roomId is not a valid room id"));
d.setRoomId(deviceSaveRequest.getRoomId()); d.setRoomId(r.getId());
d.setName(deviceSaveRequest.getName()); d.setName(deviceSaveRequest.getName());
deviceService.saveAsOwner(d, principal.getName()); deviceService.saveAsOwner(d, principal.getName());

View file

@ -98,8 +98,8 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Dimmable> s = d.cloneState(); State<? extends Dimmable> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
throw new DuplicateStateException(); throw new DuplicateStateException();
return stateRepository.save(s); return stateRepository.save(s);

View file

@ -110,8 +110,8 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState(); State<? extends Switchable> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
throw new DuplicateStateException(); throw new DuplicateStateException();
return stateRepository.save(s); return stateRepository.save(s);

View file

@ -78,8 +78,8 @@ public class SmartPlugController {
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState(); State<? extends Switchable> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
throw new DuplicateStateException(); throw new DuplicateStateException();
return stateRepository.save(s); return stateRepository.save(s);

View file

@ -75,8 +75,8 @@ public class ThermostatController {
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState(); State<? extends Switchable> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
throw new DuplicateStateException(); throw new DuplicateStateException();
return stateRepository.save(s); return stateRepository.save(s);

View file

@ -7,7 +7,6 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull;
/** Generic abstraction for a smart home device */ /** Generic abstraction for a smart home device */
@Entity @Entity
@ -46,11 +45,9 @@ public abstract class Device {
* a REST call. * a REST call.
*/ */
@Column(name = "room_id", nullable = false) @Column(name = "room_id", nullable = false)
@NotNull
private Long roomId; private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;

View file

@ -43,7 +43,6 @@ public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D
GREATER_EQUAL GREATER_EQUAL
} }
@NotNull
@Column(nullable = false) @Column(nullable = false)
private Operator operator; private Operator operator;

View file

@ -7,7 +7,6 @@ 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 javax.validation.constraints.NotNull;
/** 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
@ -45,7 +44,6 @@ public class Sensor extends InputDevice implements RangeTriggerable {
/** The type of this sensor */ /** The type of this sensor */
@Column(nullable = false) @Column(nullable = false)
@NotNull
@Enumerated(value = EnumType.STRING) @Enumerated(value = EnumType.STRING)
private SensorType sensor; private SensorType sensor;

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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;
@ -20,7 +19,6 @@ public class SmartPlug extends Switchable implements BooleanTriggerable {
/** Whether the smart plug is on */ /** Whether the smart plug is on */
@Column(name = "smart_plug_on", nullable = false) @Column(name = "smart_plug_on", nullable = false)
@NotNull
private boolean on; private boolean on;
public BigDecimal getTotalConsumption() { public BigDecimal getTotalConsumption() {

View file

@ -64,7 +64,7 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
} }
/** Temperature to be reached */ /** Temperature to be reached */
@Column @NotNull private BigDecimal targetTemperature; @Column private BigDecimal targetTemperature;
/** The temperature detected by the embedded sensor */ /** The temperature detected by the embedded sensor */
@Column(nullable = false, precision = 4, scale = 1) @Column(nullable = false, precision = 4, scale = 1)

View file

@ -37,7 +37,6 @@ public abstract class Trigger<D extends Device> {
* from a REST call. * from a REST call.
*/ */
@Column(name = "device_id", nullable = false) @Column(name = "device_id", nullable = false)
@NotNull
private Long deviceId; private Long deviceId;
@ManyToOne @ManyToOne

View file

@ -48,7 +48,11 @@ public class DeviceService {
} }
public void throwIfRoomNotOwned(Long roomId, String username) throws NotFoundException { public void throwIfRoomNotOwned(Long roomId, String username) throws NotFoundException {
roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new); final Room r =
roomRepository
.findByIdAndUsername(roomId, username)
.orElseThrow(NotFoundException::new);
if (!r.getId().equals(roomId)) throw new IllegalStateException();
} }
private void triggerTriggers(Device device, final String username) { private void triggerTriggers(Device device, final String username) {
@ -140,9 +144,7 @@ public class DeviceService {
User host = null; User host = null;
if (hostId == null) { if (hostId == null) {
if (roomId != null) { if (roomId != null) {
roomRepository throwIfRoomNotOwned(roomId, username);
.findByIdAndUsername(roomId, username)
.orElseThrow(NotFoundException::new);
devices = deviceRepository.findByRoomId(roomId); devices = deviceRepository.findByRoomId(roomId);
} else { } else {
devices = deviceRepository.findAllByUsername(username); devices = deviceRepository.findAllByUsername(username);