Sonar Fixes (7) (#2216)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -22,6 +22,7 @@ import com.cronutils.model.definition.CronDefinitionBuilder;
|
||||
import com.cronutils.model.time.ExecutionTime;
|
||||
import com.cronutils.parser.CronParser;
|
||||
import org.eclipse.hawkbit.repository.exception.InvalidMaintenanceScheduleException;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -170,14 +171,14 @@ public final class MaintenanceScheduleHelper {
|
||||
}
|
||||
|
||||
private static boolean atLeastOneNotEmpty(final String cronSchedule, final String duration, final String timezone) {
|
||||
return !(StringUtils.isEmpty(cronSchedule) && StringUtils.isEmpty(duration) && StringUtils.isEmpty(timezone));
|
||||
return !(ObjectUtils.isEmpty(cronSchedule) && ObjectUtils.isEmpty(duration) && ObjectUtils.isEmpty(timezone));
|
||||
}
|
||||
|
||||
private static boolean allNotEmpty(final String cronSchedule, final String duration, final String timezone) {
|
||||
return !StringUtils.isEmpty(cronSchedule) && !StringUtils.isEmpty(duration) && !StringUtils.isEmpty(timezone);
|
||||
return !ObjectUtils.isEmpty(cronSchedule) && !ObjectUtils.isEmpty(duration) && !ObjectUtils.isEmpty(timezone);
|
||||
}
|
||||
|
||||
private static LocalTime convertDurationToLocalTime(final String timeInterval) {
|
||||
return LocalTime.parse(StringUtils.trimWhitespace(timeInterval));
|
||||
return LocalTime.parse(timeInterval.strip());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,25 +24,25 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
@Feature("Unit Tests - Repository")
|
||||
@Story("Maintenance Schedule Utility")
|
||||
public class MaintenanceScheduleHelperTest {
|
||||
class MaintenanceScheduleHelperTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that the Cron object is returned for valid cron expression")
|
||||
public void getCronFromExpressionValid() {
|
||||
void getCronFromExpressionValid() {
|
||||
final String validCron = "0 0 0 ? * 6"; // at 00:00 every Saturday
|
||||
assertThat(MaintenanceScheduleHelper.getCronFromExpression(validCron)).isNotNull().isInstanceOf(Cron.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Verifies that the Duration object is returned for valid duration format (hh:mm or hh:mm:ss)")
|
||||
public void convertToISODurationValid() {
|
||||
void convertToISODurationValid() {
|
||||
final String duration = "00:10";
|
||||
assertThat(MaintenanceScheduleHelper.convertToISODuration(duration)).isNotNull().isInstanceOf(Duration.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Verifies that the InvalidMaintenanceScheduleException is thrown for invalid duration format")
|
||||
public void validateDurationInvalid() {
|
||||
void validateDurationInvalid() {
|
||||
final String duration = "10";
|
||||
assertThatThrownBy(() -> MaintenanceScheduleHelper.validateDuration(duration))
|
||||
.isInstanceOf(InvalidMaintenanceScheduleException.class).hasMessage("Provided duration is not valid")
|
||||
@@ -51,7 +51,7 @@ public class MaintenanceScheduleHelperTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that the InvalidMaintenanceScheduleException is thrown for invalid cron expression")
|
||||
public void validateCronScheduleInvalid() {
|
||||
void validateCronScheduleInvalid() {
|
||||
final String invalidCron = "0 0 0 * * 6";
|
||||
assertThatThrownBy(() -> MaintenanceScheduleHelper.validateCronSchedule(invalidCron))
|
||||
.isInstanceOf(InvalidMaintenanceScheduleException.class)
|
||||
@@ -60,7 +60,7 @@ public class MaintenanceScheduleHelperTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that there is a maintenance window available for correct schedule, duration and timezone")
|
||||
public void getNextMaintenanceWindowValid() {
|
||||
void getNextMaintenanceWindowValid() {
|
||||
final ZonedDateTime currentTime = ZonedDateTime.now();
|
||||
final String cronSchedule = String.format("0 %d %d %d %d ? %d", currentTime.getMinute(), currentTime.getHour(),
|
||||
currentTime.getDayOfMonth(), currentTime.getMonthValue(), currentTime.getYear());
|
||||
@@ -71,7 +71,7 @@ public class MaintenanceScheduleHelperTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies the maintenance schedule when only one required field is present")
|
||||
public void validateMaintenanceScheduleAtLeastOneNotEmpty() {
|
||||
void validateMaintenanceScheduleAtLeastOneNotEmpty() {
|
||||
final String duration = "00:10";
|
||||
assertThatThrownBy(() -> MaintenanceScheduleHelper.validateMaintenanceSchedule(null, duration, null))
|
||||
.isInstanceOf(InvalidMaintenanceScheduleException.class)
|
||||
@@ -80,7 +80,7 @@ public class MaintenanceScheduleHelperTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that there is no valid maintenance window available, scheduled before current time")
|
||||
public void validateMaintenanceScheduleBeforeCurrentTime() {
|
||||
void validateMaintenanceScheduleBeforeCurrentTime() {
|
||||
ZonedDateTime currentTime = ZonedDateTime.now();
|
||||
currentTime = currentTime.plusMinutes(-30);
|
||||
final String cronSchedule = String.format("0 %d %d %d %d ? %d", currentTime.getMinute(), currentTime.getHour(),
|
||||
|
||||
@@ -183,7 +183,7 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement {
|
||||
@Retryable(retryFor = { ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX,
|
||||
backoff = @Backoff(delay = Constants.TX_RT_DELAY))
|
||||
public void delete(final long id) {
|
||||
delete(List.of(id));
|
||||
delete0(List.of(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -191,6 +191,9 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement {
|
||||
@Retryable(retryFor = { ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX,
|
||||
backoff = @Backoff(delay = Constants.TX_RT_DELAY))
|
||||
public void delete(final Collection<Long> ids) {
|
||||
delete0(ids);
|
||||
}
|
||||
private void delete0(final Collection<Long> ids) {
|
||||
final List<JpaSoftwareModule> swModulesToDelete = softwareModuleRepository.findAllById(ids);
|
||||
if (swModulesToDelete.size() < ids.size()) {
|
||||
throw new EntityNotFoundException(SoftwareModule.class, ids,
|
||||
@@ -283,7 +286,7 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement {
|
||||
// touch to update revision and last modified timestamp
|
||||
JpaManagementHelper.touch(entityManager, softwareModuleRepository, (JpaSoftwareModule) get(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, id)));
|
||||
return createJpaMetadataCreateStream(create).map(this::saveMetadata).collect(Collectors.toList());
|
||||
return createJpaMetadataCreateStream(create).map(this::saveMetadata).toList();
|
||||
} else {
|
||||
// group by software module id to minimize database access
|
||||
final Map<Long, List<JpaSoftwareModuleMetadataCreate>> groups = createJpaMetadataCreateStream(create)
|
||||
@@ -298,7 +301,7 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement {
|
||||
JpaManagementHelper.touch(entityManager, softwareModuleRepository, (JpaSoftwareModule) get(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, id)));
|
||||
return group.stream().map(this::saveMetadata);
|
||||
}).collect(Collectors.toList());
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,11 +349,10 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement {
|
||||
@Retryable(retryFor = { ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX,
|
||||
backoff = @Backoff(delay = Constants.TX_RT_DELAY))
|
||||
public void deleteMetaData(final long id, final String key) {
|
||||
final JpaSoftwareModuleMetadata metadata = (JpaSoftwareModuleMetadata) findMetaDataBySoftwareModuleId(id,
|
||||
key).orElseThrow(() -> new EntityNotFoundException(SoftwareModuleMetadata.class, id, key));
|
||||
final JpaSoftwareModuleMetadata metadata = (JpaSoftwareModuleMetadata) findMetaDataBySoftwareModuleId0(id, key)
|
||||
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleMetadata.class, id, key));
|
||||
|
||||
JpaManagementHelper.touch(entityManager, softwareModuleRepository,
|
||||
(JpaSoftwareModule) metadata.getSoftwareModule());
|
||||
JpaManagementHelper.touch(entityManager, softwareModuleRepository, metadata.getSoftwareModule());
|
||||
softwareModuleMetadataRepository.deleteById(metadata.getId());
|
||||
}
|
||||
|
||||
@@ -427,6 +429,9 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement {
|
||||
|
||||
@Override
|
||||
public Optional<SoftwareModuleMetadata> findMetaDataBySoftwareModuleId(final long id, final String key) {
|
||||
return findMetaDataBySoftwareModuleId0(id, key);
|
||||
}
|
||||
private Optional<SoftwareModuleMetadata> findMetaDataBySoftwareModuleId0(final long id, final String key) {
|
||||
assertSoftwareModuleExists(id);
|
||||
|
||||
return softwareModuleMetadataRepository.findById(new SwMetadataCompositeKey(id, key))
|
||||
@@ -510,7 +515,7 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement {
|
||||
accessController.assertOperationAllowed(AccessController.Operation.DELETE, swModule));
|
||||
final Set<String> sha1Hashes = swModule.getArtifacts().stream().map(Artifact::getSha1Hash).collect(Collectors.toSet());
|
||||
AfterTransactionCommitExecutorHolder.getInstance().getAfterCommit().afterCommit(() ->
|
||||
sha1Hashes.forEach(sha1Hash -> ((JpaArtifactManagement) artifactManagement).clearArtifactBinary(sha1Hash)));
|
||||
sha1Hashes.forEach(((JpaArtifactManagement) artifactManagement)::clearArtifactBinary));
|
||||
}
|
||||
|
||||
private Specification<JpaSoftwareModule> buildSmSearchQuerySpec(final String searchText) {
|
||||
|
||||
@@ -176,6 +176,9 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
// Exception squid:S2229 - calling findTenants without transaction is intended in this case
|
||||
@SuppressWarnings("squid:S2229")
|
||||
public void forEachTenant(final Consumer<String> consumer) {
|
||||
forEachTenant0(consumer);
|
||||
}
|
||||
private void forEachTenant0(final Consumer<String> consumer) {
|
||||
Page<String> tenants;
|
||||
Pageable query = PageRequest.of(0, MAX_TENANTS_QUERY);
|
||||
do {
|
||||
@@ -281,7 +284,7 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
}
|
||||
|
||||
private void usageStatsPerTenant(final SystemUsageReportWithTenants report) {
|
||||
forEachTenant(tenant -> report.addTenantData(systemStatsManagement.getStatsOfTenant()));
|
||||
forEachTenant0(tenant -> report.addTenantData(systemStatsManagement.getStatsOfTenant()));
|
||||
}
|
||||
|
||||
private DistributionSetType createStandardSoftwareDataSetup() {
|
||||
|
||||
@@ -606,9 +606,8 @@ public class JpaTargetManagement implements TargetManagement {
|
||||
public Target assignType(final String controllerId, final Long targetTypeId) {
|
||||
final JpaTarget target = getByControllerIdAndThrowIfNotFound(controllerId);
|
||||
|
||||
targetRepository.getAccessController().ifPresent(acm -> {
|
||||
acm.assertOperationAllowed(AccessController.Operation.UPDATE, target);
|
||||
});
|
||||
targetRepository.getAccessController().ifPresent(acm ->
|
||||
acm.assertOperationAllowed(AccessController.Operation.UPDATE, target));
|
||||
|
||||
final JpaTargetType targetType = getTargetTypeByIdAndThrowIfNotFound(targetTypeId);
|
||||
target.setTargetType(targetType);
|
||||
|
||||
@@ -286,7 +286,7 @@ public class JpaTenantConfigurationManagement implements TenantConfigurationMana
|
||||
@SuppressWarnings("squid:S1172")
|
||||
private void assertAutoCloseValueChange(final String key, final JpaTenantConfiguration valueChange) {
|
||||
if (REPOSITORY_ACTIONS_AUTOCLOSE_ENABLED.equals(key)
|
||||
&& getConfigurationValue(MULTI_ASSIGNMENTS_ENABLED, Boolean.class).getValue()) {
|
||||
&& Boolean.TRUE.equals(getConfigurationValue(MULTI_ASSIGNMENTS_ENABLED, Boolean.class).getValue())) {
|
||||
log.debug(
|
||||
"The property '{}' must not be changed because the Multi-Assignments feature is currently enabled.",
|
||||
key);
|
||||
|
||||
@@ -43,6 +43,7 @@ import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder;
|
||||
@Table(name = "sp_target_type", indexes = {
|
||||
@Index(name = "sp_idx_target_type_prim", columnList = "tenant,id") }, uniqueConstraints = {
|
||||
@UniqueConstraint(columnNames = { "name", "tenant" }, name = "uk_target_type_name") })
|
||||
@SuppressWarnings("java:S2160") // the super class equals/hashcode shall be used
|
||||
public class JpaTargetType extends AbstractJpaTypeEntity implements TargetType, EventAwareEntity {
|
||||
|
||||
@Serial
|
||||
|
||||
@@ -11,19 +11,11 @@ package org.eclipse.hawkbit.repository.jpa.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.BaseEntity;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.repository.query.FluentQuery;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.test.context.TestPropertySource;
|
||||
@TestPropertySource(locations = "classpath:/jpa-test.properties", properties = {
|
||||
"hawkbit.server.repository.eagerPollPersistence=false",
|
||||
"hawkbit.server.repository.pollPersistenceFlushTime=1000" })
|
||||
public class LazyControllerManagementTest extends AbstractJpaIntegrationTest {
|
||||
class LazyControllerManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private RepositoryProperties repositoryProperties;
|
||||
@@ -42,7 +42,7 @@ public class LazyControllerManagementTest extends AbstractJpaIntegrationTest {
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 1),
|
||||
@Expect(type = TargetPollEvent.class, count = 2) })
|
||||
public void lazyFindOrRegisterTargetIfItDoesNotExist() throws InterruptedException {
|
||||
void lazyFindOrRegisterTargetIfItDoesNotExist() throws InterruptedException {
|
||||
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST);
|
||||
assertThat(target).as("target should not be null").isNotNull();
|
||||
|
||||
|
||||
@@ -36,13 +36,13 @@ import org.springframework.orm.jpa.vendor.Database;
|
||||
|
||||
@Feature("Component Tests - Repository")
|
||||
@Story("RSQL filter distribution set")
|
||||
public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
private DistributionSet ds;
|
||||
private SoftwareModule sm;
|
||||
|
||||
@BeforeEach
|
||||
public void setupBeforeTest() {
|
||||
void setupBeforeTest() {
|
||||
|
||||
sm = testdataFactory.createSoftwareModuleApp("SM");
|
||||
|
||||
@@ -70,7 +70,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by id")
|
||||
public void testFilterByParameterId() {
|
||||
void testFilterByParameterId() {
|
||||
assertRSQLQuery(DistributionSetFields.ID.name() + "==" + ds.getId(), 1);
|
||||
assertRSQLQuery(DistributionSetFields.ID.name() + "!=" + ds.getId(), 4);
|
||||
assertRSQLQuery(DistributionSetFields.ID.name() + "==" + -1, 0);
|
||||
@@ -87,7 +87,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by name")
|
||||
public void testFilterByParameterName() {
|
||||
void testFilterByParameterName() {
|
||||
assertRSQLQuery(DistributionSetFields.NAME.name() + "==DS", 1);
|
||||
assertRSQLQuery(DistributionSetFields.NAME.name() + "!=DS", 4);
|
||||
assertRSQLQuery(DistributionSetFields.NAME.name() + "==*DS", 4);
|
||||
@@ -98,7 +98,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by assigned software module")
|
||||
public void testFilterBySoftwareModule() {
|
||||
void testFilterBySoftwareModule() {
|
||||
assertRSQLQuery(DistributionSetFields.MODULE.name() + "." + SoftwareModuleFields.NAME.name() + "==" + sm.getName(), 1);
|
||||
assertRSQLQuery(DistributionSetFields.MODULE.name() + "." + SoftwareModuleFields.ID.name() + "==" + sm.getId(), 1);
|
||||
assertRSQLQuery(DistributionSetFields.MODULE.name() + "." + SoftwareModuleFields.NAME.name() + "==noExist", 0);
|
||||
@@ -108,7 +108,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by description")
|
||||
public void testFilterByParameterDescription() {
|
||||
void testFilterByParameterDescription() {
|
||||
assertRSQLQuery(DistributionSetFields.DESCRIPTION.name() + "==''", 1);
|
||||
assertRSQLQuery(DistributionSetFields.DESCRIPTION.name() + "!=''", 4);
|
||||
assertRSQLQuery(DistributionSetFields.DESCRIPTION.name() + "==DS", 1);
|
||||
@@ -123,7 +123,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by version")
|
||||
public void testFilterByParameterVersion() {
|
||||
void testFilterByParameterVersion() {
|
||||
assertRSQLQuery(DistributionSetFields.VERSION.name() + "==" + TestdataFactory.DEFAULT_VERSION, 1);
|
||||
assertRSQLQuery(DistributionSetFields.VERSION.name() + "!=" + TestdataFactory.DEFAULT_VERSION, 4);
|
||||
assertRSQLQuery(DistributionSetFields.VERSION.name() + "=in=(" + TestdataFactory.DEFAULT_VERSION + ",1.0.0,1.0.1)", 3);
|
||||
@@ -132,7 +132,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by complete property")
|
||||
public void testFilterByAttributeComplete() {
|
||||
void testFilterByAttributeComplete() {
|
||||
assertRSQLQuery(DistributionSetFields.COMPLETE.name() + "==true", 3);
|
||||
try {
|
||||
assertRSQLQuery(DistributionSetFields.COMPLETE.name() + "==noExist*", 0);
|
||||
@@ -145,7 +145,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by valid property")
|
||||
public void testFilterByAttributeValid() {
|
||||
void testFilterByAttributeValid() {
|
||||
assertRSQLQuery(DistributionSetFields.VALID.name() + "==true", 4);
|
||||
assertRSQLQuery(DistributionSetFields.VALID.name() + "==false", 1);
|
||||
assertThatExceptionOfType(RSQLParameterSyntaxException.class)
|
||||
@@ -156,7 +156,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by tag name")
|
||||
public void testFilterByTag() {
|
||||
void testFilterByTag() {
|
||||
assertRSQLQuery(DistributionSetFields.TAG.name() + "==Tag1", 2);
|
||||
assertRSQLQuery(DistributionSetFields.TAG.name() + "!=Tag1", 3);
|
||||
assertRSQLQuery(DistributionSetFields.TAG.name() + "==T*", 2);
|
||||
@@ -167,7 +167,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by type key")
|
||||
public void testFilterByType() {
|
||||
void testFilterByType() {
|
||||
assertRSQLQuery(DistributionSetFields.TYPE.name() + "==" + TestdataFactory.DS_TYPE_DEFAULT, 4);
|
||||
assertRSQLQuery(DistributionSetFields.TYPE.name() + "==noExist*", 0);
|
||||
assertRSQLQuery(DistributionSetFields.TYPE.name() + "=in=(" + TestdataFactory.DS_TYPE_DEFAULT + ",ecl)", 4);
|
||||
@@ -176,7 +176,7 @@ public class RSQLDistributionSetFieldTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test filter distribution set by metadata")
|
||||
public void testFilterByMetadata() {
|
||||
void testFilterByMetadata() {
|
||||
createDistributionSetWithMetadata("key.dot", "value.dot");
|
||||
|
||||
assertRSQLQuery(DistributionSetFields.METADATA.name() + ".metaKey==metaValue", 1);
|
||||
|
||||
Reference in New Issue
Block a user