Skip to content

Commit

Permalink
Merge pull request #140 from manishdait/issue-100
Browse files Browse the repository at this point in the history
Implemented tests for missing functionalities in FileClientImpl.readFile()
  • Loading branch information
Ndacyayisenga-droid authored Dec 22, 2024
2 parents b384b11 + 7d7a56c commit 2293bb1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.openelements.hiero.base.protocol.FileAppendResult;
import com.openelements.hiero.base.protocol.FileInfoRequest;
import com.openelements.hiero.base.protocol.FileInfoResponse;
import com.openelements.hiero.base.protocol.FileContentsRequest;
import com.openelements.hiero.base.protocol.FileContentsResponse;
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -263,4 +265,54 @@ void testGetFileSizeThrowsExceptionForNullId() {
);
Assertions.assertTrue(exception.getMessage().contains(message));
}

@Test
void testReadFile() throws HieroException {
// mock
final FileContentsResponse fileContentsResponse = Mockito.mock(FileContentsResponse.class);
final byte[] content = "Hello Hiero!".getBytes();

// given
final FileId fileId = FileId.fromString("1.2.3");

when(protocolLayerClient.executeFileContentsQuery(any(FileContentsRequest.class)))
.thenReturn(fileContentsResponse);
when(fileContentsResponse.contents()).thenReturn(content);

final byte[] result = fileClientImpl.readFile(fileId);

verify(protocolLayerClient, times(1))
.executeFileContentsQuery(any(FileContentsRequest.class));
verify(fileContentsResponse, times(1)).contents();

Assertions.assertArrayEquals(content, result);
}

@Test
void testReadFileThrowsExceptionForInvalidId() throws HieroException {
// given
final FileId fileId = FileId.fromString("1.2.3");
final String message = "Failed to read file with fileId " + fileId;

when(protocolLayerClient.executeFileContentsQuery(any(FileContentsRequest.class)))
.thenThrow(new HieroException("Failed to execute query"));

final HieroException exception = Assertions.assertThrows(
HieroException.class, () -> fileClientImpl.readFile(fileId)
);

Assertions.assertTrue(exception.getMessage().contains(message));
}

@Test
void testReadFileThrowsExceptionForNullValue() {
final String message = "fileId must not be null";
final FileId fileId = null;

final NullPointerException exception = Assertions.assertThrows(
NullPointerException.class, () -> fileClientImpl.readFile(fileId)
);

Assertions.assertTrue(exception.getMessage().contains(message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void testCreateFileThrowExceptionIfExpirationTimeBeforeNow() {
@Test
void testReadFileByFileId() throws Exception {
//given
final byte[] contents = "Hello, Hedera!".getBytes();
final byte[] contents = "Hello, Hiero!".getBytes();
final FileId fileId = fileClient.createFile(contents);

//when
Expand All @@ -109,7 +109,7 @@ void testReadFileByFileId() throws Exception {
@Test
void testReadFileByStringId() throws Exception {
//given
final byte[] contents = "Hello, Hedera!".getBytes();
final byte[] contents = "Hello, Hiero!".getBytes();
final String fileId = fileClient.createFile(contents).toString();

//when
Expand All @@ -122,7 +122,7 @@ void testReadFileByStringId() throws Exception {
@Test
void testReadLargeFileByStringId() throws Exception {
//given
final byte[] contents = IntStream.range(0, 500).mapToObj(i -> "Hello, Hedera!")
final byte[] contents = IntStream.range(0, 500).mapToObj(i -> "Hello, Hiero!")
.reduce((a, b) -> a + b)
.get()
.getBytes();
Expand All @@ -135,6 +135,12 @@ void testReadLargeFileByStringId() throws Exception {
Assertions.assertArrayEquals(contents, readContents);
}

@Test
void testReadFileThrowsExceptionForInvalidId() {
final FileId fileId = FileId.fromString("1.2.3");
Assertions.assertThrows(HieroException.class, () -> fileClient.readFile(fileId));
}

@Test
void testDeleteFileByFileId() throws Exception {
//given
Expand Down

0 comments on commit 2293bb1

Please sign in to comment.