Sonar Fixes (4) (#2210)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-01-21 10:20:09 +02:00
committed by GitHub
parent e64053fb59
commit 33a6250646
5 changed files with 20 additions and 17 deletions

View File

@@ -62,8 +62,9 @@ public class PropertyBasedArtifactUrlHandler implements ArtifactUrlHandler {
private static final String TENANT_ID_BASE10_PLACEHOLDER = "tenantId"; private static final String TENANT_ID_BASE10_PLACEHOLDER = "tenantId";
private static final String TENANT_ID_BASE62_PLACEHOLDER = "tenantIdBase62"; private static final String TENANT_ID_BASE62_PLACEHOLDER = "tenantIdBase62";
private static final String SOFTWARE_MODULE_ID_BASE10_PLACEHOLDER = "softwareModuleId"; private static final String SOFTWARE_MODULE_ID_BASE10_PLACEHOLDER = "softwareModuleId";
final static String DEFAULT_URL_PROTOCOL_REF = "{" + PROTOCOL_PLACEHOLDER + "}://{" + HOSTNAME_PLACEHOLDER + "}:{" + PORT_PLACEHOLDER + "}{" + CONTEXT_PATH + "}/{" + TENANT_PLACEHOLDER + "}/controller/v1/{" + CONTROLLER_ID_PLACEHOLDER + "}/softwaremodules/{" + SOFTWARE_MODULE_ID_BASE10_PLACEHOLDER + "}/artifacts/{" + ARTIFACT_FILENAME_PLACEHOLDER + "}";
private static final String SOFTWARE_MODULE_ID_BASE62_PLACEHOLDER = "softwareModuleIdBase62"; private static final String SOFTWARE_MODULE_ID_BASE62_PLACEHOLDER = "softwareModuleIdBase62";
static final String DEFAULT_URL_PROTOCOL_REF = "{" + PROTOCOL_PLACEHOLDER + "}://{" + HOSTNAME_PLACEHOLDER + "}:{" + PORT_PLACEHOLDER + "}{" + CONTEXT_PATH + "}/{" + TENANT_PLACEHOLDER + "}/controller/v1/{" + CONTROLLER_ID_PLACEHOLDER + "}/softwaremodules/{" + SOFTWARE_MODULE_ID_BASE10_PLACEHOLDER + "}/artifacts/{" + ARTIFACT_FILENAME_PLACEHOLDER + "}";
private final ArtifactUrlHandlerProperties urlHandlerProperties; private final ArtifactUrlHandlerProperties urlHandlerProperties;
private final String contextPath; private final String contextPath;

View File

@@ -31,7 +31,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
@Feature("Unit Tests - Artifact URL Handler") @Feature("Unit Tests - Artifact URL Handler")
@Story("Test to generate the artifact download URL") @Story("Test to generate the artifact download URL")
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
public class PropertyBasedArtifactUrlHandlerTest { class PropertyBasedArtifactUrlHandlerTest {
private static final String TEST_PROTO = "coap"; private static final String TEST_PROTO = "coap";
private static final String TEST_REL = "download-udp"; private static final String TEST_REL = "download-udp";
@@ -56,14 +56,14 @@ public class PropertyBasedArtifactUrlHandlerTest {
private ArtifactUrlHandlerProperties properties; private ArtifactUrlHandlerProperties properties;
@BeforeEach @BeforeEach
public void setup() { void setup() {
properties = new ArtifactUrlHandlerProperties(); properties = new ArtifactUrlHandlerProperties();
urlHandlerUnderTest = new PropertyBasedArtifactUrlHandler(properties, ""); urlHandlerUnderTest = new PropertyBasedArtifactUrlHandler(properties, "");
} }
@Test @Test
@Description("Tests the generation of http download url.") @Description("Tests the generation of http download url.")
public void urlGenerationWithDefaultConfiguration() { void urlGenerationWithDefaultConfiguration() {
properties.getProtocols().put("download-http", new UrlProtocol()); properties.getProtocols().put("download-http", new UrlProtocol());
final List<ArtifactUrl> ddiUrls = urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI); final List<ArtifactUrl> ddiUrls = urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI);
@@ -78,7 +78,7 @@ public class PropertyBasedArtifactUrlHandlerTest {
@Test @Test
@Description("Tests the generation of custom download url with a CoAP example that supports DMF only.") @Description("Tests the generation of custom download url with a CoAP example that supports DMF only.")
public void urlGenerationWithCustomConfiguration() { void urlGenerationWithCustomConfiguration() {
final UrlProtocol proto = new UrlProtocol(); final UrlProtocol proto = new UrlProtocol();
proto.setIp("127.0.0.1"); proto.setIp("127.0.0.1");
proto.setPort(5683); proto.setPort(5683);
@@ -97,7 +97,7 @@ public class PropertyBasedArtifactUrlHandlerTest {
@Test @Test
@Description("Tests the generation of custom download url using Base62 references with a CoAP example that supports DMF only.") @Description("Tests the generation of custom download url using Base62 references with a CoAP example that supports DMF only.")
public void urlGenerationWithCustomShortConfiguration() { void urlGenerationWithCustomShortConfiguration() {
final UrlProtocol proto = new UrlProtocol(); final UrlProtocol proto = new UrlProtocol();
proto.setIp("127.0.0.1"); proto.setIp("127.0.0.1");
proto.setPort(5683); proto.setPort(5683);
@@ -116,7 +116,7 @@ public class PropertyBasedArtifactUrlHandlerTest {
@Test @Test
@Description("Verifies that the full qualified host of the statically defined hostname is replaced with the host of the request.") @Description("Verifies that the full qualified host of the statically defined hostname is replaced with the host of the request.")
public void urlGenerationWithHostFromRequest() throws URISyntaxException { void urlGenerationWithHostFromRequest() throws URISyntaxException {
final String testHost = "ddi.host.com"; final String testHost = "ddi.host.com";
final UrlProtocol proto = new UrlProtocol(); final UrlProtocol proto = new UrlProtocol();
@@ -136,7 +136,7 @@ public class PropertyBasedArtifactUrlHandlerTest {
@Test @Test
@Description("Verifies that the protocol of the statically defined hostname is replaced with the protocol of the request.") @Description("Verifies that the protocol of the statically defined hostname is replaced with the protocol of the request.")
public void urlGenerationWithProtocolFromRequest() throws URISyntaxException { void urlGenerationWithProtocolFromRequest() throws URISyntaxException {
final String testHost = "ddi.host.com"; final String testHost = "ddi.host.com";
final UrlProtocol proto = new UrlProtocol(); final UrlProtocol proto = new UrlProtocol();
@@ -151,7 +151,7 @@ public class PropertyBasedArtifactUrlHandlerTest {
@Test @Test
@Description("Verifies that the port of the statically defined hostname is replaced with the port of the request.") @Description("Verifies that the port of the statically defined hostname is replaced with the port of the request.")
public void urlGenerationWithPortFromRequest() throws URISyntaxException { void urlGenerationWithPortFromRequest() throws URISyntaxException {
final UrlProtocol proto = new UrlProtocol(); final UrlProtocol proto = new UrlProtocol();
proto.setRef("{protocol}://{hostname}:{portRequest}/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/{artifactFileName}"); proto.setRef("{protocol}://{hostname}:{portRequest}/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/{artifactFileName}");
@@ -172,7 +172,7 @@ public class PropertyBasedArtifactUrlHandlerTest {
@Test @Test
@Description("Verifies that if default protocol port in request is used then url is returned without port") @Description("Verifies that if default protocol port in request is used then url is returned without port")
public void urlGenerationWithPortFromRequestForHttps() throws URISyntaxException { void urlGenerationWithPortFromRequestForHttps() throws URISyntaxException {
final String protocol = "https"; final String protocol = "https";
final UrlProtocol proto = new UrlProtocol(); final UrlProtocol proto = new UrlProtocol();
proto.setRef("{protocolRequest}://{hostnameRequest}:{portRequest}/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/{artifactFileName}"); proto.setRef("{protocolRequest}://{hostnameRequest}:{portRequest}/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/{artifactFileName}");
@@ -190,7 +190,7 @@ public class PropertyBasedArtifactUrlHandlerTest {
@Test @Test
@Description("Verifies that the domain of the statically defined hostname is replaced with the domain of the request.") @Description("Verifies that the domain of the statically defined hostname is replaced with the domain of the request.")
public void urlGenerationWithDomainFromRequest() throws URISyntaxException { void urlGenerationWithDomainFromRequest() throws URISyntaxException {
final UrlProtocol proto = new UrlProtocol(); final UrlProtocol proto = new UrlProtocol();
proto.setHostname("host.bumlux.net"); proto.setHostname("host.bumlux.net");
proto.setRef("{protocol}://{domainRequest}/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/{artifactFileName}"); proto.setRef("{protocol}://{domainRequest}/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/{artifactFileName}");

View File

@@ -55,7 +55,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cloud.bus.ConditionalOnBusEnabled; import org.springframework.cloud.bus.ConditionalOnBusEnabled;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
@@ -98,7 +97,7 @@ public class TestConfiguration implements AsyncConfigurer {
public ScheduledExecutorService scheduledExecutorService() { public ScheduledExecutorService scheduledExecutorService() {
final AtomicLong count = new AtomicLong(0); final AtomicLong count = new AtomicLong(0);
return new DelegatingSecurityContextScheduledExecutorService( return new DelegatingSecurityContextScheduledExecutorService(
Executors.newScheduledThreadPool(1, (runnable) -> { Executors.newScheduledThreadPool(1, runnable -> {
final Thread thread = Executors.defaultThreadFactory().newThread(runnable); final Thread thread = Executors.defaultThreadFactory().newThread(runnable);
thread.setName( thread.setName(
String.format( String.format(

View File

@@ -17,6 +17,8 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.im.authentication.SpPermission; import org.eclipse.hawkbit.im.authentication.SpPermission;
import org.eclipse.hawkbit.tenancy.TenantAwareAuthenticationDetails; import org.eclipse.hawkbit.tenancy.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.tenancy.TenantAwareUser; import org.eclipse.hawkbit.tenancy.TenantAwareUser;
@@ -25,6 +27,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SecurityContextSwitch { public class SecurityContextSwitch {
public static final String DEFAULT_TENANT = "DEFAULT"; public static final String DEFAULT_TENANT = "DEFAULT";

View File

@@ -13,7 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -359,9 +359,9 @@ public class TestdataFactory {
final boolean isRequiredMigrationStep, final Collection<SoftwareModule> modules) { final boolean isRequiredMigrationStep, final Collection<SoftwareModule> modules) {
return distributionSetManagement.create( return distributionSetManagement.create(
entityFactory.distributionSet().create().name(prefix != null && prefix.length() > 0 ? prefix : "DS") entityFactory.distributionSet().create().name(prefix != null && !prefix.isEmpty() ? prefix : "DS")
.version(version).description(randomDescriptionShort()).type(findOrCreateDefaultTestDsType()) .version(version).description(randomDescriptionShort()).type(findOrCreateDefaultTestDsType())
.modules(modules.stream().map(SoftwareModule::getId).collect(Collectors.toList())) .modules(modules.stream().map(SoftwareModule::getId).toList())
.requiredMigrationStep(isRequiredMigrationStep)); .requiredMigrationStep(isRequiredMigrationStep));
} }
@@ -481,7 +481,7 @@ public class TestdataFactory {
* @return {@link Artifact} entity. * @return {@link Artifact} entity.
*/ */
public Artifact createArtifact(final String artifactData, final Long moduleId, final String filename) { public Artifact createArtifact(final String artifactData, final Long moduleId, final String filename) {
final InputStream stubInputStream = IOUtils.toInputStream(artifactData, Charset.forName("UTF-8")); final InputStream stubInputStream = IOUtils.toInputStream(artifactData, StandardCharsets.UTF_8);
return artifactManagement return artifactManagement
.create(new ArtifactUpload(stubInputStream, moduleId, filename, false, artifactData.length())); .create(new ArtifactUpload(stubInputStream, moduleId, filename, false, artifactData.length()));
} }