Skip to content

Commit

Permalink
Sonar: Local-Variable Type Inference should be used
Browse files Browse the repository at this point in the history
  • Loading branch information
qmonmert committed Feb 28, 2025
1 parent a7d415e commit 9ed73b5
Show file tree
Hide file tree
Showing 19 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ class MissingMandatoryValueExceptionTest {
@Test
void shouldGetExceptionForBlankValue() {
MissingMandatoryValueException exception = MissingMandatoryValueException.forBlankValue(FIELD);
var exception = MissingMandatoryValueException.forBlankValue(FIELD);
assertDefaultInformation(exception);
assertThat(exception.getMessage()).isEqualTo("The field \"field\" is mandatory and wasn't set (blank)");
}

@Test
void shouldGetExceptionForNullValue() {
MissingMandatoryValueException exception = MissingMandatoryValueException.forNullValue(FIELD);
var exception = MissingMandatoryValueException.forNullValue(FIELD);
assertDefaultInformation(exception);
assertThat(exception.getMessage()).isEqualTo("The field \"field\" is mandatory and wasn't set (null)");
}

@Test
void shouldGetExceptionForEmptyCollection() {
MissingMandatoryValueException exception = MissingMandatoryValueException.forEmptyValue(FIELD);
var exception = MissingMandatoryValueException.forEmptyValue(FIELD);
assertDefaultInformation(exception);
assertThat(exception.getMessage()).isEqualTo("The field \"field\" is mandatory and wasn't set (empty)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NullElementInCollectionExceptionTest {
@Test
void shouldGetExceptionInformation() {
NullElementInCollectionException exception = new NullElementInCollectionException("myField");
var exception = new NullElementInCollectionException("myField");
assertThat(exception.type()).isEqualTo(AssertionErrorType.NULL_ELEMENT_IN_COLLECTION);
assertThat(exception.field()).isEqualTo("myField");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class {{baseName}}PageableTest {

@Test
void shouldGetFirstPageInformation() {
{{baseName}}Pageable pageable = new {{baseName}}Pageable(0, 15);
var pageable = new {{baseName}}Pageable(0, 15);

assertThat(pageable.page()).isZero();
assertThat(pageable.pageSize()).isEqualTo(15);
Expand All @@ -42,7 +42,7 @@ class {{baseName}}PageableTest {

@Test
void shouldGetPageableInformation() {
{{baseName}}Pageable pageable = new {{baseName}}Pageable(2, 15);
var pageable = new {{baseName}}Pageable(2, 15);

assertThat(pageable.page()).isEqualTo(2);
assertThat(pageable.pageSize()).isEqualTo(15);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class SpringLiquibaseUtil {
DataSource dataSource,
DataSourceProperties dataSourceProperties
) {
AsyncSpringLiquibase liquibase = new AsyncSpringLiquibase(executor, env, liquibaseProperties);
var liquibase = new AsyncSpringLiquibase(executor, env, liquibaseProperties);
var liquibaseDataSource = getDataSource(liquibaseDatasource, liquibaseProperties, dataSource);
if (liquibaseDataSource != null) {
liquibase.setCloseDataSourceOnceMigrated(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LiquibaseConfigurationIT {
@Test
void shouldGetLiquibaseAsync() {
SpringLiquibase springLiquibase = (SpringLiquibase) applicationContext.getBean("liquibase");
var springLiquibase = (SpringLiquibase) applicationContext.getBean("liquibase");
assertThat(springLiquibase).isNotNull();
}
Expand All @@ -36,7 +36,7 @@ class LiquibaseConfigurationIT {
@Test
void shouldGetLiquibaseSync() {
SpringLiquibase springLiquibase = (SpringLiquibase) applicationContext.getBean("liquibase");
var springLiquibase = (SpringLiquibase) applicationContext.getBean("liquibase");
assertThat(springLiquibase).isNotNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LogstashTcpConfigurationIT {
assertThat(appender).isInstanceOf(LogstashTcpSocketAppender.class);
LogstashTcpSocketAppender logstashTcpSocketAppender = (LogstashTcpSocketAppender) appender;
var logstashTcpSocketAppender = (LogstashTcpSocketAppender) appender;
assertThat(logstashTcpSocketAppender.getDestinations()).contains(new InetSocketAddress("127.0.0.1", 50000));
assertThat(logstashTcpSocketAppender.getRingBufferSize()).isEqualTo(4096);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LogstashTcpConfigurationTest {
@Test
void shouldAddAppNameOnly() throws IOException {
LogstashTcpConfiguration c = new LogstashTcpConfiguration("jhipster-lite", null, new LogstashTcpProperties());
var c = new LogstashTcpConfiguration("jhipster-lite", null, new LogstashTcpProperties());
ObjectContent<Map<String, String>> parsed = json.parse(c.logstashTcpLifeCycle().serializedFields());
assertThat(parsed.getObject()).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BeersCreator {
}

public Beer create(BeerToCreate beerToCreate) {
Beer createdBeer = beerToCreate.create();
var createdBeer = beerToCreate.create();
beers.save(createdBeer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import {{packageName}}.sample.application.BeersApplicationService;
import {{packageName}}.sample.domain.BeerId;
import {{packageName}}.sample.domain.beer.Beer;

@RestController
@Tag(name = "Beers")
Expand All @@ -33,7 +32,7 @@ class BeersResource {
@Operation(summary = "Add a beer to the catalog")
@ApiResponse(description = "Beer added to the catalog", responseCode = "201")
ResponseEntity<RestBeer> addBeer(@Validated @RequestBody RestBeerToCreate beerToCreate) {
Beer createdBeer = beers.create(beerToCreate.toDomain());
var createdBeer = beers.create(beerToCreate.toDomain());
return new ResponseEntity<>(RestBeer.from(createdBeer), HttpStatus.CREATED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BeersTest {
.sellingState(BeerSellingState.SOLD)
.build();
Beers beers = new Beers(List.of(beer(), anteMeridiem));
var beers = new Beers(List.of(beer(), anteMeridiem));
assertThat(beers.get()).usingRecursiveFieldByFieldElementComparator().containsExactly(anteMeridiem, beer());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JWTConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain

@Override
public void configure(HttpSecurity http) {
JWTFilter jwtFilter = new JWTFilter(jwt);
var jwtFilter = new JWTFilter(jwt);
http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SecurityConfiguration {
.anyRequest().authenticated()
);
JWTConfigurer jwtConfigurer = new JWTConfigurer(authenticationTokenReader());
var jwtConfigurer = new JWTConfigurer(authenticationTokenReader());
http.with(jwtConfigurer, Customizer.withDefaults());
return http.build();
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import javax.crypto.SecretKey;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import {{packageName}}.Logs;
Expand Down Expand Up @@ -58,7 +57,7 @@ class JwtReaderTest {

@Test
void shouldGetAuthenticationFromValidToken() {
Authentication authentication = reader.read(userToken()).orElseThrow();
var authentication = reader.read(userToken()).orElseThrow();
assertThat(((User) authentication.getPrincipal()).getUsername()).isEqualTo("test");
assertThat(authentication.getAuthorities().stream().map(SimpleGrantedAuthority.class::cast)).containsExactly(
Expand All @@ -78,7 +77,7 @@ class JwtReaderTest {

@Test
void shouldGetAuthenticationFromValidTokenWithoutRoles() {
Authentication authentication = reader.read(userTokenWithoutRoles()).orElseThrow();
var authentication = reader.read(userTokenWithoutRoles()).orElseThrow();
assertThat(((User) authentication.getPrincipal()).getUsername()).isEqualTo("test");
assertThat(authentication.getAuthorities()).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import {{packageName}}.account.domain.Token;

@RestController
@RequestMapping("/api/authenticate")
Expand All @@ -32,7 +31,7 @@ class AuthenticationResource {
@PostMapping
@Operation(description = "Build an authentication token for the user")
public ResponseEntity<RestToken> authorize(@Valid @RequestBody RestAuthenticationQuery authenticationQuery) {
Token token = authenticator.authenticate(authenticationQuery);
var token = authenticator.authenticate(authenticationQuery);
var httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.AUTHORIZATION, token.bearer());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package {{packageName}}.account.infrastructure.primary;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import {{packageName}}.account.application.AccountApplicationService;
Expand All @@ -20,7 +19,7 @@ class Authenticator {
}

Token authenticate(RestAuthenticationQuery query) {
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(query.authenticationToken());
var authentication = authenticationManagerBuilder.getObject().authenticate(query.authenticationToken());
SecurityContextHolder.getContext().setAuthentication(authentication);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class RolesAccesses {

private Predicate<Role> allAuthorized(Action action, Resource resource) {
return role -> {
Accesses accesses = roles.get(role);
var accesses = roles.get(role);
if (accesses == null) {
return false;
Expand All @@ -58,7 +58,7 @@ public final class RolesAccesses {

private Predicate<Role> specificAuthorized(Action action, Resource resource) {
return role -> {
Accesses accesses = roles.get(role);
var accesses = roles.get(role);
if (accesses == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class KipeMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressio
public EvaluationContext createEvaluationContext(Supplier<Authentication> authentication, MethodInvocation mi) {
MethodSecurityExpressionOperations root = buildExpressionRoot(authentication, mi);
MethodBasedEvaluationContext ctx = new MethodBasedEvaluationContext(
var ctx = new MethodBasedEvaluationContext(
root,
getSpecificMethod(mi),
mi.getArguments(),
Expand All @@ -40,7 +40,7 @@ class KipeMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressio
}

private MethodSecurityExpressionOperations buildExpressionRoot(Supplier<Authentication> authentication, MethodInvocation invocation) {
KipeMethodSecurityExpressionRoot root = new KipeMethodSecurityExpressionRoot(authentication, evaluator);
var root = new KipeMethodSecurityExpressionRoot(authentication, evaluator);
root.setThis(invocation.getThis());
root.setPermissionEvaluator(getPermissionEvaluator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AccessEvaluatorTest {

@Test
void shouldResolveOnDefaultCheckerForNullObject() {
AccessEvaluator canEvaluator = new AccessEvaluator(List.of(new ObjectAccessChecker()));
var canEvaluator = new AccessEvaluator(List.of(new ObjectAccessChecker()));
boolean can = canEvaluator.can(authentication, "action", null);
Expand All @@ -44,7 +44,7 @@ class AccessEvaluatorTest {

@Test
void shouldResolveOnDefaultCheckerForUnknownType() {
AccessEvaluator canEvaluator = new AccessEvaluator(List.of(new ObjectAccessChecker()));
var canEvaluator = new AccessEvaluator(List.of(new ObjectAccessChecker()));
boolean can = canEvaluator.can(authentication, "action", "yo");
Expand All @@ -54,14 +54,14 @@ class AccessEvaluatorTest {

@Test
void shouldGetMatchingEvaluator() {
AccessEvaluator canEvaluator = new AccessEvaluator(List.of(new ObjectAccessChecker(), new KipeDummyAccessChecker()));
var canEvaluator = new AccessEvaluator(List.of(new ObjectAccessChecker(), new KipeDummyAccessChecker()));
assertThat(canEvaluator.can(authentication, "action", new KipeDummy("authorized"))).isTrue();
}

@Test
void shouldGetMatchingEvaluatorForChildClass() {
AccessEvaluator canEvaluator = new AccessEvaluator(List.of(new ObjectAccessChecker(), new KipeDummyAccessChecker()));
var canEvaluator = new AccessEvaluator(List.of(new ObjectAccessChecker(), new KipeDummyAccessChecker()));
assertThat(canEvaluator.can(authentication, "action", new KipeDummyChild("authorized"))).isTrue();
logs.shouldHave(Level.INFO, "evaluator", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ class MissingMandatoryValueExceptionTest {

@Test
void shouldGetExceptionForBlankValue() {
MissingMandatoryValueException exception = MissingMandatoryValueException.forBlankValue(FIELD);
var exception = MissingMandatoryValueException.forBlankValue(FIELD);

assertThat(exception.getMessage()).isEqualTo("The field \"field\" is mandatory and wasn't set (blank)");
}

@Test
void shouldGetExceptionForNullValue() {
MissingMandatoryValueException exception = MissingMandatoryValueException.forNullValue(FIELD);
var exception = MissingMandatoryValueException.forNullValue(FIELD);

assertThat(exception.getMessage()).isEqualTo("The field \"field\" is mandatory and wasn't set (null)");
}

@Test
void shouldGetExceptionForEmptyCollection() {
MissingMandatoryValueException exception = MissingMandatoryValueException.forEmptyValue(FIELD);
var exception = MissingMandatoryValueException.forEmptyValue(FIELD);

assertThat(exception.getMessage()).isEqualTo("The field \"field\" is mandatory and wasn't set (empty)");
}
Expand Down

0 comments on commit 9ed73b5

Please sign in to comment.