added DimmableStateControllerTests

This commit is contained in:
Jacob Salvi 2020-05-25 15:47:19 +02:00
parent b3f24b7df3
commit 4250f07fef

View file

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