Skip to content

Commit 2293bb1

Browse files
Merge pull request #140 from manishdait/issue-100
Implemented tests for missing functionalities in FileClientImpl.readFile()
2 parents b384b11 + 7d7a56c commit 2293bb1

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.openelements.hiero.base.protocol.FileAppendResult;
1212
import com.openelements.hiero.base.protocol.FileInfoRequest;
1313
import com.openelements.hiero.base.protocol.FileInfoResponse;
14+
import com.openelements.hiero.base.protocol.FileContentsRequest;
15+
import com.openelements.hiero.base.protocol.FileContentsResponse;
1416
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
1517
import org.junit.jupiter.api.Assertions;
1618
import org.junit.jupiter.api.BeforeEach;
@@ -263,4 +265,54 @@ void testGetFileSizeThrowsExceptionForNullId() {
263265
);
264266
Assertions.assertTrue(exception.getMessage().contains(message));
265267
}
268+
269+
@Test
270+
void testReadFile() throws HieroException {
271+
// mock
272+
final FileContentsResponse fileContentsResponse = Mockito.mock(FileContentsResponse.class);
273+
final byte[] content = "Hello Hiero!".getBytes();
274+
275+
// given
276+
final FileId fileId = FileId.fromString("1.2.3");
277+
278+
when(protocolLayerClient.executeFileContentsQuery(any(FileContentsRequest.class)))
279+
.thenReturn(fileContentsResponse);
280+
when(fileContentsResponse.contents()).thenReturn(content);
281+
282+
final byte[] result = fileClientImpl.readFile(fileId);
283+
284+
verify(protocolLayerClient, times(1))
285+
.executeFileContentsQuery(any(FileContentsRequest.class));
286+
verify(fileContentsResponse, times(1)).contents();
287+
288+
Assertions.assertArrayEquals(content, result);
289+
}
290+
291+
@Test
292+
void testReadFileThrowsExceptionForInvalidId() throws HieroException {
293+
// given
294+
final FileId fileId = FileId.fromString("1.2.3");
295+
final String message = "Failed to read file with fileId " + fileId;
296+
297+
when(protocolLayerClient.executeFileContentsQuery(any(FileContentsRequest.class)))
298+
.thenThrow(new HieroException("Failed to execute query"));
299+
300+
final HieroException exception = Assertions.assertThrows(
301+
HieroException.class, () -> fileClientImpl.readFile(fileId)
302+
);
303+
304+
Assertions.assertTrue(exception.getMessage().contains(message));
305+
}
306+
307+
@Test
308+
void testReadFileThrowsExceptionForNullValue() {
309+
final String message = "fileId must not be null";
310+
final FileId fileId = null;
311+
312+
final NullPointerException exception = Assertions.assertThrows(
313+
NullPointerException.class, () -> fileClientImpl.readFile(fileId)
314+
);
315+
316+
Assertions.assertTrue(exception.getMessage().contains(message));
317+
}
266318
}

hiero-enterprise-spring/src/test/java/com/openelements/hiero/spring/test/FileClientTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void testCreateFileThrowExceptionIfExpirationTimeBeforeNow() {
9696
@Test
9797
void testReadFileByFileId() throws Exception {
9898
//given
99-
final byte[] contents = "Hello, Hedera!".getBytes();
99+
final byte[] contents = "Hello, Hiero!".getBytes();
100100
final FileId fileId = fileClient.createFile(contents);
101101

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

115115
//when
@@ -122,7 +122,7 @@ void testReadFileByStringId() throws Exception {
122122
@Test
123123
void testReadLargeFileByStringId() throws Exception {
124124
//given
125-
final byte[] contents = IntStream.range(0, 500).mapToObj(i -> "Hello, Hedera!")
125+
final byte[] contents = IntStream.range(0, 500).mapToObj(i -> "Hello, Hiero!")
126126
.reduce((a, b) -> a + b)
127127
.get()
128128
.getBytes();
@@ -135,6 +135,12 @@ void testReadLargeFileByStringId() throws Exception {
135135
Assertions.assertArrayEquals(contents, readContents);
136136
}
137137

138+
@Test
139+
void testReadFileThrowsExceptionForInvalidId() {
140+
final FileId fileId = FileId.fromString("1.2.3");
141+
Assertions.assertThrows(HieroException.class, () -> fileClient.readFile(fileId));
142+
}
143+
138144
@Test
139145
void testDeleteFileByFileId() throws Exception {
140146
//given

0 commit comments

Comments
 (0)