Skip to content

Commit

Permalink
Improve autowiring for tests (Sonar) (#2217)
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <[email protected]>
  • Loading branch information
avgustinmm authored Jan 22, 2025
1 parent 3d390b9 commit 4909a65
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ void successfullySerializeAndDeserializeEvent() {
// deserialize
when(messageMock.getPayload()).thenReturn(serializedEvent);
final Object deserializedEvent = underTest.convertFromInternal(messageMock, RemoteApplicationEvent.class, null);
assertThat(deserializedEvent).isInstanceOf(TargetCreatedEvent.class);
assertThat(deserializedEvent).isEqualTo(targetCreatedEvent);
assertThat(deserializedEvent)
.isInstanceOf(TargetCreatedEvent.class)
.isEqualTo(targetCreatedEvent);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent;
import org.eclipse.hawkbit.repository.jpa.configuration.Constants;
import org.eclipse.hawkbit.repository.jpa.executor.AfterTransactionCommitExecutor;
import org.eclipse.hawkbit.repository.jpa.model.AbstractJpaBaseEntity_;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction_;
Expand Down Expand Up @@ -60,12 +61,14 @@ public abstract class AbstractDsAssignmentStrategy {
protected final AfterTransactionCommitExecutor afterCommit;
protected final EventPublisherHolder eventPublisherHolder;
protected final ActionRepository actionRepository;

private final ActionStatusRepository actionStatusRepository;
private final QuotaManagement quotaManagement;
private final BooleanSupplier multiAssignmentsConfig;
private final BooleanSupplier confirmationFlowConfig;
private final RepositoryProperties repositoryProperties;

@SuppressWarnings("java:S107")
AbstractDsAssignmentStrategy(
final TargetRepository targetRepository,
final AfterTransactionCommitExecutor afterCommit, final EventPublisherHolder eventPublisherHolder,
Expand Down Expand Up @@ -149,7 +152,7 @@ protected List<Long> overrideObsoleteUpdateActions(final Collection<Long> target
cb.equal(root.get(JpaAction_.active), true),
cb.equal(root.get(JpaAction_.distributionSet).get(JpaDistributionSet_.requiredMigrationStep), false),
cb.notEqual(root.get(JpaAction_.status), Action.Status.CANCELING),
root.get(JpaAction_.target).get(JpaTarget_.id).in(targetsIds)
root.get(JpaAction_.target).get(AbstractJpaBaseEntity_.id).in(targetsIds)
);
});

Expand Down Expand Up @@ -187,7 +190,7 @@ protected List<Long> closeObsoleteUpdateActions(final Collection<Long> targetsId
return cb.and(
cb.equal(root.get(JpaAction_.active), true),
cb.equal(root.get(JpaAction_.distributionSet).get(JpaDistributionSet_.requiredMigrationStep), false),
root.get(JpaAction_.target).get(JpaTarget_.id).in(targetsIds)
root.get(JpaAction_.target).get(AbstractJpaBaseEntity_.id).in(targetsIds)
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
/**
* Test the remote entity events.
*/
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
public abstract class AbstractRemoteEventTest extends AbstractJpaIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
@ContextConfiguration(classes = { RepositoryApplicationConfiguration.class, TestConfiguration.class })
@Import(TestChannelBinderConfiguration.class)
@TestPropertySource(locations = "classpath:/jpa-test.properties")
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
public abstract class AbstractJpaIntegrationTest extends AbstractIntegrationTest {

protected static final String INVALID_TEXT_HTML = "</noscript><br><script>";
Expand All @@ -91,58 +92,40 @@ public abstract class AbstractJpaIntegrationTest extends AbstractIntegrationTest

@Autowired
protected TargetRepository targetRepository;

@Autowired
protected ActionRepository actionRepository;

@Autowired
protected DistributionSetRepository distributionSetRepository;

@Autowired
protected SoftwareModuleRepository softwareModuleRepository;

@Autowired
protected TenantMetaDataRepository tenantMetaDataRepository;

@Autowired
protected DistributionSetTypeRepository distributionSetTypeRepository;

@Autowired
protected SoftwareModuleTypeRepository softwareModuleTypeRepository;

@Autowired
protected TargetTagRepository targetTagRepository;

@Autowired
protected TargetTypeRepository targetTypeRepository;

@Autowired
protected DistributionSetTagRepository distributionSetTagRepository;

@Autowired
protected SoftwareModuleMetadataRepository softwareModuleMetadataRepository;

@Autowired
protected ActionStatusRepository actionStatusRepository;

@Autowired
protected LocalArtifactRepository artifactRepository;

@Autowired
protected RolloutGroupRepository rolloutGroupRepository;

@Autowired
protected RolloutTargetGroupRepository rolloutTargetGroupRepository;

@Autowired
protected RolloutRepository rolloutRepository;

@Autowired
protected TenantConfigurationProperties tenantConfigurationProperties;

@Autowired
protected RolloutTestApprovalStrategy approvalStrategy;

@Autowired
private JpaProperties jpaProperties;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
*/
@Feature("Component Tests - Repository")
@Story("Auto assign checker")
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
class AutoAssignCheckerIntTest extends AbstractJpaIntegrationTest {

private static final String SPACE_AND_DESCRIPTION = " description";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/
@Feature("Component Tests - Repository")
@Story("Action cleanup handler")
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
class AutoActionCleanupTest extends AbstractJpaIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
@Feature("Component Tests - Repository")
@Story("Auto cleanup scheduler")
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
class AutoCleanupSchedulerTest extends AbstractJpaIntegrationTest {

private final AtomicInteger counter = new AtomicInteger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
@Feature("Component Tests - Repository")
@Story("Entity Events")
@SpringBootTest(classes = { RepositoryTestConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
public class RepositoryEntityEventTest extends AbstractJpaIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.qameta.allure.Description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ void deleteDistributionSet() {
.asList(DistributionSetSpecification.isDeleted(true), DistributionSetSpecification.isCompleted(true))),
PAGE).getContent()).as("wrong size of founded ds").hasSize(noOfDistributionSets);

for (final DistributionSet ds : deploymentResult.getDistributionSets()) {
for (final DistributionSet ignored : deploymentResult.getDistributionSets()) {
testdataFactory.sendUpdateActionStatusToTargets(deploymentResult.getDeployedTargets(), Status.FINISHED,
Collections.singletonList("blabla alles gut"));
}
Expand Down Expand Up @@ -1316,7 +1316,7 @@ void deletesTargetsAndVerifyCascadeDeletes() {
final DeploymentResult deploymentResult = prepareComplexRepo(undeployedTargetPrefix, noOfUndeployedTargets,
deployedTargetPrefix, noOfDeployedTargets, noOfDistributionSets, "myTestDS");

for (final DistributionSet ds : deploymentResult.getDistributionSets()) {
for (final DistributionSet ignored : deploymentResult.getDistributionSets()) {
testdataFactory.sendUpdateActionStatusToTargets(deploymentResult.getDeployedTargets(), Status.FINISHED,
Collections.singletonList("blabla alles gut"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

@Feature("Component Tests - Repository")
@Story("RSQL filter suggestion")
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
public class RSQLParserValidationOracleTest extends AbstractJpaIntegrationTest {

private static final String[] OP_SUGGESTIONS = new String[] { "==", "!=", "=ge=", "=le=", "=gt=", "=lt=", "=in=",
Expand All @@ -35,6 +36,7 @@ public class RSQLParserValidationOracleTest extends AbstractJpaIntegrationTest {
.map(field -> field.name().toLowerCase()).toArray(String[]::new);
private static final String[] AND_OR_SUGGESTIONS = new String[] { "and", "or" };
private static final String[] NAME_VERSION_SUGGESTIONS = new String[] { "name", "version" };

@Autowired
private RsqlValidationOracle rsqlValidationOracle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,25 @@
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.event.BusProtoStuffMessageConverter;
import org.eclipse.hawkbit.im.authentication.SpRole;
import org.eclipse.hawkbit.repository.ArtifactManagement;
import org.eclipse.hawkbit.repository.ControllerManagement;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.DistributionSetInvalidationManagement;
import org.eclipse.hawkbit.repository.DistributionSetManagement;
import org.eclipse.hawkbit.repository.DistributionSetTagManagement;
import org.eclipse.hawkbit.repository.DistributionSetTypeManagement;
import org.eclipse.hawkbit.repository.EntityFactory;
import org.eclipse.hawkbit.repository.QuotaManagement;
import org.eclipse.hawkbit.repository.RolloutApprovalStrategy;
import org.eclipse.hawkbit.repository.RolloutHandler;
import org.eclipse.hawkbit.repository.RolloutManagement;
import org.eclipse.hawkbit.repository.RolloutStatusCache;
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
import org.eclipse.hawkbit.repository.SoftwareModuleTypeManagement;
import org.eclipse.hawkbit.repository.TargetFilterQueryManagement;
import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.TargetTagManagement;
import org.eclipse.hawkbit.repository.TargetTypeManagement;
import org.eclipse.hawkbit.repository.event.ApplicationEventFilter;
import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder;
import org.eclipse.hawkbit.repository.rsql.VirtualPropertyReplacer;
Expand Down Expand Up @@ -141,8 +158,22 @@ SystemManagementHolder systemManagementHolder() {
}

@Bean
TestdataFactory testdataFactory() {
return new TestdataFactory();
TestdataFactory testdataFactory(
final ControllerManagement controllerManagement, final ArtifactManagement artifactManagement,
final SoftwareModuleManagement softwareModuleManagement, final SoftwareModuleTypeManagement softwareModuleTypeManagement,
final DistributionSetManagement distributionSetManagement,
final DistributionSetInvalidationManagement distributionSetInvalidationManagement,
final DistributionSetTypeManagement distributionSetTypeManagement,
final TargetManagement targetManagement, final TargetFilterQueryManagement targetFilterQueryManagement,
final TargetTypeManagement targetTypeManagement, final TargetTagManagement targetTagManagement,
final DeploymentManagement deploymentManagement, final DistributionSetTagManagement distributionSetTagManagement,
final RolloutManagement rolloutManagement, final RolloutHandler rolloutHandler,
final QuotaManagement quotaManagement,
final EntityFactory entityFactory) {
return new TestdataFactory(controllerManagement, artifactManagement, softwareModuleManagement, softwareModuleTypeManagement,
distributionSetManagement, distributionSetInvalidationManagement, distributionSetTypeManagement, targetManagement,
targetFilterQueryManagement, targetTypeManagement, targetTagManagement, deploymentManagement,
distributionSetTagManagement, rolloutManagement, rolloutHandler, quotaManagement, entityFactory);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
listeners = { EventVerifier.class, CleanupTestExecutionListener.class },
mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
@TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true")
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
public abstract class AbstractIntegrationTest {

protected static final Pageable PAGE = PageRequest.of(0, 500, Sort.by(Direction.ASC, "id"));
Expand All @@ -120,6 +121,7 @@ public abstract class AbstractIntegrationTest {
* {@link SystemManagement#getTenantMetadata()};
*/
protected static final int DEFAULT_DS_TYPES = RepositoryConstants.DEFAULT_DS_TYPES_IN_TENANT + 1;

@Autowired
protected EntityFactory entityFactory;
@Autowired
Expand Down Expand Up @@ -478,6 +480,7 @@ protected void waitNextMillis() {
}
}

@SuppressWarnings("java:S4042")
private static File createTempDir() {
try {
final File file = Files.createTempDirectory(System.currentTimeMillis() + "_").toFile();
Expand Down
Loading

0 comments on commit 4909a65

Please sign in to comment.