From 76e5bed11937f7efb61347bfe1f8eec306e3e312 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Tue, 19 May 2020 18:06:48 +0200 Subject: [PATCH 1/2] Added basic tests to SceneController --- .../smarthut/dto/SceneSaveRequest.java | 4 + .../controller/SceneControllerTests.java | 99 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java index 59e1b2f..1da48f9 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java @@ -3,9 +3,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon; import javax.persistence.Column; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; @Data +@NoArgsConstructor +@AllArgsConstructor public class SceneSaveRequest { /** Room identifier */ diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java new file mode 100644 index 0000000..f9ac6ee --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneControllerTests.java @@ -0,0 +1,99 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import java.security.Principal; +import java.util.List; +import java.util.Optional; +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 SceneControllerTests { + @InjectMocks private SceneController sceneController; + + @Mock private UserRepository userRepository; + + @Mock private SceneRepository sceneRepository; + + @Mock private Principal mockPrincipal; + + private final User u; + + public SceneControllerTests() { + u = new User(); + u.setName("user"); + u.setId(1L); + } + + @Test + public void testGetAll() throws NotFoundException { + when(mockPrincipal.getName()).thenReturn("user"); + when(userRepository.findByUsername("user")).thenReturn(u); + when(sceneRepository.findByUsername("user")).thenReturn(List.of()); + assertThat(sceneController.findAll(mockPrincipal, null)).isEmpty(); + } + + private void equalToRequest(Scene created, SceneSaveRequest a) { + assertThat(created.getName()).isEqualTo(a.getName()); + assertThat(created.getUserId()).isEqualTo(1L); + assertThat(created.getIcon()).isEqualTo(a.getIcon()); + assertThat(created.isGuestAccessEnabled()).isEqualTo(a.isGuestAccessEnabled()); + } + + @Test + public void testCreate() { + when(mockPrincipal.getName()).thenReturn("user"); + when(userRepository.findByUsername("user")).thenReturn(u); + when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]); + + SceneSaveRequest s = new SceneSaveRequest(0, "New Scene", Icon.BATH, true); + Scene created = sceneController.create(s, mockPrincipal); + assertThat(created.getId()).isEqualTo(0L); + equalToRequest(created, s); + } + + @Test + public void testUpdate() throws NotFoundException { + when(mockPrincipal.getName()).thenReturn("user"); + when(userRepository.findByUsername("user")).thenReturn(u); + final Scene old = new Scene(); + old.setId(42L); + old.setName("Old Name"); + + when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]); + when(sceneRepository.findById(42L)).thenReturn(Optional.of(old)); + when(sceneRepository.findById(43L)).thenReturn(Optional.empty()); + + SceneSaveRequest a = new SceneSaveRequest(0, "New Scene", Icon.BATH, true); + + Scene created = sceneController.update(a.getId(), a, mockPrincipal); + assertThat(created.getId()).isEqualTo(42L); + equalToRequest(created, a); + + a.setId(43L); + assertThatThrownBy(() -> sceneController.update(a.getId(), a, mockPrincipal)) + .isInstanceOf(NotFoundException.class); + } + + @Test + public void testDelete() { + final Automation old = new Automation(); + old.setId(42L); + old.setName("Old Name"); + doNothing().when(sceneRepository).deleteById(42L); + sceneController.deleteById(42L); + } +} From 6b3886ccb1f85c699310755e0342fcfc9bf1286c Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Tue, 19 May 2020 18:38:31 +0200 Subject: [PATCH 2/2] Added basic tests to SmartPlugController --- build.gradle | 2 +- .../controller/ButtonDimmerController.java | 4 +- .../controller/KnobDimmerController.java | 4 +- .../controller/MotionSensorController.java | 4 +- .../controller/SmartPlugController.java | 1 - .../smarthut/controller/SwitchController.java | 4 +- ...est.java => GenericDeviceSaveRequest.java} | 2 +- .../smarthut/dto/SwitchableSaveRequest.java | 4 + .../smarthut/SceneSaveRequestTests.java | 2 +- .../smarthut/SwitchableSaveRequestTests.java | 8 +- .../controller/SceneControllerTests.java | 15 ++- .../controller/SmartPlugControllerTests.java | 93 +++++++++++++++++++ 12 files changed, 119 insertions(+), 24 deletions(-) rename src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/{GenericDeviceSaveReguest.java => GenericDeviceSaveRequest.java} (91%) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java diff --git a/build.gradle b/build.gradle index a6f73d3..a933dc9 100644 --- a/build.gradle +++ b/build.gradle @@ -45,7 +45,7 @@ dependencies { gradle.projectsEvaluated { tasks.withType(JavaCompile) { - options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" + options.compilerArgs << "-Xlint:deprecation" } } 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 8e3b313..5e3966c 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 @@ -1,7 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerDimRequest; -import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository; @@ -41,7 +41,7 @@ public class ButtonDimmerController @PostMapping public ButtonDimmer create( - @Valid @RequestBody final GenericDeviceSaveReguest bd, final Principal principal) + @Valid @RequestBody final GenericDeviceSaveRequest bd, final Principal principal) throws NotFoundException { deviceService.throwIfRoomNotOwned(bd.getRoomId(), principal.getName()); 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 c91144e..266713d 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 @@ -1,6 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; -import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerDimRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; @@ -40,7 +40,7 @@ public class KnobDimmerController extends InputDeviceConnectionController stateStateRepository; + private final User u; public SceneControllerTests() { @@ -41,7 +43,6 @@ public class SceneControllerTests { @Test public void testGetAll() throws NotFoundException { when(mockPrincipal.getName()).thenReturn("user"); - when(userRepository.findByUsername("user")).thenReturn(u); when(sceneRepository.findByUsername("user")).thenReturn(List.of()); assertThat(sceneController.findAll(mockPrincipal, null)).isEmpty(); } @@ -68,16 +69,16 @@ public class SceneControllerTests { @Test public void testUpdate() throws NotFoundException { when(mockPrincipal.getName()).thenReturn("user"); - when(userRepository.findByUsername("user")).thenReturn(u); final Scene old = new Scene(); old.setId(42L); + old.setUserId(1L); old.setName("Old Name"); when(sceneRepository.save(any())).thenAnswer(i -> i.getArguments()[0]); - when(sceneRepository.findById(42L)).thenReturn(Optional.of(old)); - when(sceneRepository.findById(43L)).thenReturn(Optional.empty()); + when(sceneRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(old)); + when(sceneRepository.findByIdAndUsername(43L, "user")).thenReturn(Optional.empty()); - SceneSaveRequest a = new SceneSaveRequest(0, "New Scene", Icon.BATH, true); + SceneSaveRequest a = new SceneSaveRequest(42L, "New Scene", Icon.BATH, true); Scene created = sceneController.update(a.getId(), a, mockPrincipal); assertThat(created.getId()).isEqualTo(42L); @@ -90,9 +91,7 @@ public class SceneControllerTests { @Test public void testDelete() { - final Automation old = new Automation(); - old.setId(42L); - old.setName("Old Name"); + doNothing().when(stateStateRepository).deleteAllBySceneId(42L); doNothing().when(sceneRepository).deleteById(42L); sceneController.deleteById(42L); } 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 new file mode 100644 index 0000000..ddb4cc8 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugControllerTests.java @@ -0,0 +1,93 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +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.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import java.security.Principal; +import java.util.Optional; +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 SmartPlugControllerTests { + @InjectMocks private SmartPlugController smartPlugController; + + @Mock private UserRepository userRepository; + + @Mock private SmartPlugRepository smartPlugRepository; + + @Mock private Principal mockPrincipal; + + @Mock private DeviceService deviceService; + + private final User u; + + public SmartPlugControllerTests() { + u = new User(); + u.setName("user"); + u.setId(1L); + } + + private void equalToRequest(Switchable created, SwitchableSaveRequest a) { + assertThat(created.getName()).isEqualTo(a.getName()); + assertThat(created.getRoomId()).isEqualTo(30L); + assertThat(created.isOn()).isEqualTo(a.isOn()); + } + + @Test + public void testCreate() { + when(mockPrincipal.getName()).thenReturn("user"); + when(deviceService.saveAsOwner(any(), eq("user"))).thenAnswer(i -> i.getArguments()[0]); + + SwitchableSaveRequest a = new SwitchableSaveRequest(true, 1L, 30L, "New SmartPlug"); + try { + Switchable created = smartPlugController.create(a, mockPrincipal); + assertThat(created.getId()).isEqualTo(0L); + equalToRequest(created, a); + } catch (NotFoundException e) { + fail(e.getMessage()); + } + } + + @Test + public void testUpdate() throws NotFoundException { + when(mockPrincipal.getName()).thenReturn("user"); + final SmartPlug old = new SmartPlug(); + old.setId(42L); + + when(deviceService.saveAsOwner(any(), eq("user"))).thenAnswer(i -> i.getArguments()[0]); + when(smartPlugRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(old)); + when(smartPlugRepository.findByIdAndUsername(43L, "user")).thenReturn(Optional.empty()); + + SwitchableSaveRequest a = new SwitchableSaveRequest(true, 42L, 30L, "New SmartPlug"); + + SmartPlug created = smartPlugController.update(a, mockPrincipal); + assertThat(created.getId()).isEqualTo(42L); + equalToRequest(created, a); + + a.setId(43L); + assertThatThrownBy(() -> smartPlugController.update(a, mockPrincipal)) + .isInstanceOf(NotFoundException.class); + } + + @Test + public void testDelete() throws NotFoundException { + when(mockPrincipal.getName()).thenReturn("user"); + doNothing().when(deviceService).deleteByIdAsOwner(42L, "user"); + smartPlugController.deleteById(42L, mockPrincipal); + } +}