Added basic tests to AutomationController
This commit is contained in:
parent
32aa0575eb
commit
294cdc9a5d
2 changed files with 109 additions and 0 deletions
|
@ -2,9 +2,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
public class AutomationSaveRequest {
|
public class AutomationSaveRequest {
|
||||||
private long id;
|
private long id;
|
||||||
@NotNull @NotEmpty private String name;
|
@NotNull @NotEmpty private String name;
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue