diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestEnabledController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestEnabledController.java index 6336a4e..9a06af0 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestEnabledController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestEnabledController.java @@ -10,8 +10,8 @@ import java.security.Principal; public abstract class GuestEnabledController { - private UserRepository userRepository; - private DeviceRepository deviceRepository; + private final UserRepository userRepository; + private final DeviceRepository deviceRepository; public GuestEnabledController( final UserRepository userRepository, final DeviceRepository deviceRepository) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index 29364a1..7cd8225 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -19,10 +19,10 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/regularLight") public class RegularLightController extends GuestEnabledController { - private RegularLightRepository regularLightRepository; - private SceneRepository sceneRepository; - private StateRepository stateRepository; - private DeviceService deviceService; + private final RegularLightRepository regularLightRepository; + private final SceneRepository sceneRepository; + private final StateRepository stateRepository; + private final DeviceService deviceService; @Autowired public RegularLightController( diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java index 1e4523f..323c4d2 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.anyLong; import static org.mockito.ArgumentMatchers.eq; @@ -34,18 +35,12 @@ public class RegularLightControllerTests { @Mock private RegularLightRepository regularLightRepository; - @Mock private SceneRepository sceneRepository; - - @Mock private StateRepository stateRepository; - @Mock private DeviceService deviceService; @Mock private Principal mockPrincipal; @Mock private UserRepository userRepository; - @Mock private DeviceRepository deviceRepository; - @BeforeEach public void setup() { when(mockPrincipal.getName()).thenReturn("user"); @@ -86,12 +81,27 @@ public class RegularLightControllerTests { public void testUpdate() { final SwitchableSaveRequest toSend = new SwitchableSaveRequest(); - toSend.setId(50L); toSend.setName("rl"); toSend.setRoomId(20L); toSend.setOn(true); 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.setRoomId(40L); toSend.setId(50L); diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java index a166335..bac53e0 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.Mockito.doNothing; 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.error.NotFoundException; 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.SensorService; import java.math.BigDecimal; import java.security.Principal; +import java.util.Optional; import lombok.SneakyThrows; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -35,6 +39,10 @@ public class SensorControllerTests { @Mock private Principal mockPrincipal; + @Mock private SensorRepository sensorRepository; + + @Mock private SensorService sensorService; + @BeforeEach public void setup() { when(mockPrincipal.getName()).thenReturn("user"); @@ -78,4 +86,21 @@ public class SensorControllerTests { 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); + } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchTests.java index c83bd5f..46fd574 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchTests.java @@ -1,5 +1,6 @@ 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.assertTrue; @@ -10,7 +11,8 @@ import org.junit.jupiter.api.Test; @DisplayName("A switch") public class SwitchTests { - Switch aSwitch; + private Switch aSwitch; + private SmartPlug smartPlug; @BeforeEach public void createNewSwitch() { @@ -18,10 +20,11 @@ public class SwitchTests { this.aSwitch = new Switch(); RegularLight regularLight = new RegularLight(); DimmableLight dimmableLight = new DimmableLight(); - SmartPlug smartPlug = new SmartPlug(); + smartPlug = new SmartPlug(); this.aSwitch.getSwitchables().add(regularLight); this.aSwitch.getSwitchables().add(dimmableLight); - this.aSwitch.getSwitchables().add(smartPlug); + this.aSwitch.connect(smartPlug, true); + assertThat(this.aSwitch.getOutputs()).containsAll(this.aSwitch.getSwitchables()); } @Test @@ -93,4 +96,10 @@ public class SwitchTests { assertFalse(s.isOn()); } } + + @Test + public void testDisconnect() { + this.aSwitch.connect(smartPlug, false); + assertFalse(this.aSwitch.getOutputs().contains(smartPlug)); + } }