added ThermostatConditionControllerTests
This commit is contained in:
parent
d25bcf3507
commit
7b05718286
1 changed files with 108 additions and 0 deletions
|
@ -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.ThermostatConditionSaveRequest;
|
||||||
|
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("ThermostatConditionController tests")
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
@WithMockUser(username = "user")
|
||||||
|
public class ThermostatConditionControllerTests {
|
||||||
|
@InjectMocks private ThermostatConditionController controller;
|
||||||
|
|
||||||
|
@Mock private ThermostatConditionRepository repository;
|
||||||
|
|
||||||
|
@Mock private Principal principal;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreate() {
|
||||||
|
ThermostatCondition condition = new ThermostatCondition();
|
||||||
|
condition.setMode(Thermostat.Mode.HEATING);
|
||||||
|
condition.setDeviceId(1L);
|
||||||
|
condition.setAutomationId(2L);
|
||||||
|
condition.setOperator(ThermostatCondition.Operator.EQUAL);
|
||||||
|
when(repository.save(condition)).thenReturn(condition);
|
||||||
|
ThermostatConditionSaveRequest toSend = new ThermostatConditionSaveRequest();
|
||||||
|
toSend.setAutomationId(2L);
|
||||||
|
toSend.setDeviceId(1L);
|
||||||
|
toSend.setOperator(ThermostatCondition.Operator.EQUAL);
|
||||||
|
toSend.setMode(Thermostat.Mode.HEATING);
|
||||||
|
ThermostatCondition returned = controller.create(toSend);
|
||||||
|
assertThat(returned.getMode()).isEqualTo(toSend.getMode());
|
||||||
|
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() {
|
||||||
|
ThermostatCondition condition = new ThermostatCondition();
|
||||||
|
condition.setMode(Thermostat.Mode.HEATING);
|
||||||
|
condition.setDeviceId(1L);
|
||||||
|
condition.setAutomationId(2L);
|
||||||
|
condition.setOperator(ThermostatCondition.Operator.EQUAL);
|
||||||
|
when(repository.save(condition)).thenReturn(condition);
|
||||||
|
ThermostatConditionSaveRequest toSend = new ThermostatConditionSaveRequest();
|
||||||
|
toSend.setId(34L);
|
||||||
|
toSend.setAutomationId(2L);
|
||||||
|
toSend.setDeviceId(1L);
|
||||||
|
toSend.setOperator(ThermostatCondition.Operator.EQUAL);
|
||||||
|
toSend.setMode(Thermostat.Mode.HEATING);
|
||||||
|
when(repository.findById(34L)).thenReturn(Optional.of(condition));
|
||||||
|
ThermostatCondition returned = controller.update(toSend);
|
||||||
|
assertThat(returned.getMode()).isEqualTo(toSend.getMode());
|
||||||
|
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
|
||||||
|
assertThat(returned.getOperator()).isEqualTo(toSend.getOperator());
|
||||||
|
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAll() {
|
||||||
|
ThermostatCondition condition1 = new ThermostatCondition();
|
||||||
|
condition1.setAutomationId(1L);
|
||||||
|
ThermostatCondition condition2 = new ThermostatCondition();
|
||||||
|
condition2.setAutomationId(1L);
|
||||||
|
ThermostatCondition condition3 = new ThermostatCondition();
|
||||||
|
condition3.setAutomationId(1L);
|
||||||
|
ArrayList<ThermostatCondition> list = new ArrayList<>();
|
||||||
|
list.add(condition1);
|
||||||
|
list.add(condition2);
|
||||||
|
list.add(condition3);
|
||||||
|
when(repository.findAllByAutomationId(42L)).thenReturn(list);
|
||||||
|
List<ThermostatCondition> 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue