added SwitchableStateControllerTests

This commit is contained in:
Jacob Salvi 2020-05-25 15:55:39 +02:00
parent 4250f07fef
commit a4ca37e6ec

View file

@ -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));
}
}