added RangeConditionControllerTests

This commit is contained in:
Jacob Salvi 2020-05-24 18:11:22 +02:00
parent 86ca38cd47
commit d25bcf3507

View file

@ -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<RangeCondition> list = new ArrayList<>();
list.add(condition1);
list.add(condition2);
list.add(condition3);
when(repository.findAllByAutomationId(42L)).thenReturn(list);
List<RangeCondition> 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));
}
}