added RangeTriggerControllerTests

This commit is contained in:
Jacob Salvi 2020-05-24 22:50:16 +02:00
parent 7b05718286
commit b3f24b7df3

View file

@ -0,0 +1,105 @@
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.RangeConditionOrTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
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("RangeTriggerController tests")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class RangeTriggerControllerTests {
@InjectMocks private RangeTriggerController controller;
@Mock private RangeTriggerRepository repository;
@Test
public void testDelete() {
doNothing().when(repository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
@Test
public void testGetAll() {
RangeTrigger trigger1 = new RangeTrigger();
trigger1.setAutomationId(1L);
RangeTrigger trigger2 = new RangeTrigger();
trigger2.setAutomationId(1L);
RangeTrigger trigger3 = new RangeTrigger();
trigger3.setAutomationId(1L);
ArrayList<RangeTrigger> list = new ArrayList<>();
list.add(trigger1);
list.add(trigger2);
list.add(trigger3);
when(repository.findAllByAutomationId(42L)).thenReturn(list);
List<RangeTrigger> returned = controller.getAll(42L);
assertThat(returned).contains(trigger1);
assertThat(returned).contains(trigger2);
assertThat(returned).contains(trigger3);
assertThat(returned.size()).isEqualTo(3);
}
@Test
public void testCreate() {
RangeConditionOrTriggerSaveRequest toSend = new RangeConditionOrTriggerSaveRequest();
toSend.setRange(40L);
toSend.setOperator(Operator.EQUAL);
toSend.setDeviceId(1L);
toSend.setAutomationId(2L);
RangeTrigger trigger = new RangeTrigger();
trigger.setRange(40L);
trigger.setOperator(Operator.EQUAL);
trigger.setAutomationId(2L);
trigger.setDeviceId(1L);
when(repository.save(any(RangeTrigger.class))).thenReturn(trigger);
RangeTrigger returned = controller.create(toSend);
assertThat(returned.getOperator()).isEqualTo(toSend.getOperator());
assertThat(returned.getRange()).isEqualTo(toSend.getRange());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
RangeTrigger trigger = new RangeTrigger();
trigger.setRange(40L);
trigger.setDeviceId(1L);
trigger.setAutomationId(2L);
trigger.setOperator(Operator.EQUAL);
when(repository.save(trigger)).thenReturn(trigger);
RangeConditionOrTriggerSaveRequest toSend = new RangeConditionOrTriggerSaveRequest();
toSend.setId(34L);
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setRange(40L);
when(repository.findById(34L)).thenReturn(Optional.of(trigger));
RangeTrigger 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());
}
}