From 7d974f1a1de576155a09daa8b29e392819fcdf61 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Mon, 25 May 2020 21:32:10 +0200 Subject: [PATCH] device controller is almost tested (can't fix exception throw) --- .../controller/DeviceControllerTests.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceControllerTests.java new file mode 100644 index 0000000..29b11f5 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceControllerTests.java @@ -0,0 +1,86 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DeviceSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.BadDataException; +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.List; +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.security.test.context.support.WithMockUser; + +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class DeviceControllerTests { + @InjectMocks private DeviceController deviceController; + + @Mock private DeviceService deviceService; + @Mock private DeviceRepository deviceRepository; + @Mock private RoomRepository roomRepository; + @Mock private Principal mockPrincipal; + + private User user; + private Room room; + + public DeviceControllerTests() { + room = new Room(); + room.setId(1L); + + user = new User(); + user.setId(1L); + user.setName("user"); + } + + @DisplayName("check update device") + @Test + @SneakyThrows(NotFoundException.class) + public void updateTest() throws BadDataException { + DeviceSaveRequest dto = new DeviceSaveRequest(); + dto.setId(22L); + dto.setName("DTO"); + dto.setRoomId(room.getId()); + when(mockPrincipal.getName()).thenReturn("user"); + assertThat(deviceRepository.findByIdAndUsername(34L, mockPrincipal.getName())).isEmpty(); + + Device d = new RegularLight(); + deviceService.saveAsOwner(d, mockPrincipal.getName()); + d = deviceController.update(dto, mockPrincipal); + assertThat(deviceRepository.findByIdAndUsername(dto.getId(), mockPrincipal.getName())) + .isSameAs(d); + assertThat(d.getRoomId()).isSameAs(dto.getRoomId()); + assertThat(d.getName()).isSameAs(dto.getName()); + } + + @DisplayName("check if list is empty") + @Test + @SneakyThrows(NotFoundException.class) + public void getAllEmptyTest() { + when(mockPrincipal.getName()).thenReturn("user"); + List l = deviceController.getAll(user.getId(), mockPrincipal); + assertThat(l.isEmpty()); + } + + @DisplayName("check if list contains elements added") + @Test + @SneakyThrows(NotFoundException.class) + public void getAllTest() throws BadDataException { + when(mockPrincipal.getName()).thenReturn("user"); + Device d1 = new RegularLight(); + deviceService.saveAsOwner(d1, mockPrincipal.getName()); + Device d2 = new SmartPlug(); + deviceService.saveAsOwner(d2, mockPrincipal.getName()); + + assertThat( + deviceController.getAll(user.getId(), mockPrincipal).containsAll(List.of(d1, d2))); + } +}