From 7652610c5d85d2417261880f7918a2aed4d9f968 Mon Sep 17 00:00:00 2001 From: omenem Date: Sun, 24 May 2020 14:16:07 +0200 Subject: [PATCH] I can't mock a method --- .../RegularLightControllerTests.java | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java 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 new file mode 100644 index 0000000..f3c2136 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java @@ -0,0 +1,126 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +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.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils; +import java.security.Principal; +import lombok.SneakyThrows; +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; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class RegularLightControllerTests { + + @InjectMocks private RegularLightController regularLightController; + + @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"); + } + + private void checkRegularLightAgainstRequest( + final RegularLight toCheck, final SwitchableSaveRequest request) { + assertThat(toCheck).isNotNull(); + assertThat(toCheck.isOn()).isEqualTo(request.isOn()); + assertThat(toCheck.getName()).isEqualTo(request.getName()); + assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId()); + } + + @Test + @DisplayName("when creating should return the same object") + @SneakyThrows(NotFoundException.class) + public void testCreate() { + doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user")); + when(deviceService.saveAsOwner(any(RegularLight.class), eq("user"))) + .thenAnswer(i -> i.getArguments()[0]); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + final SwitchableSaveRequest toSend = new SwitchableSaveRequest(); + toSend.setName("rl"); + toSend.setRoomId(20L); + toSend.setOn(true); + + final RegularLight regularLight = regularLightController.create(toSend, mockPrincipal); + + checkRegularLightAgainstRequest(regularLight, toSend); + } + + @Test + @DisplayName("when updating should return the same object") + @SneakyThrows(NotFoundException.class) + 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); + toSend.setId(50L); + toUpdate.setOn(false); + + when(regularLightRepository.findByIdAndUserId(anyLong(), anyLong())) + .thenReturn(java.util.Optional.of(toUpdate)); + + when(deviceService.saveAsGuest(any(RegularLight.class), eq("user"), anyLong())) + .thenAnswer(i -> i.getArguments()[0]); + + when(Utils.returnIfGuest( + any(UserRepository.class), + any(RegularLight.class), + anyLong(), + any(Principal.class))) + .thenAnswer(i -> i.getArguments()[1]); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + final RegularLight regularLight = regularLightController.update(toSend, mockPrincipal, 20L); + + checkRegularLightAgainstRequest(regularLight, toSend); + } +}