regular light controller

This commit is contained in:
omenem 2020-05-25 14:56:09 +02:00
parent 7cdbb19126
commit 66422158f6

View file

@ -14,10 +14,10 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal; import java.security.Principal;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -47,11 +47,6 @@ public class RegularLightControllerTests {
@Mock private StateRepository<State> stateRepository; @Mock private StateRepository<State> stateRepository;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
}
private void checkRegularLightAgainstRequest( private void checkRegularLightAgainstRequest(
final RegularLight toCheck, final SwitchableSaveRequest request) { final RegularLight toCheck, final SwitchableSaveRequest request) {
assertThat(toCheck).isNotNull(); assertThat(toCheck).isNotNull();
@ -60,10 +55,28 @@ public class RegularLightControllerTests {
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId()); assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
} }
@Test
public void testGetAll() {
when(regularLightRepository.findAll()).thenReturn(List.of());
assertThat(regularLightController.findAll().isEmpty());
}
@Test
public void testGet() throws NotFoundException {
RegularLight r = new RegularLight();
when(regularLightRepository.findById(1L)).thenReturn(Optional.of(r));
when(regularLightRepository.findById(2L)).thenReturn(Optional.empty());
assertThat(regularLightController.findById(1L)).isSameAs(r);
assertThatThrownBy(() -> regularLightController.findById(2L))
.isInstanceOf(NotFoundException.class);
}
@Test @Test
@DisplayName("when creating should return the same object") @DisplayName("when creating should return the same object")
@SneakyThrows(NotFoundException.class) @SneakyThrows(NotFoundException.class)
public void testCreate() { public void testCreate() {
when(mockPrincipal.getName()).thenReturn("user");
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user")); doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
when(deviceService.saveAsOwner(any(RegularLight.class), eq("user"))) when(deviceService.saveAsOwner(any(RegularLight.class), eq("user")))
.thenAnswer(i -> i.getArguments()[0]); .thenAnswer(i -> i.getArguments()[0]);
@ -86,6 +99,7 @@ public class RegularLightControllerTests {
@SneakyThrows(NotFoundException.class) @SneakyThrows(NotFoundException.class)
public void testUpdate() { public void testUpdate() {
when(mockPrincipal.getName()).thenReturn("user");
final SwitchableSaveRequest toSend = new SwitchableSaveRequest(); final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
toSend.setName("rl"); toSend.setName("rl");
toSend.setRoomId(20L); toSend.setRoomId(20L);
@ -140,6 +154,7 @@ public class RegularLightControllerTests {
@SneakyThrows(NotFoundException.class) @SneakyThrows(NotFoundException.class)
public void testDelete() { public void testDelete() {
when(mockPrincipal.getName()).thenReturn("user");
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
@ -150,6 +165,7 @@ public class RegularLightControllerTests {
@Test @Test
public void testSceneBinding() { public void testSceneBinding() {
when(mockPrincipal.getName()).thenReturn("user");
RegularLight regularLight = new RegularLight(); RegularLight regularLight = new RegularLight();
when(regularLightRepository.findByIdAndUsername(24L, "user")) when(regularLightRepository.findByIdAndUsername(24L, "user"))
.thenReturn(Optional.of(regularLight)); .thenReturn(Optional.of(regularLight));
@ -166,6 +182,7 @@ public class RegularLightControllerTests {
@Test @Test
public void testSceneBinding2() { public void testSceneBinding2() {
when(mockPrincipal.getName()).thenReturn("user");
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
RegularLight regularLight = new RegularLight(); RegularLight regularLight = new RegularLight();
when(regularLightRepository.findByIdAndUsername(24L, "user")) when(regularLightRepository.findByIdAndUsername(24L, "user"))