From d25bcf350764418960ecc6c87020cf071cf3aea2 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Sun, 24 May 2020 18:11:22 +0200 Subject: [PATCH] added RangeConditionControllerTests --- .../RangeConditionControllerTests.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionControllerTests.java new file mode 100644 index 0000000..563636c --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionControllerTests.java @@ -0,0 +1,108 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +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.RangeConditionOrTriggerSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +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("RangeConditionController tests") +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class RangeConditionControllerTests { + @InjectMocks private RangeConditionController controller; + + @Mock private RangeConditionRepository repository; + + @Mock private Principal principal; + + @Test + public void testCreate() { + RangeCondition condition = new RangeCondition(); + condition.setRange(40L); + condition.setDeviceId(1L); + condition.setAutomationId(2L); + condition.setOperator(Operator.EQUAL); + when(repository.save(condition)).thenReturn(condition); + RangeConditionOrTriggerSaveRequest toSend = new RangeConditionOrTriggerSaveRequest(); + toSend.setAutomationId(2L); + toSend.setDeviceId(1L); + toSend.setOperator(Operator.EQUAL); + toSend.setRange(40L); + RangeCondition returned = controller.create(toSend); + assertThat(returned.getRange()).isEqualTo(toSend.getRange()); + assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId()); + assertThat(returned.getOperator()).isEqualTo(toSend.getOperator()); + assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testUpdate() { + RangeCondition condition = new RangeCondition(); + condition.setRange(40L); + condition.setDeviceId(1L); + condition.setAutomationId(2L); + condition.setOperator(Operator.EQUAL); + when(repository.save(condition)).thenReturn(condition); + RangeConditionOrTriggerSaveRequest toSend = new RangeConditionOrTriggerSaveRequest(); + toSend.setId(34L); + toSend.setAutomationId(2L); + toSend.setDeviceId(1L); + toSend.setOperator(Operator.EQUAL); + toSend.setRange(40L); + when(repository.findById(34L)).thenReturn(Optional.of(condition)); + RangeCondition returned = controller.update(toSend); + assertThat(returned.getRange()).isEqualTo(toSend.getRange()); + assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId()); + assertThat(returned.getOperator()).isEqualTo(toSend.getOperator()); + assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId()); + } + + @Test + public void testGetAll() { + RangeCondition condition1 = new RangeCondition(); + condition1.setAutomationId(1L); + RangeCondition condition2 = new RangeCondition(); + condition2.setAutomationId(1L); + RangeCondition condition3 = new RangeCondition(); + 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)); + } +}