From 7d634c073994d526a7ff0f70d85fd4e6fb8f276a Mon Sep 17 00:00:00 2001 From: omenem Date: Tue, 26 May 2020 17:59:38 +0200 Subject: [PATCH 1/3] wip --- .../InputDeviceConnectionControllerTests.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java new file mode 100644 index 0000000..1f24754 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java @@ -0,0 +1,54 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import java.util.ArrayList; +import java.util.List; +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") +public class InputDeviceConnectionControllerTests { + + @InjectMocks KnobDimmerController knobDimmerController; + + @Mock DeviceService deviceService; + + @Mock KnobDimmerRepository knobDimmerRepository; + + @Mock DeviceRepository inputRepository; + + @Mock DeviceRepository outputRepository; + + @Test + public void testConnection() throws NotFoundException { + KnobDimmer knobDimmer = new KnobDimmer(); + DimmableLight dimmableLight = new DimmableLight(); + knobDimmer.addDimmable(dimmableLight); + + when(inputRepository.findByIdAndUsername(anyLong(), anyString())) + .thenReturn(java.util.Optional.of(knobDimmer)); + + when(outputRepository.findByIdAndUsername(anyLong(), anyString())) + .thenReturn(java.util.Optional.of(dimmableLight)); + + List l = new ArrayList<>(); + l.add(10L); + + Connection toTest = knobDimmerController.addOutput(5L, l, "user"); + } +} From a84378675980f0e4e7dc81d3aa32ef287b623812 Mon Sep 17 00:00:00 2001 From: omenem Date: Wed, 27 May 2020 16:21:30 +0200 Subject: [PATCH 2/3] wip --- .../InputDeviceConnectionController.java | 2 +- .../InputDeviceConnectionControllerTests.java | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java index 4d3bdcc..90558a9 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java @@ -28,7 +28,7 @@ import org.springframework.web.bind.annotation.RequestBody; public abstract class InputDeviceConnectionController< I extends InputDevice & Connectable, O extends OutputDevice> { - private class Connection { + protected class Connection { private final I input; private final List outputs; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java index 1f24754..7ac3e91 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java @@ -1,5 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyIterable; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -10,9 +12,12 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.OutputDevice; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import java.security.Principal; import java.util.ArrayList; import java.util.List; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -34,6 +39,13 @@ public class InputDeviceConnectionControllerTests { @Mock DeviceRepository outputRepository; + @Mock private Principal mockPrincipal; + + @BeforeEach + public void setup() { + when(mockPrincipal.getName()).thenReturn("user"); + } + @Test public void testConnection() throws NotFoundException { KnobDimmer knobDimmer = new KnobDimmer(); @@ -46,9 +58,14 @@ public class InputDeviceConnectionControllerTests { when(outputRepository.findByIdAndUsername(anyLong(), anyString())) .thenReturn(java.util.Optional.of(dimmableLight)); + when(deviceService.saveAllAsOwner(anyIterable(), anyString())) + .thenReturn(List.of(new DimmableLight())); + List l = new ArrayList<>(); l.add(10L); - Connection toTest = knobDimmerController.addOutput(5L, l, "user"); + List toCheck = knobDimmerController.addLight(1L, l, mockPrincipal); + + assertEquals(l.get(0), toCheck.get(0)); } } From c3b91cd1d1b71e150e5695f81fbf55ed7135d9b8 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 27 May 2020 22:51:37 +0200 Subject: [PATCH 3/3] Fixed tests --- .../controller/ButtonDimmerController.java | 2 +- .../InputDeviceConnectionController.java | 8 +++--- .../controller/KnobDimmerController.java | 2 +- .../smarthut/controller/SwitchController.java | 2 +- .../InputDeviceConnectionControllerTests.java | 25 +++++++------------ 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 6e163a1..f7fcf12 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -29,7 +29,7 @@ public class ButtonDimmerController ButtonDimmerRepository inputRepository, DimmableRepository outputRepository, DeviceService deviceService) { - super(inputRepository, outputRepository); + super(inputRepository, outputRepository, deviceService); this.deviceService = deviceService; this.buttonDimmerRepository = inputRepository; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java index 90558a9..5fb5bfb 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java @@ -12,7 +12,6 @@ import java.security.Principal; import java.util.ArrayList; import java.util.List; import java.util.Set; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -54,7 +53,7 @@ public abstract class InputDeviceConnectionController< return outputReposiory; } - @Autowired private DeviceService deviceService; + private final DeviceService deviceService; private final DeviceRepository inputRepository; @@ -67,9 +66,12 @@ public abstract class InputDeviceConnectionController< * @param outputRepository the output device repository */ protected InputDeviceConnectionController( - DeviceRepository inputRepository, DeviceRepository outputRepository) { + DeviceRepository inputRepository, + DeviceRepository outputRepository, + DeviceService deviceService) { this.inputRepository = inputRepository; this.outputReposiory = outputRepository; + this.deviceService = deviceService; } private Connection checkConnectionIDs(Long inputId, List outputs, String username) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java index 7d46fe1..492639e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java @@ -28,7 +28,7 @@ public class KnobDimmerController extends InputDeviceConnectionController outputRepository, DeviceService deviceService) { - super(inputRepository, outputRepository); + super(inputRepository, outputRepository, deviceService); this.knobDimmerRepository = inputRepository; this.deviceService = deviceService; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java index fc3878c..92d8205 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java @@ -34,7 +34,7 @@ public class SwitchController extends InputDeviceConnectionController outputRepository, DeviceService deviceService) { - super(inputRepository, outputRepository); + super(inputRepository, outputRepository, deviceService); this.deviceService = deviceService; this.switchRepository = inputRepository; } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java index 7ac3e91..8b15a5a 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java @@ -1,18 +1,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.mockito.ArgumentMatchers.anyIterable; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.OutputDevice; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; import java.util.ArrayList; @@ -20,7 +15,6 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; 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; @@ -29,15 +23,13 @@ import org.springframework.security.test.context.support.WithMockUser; @WithMockUser(username = "user") public class InputDeviceConnectionControllerTests { - @InjectMocks KnobDimmerController knobDimmerController; + @Mock private DeviceService deviceService; - @Mock DeviceService deviceService; + @Mock private KnobDimmerRepository inputRepository; - @Mock KnobDimmerRepository knobDimmerRepository; + @Mock private DimmableRepository outputRepository; - @Mock DeviceRepository inputRepository; - - @Mock DeviceRepository outputRepository; + private KnobDimmerController knobDimmerController; @Mock private Principal mockPrincipal; @@ -64,8 +56,9 @@ public class InputDeviceConnectionControllerTests { List l = new ArrayList<>(); l.add(10L); - List toCheck = knobDimmerController.addLight(1L, l, mockPrincipal); + knobDimmerController = + new KnobDimmerController(inputRepository, outputRepository, deviceService); - assertEquals(l.get(0), toCheck.get(0)); + assertDoesNotThrow(() -> knobDimmerController.addLight(1L, l, mockPrincipal)); } }