Merge branch 'tests' into 'dev'

Tests

See merge request sa4-2020/the-sanmarinoes/backend!153
This commit is contained in:
Claudio Maggioni 2020-05-19 17:34:29 +02:00
commit a0a0320c84
4 changed files with 245 additions and 0 deletions

View file

@ -2,9 +2,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class AutomationSaveRequest {
private long id;
@NotNull @NotEmpty private String name;

View file

@ -3,9 +3,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon;
import javax.persistence.Lob;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class RoomSaveRequest {
/** Room identifier */

View file

@ -0,0 +1,107 @@
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.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.*;
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 AutomationControllerTests {
@InjectMocks private AutomationController automationController;
@Mock private UserRepository userRepository;
@Mock private AutomationRepository automationRepository;
@Mock private Principal mockPrincipal;
private final User u;
public AutomationControllerTests() {
u = new User();
u.setName("user");
u.setId(1L);
}
@Test
public void testGetAll() {
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
when(automationRepository.findAllByUserId(1L)).thenReturn(List.of());
assertThat(automationController.getAll(null, mockPrincipal)).isEmpty();
}
@Test
public void testGet() throws NotFoundException {
Automation a = new Automation();
when(automationRepository.findById(1L)).thenReturn(Optional.of(a));
when(automationRepository.findById(2L)).thenReturn(Optional.empty());
assertThat(automationController.get(1L)).isSameAs(a);
assertThatThrownBy(() -> automationController.get(2L))
.isInstanceOf(NotFoundException.class);
}
private void equalToRequest(Automation created, AutomationSaveRequest a) {
assertThat(created.getName()).isEqualTo(a.getName());
assertThat(created.getUserId()).isEqualTo(1L);
}
@Test
public void testCreate() {
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
when(automationRepository.save(any(Automation.class))).thenAnswer(i -> i.getArguments()[0]);
AutomationSaveRequest a = new AutomationSaveRequest(26, "Automation");
Automation created = automationController.create(a, mockPrincipal);
assertThat(created.getId()).isEqualTo(0L);
equalToRequest(created, a);
}
@Test
public void testUpdate() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
final Automation old = new Automation();
old.setId(42L);
old.setName("Old Name");
when(automationRepository.save(any(Automation.class))).thenAnswer(i -> i.getArguments()[0]);
when(automationRepository.findById(42L)).thenReturn(Optional.of(old));
when(automationRepository.findById(43L)).thenReturn(Optional.empty());
AutomationSaveRequest a = new AutomationSaveRequest(42L, "New Name");
Automation created = automationController.update(a, mockPrincipal);
assertThat(created.getId()).isEqualTo(42L);
equalToRequest(created, a);
a.setId(43L);
assertThatThrownBy(() -> automationController.update(a, mockPrincipal))
.isInstanceOf(NotFoundException.class);
}
@Test
public void testDelete() {
final Automation old = new Automation();
old.setId(42L);
old.setName("Old Name");
doNothing().when(automationRepository).deleteById(42L);
automationController.delete(42L);
}
}

View file

@ -0,0 +1,134 @@
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.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.util.AssertionErrors.fail;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
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 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 RoomControllerTests {
@InjectMocks private RoomController roomController;
@Mock private UserRepository userRepository;
@Mock private RoomRepository roomRepository;
@Mock private Principal mockPrincipal;
@Mock private SwitchRepository switchRepository;
@Mock private KnobDimmerRepository knobDimmerRepository;
@Mock private ButtonDimmerRepository buttonDimmerRepository;
@Mock private DeviceService deviceService;
private final User u;
public RoomControllerTests() {
u = new User();
u.setName("user");
u.setId(1L);
}
@Test
public void testGetAll() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user");
when(roomRepository.findByUsername("user")).thenReturn(List.of());
assertThat(roomController.findAll(null, mockPrincipal)).isEmpty();
}
@Test
public void testGet() throws NotFoundException {
Room r = new Room();
when(roomRepository.findById(1L)).thenReturn(Optional.of(r));
when(roomRepository.findById(2L)).thenReturn(Optional.empty());
assertThat(roomController.findById(1L, mockPrincipal, null)).isSameAs(r);
assertThatThrownBy(() -> roomController.findById(2L, mockPrincipal, null))
.isInstanceOf(NotFoundException.class);
}
private void equalToRequest(Room created, RoomSaveRequest a) {
assertThat(created.getName()).isEqualTo(a.getName());
assertThat(created.getUserId()).isEqualTo(1L);
assertThat(created.getIcon()).isEqualTo(a.getIcon());
assertThat(created.getImage()).isEqualTo(a.getImage());
}
@Test
public void testCreate() {
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername("user")).thenReturn(u);
when(roomRepository.save(any(Room.class))).thenAnswer(i -> i.getArguments()[0]);
RoomSaveRequest roomSaveRequest = new RoomSaveRequest(0, Icon.BEER, null, "New Room");
Room created = roomController.create(roomSaveRequest, mockPrincipal);
assertThat(created.getId()).isNull();
equalToRequest(created, roomSaveRequest);
}
@Test
public void testUpdate() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user");
final Room old = new Room();
old.setId(42L);
old.setUserId(1L);
old.setName("Old Name");
when(roomRepository.save(any(Room.class))).thenAnswer(i -> i.getArguments()[0]);
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");
Room created =
roomController.update(roomSaveRequest.getId(), roomSaveRequest, mockPrincipal);
assertThat(created.getId()).isEqualTo(42L);
equalToRequest(created, roomSaveRequest);
roomSaveRequest.setId(43L);
assertThatThrownBy(
() ->
roomController.update(
roomSaveRequest.getId(), roomSaveRequest, mockPrincipal))
.isInstanceOf(NotFoundException.class);
}
@Test
public void testDelete() throws NotFoundException {
when(mockPrincipal.getName()).thenReturn("user");
final Room toDelete = new Room();
toDelete.setId(42L);
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());
doNothing().when(roomRepository).delete(toDelete);
try {
roomController.deleteById(42L, mockPrincipal);
} catch (NotFoundException e) {
fail(e.getMessage());
}
}
}