Merge branch 'tests' into 'dev'
Tests See merge request sa4-2020/the-sanmarinoes/backend!199
This commit is contained in:
commit
57f5415d61
1 changed files with 47 additions and 6 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue