Merge branch '90-dimmablestatecontroller-tests' into 'dev'

Resolve "DimmableStateController tests"

Closes #90

See merge request sa4-2020/the-sanmarinoes/backend!193
This commit is contained in:
Matteo Omenetti 2020-05-26 17:23:56 +02:00
commit 9a86220a79
6 changed files with 308 additions and 0 deletions

View file

@ -0,0 +1,53 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableStateSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
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;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("DimmableState controller test")
public class DimmableStateControllerTests {
@InjectMocks DimmableStateController controller;
@Mock private DimmableStateRepository dimmableStateRepository;
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
DimmableState state = new DimmableState();
state.setIntensity(40);
DimmableStateSaveRequest toSend = new DimmableStateSaveRequest();
toSend.setIntensity(40);
toSend.setId(0L);
when(dimmableStateRepository.findById(toSend.getId())).thenReturn(Optional.of(state));
assertThat(controller.update(toSend).getIntensity()).isEqualTo(toSend.getIntensity());
}
@Test
public void testDelete() {
doNothing().when(dimmableStateRepository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
}

View file

@ -0,0 +1,52 @@
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.SwitchableStateSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
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;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("SwitchableState controller test")
public class SwitchableStateControllerTests {
@InjectMocks private SwitchableStateController controller;
@Mock private SwitchableStateRepository repository;
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
SwitchableState state = new SwitchableState();
state.setOn(true);
SwitchableStateSaveRequest toSend = new SwitchableStateSaveRequest();
toSend.setOn(true);
toSend.setId(0L);
when(repository.findById(toSend.getId())).thenReturn(Optional.of(state));
assertThat(controller.update(toSend).isOn()).isEqualTo(toSend.isOn());
}
@Test
public void testDelete() {
doNothing().when(repository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
}

View file

@ -0,0 +1,74 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.ArrayList;
import java.util.Optional;
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.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("AutomationService test")
public class AutomationServiceTests {
@InjectMocks private AutomationService service;
@Mock private TriggerRepository repository;
@Mock private AutomationRepository automationRepository;
@Mock private ConditionRepository conditionRepository;
@Test
public void testFindTriggerByDeviceId() {
RangeTrigger trigger1 = new RangeTrigger();
RangeTrigger trigger2 = new RangeTrigger();
RangeTrigger trigger3 = new RangeTrigger();
trigger1.setDeviceId(0L);
trigger2.setDeviceId(0L);
trigger3.setDeviceId(1L);
ArrayList<Trigger> list = new ArrayList<>();
list.add(trigger1);
list.add(trigger1);
ArrayList<Trigger<?>> toPut = new ArrayList<>();
when(repository.findAllByDeviceId(0L)).thenReturn(list);
service.findTriggersByDeviceId(0L, toPut);
assertThat(toPut.contains(trigger1));
assertThat(toPut.contains(trigger2));
}
@Test
public void testFindByVerifiedId() {
Automation automation = new Automation();
automation.setId(0L);
when(automationRepository.findById(eq(0L))).thenReturn(Optional.of(automation));
Automation returned = service.findByVerifiedId(0L);
assertThat(returned).isSameAs(automation);
}
@Test
public void testFindAllConditionByAutomationId() {
RangeCondition condition1 = new RangeCondition();
RangeCondition condition2 = new RangeCondition();
RangeCondition condition3 = new RangeCondition();
condition1.setAutomationId(1L);
condition2.setAutomationId(0L);
condition3.setAutomationId(0L);
ArrayList<Condition> list = new ArrayList<>();
list.add(condition2);
list.add(condition3);
ArrayList<Condition<?>> toPut = new ArrayList<>();
when(conditionRepository.findAllByAutomationId(0L)).thenReturn(list);
service.findAllConditionsByAutomationId(0L, toPut);
assertThat(toPut).contains(condition2);
assertThat(toPut).contains(condition3);
}
}

View file

@ -0,0 +1,43 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import static org.mockito.Mockito.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.math.BigDecimal;
import java.util.ArrayList;
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.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("DevicePopulationService test")
public class DevicePopulationServiceTests {
@InjectMocks private DevicePopulationService service;
@Mock private ThermostatPopulationService populationService;
private void helper(Thermostat t) {
t.setMeasuredTemperature(new BigDecimal(1));
}
@Test
public void testPopulateComputedFields() {
Curtains curtains = new Curtains();
RegularLight light = new RegularLight();
Thermostat t1 = new Thermostat();
RegularLight light2 = new RegularLight();
ArrayList<Device> list = new ArrayList<>();
list.add(curtains);
list.add(light);
list.add(t1);
list.add(light2);
doNothing().when(populationService).populateMeasuredTemperature(t1);
service.populateComputedFields(list);
}
}

View file

@ -0,0 +1,40 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
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.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("MotionSensorService test")
public class MotionSensorServiceTests {
@InjectMocks private MotionSensorService service;
@Mock private SensorSocketEndpoint sensorSocketEndpoint;
@Mock private DeviceService deviceService;
@Mock private MotionSensorRepository motionSensorRepository;
@Test
public void testUpdateDetectionFromMotionSensor() {
MotionSensor sensor = new MotionSensor();
sensor.setId(1L);
User user = new User();
user.setId(1L);
when(deviceService.saveAsOwner(sensor, "user")).thenReturn(sensor);
when(motionSensorRepository.findUser(sensor.getId())).thenReturn(user);
doNothing().when(sensorSocketEndpoint).queueDeviceUpdate(sensor, user, false, null, false);
MotionSensor returned = service.updateDetectionFromMotionSensor(sensor, true, "user");
assertThat(returned).isEqualTo(sensor);
assertTrue(returned.isDetected());
}
}

View file

@ -0,0 +1,46 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.math.BigDecimal;
import java.util.Optional;
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.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@DisplayName("ThermostatPopulationService test")
public class ThermostatPopulationServiceTests {
@InjectMocks private ThermostatPopulationService service;
@Mock ThermostatRepository repository;
@Test
public void testPopulateMeasuredTemperatureIf() {
Thermostat thermostat = new Thermostat();
thermostat.setRoomId(0L);
thermostat.setUseExternalSensors(true);
when(repository.getAverageTemperature(
thermostat.getRoomId(), Sensor.SensorType.TEMPERATURE))
.thenReturn(Optional.of(new BigDecimal(17)));
service.populateMeasuredTemperature(thermostat);
assertThat(thermostat.getMeasuredTemperature()).isEqualTo(new BigDecimal(17));
}
@Test
public void testPopulateMeasuredTemperatureElse() {
Thermostat thermostat = new Thermostat();
thermostat.setRoomId(0L);
thermostat.setUseExternalSensors(false);
thermostat.setInternalSensorTemperature(new BigDecimal(19));
service.populateMeasuredTemperature(thermostat);
assertThat(thermostat.getMeasuredTemperature()).isEqualTo(new BigDecimal(19));
}
}