Merge branch 'tests' into 'dev'

Tests

See merge request sa4-2020/the-sanmarinoes/backend!199
This commit is contained in:
Claudio Maggioni 2020-05-27 10:11:14 +02:00
commit 57f5415d61

View file

@ -42,17 +42,29 @@ public class RoomControllerTests {
private final User u; private final User u;
private final User h;
public RoomControllerTests() { public RoomControllerTests() {
u = new User(); u = new User();
u.setName("user"); u.setUsername("user");
u.setId(1L); u.setId(1L);
h = new User();
h.setUsername("host");
h.setId(2L);
u.getHosts().add(h);
h.getGuests().add(u);
} }
@Test @Test
public void testGetAll() throws NotFoundException { public void testGetAll() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
when(roomRepository.findByUsername("user")).thenReturn(List.of()); 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(null, mockPrincipal)).isEmpty();
assertThat(roomController.findAll(2L, mockPrincipal)).isEmpty();
} }
@Test @Test
@ -67,11 +79,21 @@ public class RoomControllerTests {
} }
private void equalToRequest(Room created, RoomSaveRequest a) { private void equalToRequest(Room created, RoomSaveRequest a) {
if (a.getName() != null) {
assertThat(created.getName()).isEqualTo(a.getName()); assertThat(created.getName()).isEqualTo(a.getName());
}
assertThat(created.getUserId()).isEqualTo(1L); assertThat(created.getUserId()).isEqualTo(1L);
if (a.getIcon() != null) {
assertThat(created.getIcon()).isEqualTo(a.getIcon()); 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()); assertThat(created.getImage()).isEqualTo(a.getImage());
} }
}
}
@Test @Test
public void testCreate() { public void testCreate() {
@ -97,13 +119,21 @@ public class RoomControllerTests {
when(roomRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(old)); when(roomRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(old));
when(roomRepository.findByIdAndUsername(43L, "user")).thenReturn(Optional.empty()); 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 = Room created =
roomController.update(roomSaveRequest.getId(), roomSaveRequest, mockPrincipal); roomController.update(roomSaveRequest.getId(), roomSaveRequest, mockPrincipal);
assertThat(created.getId()).isEqualTo(42L); assertThat(created.getId()).isEqualTo(42L);
equalToRequest(created, roomSaveRequest); 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); roomSaveRequest.setId(43L);
assertThatThrownBy( assertThatThrownBy(
() -> () ->
@ -119,11 +149,15 @@ public class RoomControllerTests {
final Room toDelete = new Room(); final Room toDelete = new Room();
toDelete.setId(42L); toDelete.setId(42L);
final Device d = new Thermostat();
d.setId(2020L);
doNothing().when(switchRepository).deleteAllByRoomId(42L); doNothing().when(switchRepository).deleteAllByRoomId(42L);
doNothing().when(knobDimmerRepository).deleteAllByRoomId(42L); doNothing().when(knobDimmerRepository).deleteAllByRoomId(42L);
doNothing().when(buttonDimmerRepository).deleteAllByRoomId(42L); doNothing().when(buttonDimmerRepository).deleteAllByRoomId(42L);
when(roomRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(toDelete)); 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); doNothing().when(roomRepository).delete(toDelete);
try { try {
@ -132,4 +166,11 @@ public class RoomControllerTests {
fail(e.getMessage()); 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));
}
} }