diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsControllerTests.java new file mode 100644 index 0000000..eebab51 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsControllerTests.java @@ -0,0 +1,128 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertTrue; +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.DimmableSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import java.security.Principal; +import java.util.Optional; +import lombok.SneakyThrows; +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("Curtains controller test") +public class CurtainsControllerTests { + @InjectMocks CurtainsController controller; + + @Mock private DeviceService deviceService; + + @Mock private CurtainsRepository curtainsService; + + @Mock private SceneRepository sceneRepository; + + @Mock private StateRepository stateRepository; + + @Mock Principal principal; + + @Test + @SneakyThrows(NotFoundException.class) + public void testCreate() { + when(principal.getName()).thenReturn("user"); + Curtains curtains = new Curtains(); + curtains.setIntensity(50); + curtains.setName("name"); + curtains.setRoomId(42L); + when(deviceService.saveAsOwner(any(Curtains.class), eq("user"))).thenReturn(curtains); + doNothing().when(deviceService).throwIfRoomNotOwned(42L, "user"); + DimmableSaveRequest toSend = new DimmableSaveRequest(); + toSend.setRoomId(42L); + toSend.setIntensity(50); + toSend.setName("name"); + Curtains returned = controller.create(toSend, principal); + assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId()); + assertThat(returned.getName()).isEqualTo(toSend.getName()); + assertTrue(returned.isOn()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testUpdate() { + when(principal.getName()).thenReturn("user"); + Curtains curtains = new Curtains(); + curtains.setId(1L); + curtains.setIntensity(50); + curtains.setName("name"); + curtains.setRoomId(42L); + when(deviceService.saveAsOwner(any(Curtains.class), eq("user"))).thenReturn(curtains); + DimmableSaveRequest toSend = new DimmableSaveRequest(); + toSend.setId(1L); + toSend.setRoomId(42L); + toSend.setIntensity(50); + toSend.setName("name"); + when(curtainsService.findById(1L)).thenReturn(Optional.of(curtains)); + Curtains returned = controller.update(toSend, principal); + assertThat(returned.getRoomId()).isEqualTo(toSend.getRoomId()); + assertThat(returned.getName()).isEqualTo(toSend.getName()); + assertTrue(returned.isOn()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testDelete() { + when(principal.getName()).thenReturn("user"); + doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + controller.delete(42L, principal); + } + + @Test + @SneakyThrows({NotFoundException.class, DuplicateStateException.class}) + public void testSceneBinding() { + when(principal.getName()).thenReturn("user"); + Curtains curtains = new Curtains(); + when(curtainsService.findByIdAndUsername(24L, "user")).thenReturn(Optional.of(curtains)); + Scene scene = new Scene(); + scene.setId(1L); + SwitchableState state = new SwitchableState(); + state.setSceneId(1L); + State s = curtains.cloneState(); + when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene)); + when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0); + controller.sceneBinding(24L, 1L, principal); + } + + @Test + public void testSceneBinding2() { + when(principal.getName()).thenReturn("user"); + Curtains curtains = new Curtains(); + when(curtainsService.findByIdAndUsername(24L, "user")).thenReturn(Optional.of(curtains)); + Scene scene = new Scene(); + scene.setId(1L); + SwitchableState state = new SwitchableState(); + state.setSceneId(1L); + State s = curtains.cloneState(); + when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene)); + when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2); + assertThatThrownBy(() -> controller.sceneBinding(24L, 1L, principal)) + .isInstanceOf(DuplicateStateException.class); + } +}