From ce846ebe8173ca8b8cc1e095a699d66a5b0b3282 Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Sat, 16 Nov 2024 17:57:17 +0200 Subject: [PATCH] Code refactoring of hawkbit-artifact (#2050) Signed-off-by: Avgustin Marinov --- .../AbstractArtifactRepository.java | 9 +- .../repository/ArtifactStoreException.java | 2 +- .../repository/HashNotMatchException.java | 12 +- .../repository/model/AbstractDbArtifact.java | 12 +- .../artifact/repository/model/DbArtifact.java | 2 +- .../repository/urlhandler/Base62UtilTest.java | 4 +- .../PropertyBasedArtifactUrlHandlerTest.java | 150 ++++++++---------- .../urlhandler/URLPlaceholderTest.java | 7 +- .../ArtifactFileNotFoundException.java | 4 +- .../repository/ArtifactFilesystem.java | 10 +- .../ArtifactFilesystemProperties.java | 3 +- .../ArtifactFilesystemRepository.java | 11 +- .../ArtifactFilesystemConfiguration.java | 2 +- .../repository/ArtifactFilesystemTest.java | 10 +- 14 files changed, 107 insertions(+), 131 deletions(-) diff --git a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/AbstractArtifactRepository.java b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/AbstractArtifactRepository.java index 593919943..20fd9919a 100644 --- a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/AbstractArtifactRepository.java +++ b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/AbstractArtifactRepository.java @@ -27,8 +27,7 @@ import org.eclipse.hawkbit.artifact.repository.model.DbArtifactHash; import org.springframework.util.ObjectUtils; /** - * Abstract utility class for ArtifactRepository implementations with common - * functionality, e.g. computation of hashes. + * Abstract utility class for ArtifactRepository implementations with common functionality, e.g. computation of hashes. */ @Slf4j public abstract class AbstractArtifactRepository implements ArtifactRepository { @@ -36,11 +35,11 @@ public abstract class AbstractArtifactRepository implements ArtifactRepository { private static final String TEMP_FILE_PREFIX = "tmp"; private static final String TEMP_FILE_SUFFIX = "artifactrepo"; - @Override // suppress warning, of not strong enough hashing algorithm, SHA-1 and MD5 is not used security related @SuppressWarnings("squid:S2070") - public AbstractDbArtifact store(final String tenant, - final InputStream content, final String filename, final String contentType, + @Override + public AbstractDbArtifact store( + final String tenant, final InputStream content, final String filename, final String contentType, final DbArtifactHash providedHashes) { final MessageDigest mdSHA1; final MessageDigest mdMD5; diff --git a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactStoreException.java b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactStoreException.java index 6b4e3874e..9074bcc61 100644 --- a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactStoreException.java +++ b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactStoreException.java @@ -28,4 +28,4 @@ public class ArtifactStoreException extends RuntimeException { public ArtifactStoreException(final String message, final Throwable cause) { super(message, cause); } -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/HashNotMatchException.java b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/HashNotMatchException.java index 0d6b28143..c1d462fca 100644 --- a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/HashNotMatchException.java +++ b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/HashNotMatchException.java @@ -14,18 +14,18 @@ import java.io.Serial; import lombok.Getter; /** - * Thrown when provided hashes and hashes calculated during storing are not matching. + * {@link HashNotMatchException} is thrown when provided hashes and hashes calculated during storing are not matching. */ +@Getter public class HashNotMatchException extends RuntimeException { + @Serial + private static final long serialVersionUID = 1L; + public static final String SHA1 = "SHA-1"; public static final String MD5 = "MD5"; public static final String SHA256 = "SHA-256"; - @Serial - private static final long serialVersionUID = 1L; - - @Getter private final String hashFunction; /** @@ -38,4 +38,4 @@ public class HashNotMatchException extends RuntimeException { super(message); this.hashFunction = hashFunction; } -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/model/AbstractDbArtifact.java b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/model/AbstractDbArtifact.java index 61c474df2..d0a981b00 100644 --- a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/model/AbstractDbArtifact.java +++ b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/model/AbstractDbArtifact.java @@ -9,8 +9,9 @@ */ package org.eclipse.hawkbit.artifact.repository.model; +import java.util.Objects; + import lombok.Data; -import org.springframework.util.Assert; /** * Database representation of artifact. @@ -24,12 +25,9 @@ public abstract class AbstractDbArtifact implements DbArtifact { private DbArtifactHash hashes; - protected AbstractDbArtifact(final String artifactId, final DbArtifactHash hashes, final long size, - final String contentType) { - Assert.notNull(artifactId, "Artifact ID cannot be null"); - Assert.notNull(hashes, "Hashes cannot be null"); - this.artifactId = artifactId; - this.hashes = hashes; + protected AbstractDbArtifact(final String artifactId, final DbArtifactHash hashes, final long size, final String contentType) { + this.artifactId = Objects.requireNonNull(artifactId, "Artifact ID cannot be null"); + this.hashes = Objects.requireNonNull(hashes, "Hashes cannot be null"); this.size = size; this.contentType = contentType; } diff --git a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/model/DbArtifact.java b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/model/DbArtifact.java index 076c5d446..993b5a157 100644 --- a/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/model/DbArtifact.java +++ b/hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/artifact/repository/model/DbArtifact.java @@ -43,4 +43,4 @@ public interface DbArtifact { * @return {@link InputStream} to read from artifact. */ InputStream getFileInputStream(); -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/Base62UtilTest.java b/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/Base62UtilTest.java index ab6a28c50..a48c12940 100644 --- a/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/Base62UtilTest.java +++ b/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/Base62UtilTest.java @@ -21,7 +21,7 @@ import org.junit.jupiter.api.Test; class Base62UtilTest { @Test - @Description("Convert Base10 numbres to Base62 ASCII strings.") + @Description("Convert Base10 numbers to Base62 ASCII strings.") void fromBase10() { assertThat(Base62Util.fromBase10(0L)).isEqualTo("0"); assertThat(Base62Util.fromBase10(11L)).isEqualTo("B"); @@ -37,4 +37,4 @@ class Base62UtilTest { assertThat(Base62Util.toBase10("a")).isEqualTo(36L); assertThat(Base62Util.toBase10("G7")).isEqualTo(999L); } -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/PropertyBasedArtifactUrlHandlerTest.java b/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/PropertyBasedArtifactUrlHandlerTest.java index 0d190b734..504ea979f 100644 --- a/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/PropertyBasedArtifactUrlHandlerTest.java +++ b/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/PropertyBasedArtifactUrlHandlerTest.java @@ -18,13 +18,7 @@ import java.util.List; import io.qameta.allure.Description; import io.qameta.allure.Feature; import io.qameta.allure.Story; -import org.eclipse.hawkbit.artifact.repository.urlhandler.ApiType; -import org.eclipse.hawkbit.artifact.repository.urlhandler.ArtifactUrl; -import org.eclipse.hawkbit.artifact.repository.urlhandler.ArtifactUrlHandler; -import org.eclipse.hawkbit.artifact.repository.urlhandler.ArtifactUrlHandlerProperties; import org.eclipse.hawkbit.artifact.repository.urlhandler.ArtifactUrlHandlerProperties.UrlProtocol; -import org.eclipse.hawkbit.artifact.repository.urlhandler.PropertyBasedArtifactUrlHandler; -import org.eclipse.hawkbit.artifact.repository.urlhandler.URLPlaceholder; import org.eclipse.hawkbit.artifact.repository.urlhandler.URLPlaceholder.SoftwareData; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -46,17 +40,18 @@ public class PropertyBasedArtifactUrlHandlerTest { private static final String CONTROLLER_ID = "Test"; private static final String FILENAME_DECODE = "test123!ยง$%&"; private static final String FILENAME_ENCODE = "test123%21%C2%A7%24%25%26"; - private static final long SOFTWAREMODULEID = 87654L; - private static final long TARGETID = 3474366L; - private static final String TARGETID_BASE62 = "EZqA"; + private static final long SOFTWARE_MODULE_ID = 87654L; + private static final long TARGET_ID = 3474366L; + private static final String TARGET_ID_BASE62 = "EZqA"; private static final String SHA1HASH = "test12345"; - private static final long ARTIFACTID = 1345678L; - private static final String ARTIFACTID_BASE62 = "5e4U"; + private static final long ARTIFACT_ID = 1345678L; + private static final String ARTIFACT_ID_BASE62 = "5e4U"; private static final String TENANT = "TEST_TENANT"; private static final String HTTP_LOCALHOST = "http://localhost:8080/"; - private static URLPlaceholder placeholder = new URLPlaceholder(TENANT, TENANT_ID, CONTROLLER_ID, TARGETID, - new SoftwareData(SOFTWAREMODULEID, FILENAME_DECODE, ARTIFACTID, SHA1HASH)); + private static final URLPlaceholder placeHolder = new URLPlaceholder( + TENANT, TENANT_ID, CONTROLLER_ID, TARGET_ID, + new SoftwareData(SOFTWARE_MODULE_ID, FILENAME_DECODE, ARTIFACT_ID, SHA1HASH)); private ArtifactUrlHandler urlHandlerUnderTest; private ArtifactUrlHandlerProperties properties; @@ -64,7 +59,6 @@ public class PropertyBasedArtifactUrlHandlerTest { public void setup() { properties = new ArtifactUrlHandlerProperties(); urlHandlerUnderTest = new PropertyBasedArtifactUrlHandler(properties, ""); - } @Test @@ -72,13 +66,14 @@ public class PropertyBasedArtifactUrlHandlerTest { public void urlGenerationWithDefaultConfiguration() { properties.getProtocols().put("download-http", new UrlProtocol()); - final List ddiUrls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI); + final List ddiUrls = urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI); assertThat(ddiUrls).containsExactly( - new ArtifactUrl("http".toUpperCase(), "download-http", HTTP_LOCALHOST + TENANT + "/controller/v1/" - + CONTROLLER_ID + "/softwaremodules/" + SOFTWAREMODULEID + "/artifacts/" + FILENAME_ENCODE)); + new ArtifactUrl( + "http".toUpperCase(), "download-http", + HTTP_LOCALHOST + TENANT + "/controller/v1/" + + CONTROLLER_ID + "/softwaremodules/" + SOFTWARE_MODULE_ID + "/artifacts/" + FILENAME_ENCODE)); - final List dmfUrls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DMF); - assertThat(ddiUrls).isEqualTo(dmfUrls); + assertThat(ddiUrls).isEqualTo(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DMF)); } @Test @@ -93,13 +88,11 @@ public class PropertyBasedArtifactUrlHandlerTest { proto.setRef("{protocol}://{ip}:{port}/fw/{tenant}/{controllerId}/sha1/{artifactSHA1}"); properties.getProtocols().put(TEST_PROTO, proto); - List urls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI); - - assertThat(urls).isEmpty(); - urls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DMF); - - assertThat(urls).containsExactly(new ArtifactUrl(TEST_PROTO.toUpperCase(), TEST_REL, - "coap://127.0.0.1:5683/fw/" + TENANT + "/" + CONTROLLER_ID + "/sha1/" + SHA1HASH)); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI)).isEmpty(); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DMF)).containsExactly( + new ArtifactUrl( + TEST_PROTO.toUpperCase(), TEST_REL, + "coap://127.0.0.1:5683/fw/" + TENANT + "/" + CONTROLLER_ID + "/sha1/" + SHA1HASH)); } @Test @@ -114,17 +107,15 @@ public class PropertyBasedArtifactUrlHandlerTest { proto.setRef("{protocol}://{ip}:{port}/fws/{tenant}/{targetIdBase62}/{artifactIdBase62}"); properties.getProtocols().put("ftp", proto); - List urls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI); - - assertThat(urls).isEmpty(); - urls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DMF); - - assertThat(urls).containsExactly(new ArtifactUrl(TEST_PROTO.toUpperCase(), TEST_REL, - TEST_PROTO + "://127.0.0.1:5683/fws/" + TENANT + "/" + TARGETID_BASE62 + "/" + ARTIFACTID_BASE62)); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI)).isEmpty(); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DMF)).containsExactly( + new ArtifactUrl( + TEST_PROTO.toUpperCase(), TEST_REL, + TEST_PROTO + "://127.0.0.1:5683/fws/" + TENANT + "/" + TARGET_ID_BASE62 + "/" + ARTIFACT_ID_BASE62)); } @Test - @Description("Verfies 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 { final String testHost = "ddi.host.com"; @@ -137,15 +128,14 @@ public class PropertyBasedArtifactUrlHandlerTest { proto.setRef("{protocol}://{hostnameRequest}:{port}/fws/{tenant}/{targetIdBase62}/{artifactIdBase62}"); properties.getProtocols().put("ftp", proto); - final List urls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI, - new URI("https://" + testHost)); - - assertThat(urls).containsExactly(new ArtifactUrl(TEST_PROTO.toUpperCase(), TEST_REL, TEST_PROTO + "://" - + testHost + ":5683/fws/" + TENANT + "/" + TARGETID_BASE62 + "/" + ARTIFACTID_BASE62)); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI, new URI("https://" + testHost))).containsExactly( + new ArtifactUrl( + TEST_PROTO.toUpperCase(), TEST_REL, + TEST_PROTO + "://" + testHost + ":5683/fws/" + TENANT + "/" + TARGET_ID_BASE62 + "/" + ARTIFACT_ID_BASE62)); } @Test - @Description("Verfies 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 { final String testHost = "ddi.host.com"; @@ -153,74 +143,70 @@ public class PropertyBasedArtifactUrlHandlerTest { proto.setRef("{protocolRequest}://{hostname}:{port}/fws/{tenant}/{targetIdBase62}/{artifactIdBase62}"); properties.getProtocols().put("download-http", proto); - final List urls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI, - new URI("https://" + testHost)); - - assertThat(urls).containsExactly(new ArtifactUrl("http".toUpperCase(), "download-http", - "https://localhost:8080/fws/" + TENANT + "/" + TARGETID_BASE62 + "/" + ARTIFACTID_BASE62)); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI, new URI("https://" + testHost))).containsExactly( + new ArtifactUrl( + "http".toUpperCase(), "download-http", + "https://localhost:8080/fws/" + TENANT + "/" + TARGET_ID_BASE62 + "/" + ARTIFACT_ID_BASE62)); } @Test - @Description("Verfies 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 { 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}"); properties.getProtocols().put("download-http", proto); - final List ddiUrls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI, - new URI("http://anotherHost.com:8083")); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI, new URI("http://anotherHost.com:8083"))).containsExactly( + new ArtifactUrl( + "http".toUpperCase(), "download-http", + "http://localhost:8083/" + TENANT + "/controller/v1/" + + CONTROLLER_ID + "/softwaremodules/" + SOFTWARE_MODULE_ID + "/artifacts/" + FILENAME_ENCODE)); - assertThat(ddiUrls).containsExactly(new ArtifactUrl("http".toUpperCase(), "download-http", - "http://localhost:8083/" + TENANT + "/controller/v1/" + CONTROLLER_ID + "/softwaremodules/" - + SOFTWAREMODULEID + "/artifacts/" + FILENAME_ENCODE)); - - final List dmfUrls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DMF); - - assertThat(dmfUrls).containsExactly(new ArtifactUrl("http".toUpperCase(), "download-http", - "http://localhost:8080/" + TENANT + "/controller/v1/" + CONTROLLER_ID + "/softwaremodules/" - + SOFTWAREMODULEID + "/artifacts/" + FILENAME_ENCODE)); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DMF)).containsExactly( + new ArtifactUrl( + "http".toUpperCase(), "download-http", + "http://localhost:8080/" + TENANT + "/controller/v1/" + + CONTROLLER_ID + "/softwaremodules/" + SOFTWARE_MODULE_ID + "/artifacts/" + FILENAME_ENCODE)); } @Test - @Description("Verfies 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 { - String protocol = "https"; + final String protocol = "https"; 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}"); proto.setProtocol(protocol); properties.getProtocols().put("download-http", proto); - URI uri = new URI(protocol + "://anotherHost.com"); - final List urls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI, uri); - assertThat(urls).containsExactly(new ArtifactUrl(protocol.toUpperCase(), "download-http", - uri + "/" + TENANT + "/controller/v1/" + CONTROLLER_ID + "/softwaremodules/" - + SOFTWAREMODULEID + "/artifacts/" + FILENAME_ENCODE)); + final URI uri = new URI(protocol + "://anotherHost.com"); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI, uri)).containsExactly( + new ArtifactUrl( + protocol.toUpperCase(), "download-http", + uri + "/" + TENANT + "/controller/v1/" + + CONTROLLER_ID + "/softwaremodules/" + SOFTWARE_MODULE_ID + "/artifacts/" + FILENAME_ENCODE)); } @Test - @Description("Verfies 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 { final UrlProtocol proto = new UrlProtocol(); 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}"); properties.getProtocols().put("download-http", proto); - final List ddiUrls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI, - new URI("http://anotherHost.com:8083")); - assertThat(ddiUrls).containsExactly( - new ArtifactUrl("http".toUpperCase(), "download-http", "http://host.com/" + TENANT + "/controller/v1/" - + CONTROLLER_ID + "/softwaremodules/" + SOFTWAREMODULEID + "/artifacts/" + FILENAME_ENCODE)); - - final List dmfUrls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DMF); - assertThat(dmfUrls).containsExactly(new ArtifactUrl("http".toUpperCase(), "download-http", - "http://host.bumlux.net/" + TENANT + "/controller/v1/" + CONTROLLER_ID + "/softwaremodules/" - + SOFTWAREMODULEID + "/artifacts/" + FILENAME_ENCODE)); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DDI, new URI("http://anotherHost.com:8083"))).containsExactly( + new ArtifactUrl( + "http".toUpperCase(), "download-http", + "http://host.com/" + TENANT + "/controller/v1/" + + CONTROLLER_ID + "/softwaremodules/" + SOFTWARE_MODULE_ID + "/artifacts/" + FILENAME_ENCODE)); + assertThat(urlHandlerUnderTest.getUrls(placeHolder, ApiType.DMF)).containsExactly( + new ArtifactUrl( + "http".toUpperCase(), "download-http", + "http://host.bumlux.net/" + TENANT + "/controller/v1/" + + CONTROLLER_ID + "/softwaremodules/" + SOFTWARE_MODULE_ID + "/artifacts/" + FILENAME_ENCODE)); } -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/URLPlaceholderTest.java b/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/URLPlaceholderTest.java index cf927736d..e2b7c3b29 100644 --- a/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/URLPlaceholderTest.java +++ b/hawkbit-artifact/hawkbit-artifact-api/src/test/java/org/eclipse/hawkbit/artifact/repository/urlhandler/URLPlaceholderTest.java @@ -14,7 +14,6 @@ import static org.assertj.core.api.Assertions.assertThat; import io.qameta.allure.Description; import io.qameta.allure.Feature; import io.qameta.allure.Story; -import org.eclipse.hawkbit.artifact.repository.urlhandler.URLPlaceholder; import org.junit.jupiter.api.Test; @Feature("Unit Tests - Artifact URL Handler") @@ -31,10 +30,8 @@ class URLPlaceholderTest { @Test @Description("Same object should be equal") - // Exception squid:S5785 - JUnit assertTrue/assertFalse should be simplified to - // the corresponding dedicated assertion - // Need to test the equals method and need to bypass magic logic in utility - // classes + // Exception squid:S5785 - JUnit assertTrue/assertFalse should be simplified to the corresponding dedicated assertion + // Need to test the equals method and need to bypass magic logic in utility classes @SuppressWarnings({ "squid:S5838" }) void sameObjectShouldBeEqual() { assertThat(softwareData.equals(softwareData)).isTrue(); diff --git a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFileNotFoundException.java b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFileNotFoundException.java index c85a2ce32..a456d1a69 100644 --- a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFileNotFoundException.java +++ b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFileNotFoundException.java @@ -15,11 +15,11 @@ package org.eclipse.hawkbit.artifact.repository; public class ArtifactFileNotFoundException extends RuntimeException { /** - * Creates the Exception from it's cause + * Creates the Exception from its cause * * @param cause the original exception */ public ArtifactFileNotFoundException(final Exception cause) { super(cause); } -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystem.java b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystem.java index f94e05720..fcb622e64 100644 --- a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystem.java +++ b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystem.java @@ -22,24 +22,24 @@ import org.eclipse.hawkbit.artifact.repository.model.AbstractDbArtifact; import org.eclipse.hawkbit.artifact.repository.model.DbArtifactHash; /** - * {@link AbstractDbArtifact} implementation which dynamically creates a - * {@link FileInputStream} on calling {@link #getFileInputStream()}. + * {@link AbstractDbArtifact} implementation which dynamically creates a {@link FileInputStream} on calling {@link #getFileInputStream()}. */ public class ArtifactFilesystem extends AbstractDbArtifact { private final File file; - public ArtifactFilesystem(@NotNull final File file, @NotNull final String artifactId, + public ArtifactFilesystem( + @NotNull final File file, @NotNull final String artifactId, @NotNull final DbArtifactHash hashes, final Long size, final String contentType) { super(artifactId, hashes, size, contentType); this.file = Objects.requireNonNull(file, "Artifact file may not be null"); } - @Override // suppress warning, this InputStream needs to be closed by the caller, this // cannot be closed in this method @SuppressWarnings("squid:S2095") + @Override public InputStream getFileInputStream() { try { return new BufferedInputStream(new FileInputStream(file)); @@ -47,4 +47,4 @@ public class ArtifactFilesystem extends AbstractDbArtifact { throw new ArtifactFileNotFoundException(e); } } -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemProperties.java b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemProperties.java index e74cfe1cf..2386559a5 100644 --- a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemProperties.java +++ b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemProperties.java @@ -13,8 +13,7 @@ import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** - * Configuration properties for the file-system repository, e.g. the base-path - * to store the files. + * Configuration properties for the file-system repository, e.g. the base-path to store the files. */ @Data @ConfigurationProperties("org.eclipse.hawkbit.repository.file") diff --git a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemRepository.java b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemRepository.java index bd404519a..c4eaea740 100644 --- a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemRepository.java +++ b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemRepository.java @@ -23,13 +23,13 @@ import org.springframework.validation.annotation.Validated; /** * Implementation of the {@link ArtifactRepository} to store artifacts on the * file-system. The files are stored by their SHA1 hash of the artifact binary. - * Duplicate files with the same SHA1 hash will only stored once. + * Duplicate files with the same SHA1 hash will only be stored once. * * All files are stored flat in one base directory configured in the * {@link ArtifactFilesystemProperties#getPath()}. * - * Due the limit of many file-systems of files within one directory, the files - * are stored in different sub-directories based on the last four digits of the + * Due to the limit of many file-systems of files within one directory, the files + * are stored in different subdirectories based on the last four digits of the * SHA1-hash {@code (/basepath/[two digit sha1]/[two digit sha1])}. */ @Validated @@ -40,8 +40,7 @@ public class ArtifactFilesystemRepository extends AbstractArtifactRepository { /** * Constructor. * - * @param artifactResourceProperties the properties which holds the necessary configuration for the - * file-system repository + * @param artifactResourceProperties the properties which holds the necessary configuration for the file-system repository */ public ArtifactFilesystemRepository(final ArtifactFilesystemProperties artifactResourceProperties) { this.artifactResourceProperties = artifactResourceProperties; @@ -111,4 +110,4 @@ public class ArtifactFilesystemRepository extends AbstractArtifactRepository { final String folder2 = sha1.substring(length - 2, length); return Paths.get(artifactResourceProperties.getPath(), sanitizeTenant(tenant), folder1, folder2); } -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/autoconfigure/artifact/repository/filesystem/ArtifactFilesystemConfiguration.java b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/autoconfigure/artifact/repository/filesystem/ArtifactFilesystemConfiguration.java index 97460fc68..de6d39ea4 100644 --- a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/autoconfigure/artifact/repository/filesystem/ArtifactFilesystemConfiguration.java +++ b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/main/java/org/eclipse/hawkbit/autoconfigure/artifact/repository/filesystem/ArtifactFilesystemConfiguration.java @@ -33,4 +33,4 @@ public class ArtifactFilesystemConfiguration { public ArtifactRepository artifactRepository(final ArtifactFilesystemProperties artifactFilesystemProperties) { return new ArtifactFilesystemRepository(artifactFilesystemProperties); } -} +} \ No newline at end of file diff --git a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemTest.java b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemTest.java index ca59620f9..e6f355032 100644 --- a/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemTest.java +++ b/hawkbit-artifact/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemTest.java @@ -10,6 +10,7 @@ package org.eclipse.hawkbit.artifact.repository; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.File; import java.io.FileNotFoundException; @@ -35,12 +36,9 @@ class ArtifactFilesystemTest { final ArtifactFilesystem underTest = new ArtifactFilesystem( file, "fileWhichTotalDoesNotExists", new DbArtifactHash("1", "2", "3"), 0L, null); - try { - underTest.getFileInputStream(); - Assertions.fail("Expected a FileNotFoundException because file does not exists"); - } catch (final RuntimeException e) { - assertThat(e.getCause()).isInstanceOf(FileNotFoundException.class); - } + assertThatThrownBy(underTest::getFileInputStream) + .isInstanceOf(ArtifactFileNotFoundException.class) + .hasCauseInstanceOf(FileNotFoundException.class); } @Test