diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationSaveRequest.java index 00f0c07..9e57f06 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationSaveRequest.java @@ -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; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java new file mode 100644 index 0000000..1805aba --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java @@ -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); + } +}