Skip to content

Commit

Permalink
adding some tests
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Atanasov <[email protected]>
  • Loading branch information
ata-nas committed Jan 8, 2025
1 parent 1934860 commit cf1f403
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ public Path resolveLiveRawPathToBlock(final long blockNumber) {
@NonNull
@Override
public Path resolveArchiveRawPathToBlock(final long blockNumber) {
Preconditions.requireWhole(blockNumber);
throw new UnsupportedOperationException("Not implemented yet");
}

@NonNull
@Override
public Optional<Path> findBlock(final long blockNumber) { // todo add archive root search & abstract if common
Preconditions.requireWhole(blockNumber);
final Path liveRawRootPath = resolveLiveRawPathToBlock(blockNumber);
final Path compressionExtendedLiveRawRootPath =
FileUtilities.appendExtension(liveRawRootPath, compressionType.getFileExtension());

Check warning on line 64 in server/src/main/java/com/hedera/block/server/persistence/storage/path/BlockAsLocalDirPathResolver.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/hedera/block/server/persistence/storage/path/BlockAsLocalDirPathResolver.java#L61-L64

Added lines #L61 - L64 were not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ public Path resolveLiveRawPathToBlock(final long blockNumber) {
@NonNull
@Override
public Path resolveArchiveRawPathToBlock(final long blockNumber) {
Preconditions.requireWhole(blockNumber);
throw new UnsupportedOperationException("Not implemented yet");
}

@NonNull
@Override
public Optional<Path> findBlock(final long blockNumber) { // todo add archive root search & abstract if common
Preconditions.requireWhole(blockNumber);
final Path liveRawRootPath = resolveLiveRawPathToBlock(blockNumber);
final Path compressionExtendedLiveRawRootPath =
FileUtilities.appendExtension(liveRawRootPath, compressionType.getFileExtension());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ private BlockUnparsed doRead(final Path resolvedBlockPath) throws IOException, P
new ReadableStreamingData(compression.wrap(Files.newInputStream(resolvedBlockPath)))) {
return BlockUnparsed.PROTOBUF.parse(data);
} // todo this method must be extended to try to read uncompressed data if compressed data is not found
// need some mechanism that would be convenient to use not only here but also in other places
// need some mechanism that would be convenient to use not only here but also in other places
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -69,6 +70,37 @@ void testSuccessfulCompression(final String testData) throws IOException {
.hasSameBinaryContentAs(actualNoOpCompression(byteArrayTestData));
}

/**
* This test aims to verify that the
* {@link NoOpCompression#wrap(InputStream)} correctly wraps a valid
* provided {@link InputStream} and reads the test data from it`s
* destination as it is provided, no decompression is done.
*
* @param testData parameterized, test data
* @throws IOException if an I/O exception occurs
*/
@ParameterizedTest
@MethodSource("testData")
void testSuccessfulDecompression(final String testData) throws IOException {
final Path toRead = testTempDir.resolve("successfulDecompression.txt");
Files.createFile(toRead);

final byte[] expected = testData.getBytes(StandardCharsets.UTF_8);
try (final OutputStream out = Files.newOutputStream(toRead)) {
out.write(expected);
}

// assert that the target file exists and is a regular, non-empty file
assertThat(toRead).exists().isReadable().isRegularFile();

// read data
final byte[] actual;
try (final InputStream in = toTest.wrap(Files.newInputStream(toRead))) {
actual = in.readAllBytes();
}
assertThat(actual).isNotNull().isEqualTo(expected);
}

private Path actualNoOpCompression(final byte[] byteArrayTestData) throws IOException {
final Path tempFile = testTempDir.resolve("tempComparisonFile.txt");
try (final OutputStream out = Files.newOutputStream(tempFile)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
import com.hedera.block.server.util.TestConfigUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -87,6 +88,37 @@ void testSuccessfulCompression(final String testData) throws IOException {
.hasSameBinaryContentAs(actualZstdCompression(byteArrayTestData));
}

/**
* This test aims to verify that the
* {@link ZstdCompression#wrap(InputStream)} correctly wraps a valid
* provided {@link InputStream} and reads the test data from it`s
* destination but decompresses it using the Zstandard compression algorithm.
*
* @param testData parameterized, test data
* @throws IOException if an I/O exception occurs
*/
@ParameterizedTest
@MethodSource("testData")
void testSuccessfulDecompression(final String testData) throws IOException {
final Path toRead = testTempDir.resolve("successfulDecompression.txt");
Files.createFile(toRead);

final byte[] expected = testData.getBytes(StandardCharsets.UTF_8);
try (final OutputStream out = new ZstdOutputStream(Files.newOutputStream(toRead))) {
out.write(expected);
}

// assert that the target file exists and is a regular, non-empty file
assertThat(toRead).exists().isReadable().isRegularFile();

// read data
final byte[] actual;
try (final InputStream in = toTest.wrap(Files.newInputStream(toRead))) {
actual = in.readAllBytes();
}
assertThat(actual).isNotNull().isEqualTo(expected);
}

private Path actualZstdCompression(final byte[] byteArrayTestData) throws IOException {
final Path tempFile = testTempDir.resolve(
FileUtilities.appendExtension(Path.of("tempComparisonFile.txt"), toTest.getCompressionFileExtension()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -44,8 +45,8 @@ void setUp() throws IOException {

/**
* This test aims to verify that the
* {@link BlockAsLocalDirPathResolver#resolveLiveRawPathToBlock(long)} correctly
* resolves the path to a block by a given number. For the
* {@link BlockAsLocalDirPathResolver#resolveLiveRawPathToBlock(long)}
* correctly resolves the path to a block by a given number. For the
* block-as-local-directory storage strategy, the path to a block is simply
* the live root path appended with the given block number.
*
Expand All @@ -54,7 +55,7 @@ void setUp() throws IOException {
*/
@ParameterizedTest
@MethodSource("validBlockNumbers")
void testSuccessfulPathResolution(final long toResolve, final String expectedBlockFile) {
void testSuccessfulLiveRawPathResolution(final long toResolve, final String expectedBlockFile) {
final Path actual = toTest.resolveLiveRawPathToBlock(toResolve);
assertThat(actual).isNotNull().isAbsolute().isEqualByComparingTo(testLiveRootPath.resolve(expectedBlockFile));
}
Expand All @@ -69,10 +70,42 @@ void testSuccessfulPathResolution(final long toResolve, final String expectedBlo
*/
@ParameterizedTest
@MethodSource("invalidBlockNumbers")
void testInvalidBlockNumber(final long toResolve) {
void testInvalidBlockNumberLiveResolve(final long toResolve) {
assertThatIllegalArgumentException().isThrownBy(() -> toTest.resolveLiveRawPathToBlock(toResolve));
}

/**
* This test aims to verify that the
* {@link BlockAsLocalDirPathResolver#resolveArchiveRawPathToBlock(long)}
* correctly resolves the path to a block by a given number. For the
* block-as-local-directory storage strategy, the path to a block is simply
* the live root path appended with the given block number.
*
* @param toResolve parameterized, valid block number
* @param expectedBlockFile parameterized, expected block file
*/
@ParameterizedTest
@MethodSource("validBlockNumbers")
void testSuccessfulArchiveRawPathResolution(final long toResolve, final String expectedBlockFile) {
// todo this test is not yet implemented
Assertions.assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> toTest.resolveArchiveRawPathToBlock(toResolve));
}

/**
* This test aims to verify that the
* {@link BlockAsLocalDirPathResolver#resolveArchiveRawPathToBlock(long)} correctly
* throws an {@link IllegalArgumentException} when an invalid block number
* is provided. A block number is invalid if it is a strictly negative number.
*
* @param toResolve parameterized, invalid block number
*/
@ParameterizedTest
@MethodSource("invalidBlockNumbers")
void testInvalidBlockNumberArchiveResolve(final long toResolve) {
assertThatIllegalArgumentException().isThrownBy(() -> toTest.resolveArchiveRawPathToBlock(toResolve));
}

/**
* Some valid block numbers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -44,8 +45,8 @@ void setUp() throws IOException {

/**
* This test aims to verify that the
* {@link BlockAsLocalFilePathResolver#resolveLiveRawPathToBlock(long)} correctly
* resolves the path to a block by a given number. For the
* {@link BlockAsLocalFilePathResolver#resolveLiveRawPathToBlock(long)}
* correctly resolves the path to a block by a given number. For the
* block-as-file storage strategy, the path to a block is a trie structure
* where each digit of the block number is a directory and the block number
* itself is the file name.
Expand All @@ -55,7 +56,7 @@ void setUp() throws IOException {
*/
@ParameterizedTest
@MethodSource("validBlockNumbers")
void testSuccessfulPathResolution(final long toResolve, final String expectedBlockFile) {
void testSuccessfulLiveRawPathResolution(final long toResolve, final String expectedBlockFile) {
final Path actual = toTest.resolveLiveRawPathToBlock(toResolve);
assertThat(actual).isNotNull().isAbsolute().isEqualByComparingTo(testLiveRootPath.resolve(expectedBlockFile));
}
Expand All @@ -70,10 +71,43 @@ void testSuccessfulPathResolution(final long toResolve, final String expectedBlo
*/
@ParameterizedTest
@MethodSource("invalidBlockNumbers")
void testInvalidBlockNumber(final long toResolve) {
void testInvalidBlockNumberLiveResolve(final long toResolve) {
assertThatIllegalArgumentException().isThrownBy(() -> toTest.resolveLiveRawPathToBlock(toResolve));
}

/**
* This test aims to verify that the
* {@link BlockAsLocalFilePathResolver#resolveArchiveRawPathToBlock(long)}
* correctly resolves the path to a block by a given number. For the
* block-as-file storage strategy, the path to a block is a trie structure
* where each digit of the block number is a directory and the block number
* itself is the file name.
*
* @param toResolve parameterized, valid block number
* @param expectedBlockFile parameterized, expected block file
*/
@ParameterizedTest
@MethodSource("validBlockNumbers")
void testSuccessfulArchiveRawPathResolution(final long toResolve, final String expectedBlockFile) {
// todo this test is not yet implemented
Assertions.assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> toTest.resolveArchiveRawPathToBlock(toResolve));
}

/**
* This test aims to verify that the
* {@link BlockAsLocalFilePathResolver#resolveArchiveRawPathToBlock(long)} correctly
* throws an {@link IllegalArgumentException} when an invalid block number
* is provided. A block number is invalid if it is a strictly negative number.
*
* @param toResolve parameterized, invalid block number
*/
@ParameterizedTest
@MethodSource("invalidBlockNumbers")
void testInvalidBlockNumberArchiveResolve(final long toResolve) {
assertThatIllegalArgumentException().isThrownBy(() -> toTest.resolveArchiveRawPathToBlock(toResolve));
}

/**
* Some valid block numbers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,41 @@ void setUp() {
*/
@ParameterizedTest
@MethodSource({"validBlockNumbers", "invalidBlockNumbers"})
void testSuccessfulPathResolution(final long toResolve, final Path expected) {
void testSuccessfulLiveRawPathResolution(final long toResolve, final Path expected) {
final Path actual = toTest.resolveLiveRawPathToBlock(toResolve);
assertThat(actual).isNotNull().isAbsolute().isEqualByComparingTo(expected);
}

/**
* This test aims to verify that the
* {@link NoOpBlockPathResolver#resolveArchiveRawPathToBlock(long)} correctly resolves
* the path to a block by a given number. The no-op resolver does nothing,
* always returns a path resolved under '/tmp' based on the blockNumber and
* has no preconditions check. E.g. for blockNumber 0, the resolved path is
* '/tmp/hashgraph/blocknode/data/0.tmp.blk'.
*
* @param toResolve parameterized, block number
*/
@ParameterizedTest
@MethodSource({"validBlockNumbers", "invalidBlockNumbers"})
void testSuccessfulArchiveRawPathResolution(final long toResolve, final Path expected) {
final Path actual = toTest.resolveArchiveRawPathToBlock(toResolve);
assertThat(actual).isNotNull().isAbsolute().isEqualByComparingTo(expected);
}

/**
* This test aims to verify that the
* {@link NoOpBlockPathResolver#findBlock(long)} always returns an empty
* optional.
*
* @param toResolve parameterized, block number
*/
@ParameterizedTest
@MethodSource({"validBlockNumbers", "invalidBlockNumbers"})
void testSuccessfulFindBlock(final long toResolve) {
assertThat(toTest.findBlock(toResolve)).isNotNull().isEmpty();
}

/**
* Some valid block numbers.
*
Expand Down

0 comments on commit cf1f403

Please sign in to comment.