diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionControllerTests.java new file mode 100644 index 0000000..b38b26c --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionControllerTests.java @@ -0,0 +1,104 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionOrTriggerSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanConditionRepository; +import java.security.Principal; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Assertions; +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.mock.web.MockHttpServletRequest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +@DisplayName("BooleanConditionController tests") +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class BooleanConditionControllerTests { + @InjectMocks private BooleanConditionController controller; + + @Mock private BooleanConditionRepository repository; + + @Mock private Principal principal; + + @Test + public void testCreate() { + BooleanCondition condition = new BooleanCondition(); + condition.setOn(true); + condition.setDeviceId(1L); + condition.setAutomationId(2L); + when(repository.save(any())).thenAnswer(i -> i.getArgument(0)); + BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest(); + toSend.setAutomationId(2L); + toSend.setDeviceId(1L); + toSend.setOn(false); + BooleanCondition returned = controller.create(toSend); + assertThat(returned.isOn()).isEqualTo(toSend.isOn()); + assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId()); + assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testUpdate() { + BooleanCondition condition = new BooleanCondition(); + condition.setOn(true); + condition.setDeviceId(1L); + condition.setAutomationId(2L); + when(repository.save(condition)).thenReturn(condition); + BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest(); + toSend.setId(34L); + toSend.setAutomationId(2L); + toSend.setDeviceId(1L); + toSend.setOn(false); + when(repository.findById(34L)).thenReturn(Optional.of(condition)); + BooleanCondition returned = controller.update(toSend); + assertThat(returned.isOn()).isEqualTo(toSend.isOn()); + assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId()); + assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId()); + } + + @Test + public void testGetAll() { + BooleanCondition condition1 = new BooleanCondition(); + condition1.setAutomationId(1L); + BooleanCondition condition2 = new BooleanCondition(); + condition2.setAutomationId(1L); + BooleanCondition condition3 = new BooleanCondition(); + condition3.setAutomationId(1L); + ArrayList list = new ArrayList<>(); + list.add(condition1); + list.add(condition2); + list.add(condition3); + when(repository.findAllByAutomationId(42L)).thenReturn(list); + List returned = controller.getAll(42L); + assertThat(returned).contains(condition1); + assertThat(returned).contains(condition2); + assertThat(returned).contains(condition3); + assertThat(returned.size()).isEqualTo(3); + } + + @Test + public void testDelete() { + doNothing().when(repository).deleteById(eq(42L)); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + Assertions.assertDoesNotThrow(() -> controller.delete(42L)); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerControllerTests.java new file mode 100644 index 0000000..ca3c613 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerControllerTests.java @@ -0,0 +1,104 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionOrTriggerSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTriggerRepository; +import java.security.Principal; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Assertions; +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.mock.web.MockHttpServletRequest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +@DisplayName("BooleanTriggerController tests") +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class BooleanTriggerControllerTests { + @InjectMocks private BooleanTriggerController controller; + + @Mock private BooleanTriggerRepository repository; + + @Mock private Principal principal; + + @Test + public void testCreate() { + BooleanTrigger condition = new BooleanTrigger(); + condition.setOn(true); + condition.setDeviceId(1L); + condition.setAutomationId(2L); + when(repository.save(any())).thenAnswer(i -> i.getArgument(0)); + BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest(); + toSend.setAutomationId(2L); + toSend.setDeviceId(1L); + toSend.setOn(false); + BooleanTrigger returned = controller.create(toSend); + assertThat(returned.isOn()).isEqualTo(toSend.isOn()); + assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId()); + assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testUpdate() { + BooleanTrigger condition = new BooleanTrigger(); + condition.setOn(true); + condition.setDeviceId(1L); + condition.setAutomationId(2L); + when(repository.save(condition)).thenReturn(condition); + BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest(); + toSend.setId(34L); + toSend.setAutomationId(2L); + toSend.setDeviceId(1L); + toSend.setOn(false); + when(repository.findById(34L)).thenReturn(Optional.of(condition)); + BooleanTrigger returned = controller.update(toSend); + assertThat(returned.isOn()).isEqualTo(toSend.isOn()); + assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId()); + assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId()); + } + + @Test + public void testGetAll() { + BooleanTrigger condition1 = new BooleanTrigger(); + condition1.setAutomationId(1L); + BooleanTrigger condition2 = new BooleanTrigger(); + condition2.setAutomationId(1L); + BooleanTrigger condition3 = new BooleanTrigger(); + condition3.setAutomationId(1L); + ArrayList list = new ArrayList<>(); + list.add(condition1); + list.add(condition2); + list.add(condition3); + when(repository.findAllByAutomationId(42L)).thenReturn(list); + List returned = controller.getAll(42L); + assertThat(returned).contains(condition1); + assertThat(returned).contains(condition2); + assertThat(returned).contains(condition3); + assertThat(returned.size()).isEqualTo(3); + } + + @Test + public void testDelete() { + doNothing().when(repository).deleteById(eq(42L)); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + Assertions.assertDoesNotThrow(() -> controller.delete(42L)); + } +}