made 3 tests

This commit is contained in:
Jacob Salvi 2020-05-21 20:53:36 +02:00
parent 2f52be137f
commit 859a9fb53e
4 changed files with 276 additions and 9 deletions

View file

@ -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<Dimmable> 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<Dimmable> set = controller.dim(toSend, principal);
assertThat(set.size()).isEqualTo(button.getOutputs().size());
}
}

View file

@ -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 {}

View file

@ -35,12 +35,13 @@ public class KnobDimmerControllerTests {
@Mock private Principal principal; @Mock private Principal principal;
@Mock private DeviceService deviceService; @Mock private DeviceService deviceService;
@Mock private KnobDimmerRepository knobDimmerRepository; @Mock private KnobDimmerRepository knobDimmerRepository;
@Test @Test
public void testGetId() throws NotFoundException { public void testGetId() throws NotFoundException {
KnobDimmer dimmer = new KnobDimmer(); KnobDimmer dimmer = new KnobDimmer();
dimmer.setId(42l); dimmer.setId(42L);
when(knobDimmerRepository.findById(42L)).thenReturn(Optional.of(dimmer)); when(knobDimmerRepository.findById(42L)).thenReturn(Optional.of(dimmer));
when(knobDimmerRepository.findById(1L)).thenReturn(Optional.empty()); when(knobDimmerRepository.findById(1L)).thenReturn(Optional.empty());
assertThat(controller.findById(42L)).isSameAs(dimmer); assertThat(controller.findById(42L)).isSameAs(dimmer);
@ -91,6 +92,6 @@ public class KnobDimmerControllerTests {
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Set<Dimmable> set = controller.dimTo(toSend, principal); Set<Dimmable> set = controller.dimTo(toSend, principal);
assertThat(set.size()).isEqualTo(2); assertThat(set.size()).isEqualTo(dimmer.getOutputs().size());
} }
} }

View file

@ -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<Switchable> helper = new ArrayList<Switchable>();
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<Switchable> 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<Switchable> helper = new ArrayList<Switchable>();
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<Switchable> 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<Switchable> helper = new ArrayList<Switchable>();
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<Switchable> list = controller.operate(toSend, principal);
assertThat(list.size()).isEqualTo(2);
}
}