device controller is almost tested (can't fix exception throw)
This commit is contained in:
parent
af93063cf4
commit
7d974f1a1d
1 changed files with 86 additions and 0 deletions
|
@ -0,0 +1,86 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
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 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
|
||||||
|
@SneakyThrows(NotFoundException.class)
|
||||||
|
public void updateTest() throws BadDataException {
|
||||||
|
DeviceSaveRequest dto = new DeviceSaveRequest();
|
||||||
|
dto.setId(22L);
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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)));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue