From 3fec9ccd200ffe0c67a7f556da8cac6d64378fc1 Mon Sep 17 00:00:00 2001 From: omenem Date: Mon, 25 May 2020 11:33:35 +0200 Subject: [PATCH] added missing tests --- .../RegularLightControllerTests.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) 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 323c4d2..b96e6e7 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 @@ -9,12 +9,14 @@ import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException; 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.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; @@ -41,6 +43,10 @@ public class RegularLightControllerTests { @Mock private UserRepository userRepository; + @Mock private SceneRepository sceneRepository; + + @Mock private StateRepository stateRepository; + @BeforeEach public void setup() { when(mockPrincipal.getName()).thenReturn("user"); @@ -128,4 +134,50 @@ public class RegularLightControllerTests { checkRegularLightAgainstRequest(regularLight, toSend); } + + @Test + @DisplayName("an existing id should succeed") + @SneakyThrows(NotFoundException.class) + public void testDelete() { + + doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + Assertions.assertDoesNotThrow(() -> regularLightController.delete(42L, mockPrincipal)); + } + + @Test + public void testSceneBinding() { + RegularLight regularLight = new RegularLight(); + when(regularLightRepository.findByIdAndUsername(24L, "user")) + .thenReturn(Optional.of(regularLight)); + Scene scene = new Scene(); + scene.setId(1L); + SwitchableState state = new SwitchableState(); + state.setSceneId(1L); + State s = regularLight.cloneState(); + when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene)); + when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0); + Assertions.assertDoesNotThrow( + () -> regularLightController.sceneBinding(24L, 1L, mockPrincipal)); + } + + @Test + public void testSceneBinding2() { + when(mockPrincipal.getName()).thenReturn("user"); + RegularLight regularLight = new RegularLight(); + when(regularLightRepository.findByIdAndUsername(24L, "user")) + .thenReturn(Optional.of(regularLight)); + Scene scene = new Scene(); + scene.setId(1L); + SwitchableState state = new SwitchableState(); + state.setSceneId(1L); + State s = regularLight.cloneState(); + when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene)); + when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2); + assertThatThrownBy(() -> regularLightController.sceneBinding(24L, 1L, mockPrincipal)) + .isInstanceOf(DuplicateStateException.class); + } }