From 2f52be137f7cec81030e4d08f9f65a5b38d258a8 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Thu, 21 May 2020 15:47:13 +0200 Subject: [PATCH 1/2] it took me a lot of time but i managed to make to KnobDimmer tests, now that I kinda understand mockito the next tests should take considerably less time --- .../InputDeviceConnectionControllerTests.java | 7 ++ .../controller/KnobDimmerControllerTests.java | 96 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerControllerTests.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..cab98e9 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java @@ -0,0 +1,7 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class InputDeviceConnectionControllerTests {} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerControllerTests.java new file mode 100644 index 0000000..d83af6a --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerControllerTests.java @@ -0,0 +1,96 @@ +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.*; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +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.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import java.security.Principal; +import java.util.Optional; +import java.util.Set; +import lombok.SneakyThrows; +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") +@DisplayName("KnobDimmer controller test") +public class KnobDimmerControllerTests { + @InjectMocks private KnobDimmerController controller; + + @Mock private Principal principal; + + @Mock private DeviceService deviceService; + @Mock private KnobDimmerRepository knobDimmerRepository; + + @Test + public void testGetId() throws NotFoundException { + KnobDimmer dimmer = new KnobDimmer(); + dimmer.setId(42l); + when(knobDimmerRepository.findById(42L)).thenReturn(Optional.of(dimmer)); + when(knobDimmerRepository.findById(1L)).thenReturn(Optional.empty()); + assertThat(controller.findById(42L)).isSameAs(dimmer); + assertThatThrownBy(() -> controller.findById(1L)).isInstanceOf(NotFoundException.class); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testCreate() { + when(principal.getName()).thenReturn("user"); + doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user")); + when(deviceService.saveAsOwner(any(KnobDimmer.class), eq("user"))) + .thenAnswer(i -> i.getArguments()[0]); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + GenericDeviceSaveRequest toSend = new GenericDeviceSaveRequest(); + toSend.setName("dimmer"); + toSend.setRoomId(42L); + KnobDimmer dimmer = controller.create(toSend, principal); + assertThat(dimmer.getName()).isEqualTo(toSend.getName()); + assertThat(dimmer.getRoomId()).isEqualTo(toSend.getRoomId()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testDelete() { + when(principal.getName()).thenReturn("user"); + doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + controller.delete(42L, principal); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testDimTo() { + when(principal.getName()).thenReturn("user"); + KnobDimmer dimmer = new KnobDimmer(); + DimmableLight light = new DimmableLight(); + Curtains curtains = new Curtains(); + dimmer.addDimmable(light); + dimmer.addDimmable(curtains); + when(knobDimmerRepository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(dimmer)); + KnobDimmerDimRequest toSend = new KnobDimmerDimRequest(); + toSend.setIntensity(12); + toSend.setId(42L); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + Set set = controller.dimTo(toSend, principal); + assertThat(set.size()).isEqualTo(2); + } +} From 859a9fb53e77de6a47ec029bc646534d3793f47e Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Thu, 21 May 2020 20:53:36 +0200 Subject: [PATCH 2/2] made 3 tests --- .../ButtonDimmerControllerTests.java | 116 +++++++++++++ .../InputDeviceConnectionControllerTests.java | 7 - .../controller/KnobDimmerControllerTests.java | 5 +- .../controller/SwitchControllerTests.java | 157 ++++++++++++++++++ 4 files changed, 276 insertions(+), 9 deletions(-) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerControllerTests.java delete mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchControllerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerControllerTests.java new file mode 100644 index 0000000..d3fb238 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerControllerTests.java @@ -0,0 +1,116 @@ +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.*; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerDimRequest; +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.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import java.security.Principal; +import java.util.Optional; +import java.util.Set; +import lombok.SneakyThrows; +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") +@DisplayName("KnobDimmer controller test") +public class ButtonDimmerControllerTests { + @InjectMocks private ButtonDimmerController controller; + + @Mock private DeviceService service; + + @Mock private ButtonDimmerRepository repository; + + @Mock Principal principal; + + @Test + public void testGetId() throws NotFoundException { + ButtonDimmer dimmer = new ButtonDimmer(); + dimmer.setId(42L); + when(repository.findById(42L)).thenReturn(Optional.of(dimmer)); + when(repository.findById(1L)).thenReturn(Optional.empty()); + assertThat(controller.findById(42L)).isSameAs(dimmer); + assertThatThrownBy(() -> controller.findById(1L)).isInstanceOf(NotFoundException.class); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testCreate() { + when(principal.getName()).thenReturn("user"); + doNothing().when(service).throwIfRoomNotOwned(anyLong(), eq("user")); + when(service.saveAsOwner(any(ButtonDimmer.class), eq("user"))) + .thenAnswer(i -> i.getArguments()[0]); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + GenericDeviceSaveRequest toSend = new GenericDeviceSaveRequest(); + toSend.setName("dimmer"); + toSend.setRoomId(42L); + ButtonDimmer dimmer = controller.create(toSend, principal); + assertThat(dimmer.getName()).isEqualTo(toSend.getName()); + assertThat(dimmer.getRoomId()).isEqualTo(toSend.getRoomId()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testDelete() { + when(principal.getName()).thenReturn("user"); + doNothing().when(service).deleteByIdAsOwner(eq(42L), eq("user")); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + controller.delete(42L, principal); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testDimUp() { + when(principal.getName()).thenReturn("user"); + ButtonDimmer button = new ButtonDimmer(); + Curtains curtains = new Curtains(); + DimmableLight light = new DimmableLight(); + button.addDimmable(curtains); + button.addDimmable(light); + ButtonDimmerDimRequest toSend = new ButtonDimmerDimRequest(); + toSend.setId(42L); + toSend.setDimType(ButtonDimmerDimRequest.DimType.UP); + when(repository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(button)); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + Set set = controller.dim(toSend, principal); + assertThat(set.size()).isEqualTo(button.getOutputs().size()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testDimDown() { + when(principal.getName()).thenReturn("user"); + ButtonDimmer button = new ButtonDimmer(); + Curtains curtains = new Curtains(); + DimmableLight light = new DimmableLight(); + button.addDimmable(curtains); + button.addDimmable(light); + ButtonDimmerDimRequest toSend = new ButtonDimmerDimRequest(); + toSend.setId(42L); + toSend.setDimType(ButtonDimmerDimRequest.DimType.DOWN); + when(repository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(button)); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + Set set = controller.dim(toSend, principal); + assertThat(set.size()).isEqualTo(button.getOutputs().size()); + } +} 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 deleted file mode 100644 index cab98e9..0000000 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionControllerTests.java +++ /dev/null @@ -1,7 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; - -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -public class InputDeviceConnectionControllerTests {} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerControllerTests.java index d83af6a..3e17d48 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerControllerTests.java @@ -35,12 +35,13 @@ public class KnobDimmerControllerTests { @Mock private Principal principal; @Mock private DeviceService deviceService; + @Mock private KnobDimmerRepository knobDimmerRepository; @Test public void testGetId() throws NotFoundException { KnobDimmer dimmer = new KnobDimmer(); - dimmer.setId(42l); + dimmer.setId(42L); when(knobDimmerRepository.findById(42L)).thenReturn(Optional.of(dimmer)); when(knobDimmerRepository.findById(1L)).thenReturn(Optional.empty()); assertThat(controller.findById(42L)).isSameAs(dimmer); @@ -91,6 +92,6 @@ public class KnobDimmerControllerTests { MockHttpServletRequest request = new MockHttpServletRequest(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); Set set = controller.dimTo(toSend, principal); - assertThat(set.size()).isEqualTo(2); + assertThat(set.size()).isEqualTo(dimmer.getOutputs().size()); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchControllerTests.java new file mode 100644 index 0000000..ea8378a --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchControllerTests.java @@ -0,0 +1,157 @@ +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.*; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchOperationRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Switch; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Switchable; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; +import java.security.Principal; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import lombok.SneakyThrows; +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") +@DisplayName("KnobDimmer controller test") +public class SwitchControllerTests { + @InjectMocks private SwitchController controller; + + @Mock private DeviceService deviceService; + + @Mock private SwitchRepository repository; + + @Mock private Principal principal; + + @Test + public void testGetId() throws NotFoundException { + Switch aSwitch = new Switch(); + aSwitch.setId(42L); + when(repository.findById(42L)).thenReturn(Optional.of(aSwitch)); + when(repository.findById(1L)).thenReturn(Optional.empty()); + assertThat(controller.findById(42L)).isSameAs(aSwitch); + assertThatThrownBy(() -> controller.findById(1L)).isInstanceOf(NotFoundException.class); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testCreate() { + when(principal.getName()).thenReturn("user"); + doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user")); + when(deviceService.saveAsOwner(any(Switch.class), eq("user"))) + .thenAnswer(i -> i.getArguments()[0]); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + GenericDeviceSaveRequest toSend = new GenericDeviceSaveRequest(); + toSend.setName("dimmer"); + toSend.setRoomId(42L); + Switch aSwitch = controller.create(toSend, principal); + assertThat(aSwitch.getName()).isEqualTo(toSend.getName()); + assertThat(aSwitch.getRoomId()).isEqualTo(toSend.getRoomId()); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testDelete() { + when(principal.getName()).thenReturn("user"); + doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user")); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + controller.deleteById(42L, principal); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testOperate() { + when(principal.getName()).thenReturn("user"); + Switch aSwitch = new Switch(); + aSwitch.setOn(true); + RegularLight light = new RegularLight(); + RegularLight light2 = new RegularLight(); + aSwitch.connect(light, true); + aSwitch.connect(light2, true); + SwitchOperationRequest toSend = new SwitchOperationRequest(); + toSend.setType(SwitchOperationRequest.OperationType.ON); + toSend.setId(42L); + when(repository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(aSwitch)); + ArrayList helper = new ArrayList(); + helper.add(light); + helper.add(light2); + when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName())) + .thenReturn(helper); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + List list = controller.operate(toSend, principal); + assertThat(list.size()).isEqualTo(2); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testOperateOFF() { + when(principal.getName()).thenReturn("user"); + Switch aSwitch = new Switch(); + aSwitch.setOn(true); + RegularLight light = new RegularLight(); + RegularLight light2 = new RegularLight(); + aSwitch.connect(light, true); + aSwitch.connect(light2, true); + SwitchOperationRequest toSend = new SwitchOperationRequest(); + toSend.setType(SwitchOperationRequest.OperationType.OFF); + toSend.setId(42L); + when(repository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(aSwitch)); + ArrayList helper = new ArrayList(); + helper.add(light); + helper.add(light2); + when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName())) + .thenReturn(helper); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + List list = controller.operate(toSend, principal); + assertThat(list.size()).isEqualTo(2); + } + + @Test + @SneakyThrows(NotFoundException.class) + public void testOperateToggle() { + when(principal.getName()).thenReturn("user"); + Switch aSwitch = new Switch(); + aSwitch.setOn(true); + RegularLight light = new RegularLight(); + RegularLight light2 = new RegularLight(); + aSwitch.connect(light, true); + aSwitch.connect(light2, true); + SwitchOperationRequest toSend = new SwitchOperationRequest(); + toSend.setType(SwitchOperationRequest.OperationType.TOGGLE); + toSend.setId(42L); + when(repository.findByIdAndUsername(42L, "user")).thenReturn(Optional.of(aSwitch)); + ArrayList helper = new ArrayList(); + helper.add(light); + helper.add(light2); + when(deviceService.saveAllAsOwner(aSwitch.getOutputs(), principal.getName())) + .thenReturn(helper); + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + List list = controller.operate(toSend, principal); + assertThat(list.size()).isEqualTo(2); + } +}