diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java index d6f7ef5..22b6f8f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java @@ -38,6 +38,10 @@ public class ScenePriority { return id; } + public void setId(long id) { + this.id = id; + } + public Integer getPriority() { return priority; } 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 b96e6e7..8d9e548 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 @@ -14,10 +14,10 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; +import java.util.List; import java.util.Optional; import lombok.SneakyThrows; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -47,11 +47,6 @@ public class RegularLightControllerTests { @Mock private StateRepository stateRepository; - @BeforeEach - public void setup() { - when(mockPrincipal.getName()).thenReturn("user"); - } - private void checkRegularLightAgainstRequest( final RegularLight toCheck, final SwitchableSaveRequest request) { assertThat(toCheck).isNotNull(); @@ -60,10 +55,28 @@ public class RegularLightControllerTests { assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId()); } + @Test + public void testGetAll() { + when(regularLightRepository.findAll()).thenReturn(List.of()); + assertThat(regularLightController.findAll().isEmpty()); + } + + @Test + public void testGet() throws NotFoundException { + RegularLight r = new RegularLight(); + when(regularLightRepository.findById(1L)).thenReturn(Optional.of(r)); + when(regularLightRepository.findById(2L)).thenReturn(Optional.empty()); + + assertThat(regularLightController.findById(1L)).isSameAs(r); + assertThatThrownBy(() -> regularLightController.findById(2L)) + .isInstanceOf(NotFoundException.class); + } + @Test @DisplayName("when creating should return the same object") @SneakyThrows(NotFoundException.class) public void testCreate() { + when(mockPrincipal.getName()).thenReturn("user"); doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user")); when(deviceService.saveAsOwner(any(RegularLight.class), eq("user"))) .thenAnswer(i -> i.getArguments()[0]); @@ -86,6 +99,7 @@ public class RegularLightControllerTests { @SneakyThrows(NotFoundException.class) public void testUpdate() { + when(mockPrincipal.getName()).thenReturn("user"); final SwitchableSaveRequest toSend = new SwitchableSaveRequest(); toSend.setName("rl"); toSend.setRoomId(20L); @@ -140,6 +154,7 @@ public class RegularLightControllerTests { @SneakyThrows(NotFoundException.class) public void testDelete() { + when(mockPrincipal.getName()).thenReturn("user"); doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -150,6 +165,7 @@ public class RegularLightControllerTests { @Test public void testSceneBinding() { + when(mockPrincipal.getName()).thenReturn("user"); RegularLight regularLight = new RegularLight(); when(regularLightRepository.findByIdAndUsername(24L, "user")) .thenReturn(Optional.of(regularLight)); @@ -166,6 +182,7 @@ public class RegularLightControllerTests { @Test public void testSceneBinding2() { + when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user"); RegularLight regularLight = new RegularLight(); when(regularLightRepository.findByIdAndUsername(24L, "user")) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableStateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableStateTests.java index dac93e1..11ce6b2 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableStateTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableStateTests.java @@ -33,4 +33,13 @@ public class DimmableStateTests { this.dimmableState.apply(); assertEquals(30, d.getIntensity()); } + + @Test + @DisplayName("apply") + public void copy() { + this.dimmableState.setIntensity(30); + + DimmableState copy = this.dimmableState.copy(); + assertEquals(30, copy.getIntensity()); + } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityTests.java index 574525c..04e2131 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityTests.java @@ -24,6 +24,14 @@ public class ScenePriorityTests { assertEquals(20, scenePriority.getAutomationId()); } + @Test + @DisplayName("get and set id") + public void getAndSetId() { + scenePriority.setId(20L); + + assertEquals(20, scenePriority.getId()); + } + @Test @DisplayName("get and set scene id") public void getAndSetSceneId() { diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateTests.java index 4b4224d..fa88ef2 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateTests.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -45,4 +46,44 @@ public class StateTests { this.state.setSceneId(50L); assertEquals(50, this.state.getSceneId()); } + + @Test + @DisplayName("copy to scene id") + public void copyToSceneId() { + this.state.setSceneId(50L); + Scene s = new Scene(); + this.state.setScene(s); + + this.state.setDeviceId(30L); + DimmableLight d = new DimmableLight(); + this.state.setDevice(d); + + State stat = this.state.copyToSceneId(10L); + + assertEquals(10, stat.getSceneId()); + assertEquals(30, stat.getDeviceId()); + + assertEquals(s, stat.getScene()); + assertEquals(d, stat.getDevice()); + } + + @Test + @DisplayName("preremove") + public void preRemove() { + this.state.setSceneId(50L); + Scene s = new Scene(); + this.state.setScene(s); + + this.state.setDeviceId(30L); + DimmableLight d = new DimmableLight(); + this.state.setDevice(d); + + state.removeDeviceAndScene(); + + assertNull(state.getSceneId()); + assertNull(state.getDeviceId()); + + assertNull(state.getScene()); + assertNull(state.getDevice()); + } }