Fix sonar findings: Fix artifact file repo tests (#1986)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-11-09 17:31:59 +02:00
committed by GitHub
parent fa9a715aa6
commit 313abf330b
5 changed files with 42 additions and 42 deletions

View File

@@ -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
}
}
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -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