From 859a9fb53e77de6a47ec029bc646534d3793f47e Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Thu, 21 May 2020 20:53:36 +0200 Subject: [PATCH] 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); + } +}