Skip to content

Commit

Permalink
Sonar cleanup (#374)
Browse files Browse the repository at this point in the history
Sonar cleanup
* Local variables should not shadow class fields java:S1117
* "Stream.toList()" method should be used instead of "collectors"
  when unmodifiable list needed java:S6204
* Methods should not have identical implementations java:S4144
  • Loading branch information
sleberknight authored Jun 3, 2024
1 parent aafd2fe commit ca26310
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ void setUp() {

@Test
void shouldHaveDefaults() {
var config = new CleanupConfig();

assertAll(
() -> assertThat(config.getCleanupStrategy()).isEqualTo(CleanupConfig.CleanupStrategy.ALL_ERRORS),
() -> assertThat(config.getResolvedErrorExpiration()).isEqualTo(Duration.days(14)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.kiwiproject.dropwizard.error.dao;

import static com.google.common.base.Verify.verify;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
Expand Down Expand Up @@ -695,8 +694,8 @@ void shouldIncrementCount_WhenErrorExists(SoftAssertions softly) {

@Test
void shouldNotChangeOtherFields_OnExistingError(SoftAssertions softly) {
var description = "uh oh uh oh";
var id = insertApplicationError(ApplicationError.newUnresolvedError(description));
var desc = "uh oh uh oh";
var id = insertApplicationError(ApplicationError.newUnresolvedError(desc));
softlyAssertNumTimesOccurred(softly, id, 1);

var originalError = getErrorOrThrow(id);
Expand Down Expand Up @@ -727,7 +726,7 @@ private List<Long> insertErrorsWithResolvedAs(int count, Resolved resolved) {
private List<Long> insertErrorsWithResolvedAs(int count, Resolved resolved, String host) {
return IntStream.rangeClosed(1, count).mapToObj(value ->
insertApplicationError(randomApplicationErrorWithResolvedAndHost(resolved, host)))
.collect(toList());
.toList();
}

private ApplicationError defaultApplicationError() {
Expand All @@ -752,8 +751,8 @@ private ApplicationError randomUnresolvedApplicationErrorOnHost(String host) {

private ApplicationError randomApplicationErrorWithResolvedAndHost(Resolved resolved, String hostName) {
var number = random.nextInt(100_000);
var throwable = newThrowable("message " + number, "cause message " + number);
return ApplicationError.newError(description + " " + number, resolved, hostName, ipAddress, port, throwable);
var error = newThrowable("message " + number, "cause message " + number);
return ApplicationError.newError(description + " " + number, resolved, hostName, ipAddress, port, error);
}

private ApplicationError newApplicationError(String description, Resolved resolved) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CreateInMemoryH2Database {

@Test
void shouldCreateInMemoryDatabase() {
var dataSourceFactory = ApplicationErrorJdbc.createInMemoryH2Database();
dataSourceFactory = ApplicationErrorJdbc.createInMemoryH2Database();

var url = dataSourceFactory.getUrl();
var user = dataSourceFactory.getUser();
Expand Down Expand Up @@ -81,7 +81,7 @@ class GetDatabaseProductNameOrUnknown {

@ClearBoxTest
void shouldGetDatabaseProductName() throws SQLException {
var dataSourceFactory = ApplicationErrorJdbc.createInMemoryH2Database();
dataSourceFactory = ApplicationErrorJdbc.createInMemoryH2Database();

var url = dataSourceFactory.getUrl();
var user = dataSourceFactory.getUser();
Expand All @@ -95,7 +95,7 @@ void shouldGetDatabaseProductName() throws SQLException {

@ClearBoxTest
void shouldReturnUnknownIfExceptionThrown() throws SQLException {
var dataSourceFactory = ApplicationErrorJdbc.createInMemoryH2Database();
dataSourceFactory = ApplicationErrorJdbc.createInMemoryH2Database();

var url = dataSourceFactory.getUrl();
var user = dataSourceFactory.getUser();
Expand Down Expand Up @@ -204,7 +204,6 @@ void shouldReturnFalse_WhenGivenNullDataSourceFactory() {
"jdbc:sqlite:sample.db"
})
void shouldReturnFalse_WhenDriverIsH2_ButUrlIsNotH2(String jdbcUrl) {
var dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setDriverClass(Driver.class.getName());
dataSourceFactory.setUrl(jdbcUrl);
assertThat(ApplicationErrorJdbc.isH2DataStore(dataSourceFactory)).isFalse();
Expand All @@ -217,7 +216,6 @@ void shouldReturnFalse_WhenDriverIsH2_ButUrlIsNotH2(String jdbcUrl) {
"org.acme.db.Driver"
})
void shouldReturnFalse_WhenGivenNonH2DataSourceFactory(String value) {
var dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setDriverClass(value);
assertThat(ApplicationErrorJdbc.isH2DataStore(dataSourceFactory)).isFalse();
}
Expand All @@ -229,7 +227,6 @@ void shouldReturnFalse_WhenGivenNonH2DataSourceFactory(String value) {
"jdbc:h2:tcp://localhost/~/test"
})
void shouldReturnTrue_WhenGivenH2DataSourceFactory(String jdbcUrl) {
var dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setDriverClass(Driver.class.getName());
dataSourceFactory.setUrl(jdbcUrl);
assertThat(ApplicationErrorJdbc.isH2DataStore(dataSourceFactory)).isTrue();
Expand All @@ -254,7 +251,6 @@ void shouldReturnFalse_WhenGivenNullDataSourceFactory() {
org.h2.Driver, null
""", nullValues = "null")
void shouldReturnFalse_WhenGivenNonH2_DataSourceFactory(String driverClass, String jdbcUrl) {
var dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setDriverClass(driverClass);
dataSourceFactory.setUrl(jdbcUrl);
assertThat(ApplicationErrorJdbc.isH2EmbeddedDataStore(dataSourceFactory)).isFalse();
Expand All @@ -267,7 +263,6 @@ void shouldReturnFalse_WhenGivenNonH2_DataSourceFactory(String driverClass, Stri
"org.acme.db.Driver"
})
void shouldReturnFalse_WhenGivenNonH2DataSourceFactory(String value) {
var dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setDriverClass(value);
assertThat(ApplicationErrorJdbc.isH2EmbeddedDataStore(dataSourceFactory)).isFalse();
}
Expand Down Expand Up @@ -304,7 +299,6 @@ void shouldReturnFalse_WhenGivenNonH2DataSourceFactory(String value) {
jdbc:h2:/data/test;auto_server=true, false
""")
void shouldReturnTrue_WhenGivenEmbeddedH2DataSourceFactory(String url, boolean isEmbeddedUrl) {
var dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setDriverClass(Driver.class.getName());
dataSourceFactory.setUrl(url);
assertThat(ApplicationErrorJdbc.isH2EmbeddedDataStore(dataSourceFactory)).isEqualTo(isEmbeddedUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ShouldConstruct {
"72, HOURS, 3 days",
})
void fromAmountAndChronoUnit(long windowAmount, ChronoUnit windowUnit, String expectedReadableWindow) {
var healthCheck = newHealthCheck(windowAmount, windowUnit);
healthCheck = newHealthCheck(windowAmount, windowUnit);

assertThat(healthCheck.getTimeWindowAmount()).isEqualTo(windowAmount);
assertThat(healthCheck.getTimeWindowUnit()).isEqualTo(windowUnit);
Expand All @@ -99,7 +99,7 @@ private RecentErrorsHealthCheck newHealthCheck(long timeWindowAmount, TemporalUn
})
void fromTimeWindowObject(String durationString, String expectedReadableWindow) {
var duration = Duration.parse(durationString);
var healthCheck = newHealthCheck(new TimeWindow(duration));
healthCheck = newHealthCheck(new TimeWindow(duration));

var javaTimeDuration = duration.toJavaDuration();
assertThat(healthCheck.getTimeWindowAmount()).isEqualTo(javaTimeDuration.toMillis());
Expand All @@ -123,7 +123,7 @@ private RecentErrorsHealthCheck newHealthCheck(TimeWindow timeWindow) {
})
void fromDropwizardDuration(String durationString, String expectedReadableWindow) {
var duration = Duration.parse(durationString);
var healthCheck = newHealthCheck(duration);
healthCheck = newHealthCheck(duration);

assertThat(healthCheck.getTimeWindow()).isEqualTo(duration.toJavaDuration());
assertThat(healthCheck.getHumanReadableTimeWindow()).isEqualTo(expectedReadableWindow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ class NewUnresolvedError {

@Test
void shouldCreateWithOnlyDescription_AndPresetPersistentHostInformation(SoftAssertions softly) {
error = ApplicationError.newUnresolvedError(description, throwable);
error = ApplicationError.newUnresolvedError(description);

softly.assertThat(error.isResolved()).isFalse();
assertCommonProperties(softly);
assertExceptionProperties(softly);
assertNoExceptionProperties(softly);
}

@Test
Expand Down Expand Up @@ -246,7 +246,7 @@ void shouldValidateArguments(String description,
String ipAddress,
int port,
String expectedErrorMessage) {

assertThatThrownBy(() ->
ApplicationError.newError(description, resolved, hostName, ipAddress, port, null))
.isExactlyInstanceOf(IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.dropwizard.error.resource;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.kiwiproject.test.jaxrs.JaxrsTestHelper.assertInternalServerErrorResponse;
import static org.kiwiproject.test.jaxrs.JaxrsTestHelper.assertNotFoundResponse;
Expand Down Expand Up @@ -99,7 +98,7 @@ void shouldGetAllAllErrors() {
var randomBoolean = ThreadLocalRandom.current().nextBoolean();
return newApplicationError("error " + value, Resolved.of(randomBoolean));
})
.collect(toList());
.toList();

when(ERROR_DAO.getErrors(ApplicationErrorStatus.ALL, pageNumber, pageSize)).thenReturn(errors);
when(ERROR_DAO.count(ApplicationErrorStatus.ALL)).thenReturn(totalCount);
Expand All @@ -121,7 +120,7 @@ void shouldGetUnresolvedErrors() {
var totalCount = 42L;
var errors = IntStream.rangeClosed(1, pageSize)
.mapToObj(value -> newApplicationError("error " + value, Resolved.NO))
.collect(toList());
.toList();

when(ERROR_DAO.getErrors(ApplicationErrorStatus.UNRESOLVED, pageNumber, pageSize)).thenReturn(errors);
when(ERROR_DAO.count(ApplicationErrorStatus.UNRESOLVED)).thenReturn(totalCount);
Expand All @@ -143,7 +142,7 @@ void shouldGetResolvedErrors() {
var totalCount = 42L;
var errors = IntStream.rangeClosed(1, pageSize)
.mapToObj(value -> newApplicationError("error " + value, Resolved.YES))
.collect(toList());
.toList();

when(ERROR_DAO.getErrors(ApplicationErrorStatus.RESOLVED, pageNumber, pageSize)).thenReturn(errors);
when(ERROR_DAO.count(ApplicationErrorStatus.RESOLVED)).thenReturn(totalCount);
Expand Down Expand Up @@ -231,7 +230,7 @@ private void assertApplicationsErrorsResponse(Response response,
assertThat(applicationErrors.getPageSize()).isEqualTo(expectedPageSize);
assertThat(applicationErrors.getTotalCount()).isEqualTo(expectedTotalCount);

List<String> errorDescriptions = expectedErrors.stream().map(ApplicationError::getDescription).collect(toList());
List<String> errorDescriptions = expectedErrors.stream().map(ApplicationError::getDescription).toList();

assertThat(applicationErrors.getItems())
.describedAs("should have %d errors with matching descriptions", expectedPageSize)
Expand Down

0 comments on commit ca26310

Please sign in to comment.