From 4250f07fef384b090ee03dab527e25f927eec35d Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Mon, 25 May 2020 15:47:19 +0200 Subject: [PATCH 1/6] added DimmableStateControllerTests --- .../DimmableStateControllerTests.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateControllerTests.java new file mode 100644 index 0000000..2e90862 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateControllerTests.java @@ -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)); + } +} From a4ca37e6ec94f318037a9f8389e3b0079e4c4bcc Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Mon, 25 May 2020 15:55:39 +0200 Subject: [PATCH 2/6] added SwitchableStateControllerTests --- .../SwitchableStateControllerTests.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateControllerTests.java new file mode 100644 index 0000000..384d933 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateControllerTests.java @@ -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)); + } +} From 82900b1d02defcd3c397a2b8bc2665d15ed75316 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Mon, 25 May 2020 16:19:27 +0200 Subject: [PATCH 3/6] added AutomationServiceTests --- .../service/AutomationServiceTests.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java new file mode 100644 index 0000000..2cf74db --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java @@ -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("SwitchableState controller 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 list = new ArrayList<>(); + list.add(trigger1); + list.add(trigger1); + ArrayList> 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 list = new ArrayList<>(); + list.add(condition2); + list.add(condition3); + ArrayList> toPut = new ArrayList<>(); + when(conditionRepository.findAllByAutomationId(0L)).thenReturn(list); + service.findAllConditionsByAutomationId(0L, toPut); + assertThat(toPut).contains(condition2); + assertThat(toPut).contains(condition3); + } +} From e7def27d2267777be9b89dc95dcf2cc8a7b5894c Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Mon, 25 May 2020 16:30:31 +0200 Subject: [PATCH 4/6] added ThermostatPopulationServiceTests --- .../service/AutomationServiceTests.java | 2 +- .../ThermostatPopulationServiceTests.java | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationServiceTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java index 2cf74db..6d1850f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java @@ -17,7 +17,7 @@ import org.springframework.security.test.context.support.WithMockUser; @ExtendWith(MockitoExtension.class) @WithMockUser(username = "user") -@DisplayName("SwitchableState controller test") +@DisplayName("AutomationService test") public class AutomationServiceTests { @InjectMocks private AutomationService service; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationServiceTests.java new file mode 100644 index 0000000..a48b59b --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationServiceTests.java @@ -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)); + } +} From cffeeca9f845357daae6670a57b981bde124bf06 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Mon, 25 May 2020 16:47:06 +0200 Subject: [PATCH 5/6] added DevicePopulationServiceTests --- .../service/DevicePopulationServiceTests.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationServiceTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationServiceTests.java new file mode 100644 index 0000000..693d11b --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationServiceTests.java @@ -0,0 +1,42 @@ +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 list = new ArrayList<>(); + list.add(curtains); + list.add(light); + list.add(t1); + list.add(light2); + doNothing().when(populationService).populateMeasuredTemperature(t1); + service.populateComputedFields(list); + } +} From 49707917925641e0431edb1158a2ce2654c3fdb1 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Mon, 25 May 2020 17:31:48 +0200 Subject: [PATCH 6/6] added MotionSensorService tests --- .../service/MotionSensorServiceTests.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorServiceTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorServiceTests.java new file mode 100644 index 0000000..12c5451 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/MotionSensorServiceTests.java @@ -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()); + } +}