From 7519212ddc9409a8abc85cd0e47604b506a378c3 Mon Sep 17 00:00:00 2001 From: omenem Date: Thu, 21 May 2020 11:10:12 +0200 Subject: [PATCH] Test for ThermostatController --- .../controller/SensorControllerTests.java | 5 +- .../controller/ThermostatControllerTests.java | 131 ++++++++++++++++++ 2 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java index 56b15b5..a166335 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java @@ -12,6 +12,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.math.BigDecimal; import java.security.Principal; import lombok.SneakyThrows; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -66,7 +67,7 @@ public class SensorControllerTests { checkSensorAgainstRequest(created, toSend); } - @DisplayName("when deleting an existant id should succeed") + @DisplayName("when deleting an existing id should succeed") @Test @SneakyThrows(NotFoundException.class) public void testDelete() { @@ -75,6 +76,6 @@ public class SensorControllerTests { MockHttpServletRequest request = new MockHttpServletRequest(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); - sensorController.deleteById(42L, mockPrincipal); + Assertions.assertDoesNotThrow(() -> sensorController.deleteById(42L, mockPrincipal)); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatControllerTests.java new file mode 100644 index 0000000..db66ddd --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatControllerTests.java @@ -0,0 +1,131 @@ +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.anyLong; +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.ThermostatSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatPopulationService; +import java.math.BigDecimal; +import java.security.Principal; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +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") +public class ThermostatControllerTests { + + @InjectMocks private ThermostatController thermostatController; + + @Mock private StateRepository stateRepository; + @Mock private SceneRepository sceneRepository; + @Mock private ThermostatPopulationService thermostatService; + @Mock private ThermostatRepository thermostatRepository; + @Mock private DeviceService deviceService; + @Mock private Principal mockPrincipal; + + @BeforeEach + public void setup() { + when(mockPrincipal.getName()).thenReturn("user"); + } + + private void checkThermostatAgainstRequest( + final Thermostat toCheck, final ThermostatSaveRequest request) { + assertThat(toCheck).isNotNull(); + assertThat(toCheck.isOn()).isEqualTo(request.isTurnOn()); + assertThat(toCheck.getTargetTemperature()).isEqualTo(request.getTargetTemperature()); + assertThat(toCheck.isUseExternalSensors()).isEqualTo(request.isUseExternalSensors()); + assertThat(toCheck.getName()).isEqualTo(request.getName()); + assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId()); + } + + @Test + @DisplayName("when creating should return the same object") + @SneakyThrows(NotFoundException.class) + public void testCreate() { + doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user")); + doNothing().when(thermostatService).populateMeasuredTemperature(any(Thermostat.class)); + when(deviceService.saveAsOwner(any(Thermostat.class), eq("user"))) + .thenAnswer(i -> i.getArguments()[0]); + when(thermostatRepository.save(any(Thermostat.class))).thenAnswer(i -> i.getArguments()[0]); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + final ThermostatSaveRequest toSend = new ThermostatSaveRequest(); + toSend.setRoomId(42L); + toSend.setTargetTemperature(new BigDecimal(40)); + toSend.setName("Thermostat Test"); + toSend.setUseExternalSensors(true); + + final Thermostat thermostat = thermostatController.create(toSend, mockPrincipal); + + checkThermostatAgainstRequest(thermostat, toSend); + } + + @Test + @DisplayName("when updating should return the same object") + @SneakyThrows(NotFoundException.class) + public void testUpdate() { + + final ThermostatSaveRequest toSend = new ThermostatSaveRequest(); + toSend.setRoomId(42L); + toSend.setTargetTemperature(new BigDecimal(40)); + toSend.setName("Thermostat Test"); + toSend.setUseExternalSensors(true); + + final Thermostat toUpdate = new Thermostat(); + toSend.setRoomId(20L); + toSend.setTargetTemperature(new BigDecimal(50)); + toSend.setName("Thermostat"); + toSend.setUseExternalSensors(false); + + when(thermostatRepository.findByIdAndUsername(anyLong(), any(String.class))) + .thenReturn(java.util.Optional.of(toUpdate)); + when(thermostatRepository.save(any(Thermostat.class))).thenAnswer(i -> i.getArguments()[0]); + doNothing().when(thermostatService).populateMeasuredTemperature(any(Thermostat.class)); + when(deviceService.saveAsOwner(any(Thermostat.class), eq("user"))) + .thenAnswer(i -> i.getArguments()[0]); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + final Thermostat thermostat = thermostatController.update(toSend, mockPrincipal); + + checkThermostatAgainstRequest(thermostat, toSend); + } + + @Test + @DisplayName("an existing id should succeed") + @SneakyThrows(NotFoundException.class) + public void testDelete() { + + doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + Assertions.assertDoesNotThrow(() -> thermostatController.deleteById(42L, mockPrincipal)); + } +}