From 66422158f638d551212213daedf278ab73b6fa4f Mon Sep 17 00:00:00 2001 From: omenem Date: Mon, 25 May 2020 14:56:09 +0200 Subject: [PATCH] regular light controller --- .../RegularLightControllerTests.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) 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"))