Merge branch 'sonar-fix' into 'dev'

Even another batch of sonar fixes

See merge request sa4-2020/the-sanmarinoes/backend!130
This commit is contained in:
Claudio Maggioni 2020-05-10 00:41:06 +02:00
commit 4b10466f1a
8 changed files with 19 additions and 16 deletions

View file

@ -99,7 +99,7 @@ public class AutomationController {
req.getScenes() req.getScenes()
.stream() .stream()
.map(AutomationFastUpdateRequest.ScenePriorityDTO::toModel) .map(AutomationFastUpdateRequest.ScenePriorityDTO::toModel)
.peek( .map(
t -> { t -> {
t.setAutomationId(a.getId()); t.setAutomationId(a.getId());
@ -107,6 +107,7 @@ public class AutomationController {
// please do not replicate unless the quality gate sees // please do not replicate unless the quality gate sees
// it as a bug // it as a bug
t.setAutomation(a); t.setAutomation(a);
return t;
}) })
.collect(Collectors.toList())); .collect(Collectors.toList()));

View file

@ -53,7 +53,7 @@ public class ConfirmationToken {
} }
public Date getCreatedDate() { public Date getCreatedDate() {
return createdDate; return (Date) createdDate.clone();
} }
public User getUser() { public User getUser() {
@ -69,7 +69,7 @@ public class ConfirmationToken {
} }
public void setCreatedDate(Date createdDate) { public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate; this.createdDate = (Date) createdDate.clone();
} }
public void setUser(User user) { public void setUser(User user) {

View file

@ -75,7 +75,7 @@ public class DeviceService {
sceneRepository sceneRepository
.findById(t.getSceneId()) .findById(t.getSceneId())
.orElseThrow(IllegalStateException::new)) .orElseThrow(IllegalStateException::new))
.forEach((s) -> sceneService.apply(s, username, true)); .forEach(s -> sceneService.apply(s, username, true));
} }
public List<Device> findAll(Long hostId, String username) throws NotFoundException { public List<Device> findAll(Long hostId, String username) throws NotFoundException {
@ -123,7 +123,7 @@ public class DeviceService {
List<T> toReturn = List<T> toReturn =
devicePropagationService.saveAllAsOwner(devices, username, fromScene, fromTrigger); devicePropagationService.saveAllAsOwner(devices, username, fromScene, fromTrigger);
if (!fromScene) { if (!fromScene) {
toReturn.forEach((d) -> this.triggerTriggers(d, username)); toReturn.forEach(d -> this.triggerTriggers(d, username));
} }
return toReturn; return toReturn;
} }
@ -170,6 +170,10 @@ public class DeviceService {
devicePopulationService.populateComputedFields(devices); devicePopulationService.populateComputedFields(devices);
return filterOutCamerasIfNeeded(host, devices);
}
private List<Device> filterOutCamerasIfNeeded(User host, Iterable<Device> devices) {
if (host != null && !host.isCameraEnabled()) { if (host != null && !host.isCameraEnabled()) {
return StreamSupport.stream(devices.spliterator(), true) return StreamSupport.stream(devices.spliterator(), true)
.filter(d -> !(d instanceof SecurityCamera)) .filter(d -> !(d instanceof SecurityCamera))

View file

@ -15,7 +15,7 @@ public class JWTUserDetailsService implements UserDetailsService {
@Autowired private UserRepository repository; @Autowired private UserRepository repository;
@Override @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String username) {
User toReturn = repository.findByUsername(username); User toReturn = repository.findByUsername(username);
if (toReturn != null && toReturn.getEnabled()) { if (toReturn != null && toReturn.getEnabled()) {
return new org.springframework.security.core.userdetails.User( return new org.springframework.security.core.userdetails.User(

View file

@ -42,8 +42,8 @@ public class SensorSocketConfig extends ServerEndpointConfig.Configurator {
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
try { try {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final T instance = (T) this.instance; final T thaInstance = (T) this.instance;
return instance; return thaInstance;
} catch (ClassCastException e) { } catch (ClassCastException e) {
final var e2 = final var e2 =
new InstantiationException("Cannot cast SensorSocketEndpoint to desired type"); new InstantiationException("Cannot cast SensorSocketEndpoint to desired type");

View file

@ -122,7 +122,8 @@ public class SensorSocketEndpoint extends Endpoint {
} else { } else {
try { try {
session.close(); session.close();
} catch (IOException ignored) { } catch (IOException e) {
logger.warn(e.getLocalizedMessage(), e);
} }
} }
} }

View file

@ -41,7 +41,7 @@ public abstract class SmartHutTest {
final ConfirmationTokenRepository tokenRepository) { final ConfirmationTokenRepository tokenRepository) {
final ResponseEntity<Object> res2 = final ResponseEntity<Object> res2 =
restTemplate.postForEntity(this.url("/register"), enabledUser, Object.class); 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 User persistedEnabledUser = userRepository.findByUsername("enabled");
final ConfirmationToken token = tokenRepository.findByUser(persistedEnabledUser); final ConfirmationToken token = tokenRepository.findByUser(persistedEnabledUser);

View file

@ -16,11 +16,8 @@ public class SmarthutApplicationTests extends SmartHutTest {
@Autowired private TestRestTemplate restTemplate; @Autowired private TestRestTemplate restTemplate;
@Test @Test
public void anonymousGreetingShouldNotBeAuthorized() throws Exception { public void anonymousGreetingShouldNotBeAuthorized() {
assertThat( assertThat(this.restTemplate.getForEntity(getBaseURL(), Void.class).getStatusCode())
this.restTemplate .isEqualTo(HttpStatus.UNAUTHORIZED);
.getForEntity(getBaseURL(), Void.class)
.getStatusCode()
.equals(HttpStatus.UNAUTHORIZED));
} }
} }