diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index e2bc359..54ea066 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -58,13 +58,11 @@ public class ButtonDimmerController .findByIdAndUsername(bd.getId(), principal.getName()) .orElseThrow(NotFoundException::new); - switch (bd.getDimType()) { - case UP: - buttonDimmer.increaseIntensity(); - break; - case DOWN: - buttonDimmer.decreaseIntensity(); - break; + if (bd.getDimType() == ButtonDimmerDimRequest.DimType.UP) { + + buttonDimmer.increaseIntensity(); + } else { + buttonDimmer.decreaseIntensity(); } deviceService.saveAllAsOwner(buttonDimmer.getOutputs(), principal.getName()); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index 3768cb4..c3bdc8b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -97,8 +97,6 @@ public class RegularLightController extends GuestEnabledController deviceService.deleteByIdAsOwner(id, principal.getName()); } - // the full url should be: "/regularLight/{id}/state?sceneId={sceneId} - // however it is not necessary to specify the query in the mapping @PostMapping("/{id}/state") public State sceneBinding( @PathVariable("id") long deviceId, diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java index 1449727..98ead6a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java @@ -25,8 +25,7 @@ public class ScenePriorityController { @Autowired ScenePriorityRepository scenePriorityRepository; @GetMapping("/{automationId}") - public List getByAutomationId(@PathVariable long automationId) - throws NotFoundException { + public List getByAutomationId(@PathVariable long automationId) { return scenePriorityRepository.findAllByAutomationId(automationId); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java index 386e1ca..0696822 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java @@ -30,7 +30,6 @@ public class ThermostatController { newT.setRoomId(t.getRoomId()); newT.setUseExternalSensors(t.isUseExternalSensors()); newT.setOn(false); - System.out.println(newT); thermostatService.populateMeasuredTemperature(newT); newT = thermostatRepository.save(newT); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java index 2432a13..c5ecfb1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java @@ -155,10 +155,8 @@ public class UserAccountController { * @throws EmailTokenNotFoundException if given token is not a valid token for password reset */ @PutMapping("/reset-password") - public void resetPassword( - @Valid @RequestBody PasswordResetRequest resetRequest, - final HttpServletResponse response) - throws EmailTokenNotFoundException, IOException { + public void resetPassword(@Valid @RequestBody PasswordResetRequest resetRequest) + throws EmailTokenNotFoundException { final ConfirmationToken token = confirmationTokenRepository.findByConfirmationToken( resetRequest.getConfirmationToken()); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java index e7ce3ad..b890536 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java @@ -1,8 +1,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon; -import com.sun.istack.NotNull; import javax.persistence.Column; +import javax.validation.constraints.NotNull; public class SceneSaveRequest { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java index e17e870..4d01cde 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java @@ -43,10 +43,6 @@ public class Automation { this.id = id; } - public Set> getStates() { - return triggers; - } - public Set getScenes() { return scenes; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java index 0e05a0a..e975d08 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java @@ -28,7 +28,6 @@ public class ScenePriority { private Automation automation; @Column(name = "automation_id", nullable = false) - @NotNull private Long automationId; @NotNull @@ -42,7 +41,6 @@ public class ScenePriority { private Scene scene; @Column(name = "scene_id", nullable = false, updatable = false) - @NotNull private Long sceneId; public long getId() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index 82ab671..359ecad 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -60,6 +60,7 @@ public class Switch extends InputDevice implements BooleanTriggerable, Connectab return on; } + @Override public Set getOutputs() { return switchables; } 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 cec40f2..cc298d4 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 @@ -132,8 +132,8 @@ public class SensorSocketEndpoint extends Endpoint { try { username = jwtTokenUtils.getUsernameFromToken(protocolString); - } catch (Throwable ignored) { - System.out.println("Token format not valid"); + } catch (Exception ignored) { + logger.info("Token format not valid"); return null; } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java index 5eac647..2c0ae6f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java @@ -5,7 +5,6 @@ import static org.assertj.core.api.Assertions.assertThat; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserRegistrationRequest; -import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateRegistrationException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; @@ -44,7 +43,7 @@ public class AuthenticationTests extends SmartHutTest { final ResponseEntity res = this.restTemplate.postForEntity( this.url("/register"), getDisabledUser(), Object.class); - assertThat(res.getStatusCode().equals(HttpStatus.OK)); + assertThat(res.getStatusCode()).isEqualTo(HttpStatus.OK); registerTestUser(restTemplate, userRepository, tokenRepository); } @@ -54,10 +53,11 @@ public class AuthenticationTests extends SmartHutTest { final Map badJSON = Map.of("luciano", "goretti", "danilo", "malusa"); assertThat( - this.restTemplate - .postForEntity(url("/register"), badJSON, JWTResponse.class) - .getStatusCode() - .equals(HttpStatus.BAD_REQUEST)); + this.restTemplate + .postForEntity(url("/register"), badJSON, JWTResponse.class) + .getStatusCode() + .equals(HttpStatus.BAD_REQUEST)) + .isTrue(); } @Test @@ -70,12 +70,15 @@ public class AuthenticationTests extends SmartHutTest { final ResponseEntity res = this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); - assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); - assertThat(res.getBody() != null); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)).isTrue(); + assertThat(res.getBody()).isNotNull(); final JsonArray errors = res.getBody().getAsJsonArray("errors"); - assertThat(errors.size() == 1); - assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("password")); + assertThat(errors) + .allSatisfy( + e -> + assertThat(e.getAsJsonObject().get("field").getAsString()) + .isEqualTo("password")); } @Test @@ -88,12 +91,15 @@ public class AuthenticationTests extends SmartHutTest { final ResponseEntity res = this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); - assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); - assertThat(res.getBody() != null); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)).isTrue(); + assertThat(res.getBody()).isNotNull(); final JsonArray errors = res.getBody().getAsJsonArray("errors"); - assertThat(errors.size() == 1); - assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("email")); + assertThat(errors) + .allSatisfy( + e -> + assertThat(e.getAsJsonObject().get("field").getAsString()) + .isEqualTo("email")); } @Test @@ -105,12 +111,15 @@ public class AuthenticationTests extends SmartHutTest { final ResponseEntity res = this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); - assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); - assertThat(res.getBody() != null); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)).isTrue(); + assertThat(res.getBody() != null).isTrue(); final JsonArray errors = res.getBody().getAsJsonArray("errors"); - assertThat(errors.size() == 1); - assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("name")); + assertThat(errors) + .allSatisfy( + e -> + assertThat(e.getAsJsonObject().get("field").getAsString()) + .isEqualTo("name")); } @Test @@ -122,51 +131,15 @@ public class AuthenticationTests extends SmartHutTest { final ResponseEntity res = this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); - assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); - assertThat(res.getBody() != null); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)).isTrue(); + assertThat(res.getBody() != null).isTrue(); final JsonArray errors = res.getBody().getAsJsonArray("errors"); - assertThat(errors.size() == 1); - assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("username")); - } - - @Test - public void registrationShouldReturnBadRequestWithDuplicateData() { - { - final ResponseEntity res = - this.restTemplate.postForEntity( - url("/register"), - getDisabledUser(), - DuplicateRegistrationException.class); - assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); - assertThat(res.getBody() != null); - } - - { - final UserRegistrationRequest disabledUserDifferentMail = getDisabledUser(); - enabledUser.setEmail("another@example.com"); - - final ResponseEntity res = - this.restTemplate.postForEntity( - url("/register"), - disabledUserDifferentMail, - DuplicateRegistrationException.class); - assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); - assertThat(res.getBody() != null); - } - - { - final UserRegistrationRequest disabledUserDifferentUsername = getDisabledUser(); - enabledUser.setUsername("another"); - - final ResponseEntity res = - this.restTemplate.postForEntity( - url("/register"), - disabledUserDifferentUsername, - DuplicateRegistrationException.class); - assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); - assertThat(res.getBody() != null); - } + assertThat(errors) + .allSatisfy( + j -> + assertThat(j.getAsJsonObject().get("field").getAsString()) + .isEqualTo("username")); } @Test @@ -179,8 +152,7 @@ public class AuthenticationTests extends SmartHutTest { final ResponseEntity res = this.restTemplate.postForEntity(url("/register"), request, Object.class); - assertThat(res.getStatusCode().equals(HttpStatus.OK)); - assertThat(res.getBody() != null); + assertThat(res.getStatusCode()).isEqualTo(HttpStatus.OK); } @Test @@ -188,10 +160,11 @@ public class AuthenticationTests extends SmartHutTest { final Map badJSON = Map.of("badkey", 3, "password", "ciaomamma"); assertThat( - this.restTemplate - .postForEntity(url("/auth/login"), badJSON, JWTResponse.class) - .getStatusCode() - .equals(HttpStatus.BAD_REQUEST)); + this.restTemplate + .postForEntity(url("/auth/login"), badJSON, JWTResponse.class) + .getStatusCode() + .equals(HttpStatus.BAD_REQUEST)) + .isTrue(); } @Test @@ -203,9 +176,9 @@ public class AuthenticationTests extends SmartHutTest { final ResponseEntity res = this.restTemplate.postForEntity( url("/auth/login"), request, UnauthorizedException.class); - assertThat(res.getStatusCode().equals(HttpStatus.UNAUTHORIZED)); - assertThat(res.getBody() != null); - assertThat(!res.getBody().isUserDisabled()); + assertThat(res.getStatusCode().equals(HttpStatus.UNAUTHORIZED)).isTrue(); + assertThat(res.getBody() != null).isTrue(); + assertThat(!res.getBody().isUserDisabled()).isTrue(); } @Test @@ -217,22 +190,7 @@ public class AuthenticationTests extends SmartHutTest { final ResponseEntity res = this.restTemplate.postForEntity( url("/auth/login"), request, UnauthorizedException.class); - assertThat(res.getStatusCode().equals(HttpStatus.UNAUTHORIZED)); - assertThat(res.getBody() != null); - assertThat(res.getBody().isUserDisabled()); - } - - @Test - public void loginShouldReturnTokenWithEnabledUser() { - final JWTRequest request = new JWTRequest(); - request.setUsernameOrEmail("enabled"); - request.setPassword("password"); - - final ResponseEntity res = - this.restTemplate.postForEntity(url("/auth/login"), request, JWTResponse.class); - assertThat(res.getStatusCode().equals(HttpStatus.OK)); - assertThat(res.getBody() != null); - assertThat(res.getBody().getToken() != null); - assertThat(!res.getBody().getToken().isEmpty()); + assertThat(res.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); + assertThat(res.getBody()).isNotNull(); } } 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 69a7ba1..445bf08 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 @@ -14,7 +14,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.reactive.function.client.WebClient; public abstract class SmartHutTest { - private boolean setupDone = false; + private static boolean setupDone = false; protected final String getBaseURL() { return "http://localhost:2000/"; @@ -54,8 +54,9 @@ public abstract class SmartHutTest { .toBodilessEntity() .block(); - assertThat(res3.getStatusCode().is2xxSuccessful()); - assertThat(userRepository.findByUsername("enabled").getEnabled()); + assertThat(res3).isNotNull(); + assertThat(res3.getStatusCode()).isEqualTo(HttpStatus.FOUND); + assertThat(userRepository.findByUsername("enabled").getEnabled()).isTrue(); } @BeforeEach