From a2470d132de6c2139a8a4e10948127bc3dc0f7b8 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 27 May 2020 10:11:00 +0200 Subject: [PATCH] Tests --- .../controller/RoomControllerTests.java | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java index c86a1ec..fb4e105 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java @@ -42,17 +42,29 @@ public class RoomControllerTests { private final User u; + private final User h; + public RoomControllerTests() { u = new User(); - u.setName("user"); + u.setUsername("user"); u.setId(1L); + + h = new User(); + h.setUsername("host"); + h.setId(2L); + u.getHosts().add(h); + h.getGuests().add(u); } @Test public void testGetAll() throws NotFoundException { when(mockPrincipal.getName()).thenReturn("user"); when(roomRepository.findByUsername("user")).thenReturn(List.of()); + when(roomRepository.findByUserId(2L)).thenReturn(List.of()); + when(userRepository.findById(2L)).thenReturn(Optional.of(h)); + when(userRepository.findByUsername("user")).thenReturn(u); assertThat(roomController.findAll(null, mockPrincipal)).isEmpty(); + assertThat(roomController.findAll(2L, mockPrincipal)).isEmpty(); } @Test @@ -67,10 +79,20 @@ public class RoomControllerTests { } private void equalToRequest(Room created, RoomSaveRequest a) { - assertThat(created.getName()).isEqualTo(a.getName()); + if (a.getName() != null) { + assertThat(created.getName()).isEqualTo(a.getName()); + } assertThat(created.getUserId()).isEqualTo(1L); - assertThat(created.getIcon()).isEqualTo(a.getIcon()); - assertThat(created.getImage()).isEqualTo(a.getImage()); + if (a.getIcon() != null) { + assertThat(created.getIcon()).isEqualTo(a.getIcon()); + } + if (a.getImage() != null) { + if (a.getImage().isEmpty()) { + assertThat(created.getImage()).isNull(); + } else { + assertThat(created.getImage()).isEqualTo(a.getImage()); + } + } } @Test @@ -97,13 +119,21 @@ public class RoomControllerTests { when(roomRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(old)); when(roomRepository.findByIdAndUsername(43L, "user")).thenReturn(Optional.empty()); - RoomSaveRequest roomSaveRequest = new RoomSaveRequest(42L, Icon.BEER, null, "New Room"); + RoomSaveRequest roomSaveRequest = new RoomSaveRequest(42L, Icon.BEER, "image", "New Room"); Room created = roomController.update(roomSaveRequest.getId(), roomSaveRequest, mockPrincipal); assertThat(created.getId()).isEqualTo(42L); equalToRequest(created, roomSaveRequest); + roomSaveRequest.setImage(""); + roomSaveRequest.setIcon(null); + roomSaveRequest.setName(null); + + created = roomController.update(roomSaveRequest.getId(), roomSaveRequest, mockPrincipal); + assertThat(created.getId()).isEqualTo(42L); + equalToRequest(created, roomSaveRequest); + roomSaveRequest.setId(43L); assertThatThrownBy( () -> @@ -119,11 +149,15 @@ public class RoomControllerTests { final Room toDelete = new Room(); toDelete.setId(42L); + final Device d = new Thermostat(); + d.setId(2020L); + doNothing().when(switchRepository).deleteAllByRoomId(42L); doNothing().when(knobDimmerRepository).deleteAllByRoomId(42L); doNothing().when(buttonDimmerRepository).deleteAllByRoomId(42L); when(roomRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(toDelete)); - when(deviceService.findAll(42L, null, "user")).thenReturn(List.of()); + when(deviceService.findAll(42L, null, "user")).thenReturn(List.of(d)); + doNothing().when(deviceService).deleteByIdAsOwner(2020L, "user"); doNothing().when(roomRepository).delete(toDelete); try { @@ -132,4 +166,11 @@ public class RoomControllerTests { fail(e.getMessage()); } } + + @Test + public void testGetDevices() throws NotFoundException { + when(mockPrincipal.getName()).thenReturn("user"); + when(deviceService.findAll(2020L, 10L, "user")).thenReturn(List.of()); + assertThat(roomController.getDevices(2020L, mockPrincipal, 10L)); + } }