diff --git a/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java index e48f1fc..9c4b4a2 100644 --- a/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java +++ b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java @@ -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; @@ -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)); + } } diff --git a/hiero-enterprise-spring/src/test/java/com/openelements/hiero/spring/test/FileClientTests.java b/hiero-enterprise-spring/src/test/java/com/openelements/hiero/spring/test/FileClientTests.java index 3289c70..5b0b6b7 100644 --- a/hiero-enterprise-spring/src/test/java/com/openelements/hiero/spring/test/FileClientTests.java +++ b/hiero-enterprise-spring/src/test/java/com/openelements/hiero/spring/test/FileClientTests.java @@ -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 @@ -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 @@ -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(); @@ -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