From 4253e652411414f3a455e516626c70ec85ef7ad7 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 24 May 2020 22:27:00 +0200 Subject: [PATCH] Fixes --- .../smarthut/config/JWTRequestFilter.java | 10 ++++++-- .../RegularLightControllerTests.java | 23 ++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTRequestFilter.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTRequestFilter.java index 3cfc094..f5d7d18 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTRequestFilter.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTRequestFilter.java @@ -18,9 +18,15 @@ import org.springframework.web.filter.OncePerRequestFilter; @Component 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 protected void doFilterInternal( diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java index f3c2136..1e4523f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java @@ -9,16 +9,10 @@ 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.DeviceRepository; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository; -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.StateRepository; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; -import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils; import java.security.Principal; +import java.util.Optional; import lombok.SneakyThrows; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -109,12 +103,13 @@ public class RegularLightControllerTests { when(deviceService.saveAsGuest(any(RegularLight.class), eq("user"), anyLong())) .thenAnswer(i -> i.getArguments()[0]); - when(Utils.returnIfGuest( - any(UserRepository.class), - any(RegularLight.class), - anyLong(), - any(Principal.class))) - .thenAnswer(i -> i.getArguments()[1]); + 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));