Merge branch '88-device-controller-test' into 'dev'

Resolve "device controller test"

Closes #88

See merge request sa4-2020/the-sanmarinoes/backend!188
This commit is contained in:
Claudio Maggioni 2020-05-27 00:06:23 +02:00
commit ca586020ba

View file

@ -0,0 +1,124 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
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;
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 java.util.Optional;
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<Device> 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
public void updateDeviceRepositoryTest() {
DeviceSaveRequest dto = new DeviceSaveRequest();
dto.setId(3L);
dto.setName("dto");
dto.setRoomId(room.getId());
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")
@Test
@SneakyThrows(NotFoundException.class)
public void getAllEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user");
List<Device> 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)));
}
}