From 86ca38cd47595b755e1425943191378568025a4e Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Sun, 24 May 2020 17:42:53 +0200 Subject: [PATCH 1/3] added a couple of tests to smartPlugControllerTests --- .../controller/SmartPlugControllerTests.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java index 3907f3d..86a5c7f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java @@ -9,11 +9,14 @@ import static org.mockito.Mockito.when; import static org.springframework.test.util.AssertionErrors.fail; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import java.math.BigDecimal; import java.security.Principal; import java.util.Optional; +import lombok.SneakyThrows; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -35,6 +38,10 @@ public class SmartPlugControllerTests { @Mock private DeviceService deviceService; + @Mock private SceneRepository sceneRepository; + + @Mock private StateRepository stateRepository; + private final User u; public SmartPlugControllerTests() { @@ -91,4 +98,51 @@ public class SmartPlugControllerTests { doNothing().when(deviceService).deleteByIdAsOwner(42L, "user"); Assertions.assertDoesNotThrow(() -> smartPlugController.deleteById(42L, mockPrincipal)); } + + @Test + @SneakyThrows(NotFoundException.class) + public void testResetMeter() { + when(mockPrincipal.getName()).thenReturn("user"); + SmartPlug s = new SmartPlug(); + s.setId(24L); + when(smartPlugRepository.findByIdAndUsername(24L, "user")).thenReturn(Optional.of(s)); + s.resetTotalConsumption(); + when(smartPlugRepository.save(s)).thenReturn(s); + SmartPlug toCheck = smartPlugController.resetMeter(24L, mockPrincipal); + assertThat(toCheck.getTotalConsumption()).isEqualTo(new BigDecimal(0)); + } + + @Test + public void testSceneBinding() { + when(mockPrincipal.getName()).thenReturn("user"); + SmartPlug smartPlug = new SmartPlug(); + when(smartPlugRepository.findByIdAndUsername(24L, "user")) + .thenReturn(Optional.of(smartPlug)); + Scene scene = new Scene(); + scene.setId(1L); + SwitchableState state = new SwitchableState(); + state.setSceneId(1L); + State s = smartPlug.cloneState(); + when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene)); + when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0); + Assertions.assertDoesNotThrow( + () -> smartPlugController.sceneBinding(24L, 1L, mockPrincipal)); + } + + @Test + public void testSceneBinding2() { + when(mockPrincipal.getName()).thenReturn("user"); + SmartPlug smartPlug = new SmartPlug(); + when(smartPlugRepository.findByIdAndUsername(24L, "user")) + .thenReturn(Optional.of(smartPlug)); + Scene scene = new Scene(); + scene.setId(1L); + SwitchableState state = new SwitchableState(); + state.setSceneId(1L); + State s = smartPlug.cloneState(); + when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene)); + when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2); + assertThatThrownBy(() -> smartPlugController.sceneBinding(24L, 1L, mockPrincipal)) + .isInstanceOf(DuplicateStateException.class); + } } From d25bcf350764418960ecc6c87020cf071cf3aea2 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Sun, 24 May 2020 18:11:22 +0200 Subject: [PATCH 2/3] added RangeConditionControllerTests --- .../RangeConditionControllerTests.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionControllerTests.java new file mode 100644 index 0000000..563636c --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionControllerTests.java @@ -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.RangeConditionOrTriggerSaveRequest; +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("RangeConditionController tests") +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +public class RangeConditionControllerTests { + @InjectMocks private RangeConditionController controller; + + @Mock private RangeConditionRepository repository; + + @Mock private Principal principal; + + @Test + public void testCreate() { + RangeCondition condition = new RangeCondition(); + condition.setRange(40L); + condition.setDeviceId(1L); + condition.setAutomationId(2L); + condition.setOperator(Operator.EQUAL); + when(repository.save(condition)).thenReturn(condition); + RangeConditionOrTriggerSaveRequest toSend = new RangeConditionOrTriggerSaveRequest(); + toSend.setAutomationId(2L); + toSend.setDeviceId(1L); + toSend.setOperator(Operator.EQUAL); + toSend.setRange(40L); + RangeCondition returned = controller.create(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()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testUpdate() { + RangeCondition condition = new RangeCondition(); + condition.setRange(40L); + condition.setDeviceId(1L); + condition.setAutomationId(2L); + condition.setOperator(Operator.EQUAL); + when(repository.save(condition)).thenReturn(condition); + RangeConditionOrTriggerSaveRequest toSend = new RangeConditionOrTriggerSaveRequest(); + toSend.setId(34L); + toSend.setAutomationId(2L); + toSend.setDeviceId(1L); + toSend.setOperator(Operator.EQUAL); + toSend.setRange(40L); + when(repository.findById(34L)).thenReturn(Optional.of(condition)); + RangeCondition 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()); + } + + @Test + public void testGetAll() { + RangeCondition condition1 = new RangeCondition(); + condition1.setAutomationId(1L); + RangeCondition condition2 = new RangeCondition(); + condition2.setAutomationId(1L); + RangeCondition condition3 = new RangeCondition(); + condition3.setAutomationId(1L); + ArrayList list = new ArrayList<>(); + list.add(condition1); + list.add(condition2); + list.add(condition3); + when(repository.findAllByAutomationId(42L)).thenReturn(list); + List 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)); + } +} From 7b05718286c9d8ada571fccce110bb2d81651c93 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Sun, 24 May 2020 22:23:07 +0200 Subject: [PATCH 3/3] added ThermostatConditionControllerTests --- .../ThermostatConditionControllerTests.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatConditionControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatConditionControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatConditionControllerTests.java new file mode 100644 index 0000000..2210ce4 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatConditionControllerTests.java @@ -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 list = new ArrayList<>(); + list.add(condition1); + list.add(condition2); + list.add(condition3); + when(repository.findAllByAutomationId(42L)).thenReturn(list); + List 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)); + } +}