Fixes
This commit is contained in:
parent
00f2442397
commit
48942ee8c2
5 changed files with 60 additions and 16 deletions
|
@ -10,8 +10,8 @@ import java.security.Principal;
|
||||||
|
|
||||||
public abstract class GuestEnabledController<T extends Device> {
|
public abstract class GuestEnabledController<T extends Device> {
|
||||||
|
|
||||||
private UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private DeviceRepository<T> deviceRepository;
|
private final DeviceRepository<T> deviceRepository;
|
||||||
|
|
||||||
public GuestEnabledController(
|
public GuestEnabledController(
|
||||||
final UserRepository userRepository, final DeviceRepository<T> deviceRepository) {
|
final UserRepository userRepository, final DeviceRepository<T> deviceRepository) {
|
||||||
|
|
|
@ -19,10 +19,10 @@ import org.springframework.web.bind.annotation.*;
|
||||||
@RequestMapping("/regularLight")
|
@RequestMapping("/regularLight")
|
||||||
public class RegularLightController extends GuestEnabledController<RegularLight> {
|
public class RegularLightController extends GuestEnabledController<RegularLight> {
|
||||||
|
|
||||||
private RegularLightRepository regularLightRepository;
|
private final RegularLightRepository regularLightRepository;
|
||||||
private SceneRepository sceneRepository;
|
private final SceneRepository sceneRepository;
|
||||||
private StateRepository<State> stateRepository;
|
private final StateRepository<State> stateRepository;
|
||||||
private DeviceService deviceService;
|
private final DeviceService deviceService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public RegularLightController(
|
public RegularLightController(
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyLong;
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
@ -34,18 +35,12 @@ public class RegularLightControllerTests {
|
||||||
|
|
||||||
@Mock private RegularLightRepository regularLightRepository;
|
@Mock private RegularLightRepository regularLightRepository;
|
||||||
|
|
||||||
@Mock private SceneRepository sceneRepository;
|
|
||||||
|
|
||||||
@Mock private StateRepository<State> stateRepository;
|
|
||||||
|
|
||||||
@Mock private DeviceService deviceService;
|
@Mock private DeviceService deviceService;
|
||||||
|
|
||||||
@Mock private Principal mockPrincipal;
|
@Mock private Principal mockPrincipal;
|
||||||
|
|
||||||
@Mock private UserRepository userRepository;
|
@Mock private UserRepository userRepository;
|
||||||
|
|
||||||
@Mock private DeviceRepository<RegularLight> deviceRepository;
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setup() {
|
public void setup() {
|
||||||
when(mockPrincipal.getName()).thenReturn("user");
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
|
@ -86,12 +81,27 @@ public class RegularLightControllerTests {
|
||||||
public void testUpdate() {
|
public void testUpdate() {
|
||||||
|
|
||||||
final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
|
final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
|
||||||
toSend.setId(50L);
|
|
||||||
toSend.setName("rl");
|
toSend.setName("rl");
|
||||||
toSend.setRoomId(20L);
|
toSend.setRoomId(20L);
|
||||||
toSend.setOn(true);
|
toSend.setOn(true);
|
||||||
|
|
||||||
final RegularLight toUpdate = new RegularLight();
|
final RegularLight toUpdate = new RegularLight();
|
||||||
|
toUpdate.setName("OOO");
|
||||||
|
toUpdate.setRoomId(40L);
|
||||||
|
toUpdate.setOn(false);
|
||||||
|
|
||||||
|
when(regularLightRepository.findByIdAndUsername(50L, "user"))
|
||||||
|
.thenReturn(Optional.of(toUpdate));
|
||||||
|
when(regularLightRepository.findByIdAndUsername(60L, "user")).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
when(deviceService.saveAsOwner(toUpdate, "user")).thenReturn(toUpdate);
|
||||||
|
|
||||||
|
toSend.setId(60L);
|
||||||
|
assertThatThrownBy(() -> regularLightController.update(toSend, mockPrincipal, null))
|
||||||
|
.isInstanceOf(NotFoundException.class);
|
||||||
|
toSend.setId(50L);
|
||||||
|
assertThat(regularLightController.update(toSend, mockPrincipal, null)).isEqualTo(toUpdate);
|
||||||
|
|
||||||
toUpdate.setName("OOO");
|
toUpdate.setName("OOO");
|
||||||
toUpdate.setRoomId(40L);
|
toUpdate.setRoomId(40L);
|
||||||
toSend.setId(50L);
|
toSend.setId(50L);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.mockito.ArgumentMatchers.*;
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
import static org.mockito.Mockito.doNothing;
|
import static org.mockito.Mockito.doNothing;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
@ -8,9 +9,12 @@ import static org.mockito.Mockito.when;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
|
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.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SensorRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SensorService;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
|
import java.util.Optional;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
@ -35,6 +39,10 @@ public class SensorControllerTests {
|
||||||
|
|
||||||
@Mock private Principal mockPrincipal;
|
@Mock private Principal mockPrincipal;
|
||||||
|
|
||||||
|
@Mock private SensorRepository sensorRepository;
|
||||||
|
|
||||||
|
@Mock private SensorService sensorService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setup() {
|
public void setup() {
|
||||||
when(mockPrincipal.getName()).thenReturn("user");
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
|
@ -78,4 +86,21 @@ public class SensorControllerTests {
|
||||||
|
|
||||||
Assertions.assertDoesNotThrow(() -> sensorController.deleteById(42L, mockPrincipal));
|
Assertions.assertDoesNotThrow(() -> sensorController.deleteById(42L, mockPrincipal));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateValue() throws NotFoundException {
|
||||||
|
final Sensor s = new Sensor();
|
||||||
|
when(sensorRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(s));
|
||||||
|
when(sensorRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty());
|
||||||
|
when(sensorService.updateValueFromSensor(s, BigDecimal.valueOf(30)))
|
||||||
|
.thenAnswer(
|
||||||
|
i -> {
|
||||||
|
s.setValue(i.getArgument(1));
|
||||||
|
return s;
|
||||||
|
});
|
||||||
|
assertThatThrownBy(() -> sensorController.updateValue(2L, BigDecimal.ZERO, mockPrincipal))
|
||||||
|
.isInstanceOf(NotFoundException.class);
|
||||||
|
assertThat(sensorController.updateValue(1L, BigDecimal.valueOf(30), mockPrincipal))
|
||||||
|
.isSameAs(s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@ -10,7 +11,8 @@ import org.junit.jupiter.api.Test;
|
||||||
@DisplayName("A switch")
|
@DisplayName("A switch")
|
||||||
public class SwitchTests {
|
public class SwitchTests {
|
||||||
|
|
||||||
Switch aSwitch;
|
private Switch aSwitch;
|
||||||
|
private SmartPlug smartPlug;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void createNewSwitch() {
|
public void createNewSwitch() {
|
||||||
|
@ -18,10 +20,11 @@ public class SwitchTests {
|
||||||
this.aSwitch = new Switch();
|
this.aSwitch = new Switch();
|
||||||
RegularLight regularLight = new RegularLight();
|
RegularLight regularLight = new RegularLight();
|
||||||
DimmableLight dimmableLight = new DimmableLight();
|
DimmableLight dimmableLight = new DimmableLight();
|
||||||
SmartPlug smartPlug = new SmartPlug();
|
smartPlug = new SmartPlug();
|
||||||
this.aSwitch.getSwitchables().add(regularLight);
|
this.aSwitch.getSwitchables().add(regularLight);
|
||||||
this.aSwitch.getSwitchables().add(dimmableLight);
|
this.aSwitch.getSwitchables().add(dimmableLight);
|
||||||
this.aSwitch.getSwitchables().add(smartPlug);
|
this.aSwitch.connect(smartPlug, true);
|
||||||
|
assertThat(this.aSwitch.getOutputs()).containsAll(this.aSwitch.getSwitchables());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -93,4 +96,10 @@ public class SwitchTests {
|
||||||
assertFalse(s.isOn());
|
assertFalse(s.isOn());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDisconnect() {
|
||||||
|
this.aSwitch.connect(smartPlug, false);
|
||||||
|
assertFalse(this.aSwitch.getOutputs().contains(smartPlug));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue