Sonarqube fixes to tests

This commit is contained in:
Claudio Maggioni 2020-05-09 18:35:28 +02:00
parent bd47a571d5
commit 5d4aea2e56
12 changed files with 61 additions and 115 deletions

View file

@ -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());

View file

@ -97,8 +97,6 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
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<? extends Switchable> sceneBinding(
@PathVariable("id") long deviceId,

View file

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

View file

@ -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);

View file

@ -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());

View file

@ -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 {

View file

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

View file

@ -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() {

View file

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

View file

@ -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;
}

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.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<Object> 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<String, Object> 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<JsonObject> 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<JsonObject> 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<JsonObject> 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<JsonObject> 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<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);
}
assertThat(errors)
.allSatisfy(
j ->
assertThat(j.getAsJsonObject().get("field").getAsString())
.isEqualTo("username"));
}
@Test
@ -179,8 +152,7 @@ public class AuthenticationTests extends SmartHutTest {
final ResponseEntity<Object> 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<String, Object> 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<UnauthorizedException> 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<UnauthorizedException> 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<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());
assertThat(res.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
assertThat(res.getBody()).isNotNull();
}
}

View file

@ -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