From 313abf330bb2a874998a35b99a9107eebf5e1fd6 Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Sat, 9 Nov 2024 17:31:59 +0200 Subject: [PATCH] Fix sonar findings: Fix artifact file repo tests (#1986) Signed-off-by: Avgustin Marinov --- .../ArtifactFilesystemRepositoryTest.java | 51 +++++++++---------- .../repository/ArtifactFilesystemTest.java | 18 +++---- .../AbstractArtifactRepository.java | 7 +-- .../repository/builder/TargetTypeUpdate.java | 2 +- .../repository/RolloutGroupRepository.java | 6 +-- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemRepositoryTest.java b/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemRepositoryTest.java index 73b2c3aec..e5c235729 100644 --- a/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemRepositoryTest.java +++ b/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemRepositoryTest.java @@ -32,16 +32,15 @@ import org.junit.jupiter.api.Test; @Slf4j @Feature("Unit Tests - Artifact File System Repository") @Story("Test storing artifact binaries in the file-system") -public class ArtifactFilesystemRepositoryTest { +class ArtifactFilesystemRepositoryTest { private static final String TENANT = "test_tenant"; private static ArtifactFilesystemProperties artifactResourceProperties; - private static ArtifactFilesystemRepository artifactFilesystemRepository; @BeforeAll - public static void setup() throws IOException { + static void setup() throws IOException { artifactResourceProperties = new ArtifactFilesystemProperties(); artifactResourceProperties.setPath(Files.createTempDirectory(null).toString()); @@ -49,7 +48,7 @@ public class ArtifactFilesystemRepositoryTest { } @AfterAll - public static void afterClass() { + static void afterClass() { if (new File(artifactResourceProperties.getPath()).exists()) { try { FileUtils.deleteDirectory(new File(artifactResourceProperties.getPath())); @@ -60,44 +59,38 @@ public class ArtifactFilesystemRepositoryTest { } @Test - @Description("Verfies that an artifact can be successfully stored in the file-system repository") - public void storeSuccessfully() throws IOException { + @Description("Verifies that an artifact can be successfully stored in the file-system repository") + void storeSuccessfully() throws IOException { final byte[] fileContent = randomBytes(); final AbstractDbArtifact artifact = storeRandomArtifact(fileContent); final byte[] readContent = new byte[fileContent.length]; IOUtils.read(artifact.getFileInputStream(), readContent); - assertThat(readContent).isEqualTo(fileContent); } @Test - @Description("Verfies that an artifact can be successfully stored in the file-system repository") - public void getStoredArtifactBasedOnSHA1Hash() { - + @Description("Verifies that an artifact can be successfully stored in the file-system repository") + void getStoredArtifactBasedOnSHA1Hash() { final byte[] fileContent = randomBytes(); final AbstractDbArtifact artifact = storeRandomArtifact(fileContent); - final AbstractDbArtifact artifactBySha1 = artifactFilesystemRepository.getArtifactBySha1(TENANT, - artifact.getHashes().getSha1()); - assertThat(artifactBySha1).isNotNull(); + assertThat(artifactFilesystemRepository.getArtifactBySha1(TENANT, artifact.getHashes().getSha1())).isNotNull(); } @Test - @Description("Verfies that an artifact can be deleted in the file-system repository") - public void deleteStoredArtifactBySHA1Hash() { + @Description("Verifies that an artifact can be deleted in the file-system repository") + void deleteStoredArtifactBySHA1Hash() { final AbstractDbArtifact artifact = storeRandomArtifact(randomBytes()); - artifactFilesystemRepository.deleteBySha1(TENANT, artifact.getHashes().getSha1()); assertThat(artifactFilesystemRepository.getArtifactBySha1(TENANT, artifact.getHashes().getSha1())).isNull(); } @Test - @Description("Verfies that all artifacts of a tenant can be deleted in the file-system repository") - public void deleteStoredArtifactOfTenant() { + @Description("Verifies that all artifacts of a tenant can be deleted in the file-system repository") + void deleteStoredArtifactOfTenant() { final AbstractDbArtifact artifact = storeRandomArtifact(randomBytes()); - artifactFilesystemRepository.deleteByTenant(TENANT); assertThat(artifactFilesystemRepository.getArtifactBySha1(TENANT, artifact.getHashes().getSha1())).isNull(); @@ -105,7 +98,7 @@ public class ArtifactFilesystemRepositoryTest { @Test @Description("Verfies that an artifact which does not exists is deleted quietly in the file-system repository") - public void deleteArtifactWhichDoesNotExistsBySHA1HashWithoutException() { + void deleteArtifactWhichDoesNotExistsBySHA1HashWithoutException() { try { artifactFilesystemRepository.deleteBySha1(TENANT, "sha1HashWhichDoesNotExists"); } catch (final Exception e) { @@ -122,15 +115,21 @@ public class ArtifactFilesystemRepositoryTest { private static byte[] randomBytes() { final byte[] randomBytes = new byte[20]; - final Random ran = new Random(); - ran.nextBytes(randomBytes); + new Random().nextBytes(randomBytes); return randomBytes; } private AbstractDbArtifact storeRandomArtifact(final byte[] fileContent) { - final String fileName = "filename.tmp"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContent); - return artifactFilesystemRepository.store(TENANT, inputStream, fileName, "application/txt", null); + try { + return artifactFilesystemRepository.store(TENANT, inputStream, "filename.tmp", "application/txt", null); + } finally { + try { + inputStream.close(); + } catch (final IOException e) { + // do nothing + // still return the artifact + } + } } - -} +} \ No newline at end of file diff --git a/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemTest.java b/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemTest.java index 744ad84c4..ca59620f9 100644 --- a/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemTest.java +++ b/hawkbit-artifact-repository-filesystem/src/test/java/org/eclipse/hawkbit/artifact/repository/ArtifactFilesystemTest.java @@ -26,15 +26,15 @@ import org.junit.jupiter.api.Test; @Feature("Unit Tests - Artifact File System Repository") @Story("Test storing artifact binaries in the file-system") -public class ArtifactFilesystemTest { +class ArtifactFilesystemTest { @Test @Description("Verifies that an exception is thrown on opening an InputStream when file does not exists") - public void getInputStreamOfNonExistingFileThrowsException() { + void getInputStreamOfNonExistingFileThrowsException() { final File file = new File("fileWhichTotalDoesNotExists"); - final ArtifactFilesystem underTest = new ArtifactFilesystem(file, "fileWhichTotalDoesNotExists", + 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"); @@ -45,13 +45,13 @@ public class ArtifactFilesystemTest { @Test @Description("Verifies that an InputStream can be opened if file exists") - public void getInputStreamOfExistingFile() throws IOException { + void getInputStreamOfExistingFile() throws IOException { final File createTempFile = Files.createTempFile(ArtifactFilesystemTest.class.getSimpleName(), "").toFile(); createTempFile.deleteOnExit(); - final ArtifactFilesystem underTest = new ArtifactFilesystem(createTempFile, - ArtifactFilesystemTest.class.getSimpleName(), new DbArtifactHash("1", "2", "3"), 0L, null); + final ArtifactFilesystem underTest = new ArtifactFilesystem( + createTempFile, ArtifactFilesystemTest.class.getSimpleName(), new DbArtifactHash("1", "2", "3"), 0L, null); final byte[] buffer = new byte[1024]; - IOUtils.read(underTest.getFileInputStream(), buffer); + assertThat(IOUtils.read(underTest.getFileInputStream(), buffer)).isEqualTo(0); } -} +} \ No newline at end of file diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/artifact/repository/AbstractArtifactRepository.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/artifact/repository/AbstractArtifactRepository.java index 8520d9c8a..593919943 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/artifact/repository/AbstractArtifactRepository.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/artifact/repository/AbstractArtifactRepository.java @@ -108,11 +108,12 @@ public abstract class AbstractArtifactRepository implements ArtifactRepository { private static File createTempFile() { try { final File file = Files.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX).toFile(); - if (!(file.setReadable(true, true) && - file.setWritable(true, true) && - file.setExecutable(false))) { + if (!file.setReadable(true, true) || + !file.setWritable(true, true)) { throw new IOException("Can't set proper permissions!"); } + // try, if not supported - ok + file.setExecutable(false); file.deleteOnExit(); return file; } catch (final IOException e) { diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/builder/TargetTypeUpdate.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/builder/TargetTypeUpdate.java index fbaaaf51f..0334f4d9b 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/builder/TargetTypeUpdate.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/builder/TargetTypeUpdate.java @@ -37,5 +37,5 @@ public interface TargetTypeUpdate { * @param name Name * @return updated builder instance */ - TargetTypeUpdate name(@Size(max = TargetType.NAME_MAX_SIZE) String name); + TargetTypeUpdate name(@Size(max = NamedEntity.NAME_MAX_SIZE) String name); } diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/RolloutGroupRepository.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/RolloutGroupRepository.java index e336b2042..a05b6dc6f 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/RolloutGroupRepository.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/RolloutGroupRepository.java @@ -80,9 +80,9 @@ public interface RolloutGroupRepository * states */ long countByRolloutIdAndStatusNotAndStatusNotAndStatusNot(@Param("rolloutId") long rolloutId, - @Param("status1") JpaRolloutGroup.RolloutGroupStatus status1, - @Param("status2") JpaRolloutGroup.RolloutGroupStatus status2, - @Param("status3") JpaRolloutGroup.RolloutGroupStatus status3); + @Param("status1") RolloutGroup.RolloutGroupStatus status1, + @Param("status2") RolloutGroup.RolloutGroupStatus status2, + @Param("status3") RolloutGroup.RolloutGroupStatus status3); /** * Retrieves all {@link RolloutGroup} for a specific parent in a specific