diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateControllerTests.java new file mode 100644 index 0000000..2e90862 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateControllerTests.java @@ -0,0 +1,53 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableStateSaveRequest; +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("DimmableState controller test") +public class DimmableStateControllerTests { + + @InjectMocks DimmableStateController controller; + + @Mock private DimmableStateRepository dimmableStateRepository; + + @Test + @SneakyThrows(NotFoundException.class) + public void testUpdate() { + DimmableState state = new DimmableState(); + state.setIntensity(40); + DimmableStateSaveRequest toSend = new DimmableStateSaveRequest(); + toSend.setIntensity(40); + toSend.setId(0L); + when(dimmableStateRepository.findById(toSend.getId())).thenReturn(Optional.of(state)); + assertThat(controller.update(toSend).getIntensity()).isEqualTo(toSend.getIntensity()); + } + + @Test + public void testDelete() { + doNothing().when(dimmableStateRepository).deleteById(eq(42L)); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + Assertions.assertDoesNotThrow(() -> controller.delete(42L)); + } +}