Merge branch '80-regularlightcontroller-tests' into 'dev'
Resolve "RegularLightController Tests" Closes #80 See merge request sa4-2020/the-sanmarinoes/backend!176
This commit is contained in:
commit
12c65ed12f
3 changed files with 167 additions and 2 deletions
|
@ -18,9 +18,15 @@ import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
@Component
|
@Component
|
||||||
public class JWTRequestFilter extends OncePerRequestFilter {
|
public class JWTRequestFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
@Autowired private JWTUserDetailsService jwtUserDetailsService;
|
private final JWTUserDetailsService jwtUserDetailsService;
|
||||||
|
private final JWTTokenUtils jwtTokenUtils;
|
||||||
|
|
||||||
@Autowired private JWTTokenUtils jwtTokenUtils;
|
@Autowired
|
||||||
|
public JWTRequestFilter(
|
||||||
|
JWTUserDetailsService jwtUserDetailsService, JWTTokenUtils jwtTokenUtils) {
|
||||||
|
this.jwtUserDetailsService = jwtUserDetailsService;
|
||||||
|
this.jwtTokenUtils = jwtTokenUtils;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(
|
protected void doFilterInternal(
|
||||||
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
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 lombok.SneakyThrows;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
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")
|
||||||
|
public class RegularLightControllerTests {
|
||||||
|
|
||||||
|
@InjectMocks private RegularLightController regularLightController;
|
||||||
|
|
||||||
|
@Mock private RegularLightRepository regularLightRepository;
|
||||||
|
|
||||||
|
@Mock private SceneRepository sceneRepository;
|
||||||
|
|
||||||
|
@Mock private StateRepository<State> stateRepository;
|
||||||
|
|
||||||
|
@Mock private DeviceService deviceService;
|
||||||
|
|
||||||
|
@Mock private Principal mockPrincipal;
|
||||||
|
|
||||||
|
@Mock private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Mock private DeviceRepository<RegularLight> deviceRepository;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkRegularLightAgainstRequest(
|
||||||
|
final RegularLight toCheck, final SwitchableSaveRequest request) {
|
||||||
|
assertThat(toCheck).isNotNull();
|
||||||
|
assertThat(toCheck.isOn()).isEqualTo(request.isOn());
|
||||||
|
assertThat(toCheck.getName()).isEqualTo(request.getName());
|
||||||
|
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("when creating should return the same object")
|
||||||
|
@SneakyThrows(NotFoundException.class)
|
||||||
|
public void testCreate() {
|
||||||
|
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
|
||||||
|
when(deviceService.saveAsOwner(any(RegularLight.class), eq("user")))
|
||||||
|
.thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
|
final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
|
||||||
|
toSend.setName("rl");
|
||||||
|
toSend.setRoomId(20L);
|
||||||
|
toSend.setOn(true);
|
||||||
|
|
||||||
|
final RegularLight regularLight = regularLightController.create(toSend, mockPrincipal);
|
||||||
|
|
||||||
|
checkRegularLightAgainstRequest(regularLight, toSend);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("when updating should return the same object")
|
||||||
|
@SneakyThrows(NotFoundException.class)
|
||||||
|
public void testUpdate() {
|
||||||
|
|
||||||
|
final SwitchableSaveRequest toSend = new SwitchableSaveRequest();
|
||||||
|
toSend.setId(50L);
|
||||||
|
toSend.setName("rl");
|
||||||
|
toSend.setRoomId(20L);
|
||||||
|
toSend.setOn(true);
|
||||||
|
|
||||||
|
final RegularLight toUpdate = new RegularLight();
|
||||||
|
toUpdate.setName("OOO");
|
||||||
|
toUpdate.setRoomId(40L);
|
||||||
|
toSend.setId(50L);
|
||||||
|
toUpdate.setOn(false);
|
||||||
|
|
||||||
|
when(regularLightRepository.findByIdAndUserId(anyLong(), anyLong()))
|
||||||
|
.thenReturn(java.util.Optional.of(toUpdate));
|
||||||
|
|
||||||
|
when(deviceService.saveAsGuest(any(RegularLight.class), eq("user"), anyLong()))
|
||||||
|
.thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
|
||||||
|
User guest = new User();
|
||||||
|
User host = new User();
|
||||||
|
host.getGuests().add(guest);
|
||||||
|
guest.getHosts().add(host);
|
||||||
|
|
||||||
|
when(userRepository.findById(20L)).thenReturn(Optional.of(host));
|
||||||
|
when(userRepository.findByUsername("user")).thenReturn(guest);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
|
final RegularLight regularLight = regularLightController.update(toSend, mockPrincipal, 20L);
|
||||||
|
|
||||||
|
checkRegularLightAgainstRequest(regularLight, toSend);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
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.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyLong;
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
@ -8,16 +9,20 @@ import static org.mockito.Mockito.doNothing;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatSaveRequest;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatSaveRequest;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatPopulationService;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatPopulationService;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
|
import java.util.Optional;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
@ -128,4 +133,37 @@ public class ThermostatControllerTests {
|
||||||
|
|
||||||
Assertions.assertDoesNotThrow(() -> thermostatController.deleteById(42L, mockPrincipal));
|
Assertions.assertDoesNotThrow(() -> thermostatController.deleteById(42L, mockPrincipal));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSceneBinding() {
|
||||||
|
Thermostat thermostat = new Thermostat();
|
||||||
|
when(thermostatRepository.findByIdAndUsername(24L, "user"))
|
||||||
|
.thenReturn(Optional.of(thermostat));
|
||||||
|
Scene scene = new Scene();
|
||||||
|
scene.setId(1L);
|
||||||
|
SwitchableState state = new SwitchableState();
|
||||||
|
state.setSceneId(1L);
|
||||||
|
State s = thermostat.cloneState();
|
||||||
|
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
|
||||||
|
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(0);
|
||||||
|
Assertions.assertDoesNotThrow(
|
||||||
|
() -> thermostatController.sceneBinding(24L, 1L, mockPrincipal));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSceneBinding2() {
|
||||||
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
|
Thermostat thermostat = new Thermostat();
|
||||||
|
when(thermostatRepository.findByIdAndUsername(24L, "user"))
|
||||||
|
.thenReturn(Optional.of(thermostat));
|
||||||
|
Scene scene = new Scene();
|
||||||
|
scene.setId(1L);
|
||||||
|
SwitchableState state = new SwitchableState();
|
||||||
|
state.setSceneId(1L);
|
||||||
|
State s = thermostat.cloneState();
|
||||||
|
when(sceneRepository.findById(1L)).thenReturn(Optional.of(scene));
|
||||||
|
when(stateRepository.countByDeviceIdAndSceneId(24L, 1L)).thenReturn(2);
|
||||||
|
assertThatThrownBy(() -> thermostatController.sceneBinding(24L, 1L, mockPrincipal))
|
||||||
|
.isInstanceOf(DuplicateStateException.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue