From a4ca37e6ec94f318037a9f8389e3b0079e4c4bcc Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Mon, 25 May 2020 15:55:39 +0200 Subject: [PATCH] added SwitchableStateControllerTests --- .../SwitchableStateControllerTests.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateControllerTests.java new file mode 100644 index 0000000..384d933 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateControllerTests.java @@ -0,0 +1,52 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +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.SwitchableStateSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import java.util.Optional; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Assertions; +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") +@DisplayName("SwitchableState controller test") +public class SwitchableStateControllerTests { + @InjectMocks private SwitchableStateController controller; + + @Mock private SwitchableStateRepository repository; + + @Test + @SneakyThrows(NotFoundException.class) + public void testUpdate() { + SwitchableState state = new SwitchableState(); + state.setOn(true); + SwitchableStateSaveRequest toSend = new SwitchableStateSaveRequest(); + toSend.setOn(true); + toSend.setId(0L); + when(repository.findById(toSend.getId())).thenReturn(Optional.of(state)); + assertThat(controller.update(toSend).isOn()).isEqualTo(toSend.isOn()); + } + + @Test + public void testDelete() { + doNothing().when(repository).deleteById(eq(42L)); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + Assertions.assertDoesNotThrow(() -> controller.delete(42L)); + } +}