device controller is covered (yay)

This commit is contained in:
Tommaso Rodolfo Masera 2020-05-26 16:07:46 +02:00
parent 7d974f1a1d
commit f784796852

View file

@ -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")