From 7d974f1a1de576155a09daa8b29e392819fcdf61 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Mon, 25 May 2020 21:32:10 +0200 Subject: [PATCH 1/2] 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))); + } +} From f7847968522f70ee6be940d1c0633cdf31b5efc6 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Tue, 26 May 2020 16:07:46 +0200 Subject: [PATCH 2/2] device controller is covered (yay) --- .../controller/DeviceControllerTests.java | 66 +++++++++++++++---- 1 file changed, 52 insertions(+), 14 deletions(-) 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 index 29b11f5..48be44a 100644 --- 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 @@ -1,7 +1,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DeviceSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.BadDataException; @@ -10,6 +11,7 @@ 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 java.util.Optional; import lombok.SneakyThrows; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -43,22 +45,58 @@ public class DeviceControllerTests { @DisplayName("check update device") @Test - @SneakyThrows(NotFoundException.class) - public void updateTest() throws BadDataException { + public void updateDeviceRepositoryTest() { DeviceSaveRequest dto = new DeviceSaveRequest(); - dto.setId(22L); - dto.setName("DTO"); + dto.setId(3L); + 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()); + when(deviceRepository.findByIdAndUsername(dto.getId(), mockPrincipal.getName())) + .thenReturn(Optional.empty()); + + assertThatThrownBy(() -> deviceController.update(dto, mockPrincipal)) + .isInstanceOf(NotFoundException.class); + } + + @DisplayName("check update device") + @Test + public void updateRoomRepositoryTest() { + DeviceSaveRequest dto = new DeviceSaveRequest(); + dto.setId(3L); + dto.setName("dto"); + dto.setRoomId(room.getId()); + + when(deviceRepository.findByIdAndUsername(dto.getId(), mockPrincipal.getName())) + .thenReturn(Optional.of(new RegularLight())); + when(roomRepository.findByIdAndUsername(room.getId(), mockPrincipal.getName())) + .thenReturn(Optional.empty()); + + assertThatThrownBy(() -> deviceController.update(dto, mockPrincipal)) + .isInstanceOf(BadDataException.class); + } + + @DisplayName("check update device") + @Test + @SneakyThrows({NotFoundException.class, BadDataException.class}) + public void updateTest() { + DeviceSaveRequest dto = new DeviceSaveRequest(); + dto.setId(3L); + dto.setName("dto"); + dto.setRoomId(room.getId()); + + Device rl = new RegularLight(); + rl.setRoomId(dto.getRoomId()); + rl.setName(dto.getName()); + + when(deviceRepository.findByIdAndUsername(dto.getId(), mockPrincipal.getName())) + .thenReturn(Optional.of(rl)); + when(roomRepository.findByIdAndUsername(room.getId(), mockPrincipal.getName())) + .thenReturn(Optional.of(room)); + when(deviceService.saveAsOwner(rl, mockPrincipal.getName())).thenReturn(rl); + + Device returned = deviceController.update(dto, mockPrincipal); + assertThat(returned.getRoomId()).isSameAs(rl.getRoomId()); + assertThat(returned.getName()).isSameAs(rl.getName()); } @DisplayName("check if list is empty")