Fix sonar findings: Fix artifact file repo tests (#1986)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -32,16 +32,15 @@ import org.junit.jupiter.api.Test;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Feature("Unit Tests - Artifact File System Repository")
|
@Feature("Unit Tests - Artifact File System Repository")
|
||||||
@Story("Test storing artifact binaries in the file-system")
|
@Story("Test storing artifact binaries in the file-system")
|
||||||
public class ArtifactFilesystemRepositoryTest {
|
class ArtifactFilesystemRepositoryTest {
|
||||||
|
|
||||||
private static final String TENANT = "test_tenant";
|
private static final String TENANT = "test_tenant";
|
||||||
|
|
||||||
private static ArtifactFilesystemProperties artifactResourceProperties;
|
private static ArtifactFilesystemProperties artifactResourceProperties;
|
||||||
|
|
||||||
private static ArtifactFilesystemRepository artifactFilesystemRepository;
|
private static ArtifactFilesystemRepository artifactFilesystemRepository;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setup() throws IOException {
|
static void setup() throws IOException {
|
||||||
artifactResourceProperties = new ArtifactFilesystemProperties();
|
artifactResourceProperties = new ArtifactFilesystemProperties();
|
||||||
artifactResourceProperties.setPath(Files.createTempDirectory(null).toString());
|
artifactResourceProperties.setPath(Files.createTempDirectory(null).toString());
|
||||||
|
|
||||||
@@ -49,7 +48,7 @@ public class ArtifactFilesystemRepositoryTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@AfterAll
|
@AfterAll
|
||||||
public static void afterClass() {
|
static void afterClass() {
|
||||||
if (new File(artifactResourceProperties.getPath()).exists()) {
|
if (new File(artifactResourceProperties.getPath()).exists()) {
|
||||||
try {
|
try {
|
||||||
FileUtils.deleteDirectory(new File(artifactResourceProperties.getPath()));
|
FileUtils.deleteDirectory(new File(artifactResourceProperties.getPath()));
|
||||||
@@ -60,44 +59,38 @@ public class ArtifactFilesystemRepositoryTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verfies that an artifact can be successfully stored in the file-system repository")
|
@Description("Verifies that an artifact can be successfully stored in the file-system repository")
|
||||||
public void storeSuccessfully() throws IOException {
|
void storeSuccessfully() throws IOException {
|
||||||
final byte[] fileContent = randomBytes();
|
final byte[] fileContent = randomBytes();
|
||||||
final AbstractDbArtifact artifact = storeRandomArtifact(fileContent);
|
final AbstractDbArtifact artifact = storeRandomArtifact(fileContent);
|
||||||
|
|
||||||
final byte[] readContent = new byte[fileContent.length];
|
final byte[] readContent = new byte[fileContent.length];
|
||||||
IOUtils.read(artifact.getFileInputStream(), readContent);
|
IOUtils.read(artifact.getFileInputStream(), readContent);
|
||||||
|
|
||||||
assertThat(readContent).isEqualTo(fileContent);
|
assertThat(readContent).isEqualTo(fileContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verfies that an artifact can be successfully stored in the file-system repository")
|
@Description("Verifies that an artifact can be successfully stored in the file-system repository")
|
||||||
public void getStoredArtifactBasedOnSHA1Hash() {
|
void getStoredArtifactBasedOnSHA1Hash() {
|
||||||
|
|
||||||
final byte[] fileContent = randomBytes();
|
final byte[] fileContent = randomBytes();
|
||||||
final AbstractDbArtifact artifact = storeRandomArtifact(fileContent);
|
final AbstractDbArtifact artifact = storeRandomArtifact(fileContent);
|
||||||
|
|
||||||
final AbstractDbArtifact artifactBySha1 = artifactFilesystemRepository.getArtifactBySha1(TENANT,
|
assertThat(artifactFilesystemRepository.getArtifactBySha1(TENANT, artifact.getHashes().getSha1())).isNotNull();
|
||||||
artifact.getHashes().getSha1());
|
|
||||||
assertThat(artifactBySha1).isNotNull();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verfies that an artifact can be deleted in the file-system repository")
|
@Description("Verifies that an artifact can be deleted in the file-system repository")
|
||||||
public void deleteStoredArtifactBySHA1Hash() {
|
void deleteStoredArtifactBySHA1Hash() {
|
||||||
final AbstractDbArtifact artifact = storeRandomArtifact(randomBytes());
|
final AbstractDbArtifact artifact = storeRandomArtifact(randomBytes());
|
||||||
|
|
||||||
artifactFilesystemRepository.deleteBySha1(TENANT, artifact.getHashes().getSha1());
|
artifactFilesystemRepository.deleteBySha1(TENANT, artifact.getHashes().getSha1());
|
||||||
|
|
||||||
assertThat(artifactFilesystemRepository.getArtifactBySha1(TENANT, artifact.getHashes().getSha1())).isNull();
|
assertThat(artifactFilesystemRepository.getArtifactBySha1(TENANT, artifact.getHashes().getSha1())).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verfies that all artifacts of a tenant can be deleted in the file-system repository")
|
@Description("Verifies that all artifacts of a tenant can be deleted in the file-system repository")
|
||||||
public void deleteStoredArtifactOfTenant() {
|
void deleteStoredArtifactOfTenant() {
|
||||||
final AbstractDbArtifact artifact = storeRandomArtifact(randomBytes());
|
final AbstractDbArtifact artifact = storeRandomArtifact(randomBytes());
|
||||||
|
|
||||||
artifactFilesystemRepository.deleteByTenant(TENANT);
|
artifactFilesystemRepository.deleteByTenant(TENANT);
|
||||||
|
|
||||||
assertThat(artifactFilesystemRepository.getArtifactBySha1(TENANT, artifact.getHashes().getSha1())).isNull();
|
assertThat(artifactFilesystemRepository.getArtifactBySha1(TENANT, artifact.getHashes().getSha1())).isNull();
|
||||||
@@ -105,7 +98,7 @@ public class ArtifactFilesystemRepositoryTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verfies that an artifact which does not exists is deleted quietly in the file-system repository")
|
@Description("Verfies that an artifact which does not exists is deleted quietly in the file-system repository")
|
||||||
public void deleteArtifactWhichDoesNotExistsBySHA1HashWithoutException() {
|
void deleteArtifactWhichDoesNotExistsBySHA1HashWithoutException() {
|
||||||
try {
|
try {
|
||||||
artifactFilesystemRepository.deleteBySha1(TENANT, "sha1HashWhichDoesNotExists");
|
artifactFilesystemRepository.deleteBySha1(TENANT, "sha1HashWhichDoesNotExists");
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
@@ -122,15 +115,21 @@ public class ArtifactFilesystemRepositoryTest {
|
|||||||
|
|
||||||
private static byte[] randomBytes() {
|
private static byte[] randomBytes() {
|
||||||
final byte[] randomBytes = new byte[20];
|
final byte[] randomBytes = new byte[20];
|
||||||
final Random ran = new Random();
|
new Random().nextBytes(randomBytes);
|
||||||
ran.nextBytes(randomBytes);
|
|
||||||
return randomBytes;
|
return randomBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private AbstractDbArtifact storeRandomArtifact(final byte[] fileContent) {
|
private AbstractDbArtifact storeRandomArtifact(final byte[] fileContent) {
|
||||||
final String fileName = "filename.tmp";
|
|
||||||
final ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContent);
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
@@ -26,15 +26,15 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
@Feature("Unit Tests - Artifact File System Repository")
|
@Feature("Unit Tests - Artifact File System Repository")
|
||||||
@Story("Test storing artifact binaries in the file-system")
|
@Story("Test storing artifact binaries in the file-system")
|
||||||
public class ArtifactFilesystemTest {
|
class ArtifactFilesystemTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verifies that an exception is thrown on opening an InputStream when file does not exists")
|
@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 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);
|
new DbArtifactHash("1", "2", "3"), 0L, null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
underTest.getFileInputStream();
|
underTest.getFileInputStream();
|
||||||
Assertions.fail("Expected a FileNotFoundException because file does not exists");
|
Assertions.fail("Expected a FileNotFoundException because file does not exists");
|
||||||
@@ -45,13 +45,13 @@ public class ArtifactFilesystemTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verifies that an InputStream can be opened if file exists")
|
@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();
|
final File createTempFile = Files.createTempFile(ArtifactFilesystemTest.class.getSimpleName(), "").toFile();
|
||||||
createTempFile.deleteOnExit();
|
createTempFile.deleteOnExit();
|
||||||
|
|
||||||
final ArtifactFilesystem underTest = new ArtifactFilesystem(createTempFile,
|
final ArtifactFilesystem underTest = new ArtifactFilesystem(
|
||||||
ArtifactFilesystemTest.class.getSimpleName(), new DbArtifactHash("1", "2", "3"), 0L, null);
|
createTempFile, ArtifactFilesystemTest.class.getSimpleName(), new DbArtifactHash("1", "2", "3"), 0L, null);
|
||||||
final byte[] buffer = new byte[1024];
|
final byte[] buffer = new byte[1024];
|
||||||
IOUtils.read(underTest.getFileInputStream(), buffer);
|
assertThat(IOUtils.read(underTest.getFileInputStream(), buffer)).isEqualTo(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,11 +108,12 @@ public abstract class AbstractArtifactRepository implements ArtifactRepository {
|
|||||||
private static File createTempFile() {
|
private static File createTempFile() {
|
||||||
try {
|
try {
|
||||||
final File file = Files.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX).toFile();
|
final File file = Files.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX).toFile();
|
||||||
if (!(file.setReadable(true, true) &&
|
if (!file.setReadable(true, true) ||
|
||||||
file.setWritable(true, true) &&
|
!file.setWritable(true, true)) {
|
||||||
file.setExecutable(false))) {
|
|
||||||
throw new IOException("Can't set proper permissions!");
|
throw new IOException("Can't set proper permissions!");
|
||||||
}
|
}
|
||||||
|
// try, if not supported - ok
|
||||||
|
file.setExecutable(false);
|
||||||
file.deleteOnExit();
|
file.deleteOnExit();
|
||||||
return file;
|
return file;
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
|
|||||||
@@ -37,5 +37,5 @@ public interface TargetTypeUpdate {
|
|||||||
* @param name Name
|
* @param name Name
|
||||||
* @return updated builder instance
|
* @return updated builder instance
|
||||||
*/
|
*/
|
||||||
TargetTypeUpdate name(@Size(max = TargetType.NAME_MAX_SIZE) String name);
|
TargetTypeUpdate name(@Size(max = NamedEntity.NAME_MAX_SIZE) String name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,9 +80,9 @@ public interface RolloutGroupRepository
|
|||||||
* states
|
* states
|
||||||
*/
|
*/
|
||||||
long countByRolloutIdAndStatusNotAndStatusNotAndStatusNot(@Param("rolloutId") long rolloutId,
|
long countByRolloutIdAndStatusNotAndStatusNotAndStatusNot(@Param("rolloutId") long rolloutId,
|
||||||
@Param("status1") JpaRolloutGroup.RolloutGroupStatus status1,
|
@Param("status1") RolloutGroup.RolloutGroupStatus status1,
|
||||||
@Param("status2") JpaRolloutGroup.RolloutGroupStatus status2,
|
@Param("status2") RolloutGroup.RolloutGroupStatus status2,
|
||||||
@Param("status3") JpaRolloutGroup.RolloutGroupStatus status3);
|
@Param("status3") RolloutGroup.RolloutGroupStatus status3);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves all {@link RolloutGroup} for a specific parent in a specific
|
* Retrieves all {@link RolloutGroup} for a specific parent in a specific
|
||||||
|
|||||||
Reference in New Issue
Block a user