diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java index 33f7331..0134642 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.when; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest; 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.SceneService; import java.security.Principal; import java.util.List; import java.util.Optional; @@ -33,12 +34,72 @@ public class SceneControllerTests { @Mock private StateRepository stateStateRepository; + @Mock private SceneService sceneService; + private final User u; + private final User h; + public SceneControllerTests() { u = new User(); u.setName("user"); u.setId(1L); + + h = new User(); + h.setId(2L); + h.setUsername("host"); + + u.getHosts().add(h); + h.getGuests().add(u); + } + + @Test + public void testCopy() throws NotFoundException { + final Scene scene = new Scene(); + final Scene copyFrom = new Scene(); + + when(mockPrincipal.getName()).thenReturn("user"); + when(sceneRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(scene)); + when(sceneRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty()); + + when(sceneRepository.findByIdAndUsername(10L, "user")).thenReturn(Optional.of(copyFrom)); + when(sceneRepository.findByIdAndUsername(20L, "user")).thenReturn(Optional.empty()); + + assertThatThrownBy(() -> sceneController.copy(2L, 10L, mockPrincipal)) + .isInstanceOf(NotFoundException.class); + assertThatThrownBy(() -> sceneController.copy(1L, 20L, mockPrincipal)) + .isInstanceOf(NotFoundException.class); + + when(sceneService.copyStates(scene, copyFrom)).thenReturn(List.of()); + + assertThat(sceneController.copy(1L, 10L, mockPrincipal)).isEmpty(); + } + + @Test + public void testApply() throws NotFoundException { + final Scene s = new Scene(); + when(mockPrincipal.getName()).thenReturn("user"); + when(sceneRepository.findByIdAndUsername(1L, "user")).thenReturn(Optional.of(s)); + when(sceneRepository.findByIdAndUsername(2L, "user")).thenReturn(Optional.empty()); + when(sceneService.apply(s, "user", false)).thenReturn(List.of()); + + assertThatThrownBy(() -> sceneController.apply(2L, mockPrincipal, null)) + .isInstanceOf(NotFoundException.class); + assertThat(sceneController.apply(1L, mockPrincipal, null)).isEmpty(); + + when(userRepository.findByUsername("user")).thenReturn(u); + when(userRepository.findById(2L)).thenReturn(Optional.of(h)); + + when(sceneRepository.findByIdAndUserIdAndGuestAccessEnabled(1L, 2L, true)) + .thenReturn(Optional.of(s)); + when(sceneRepository.findByIdAndUserIdAndGuestAccessEnabled(2L, 2L, true)) + .thenReturn(Optional.empty()); + + when(sceneService.applyAsGuest(s, "user", 2L)).thenReturn(List.of()); + + assertThatThrownBy(() -> sceneController.apply(2L, mockPrincipal, 2L)) + .isInstanceOf(NotFoundException.class); + assertThat(sceneController.apply(1L, mockPrincipal, 2L)).isEmpty(); } @Test @@ -46,10 +107,14 @@ public class SceneControllerTests { when(mockPrincipal.getName()).thenReturn("user"); when(sceneRepository.findByUsername("user")).thenReturn(List.of()); assertThat(sceneController.findAll(mockPrincipal, null)).isEmpty(); + + when(userRepository.findByUsername("user")).thenReturn(u); + when(userRepository.findById(2L)).thenReturn(Optional.of(h)); + assertThat(sceneController.findAll(mockPrincipal, 2L)).isEmpty(); } private void equalToRequest(Scene created, SceneSaveRequest a) { - assertThat(created.getName()).isEqualTo(a.getName()); + if (a.getName() != null) assertThat(created.getName()).isEqualTo(a.getName()); assertThat(created.getUserId()).isEqualTo(1L); assertThat(created.getIcon()).isEqualTo(a.getIcon()); assertThat(created.isGuestAccessEnabled()).isEqualTo(a.isGuestAccessEnabled()); @@ -85,6 +150,11 @@ public class SceneControllerTests { assertThat(created.getId()).isEqualTo(42L); equalToRequest(created, a); + a.setName(null); + created = sceneController.update(a.getId(), a, mockPrincipal); + assertThat(created.getId()).isEqualTo(42L); + equalToRequest(created, a); + a.setId(43L); assertThatThrownBy(() -> sceneController.update(a.getId(), a, mockPrincipal)) .isInstanceOf(NotFoundException.class); @@ -96,4 +166,10 @@ public class SceneControllerTests { doNothing().when(sceneRepository).deleteById(42L); Assertions.assertDoesNotThrow(() -> sceneController.deleteById(42L)); } + + @Test + public void testGetStates() { + when(stateStateRepository.findBySceneId(1L)).thenReturn(List.of()); + assertThat(sceneController.getStates(1L)).isEmpty(); + } }