Skip to content

Commit

Permalink
Remove some of the field injections (Sonar recomendtion)
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <[email protected]>
  • Loading branch information
avgustinmm committed Jan 23, 2025
1 parent 4909a65 commit bbd2770
Show file tree
Hide file tree
Showing 25 changed files with 293 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ protected String storeTempFile(final InputStream content) throws IOException {
protected abstract AbstractDbArtifact store(final String tenant, final DbArtifactHash base16Hashes,
final String contentType, final String tempFile) throws IOException;

// java:S1066 - more readable with separate "if" statements
// java:S4042 - delete reason is not needed
@SuppressWarnings({ "java:S1066", "java:S4042" })
static File createTempFile(final boolean directory) {
try {
final File file = (directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ protected static ObjectMapper getMapper() {

protected ResultActions postDeploymentFeedback(final String controllerId, final Long actionId, final String content,
final ResultMatcher statusMatcher) throws Exception {
return postDeploymentFeedback(MediaType.APPLICATION_JSON_UTF8, controllerId, actionId, content.getBytes(), statusMatcher);
return postDeploymentFeedback(MediaType.APPLICATION_JSON, controllerId, actionId, content.getBytes(), statusMatcher);
}

protected ResultActions putInstalledBase(final String controllerId, final String content, final ResultMatcher statusMatcher)
throws Exception {
return mvc.perform(put(INSTALLED_BASE_ROOT, tenantAware.getCurrentTenant(), controllerId)
.content(content.getBytes()).contentType(MediaType.APPLICATION_JSON_UTF8))
.content(content.getBytes()).contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print())
.andExpect(statusMatcher);
}
Expand All @@ -154,7 +154,7 @@ protected ResultActions postDeploymentFeedback(
protected ResultActions postCancelFeedback(
final String controllerId, final Long actionId, final String content,
final ResultMatcher statusMatcher) throws Exception {
return postCancelFeedback(MediaType.APPLICATION_JSON_UTF8, controllerId, actionId, content.getBytes(), statusMatcher);
return postCancelFeedback(MediaType.APPLICATION_JSON, controllerId, actionId, content.getBytes(), statusMatcher);
}

protected ResultActions postCancelFeedback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@
@PropertySource("classpath:/hawkbit-dmf-defaults.properties")
public class AmqpConfiguration {

@Autowired
private AmqpProperties amqpProperties;
@Autowired
private AmqpDeadletterProperties amqpDeadletterProperties;
@Autowired
private ConnectionFactory rabbitConnectionFactory;
@Autowired(required = false)
private final AmqpProperties amqpProperties;
private final AmqpDeadletterProperties amqpDeadletterProperties;
private final ConnectionFactory rabbitConnectionFactory;
private ServiceMatcher serviceMatcher;

public AmqpConfiguration(final AmqpProperties amqpProperties, final AmqpDeadletterProperties amqpDeadletterProperties, final ConnectionFactory rabbitConnectionFactory) {
this.amqpProperties = amqpProperties;
this.amqpDeadletterProperties = amqpDeadletterProperties;
this.rabbitConnectionFactory = rabbitConnectionFactory;
}

@Autowired(required = false) // spring setter injection
public void setServiceMatcher(final ServiceMatcher serviceMatcher) {
this.serviceMatcher = serviceMatcher;
}

/**
* Creates a custom error handler bean.
*
Expand Down Expand Up @@ -262,8 +269,7 @@ public AmqpMessageSenderService amqpSenderServiceBean() {
}

/**
* Create RabbitListenerContainerFactory bean if no listenerContainerFactory
* bean found
* Create RabbitListenerContainerFactory bean if no listenerContainerFactory bean found
*
* @return RabbitListenerContainerFactory bean
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ private static boolean isCorrelationIdNotEmpty(final Message message) {
return StringUtils.hasLength(message.getMessageProperties().getCorrelationId());
}

// Exception squid:MethodCyclomaticComplexity - false positive, is a simple mapping
@SuppressWarnings("squid:MethodCyclomaticComplexity")
@SuppressWarnings("java:S2637" ) // java:S2637 - logAndThrowMessageError throws exception, i.e. doesn't return null
private static @NotNull Status mapStatus(final Message message, final DmfActionUpdateStatus actionUpdateStatus, final Action action) {
Status status = null;
switch (actionUpdateStatus.getActionStatus()) {
Expand Down Expand Up @@ -261,7 +260,7 @@ private static boolean isCorrelationIdNotEmpty(final Message message) {
break;
}
default: {
logAndThrowMessageError(message, "Status for action does not exisit.");
logAndThrowMessageError(message, "Status for action does not exist.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ void convertMessageTest() {
final DmfActionUpdateStatus actionUpdateStatus = createActionStatus();
when(rabbitTemplate.getMessageConverter()).thenReturn(new Jackson2JsonMessageConverter());

final Message message = rabbitTemplate.getMessageConverter().toMessage(actionUpdateStatus,
createJsonProperties());
final DmfActionUpdateStatus convertedActionUpdateStatus = baseAmqpService.convertMessage(message,
DmfActionUpdateStatus.class);

assertThat(convertedActionUpdateStatus).isEqualToComparingFieldByField(actionUpdateStatus);
final Message message = rabbitTemplate.getMessageConverter().toMessage(actionUpdateStatus, createJsonProperties());
final DmfActionUpdateStatus convertedActionUpdateStatus = baseAmqpService.convertMessage(message, DmfActionUpdateStatus.class);
assertThat(convertedActionUpdateStatus).usingRecursiveComparison().isEqualTo(actionUpdateStatus);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,14 @@ protected String getExchange() {
}

@Step
protected DistributionSet createTargetAndDistributionSetAndAssign(final String controllerId,
final Action.ActionType actionType) {
protected DistributionSet createTargetAndDistributionSetAndAssign(final String controllerId, final Action.ActionType actionType) {
registerAndAssertTargetWithExistingTenant(controllerId);

final DistributionSet distributionSet = testdataFactory.createDistributionSet(UUID.randomUUID().toString());
testdataFactory.addSoftwareModuleMetadata(distributionSet);
final DistributionSet distributionSetLocal = testdataFactory.createDistributionSet(UUID.randomUUID().toString());
testdataFactory.addSoftwareModuleMetadata(distributionSetLocal);

assignDistributionSet(distributionSet.getId(), controllerId, actionType);
return distributionSet;
assignDistributionSet(distributionSetLocal.getId(), controllerId, actionType);
return distributionSetLocal;
}

protected void assertSoftwareModules(final Set<SoftwareModule> expectedSoftwareModules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
@Import(TestChannelBinderConfiguration.class)
// Dirty context is necessary to create a new vhost and recreate all necessary beans after every test class.
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
public abstract class AbstractAmqpIntegrationTest extends AbstractIntegrationTest {

private static final Duration TIMEOUT = Duration.ofSeconds(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,40 @@
import java.util.Optional;
import java.util.Set;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.repository.exception.ArtifactEncryptionUnsupportedException;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Service responsible for encryption operations.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings("java:S6548") // singleton holder ensures static access to spring resources in some places
public final class ArtifactEncryptionService {

private static final ArtifactEncryptionService SINGLETON = new ArtifactEncryptionService();

@Autowired(required = false)
private ArtifactEncryption artifactEncryption;

@Autowired(required = false)
private ArtifactEncryptionSecretsStore artifactEncryptionSecretsStore;

private ArtifactEncryptionService() {
}

/**
* @return the artifact encryption service singleton instance
*/
public static ArtifactEncryptionService getInstance() {
return SINGLETON;
}

@Autowired(required = false) // spring setter injection
public void setArtifactEncryption(final ArtifactEncryption artifactEncryption) {
this.artifactEncryption = artifactEncryption;
}

@Autowired(required = false) // spring setter injection
public void setArtifactEncryptionSecretsStore(final ArtifactEncryptionSecretsStore artifactEncryptionSecretsStore) {
this.artifactEncryptionSecretsStore = artifactEncryptionSecretsStore;
}

/**
* Checks if required encryption and secrets store beans are present.
*
Expand All @@ -51,8 +59,7 @@ public boolean isEncryptionSupported() {
}

/**
* Generates encryption secrets and saves them in secret store by software
* module id reference.
* Generates encryption secrets and saves them in secret store by software module id reference.
*
* @param smId software module id
*/
Expand Down Expand Up @@ -116,7 +123,6 @@ private Map<String, String> getSoftwareModuleEncryptionSecrets(final long smId)
requiredSecretsKey);
requiredSecretsValue.ifPresent(secretValue -> requiredSecrets.put(requiredSecretsKey, secretValue));
}

return requiredSecrets;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
* A singleton bean which holds the event entity manager to have autowiring in the events.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings("java:S6548") // java:S6548 - singleton holder ensures static access to spring resources in some places
public final class EventEntityManagerHolder {

private static final EventEntityManagerHolder SINGLETON = new EventEntityManagerHolder();

@Autowired
private EventEntityManager eventEntityManager;

/**
Expand All @@ -31,6 +31,11 @@ public static EventEntityManagerHolder getInstance() {
return SINGLETON;
}

@Autowired // spring setter injection
public void setEventEntityManager(final EventEntityManager eventEntityManager) {
this.eventEntityManager = eventEntityManager;
}

/**
* @return the eventEntityManager
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
@Getter
@SuppressWarnings("java:S6548") // singleton holder ensures static access to spring resources in some places
public final class RsqlConfigHolder {

private static final RsqlConfigHolder SINGLETON = new RsqlConfigHolder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@
import org.springframework.context.ApplicationEventPublisher;

/**
* A singleton bean which holds the event publisher and service origin Id in
* order to publish remote application events. It can be used in beans not
* instantiated by spring e.g. JPA entities which cannot be auto-wired.
* A singleton bean which holds the event publisher and service origin id in order to publish remote application events.
* It can be used in beans not instantiated by spring e.g. JPA entities which cannot be auto-wired.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings("java:S6548") // java:S6548 - singleton holder ensures static access to spring resources in some places
public final class EventPublisherHolder {

private static final EventPublisherHolder SINGLETON = new EventPublisherHolder();

@Getter
@Autowired
private ApplicationEventPublisher eventPublisher;
@Autowired(required = false)
private ServiceMatcher serviceMatcher;
@Autowired
private BusProperties bus;

/**
Expand All @@ -42,9 +39,24 @@ public static EventPublisherHolder getInstance() {
return SINGLETON;
}

@Autowired // spring setter injection
public void setApplicationEventPublisher(final ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}

@Autowired(required = false) // spring setter injection
public void setServiceMatcher(final ServiceMatcher serviceMatcher) {
this.serviceMatcher = serviceMatcher;
}

@Autowired // spring setter injection
public void setBusProperties(final BusProperties bus) {
this.bus = bus;
}

/**
* @return the service origin Id coming either from {@link ServiceMatcher}
* when available or {@link BusProperties} otherwise.
* @return the service origin Id coming either from {@link ServiceMatcher} when available or {@link BusProperties}
* otherwise.
*/
public String getApplicationId() {
String id = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@
import org.springframework.beans.factory.annotation.Autowired;

/**
* A singleton bean which holds {@link SystemSecurityContext} service and makes
* it accessible to beans which are not managed by spring, e.g. JPA entities.
* A singleton bean which holds {@link SystemSecurityContext} service and makes it accessible to beans which are not
* managed by spring, e.g. JPA entities.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings("java:S6548") // java:S6548 - singleton holder ensures static access to spring resources in some places
public final class SystemSecurityContextHolder {

private static final SystemSecurityContextHolder INSTANCE = new SystemSecurityContextHolder();
private static final SystemSecurityContextHolder SINGLETON = new SystemSecurityContextHolder();

@Getter
@Autowired
private SystemSecurityContext systemSecurityContext;

/**
* @return the singleton {@link SystemSecurityContextHolder} instance
*/
public static SystemSecurityContextHolder getInstance() {
return INSTANCE;
return SINGLETON;
}

@Autowired // spring setter injection
public void setSystemSecurityContext(final SystemSecurityContext systemSecurityContext) {
this.systemSecurityContext = systemSecurityContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@
import org.springframework.beans.factory.annotation.Autowired;

/**
* A singleton bean which holds {@link TenantConfigurationManagement} service
* and makes it accessible to beans which are not managed by spring, e.g. JPA
* entities.
* A singleton bean which holds {@link TenantConfigurationManagement} service and makes it accessible to beans which are
* not managed by spring, e.g. JPA entities.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings("java:S6548") // java:S6548 - singleton holder ensures static access to spring resources in some places
public final class TenantConfigurationManagementHolder {

private static final TenantConfigurationManagementHolder INSTANCE = new TenantConfigurationManagementHolder();
private static final TenantConfigurationManagementHolder SINGLETON = new TenantConfigurationManagementHolder();

@Getter
@Autowired
private TenantConfigurationManagement tenantConfigurationManagement;

/**
* @return the singleton {@link TenantConfigurationManagementHolder} instance
*/
public static TenantConfigurationManagementHolder getInstance() {
return INSTANCE;
return SINGLETON;
}

@Autowired // spring setter injection
public void setTenantConfigurationManagement(final TenantConfigurationManagement tenantConfigurationManagement) {
this.tenantConfigurationManagement = tenantConfigurationManagement;
}
}
Loading

0 comments on commit bbd2770

Please sign in to comment.