From 483a42b1e0efc3469f7a24feff67cbc6928f072a Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 10 May 2020 00:38:13 +0200 Subject: [PATCH] Even another batch of sonar fixes --- .../smarthut/controller/AutomationController.java | 3 ++- .../sanmarinoes/smarthut/models/ConfirmationToken.java | 4 ++-- .../sa4/sanmarinoes/smarthut/service/DeviceService.java | 8 ++++++-- .../smarthut/service/JWTUserDetailsService.java | 2 +- .../sanmarinoes/smarthut/socket/SensorSocketConfig.java | 4 ++-- .../smarthut/socket/SensorSocketEndpoint.java | 3 ++- .../usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java | 2 +- .../sanmarinoes/smarthut/SmarthutApplicationTests.java | 9 +++------ 8 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java index c3027ae..af5f727 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java @@ -99,7 +99,7 @@ public class AutomationController { req.getScenes() .stream() .map(AutomationFastUpdateRequest.ScenePriorityDTO::toModel) - .peek( + .map( t -> { t.setAutomationId(a.getId()); @@ -107,6 +107,7 @@ public class AutomationController { // please do not replicate unless the quality gate sees // it as a bug t.setAutomation(a); + return t; }) .collect(Collectors.toList())); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java index ac1f151..1aea856 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java @@ -53,7 +53,7 @@ public class ConfirmationToken { } public Date getCreatedDate() { - return createdDate; + return (Date) createdDate.clone(); } public User getUser() { @@ -69,7 +69,7 @@ public class ConfirmationToken { } public void setCreatedDate(Date createdDate) { - this.createdDate = createdDate; + this.createdDate = (Date) createdDate.clone(); } public void setUser(User user) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java index bf16b88..3678bc0 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java @@ -75,7 +75,7 @@ public class DeviceService { sceneRepository .findById(t.getSceneId()) .orElseThrow(IllegalStateException::new)) - .forEach((s) -> sceneService.apply(s, username, true)); + .forEach(s -> sceneService.apply(s, username, true)); } public List findAll(Long hostId, String username) throws NotFoundException { @@ -123,7 +123,7 @@ public class DeviceService { List toReturn = devicePropagationService.saveAllAsOwner(devices, username, fromScene, fromTrigger); if (!fromScene) { - toReturn.forEach((d) -> this.triggerTriggers(d, username)); + toReturn.forEach(d -> this.triggerTriggers(d, username)); } return toReturn; } @@ -170,6 +170,10 @@ public class DeviceService { devicePopulationService.populateComputedFields(devices); + return filterOutCamerasIfNeeded(host, devices); + } + + private List filterOutCamerasIfNeeded(User host, Iterable devices) { if (host != null && !host.isCameraEnabled()) { return StreamSupport.stream(devices.spliterator(), true) .filter(d -> !(d instanceof SecurityCamera)) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/JWTUserDetailsService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/JWTUserDetailsService.java index 8ab5f8a..f8c8e5b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/JWTUserDetailsService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/JWTUserDetailsService.java @@ -15,7 +15,7 @@ public class JWTUserDetailsService implements UserDetailsService { @Autowired private UserRepository repository; @Override - public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + public UserDetails loadUserByUsername(String username) { User toReturn = repository.findByUsername(username); if (toReturn != null && toReturn.getEnabled()) { return new org.springframework.security.core.userdetails.User( diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java index 503667a..e1b29ce 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java @@ -42,8 +42,8 @@ public class SensorSocketConfig extends ServerEndpointConfig.Configurator { public T getEndpointInstance(Class endpointClass) throws InstantiationException { try { @SuppressWarnings("unchecked") - final T instance = (T) this.instance; - return instance; + final T thaInstance = (T) this.instance; + return thaInstance; } catch (ClassCastException e) { final var e2 = new InstantiationException("Cannot cast SensorSocketEndpoint to desired type"); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java index cc298d4..009dfab 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java @@ -122,7 +122,8 @@ public class SensorSocketEndpoint extends Endpoint { } else { try { session.close(); - } catch (IOException ignored) { + } catch (IOException e) { + logger.warn(e.getLocalizedMessage(), e); } } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java index 445bf08..1b3c96b 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java @@ -41,7 +41,7 @@ public abstract class SmartHutTest { final ConfirmationTokenRepository tokenRepository) { final ResponseEntity res2 = restTemplate.postForEntity(this.url("/register"), enabledUser, Object.class); - assertThat(res2.getStatusCode().equals(HttpStatus.OK)); + assertThat(res2.getStatusCode()).isEqualTo(HttpStatus.OK); final User persistedEnabledUser = userRepository.findByUsername("enabled"); final ConfirmationToken token = tokenRepository.findByUser(persistedEnabledUser); diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java index dbd7e21..2aaa1b1 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java @@ -16,11 +16,8 @@ public class SmarthutApplicationTests extends SmartHutTest { @Autowired private TestRestTemplate restTemplate; @Test - public void anonymousGreetingShouldNotBeAuthorized() throws Exception { - assertThat( - this.restTemplate - .getForEntity(getBaseURL(), Void.class) - .getStatusCode() - .equals(HttpStatus.UNAUTHORIZED)); + public void anonymousGreetingShouldNotBeAuthorized() { + assertThat(this.restTemplate.getForEntity(getBaseURL(), Void.class).getStatusCode()) + .isEqualTo(HttpStatus.UNAUTHORIZED); } }