added DimmableStateControllerTests
This commit is contained in:
parent
b3f24b7df3
commit
4250f07fef
1 changed files with 53 additions and 0 deletions
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue