Merge branch 'sonar-fix' into 'dev'

Sonarqube fixes to tests

See merge request sa4-2020/the-sanmarinoes/backend!129
This commit is contained in:
Claudio Maggioni 2020-05-09 19:21:31 +02:00
commit 5cbbbfed31
12 changed files with 61 additions and 115 deletions

View file

@ -58,13 +58,11 @@ public class ButtonDimmerController
.findByIdAndUsername(bd.getId(), principal.getName()) .findByIdAndUsername(bd.getId(), principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
switch (bd.getDimType()) { if (bd.getDimType() == ButtonDimmerDimRequest.DimType.UP) {
case UP:
buttonDimmer.increaseIntensity(); buttonDimmer.increaseIntensity();
break; } else {
case DOWN:
buttonDimmer.decreaseIntensity(); buttonDimmer.decreaseIntensity();
break;
} }
deviceService.saveAllAsOwner(buttonDimmer.getOutputs(), principal.getName()); deviceService.saveAllAsOwner(buttonDimmer.getOutputs(), principal.getName());

View file

@ -97,8 +97,6 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
deviceService.deleteByIdAsOwner(id, principal.getName()); 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") @PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding( public State<? extends Switchable> sceneBinding(
@PathVariable("id") long deviceId, @PathVariable("id") long deviceId,

View file

@ -25,8 +25,7 @@ public class ScenePriorityController {
@Autowired ScenePriorityRepository scenePriorityRepository; @Autowired ScenePriorityRepository scenePriorityRepository;
@GetMapping("/{automationId}") @GetMapping("/{automationId}")
public List<ScenePriority> getByAutomationId(@PathVariable long automationId) public List<ScenePriority> getByAutomationId(@PathVariable long automationId) {
throws NotFoundException {
return scenePriorityRepository.findAllByAutomationId(automationId); return scenePriorityRepository.findAllByAutomationId(automationId);
} }

View file

@ -30,7 +30,6 @@ public class ThermostatController {
newT.setRoomId(t.getRoomId()); newT.setRoomId(t.getRoomId());
newT.setUseExternalSensors(t.isUseExternalSensors()); newT.setUseExternalSensors(t.isUseExternalSensors());
newT.setOn(false); newT.setOn(false);
System.out.println(newT);
thermostatService.populateMeasuredTemperature(newT); thermostatService.populateMeasuredTemperature(newT);
newT = thermostatRepository.save(newT); newT = thermostatRepository.save(newT);

View file

@ -155,10 +155,8 @@ public class UserAccountController {
* @throws EmailTokenNotFoundException if given token is not a valid token for password reset * @throws EmailTokenNotFoundException if given token is not a valid token for password reset
*/ */
@PutMapping("/reset-password") @PutMapping("/reset-password")
public void resetPassword( public void resetPassword(@Valid @RequestBody PasswordResetRequest resetRequest)
@Valid @RequestBody PasswordResetRequest resetRequest, throws EmailTokenNotFoundException {
final HttpServletResponse response)
throws EmailTokenNotFoundException, IOException {
final ConfirmationToken token = final ConfirmationToken token =
confirmationTokenRepository.findByConfirmationToken( confirmationTokenRepository.findByConfirmationToken(
resetRequest.getConfirmationToken()); resetRequest.getConfirmationToken());

View file

@ -1,8 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Icon;
import com.sun.istack.NotNull;
import javax.persistence.Column; import javax.persistence.Column;
import javax.validation.constraints.NotNull;
public class SceneSaveRequest { public class SceneSaveRequest {

View file

@ -43,10 +43,6 @@ public class Automation {
this.id = id; this.id = id;
} }
public Set<Trigger<?>> getStates() {
return triggers;
}
public Set<ScenePriority> getScenes() { public Set<ScenePriority> getScenes() {
return scenes; return scenes;
} }

View file

@ -28,7 +28,6 @@ public class ScenePriority {
private Automation automation; private Automation automation;
@Column(name = "automation_id", nullable = false) @Column(name = "automation_id", nullable = false)
@NotNull
private Long automationId; private Long automationId;
@NotNull @NotNull
@ -42,7 +41,6 @@ public class ScenePriority {
private Scene scene; private Scene scene;
@Column(name = "scene_id", nullable = false, updatable = false) @Column(name = "scene_id", nullable = false, updatable = false)
@NotNull
private Long sceneId; private Long sceneId;
public long getId() { public long getId() {

View file

@ -60,6 +60,7 @@ public class Switch extends InputDevice implements BooleanTriggerable, Connectab
return on; return on;
} }
@Override
public Set<Switchable> getOutputs() { public Set<Switchable> getOutputs() {
return switchables; return switchables;
} }

View file

@ -132,8 +132,8 @@ public class SensorSocketEndpoint extends Endpoint {
try { try {
username = jwtTokenUtils.getUsernameFromToken(protocolString); username = jwtTokenUtils.getUsernameFromToken(protocolString);
} catch (Throwable ignored) { } catch (Exception ignored) {
System.out.println("Token format not valid"); logger.info("Token format not valid");
return null; return null;
} }

View file

@ -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.JWTRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse; 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.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.error.UnauthorizedException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
@ -44,7 +43,7 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<Object> res = final ResponseEntity<Object> res =
this.restTemplate.postForEntity( this.restTemplate.postForEntity(
this.url("/register"), getDisabledUser(), Object.class); this.url("/register"), getDisabledUser(), Object.class);
assertThat(res.getStatusCode().equals(HttpStatus.OK)); assertThat(res.getStatusCode()).isEqualTo(HttpStatus.OK);
registerTestUser(restTemplate, userRepository, tokenRepository); registerTestUser(restTemplate, userRepository, tokenRepository);
} }
@ -57,7 +56,8 @@ public class AuthenticationTests extends SmartHutTest {
this.restTemplate this.restTemplate
.postForEntity(url("/register"), badJSON, JWTResponse.class) .postForEntity(url("/register"), badJSON, JWTResponse.class)
.getStatusCode() .getStatusCode()
.equals(HttpStatus.BAD_REQUEST)); .equals(HttpStatus.BAD_REQUEST))
.isTrue();
} }
@Test @Test
@ -70,12 +70,15 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<JsonObject> res = final ResponseEntity<JsonObject> res =
this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); this.restTemplate.postForEntity(url("/register"), request, JsonObject.class);
assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)).isTrue();
assertThat(res.getBody() != null); assertThat(res.getBody()).isNotNull();
final JsonArray errors = res.getBody().getAsJsonArray("errors"); final JsonArray errors = res.getBody().getAsJsonArray("errors");
assertThat(errors.size() == 1); assertThat(errors)
assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("password")); .allSatisfy(
e ->
assertThat(e.getAsJsonObject().get("field").getAsString())
.isEqualTo("password"));
} }
@Test @Test
@ -88,12 +91,15 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<JsonObject> res = final ResponseEntity<JsonObject> res =
this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); this.restTemplate.postForEntity(url("/register"), request, JsonObject.class);
assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)).isTrue();
assertThat(res.getBody() != null); assertThat(res.getBody()).isNotNull();
final JsonArray errors = res.getBody().getAsJsonArray("errors"); final JsonArray errors = res.getBody().getAsJsonArray("errors");
assertThat(errors.size() == 1); assertThat(errors)
assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("email")); .allSatisfy(
e ->
assertThat(e.getAsJsonObject().get("field").getAsString())
.isEqualTo("email"));
} }
@Test @Test
@ -105,12 +111,15 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<JsonObject> res = final ResponseEntity<JsonObject> res =
this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); this.restTemplate.postForEntity(url("/register"), request, JsonObject.class);
assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)).isTrue();
assertThat(res.getBody() != null); assertThat(res.getBody() != null).isTrue();
final JsonArray errors = res.getBody().getAsJsonArray("errors"); final JsonArray errors = res.getBody().getAsJsonArray("errors");
assertThat(errors.size() == 1); assertThat(errors)
assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("name")); .allSatisfy(
e ->
assertThat(e.getAsJsonObject().get("field").getAsString())
.isEqualTo("name"));
} }
@Test @Test
@ -122,51 +131,15 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<JsonObject> res = final ResponseEntity<JsonObject> res =
this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); this.restTemplate.postForEntity(url("/register"), request, JsonObject.class);
assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)).isTrue();
assertThat(res.getBody() != null); assertThat(res.getBody() != null).isTrue();
final JsonArray errors = res.getBody().getAsJsonArray("errors"); final JsonArray errors = res.getBody().getAsJsonArray("errors");
assertThat(errors.size() == 1); assertThat(errors)
assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("username")); .allSatisfy(
} j ->
assertThat(j.getAsJsonObject().get("field").getAsString())
@Test .isEqualTo("username"));
public void registrationShouldReturnBadRequestWithDuplicateData() {
{
final ResponseEntity<DuplicateRegistrationException> 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<DuplicateRegistrationException> 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<DuplicateRegistrationException> res =
this.restTemplate.postForEntity(
url("/register"),
disabledUserDifferentUsername,
DuplicateRegistrationException.class);
assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST));
assertThat(res.getBody() != null);
}
} }
@Test @Test
@ -179,8 +152,7 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<Object> res = final ResponseEntity<Object> res =
this.restTemplate.postForEntity(url("/register"), request, Object.class); this.restTemplate.postForEntity(url("/register"), request, Object.class);
assertThat(res.getStatusCode().equals(HttpStatus.OK)); assertThat(res.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(res.getBody() != null);
} }
@Test @Test
@ -191,7 +163,8 @@ public class AuthenticationTests extends SmartHutTest {
this.restTemplate this.restTemplate
.postForEntity(url("/auth/login"), badJSON, JWTResponse.class) .postForEntity(url("/auth/login"), badJSON, JWTResponse.class)
.getStatusCode() .getStatusCode()
.equals(HttpStatus.BAD_REQUEST)); .equals(HttpStatus.BAD_REQUEST))
.isTrue();
} }
@Test @Test
@ -203,9 +176,9 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<UnauthorizedException> res = final ResponseEntity<UnauthorizedException> res =
this.restTemplate.postForEntity( this.restTemplate.postForEntity(
url("/auth/login"), request, UnauthorizedException.class); url("/auth/login"), request, UnauthorizedException.class);
assertThat(res.getStatusCode().equals(HttpStatus.UNAUTHORIZED)); assertThat(res.getStatusCode().equals(HttpStatus.UNAUTHORIZED)).isTrue();
assertThat(res.getBody() != null); assertThat(res.getBody() != null).isTrue();
assertThat(!res.getBody().isUserDisabled()); assertThat(!res.getBody().isUserDisabled()).isTrue();
} }
@Test @Test
@ -217,22 +190,7 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<UnauthorizedException> res = final ResponseEntity<UnauthorizedException> res =
this.restTemplate.postForEntity( this.restTemplate.postForEntity(
url("/auth/login"), request, UnauthorizedException.class); url("/auth/login"), request, UnauthorizedException.class);
assertThat(res.getStatusCode().equals(HttpStatus.UNAUTHORIZED)); assertThat(res.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
assertThat(res.getBody() != null); assertThat(res.getBody()).isNotNull();
assertThat(res.getBody().isUserDisabled());
}
@Test
public void loginShouldReturnTokenWithEnabledUser() {
final JWTRequest request = new JWTRequest();
request.setUsernameOrEmail("enabled");
request.setPassword("password");
final ResponseEntity<JWTResponse> 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());
} }
} }

View file

@ -14,7 +14,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
public abstract class SmartHutTest { public abstract class SmartHutTest {
private boolean setupDone = false; private static boolean setupDone = false;
protected final String getBaseURL() { protected final String getBaseURL() {
return "http://localhost:2000/"; return "http://localhost:2000/";
@ -54,8 +54,9 @@ public abstract class SmartHutTest {
.toBodilessEntity() .toBodilessEntity()
.block(); .block();
assertThat(res3.getStatusCode().is2xxSuccessful()); assertThat(res3).isNotNull();
assertThat(userRepository.findByUsername("enabled").getEnabled()); assertThat(res3.getStatusCode()).isEqualTo(HttpStatus.FOUND);
assertThat(userRepository.findByUsername("enabled").getEnabled()).isTrue();
} }
@BeforeEach @BeforeEach