From 86ca38cd47595b755e1425943191378568025a4e Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Sun, 24 May 2020 17:42:53 +0200 Subject: [PATCH] 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); + } }