Code refactoring of hawkbit-artifact (#2050)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,4 +33,4 @@ public class ArtifactFilesystemConfiguration {
|
||||
public ArtifactRepository artifactRepository(final ArtifactFilesystemProperties artifactFilesystemProperties) {
|
||||
return new ArtifactFilesystemRepository(artifactFilesystemProperties);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user