|
| 1 | +package com.openelements.hiero.base.test; |
| 2 | + |
| 3 | +import com.hedera.hashgraph.sdk.FileId; |
| 4 | +import com.openelements.hiero.base.HieroException; |
| 5 | +import com.openelements.hiero.base.implementation.FileClientImpl; |
| 6 | +import com.openelements.hiero.base.protocol.FileInfoRequest; |
| 7 | +import com.openelements.hiero.base.protocol.FileInfoResponse; |
| 8 | +import com.openelements.hiero.base.protocol.ProtocolLayerClient; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.mockito.Mockito; |
| 13 | + |
| 14 | +import static org.mockito.ArgumentMatchers.any; |
| 15 | +import static org.mockito.Mockito.*; |
| 16 | + |
| 17 | +public class FileClientImplTest { |
| 18 | + ProtocolLayerClient protocolLayerClient; |
| 19 | + FileClientImpl fileClientImpl; |
| 20 | + |
| 21 | + @BeforeEach |
| 22 | + void setup() { |
| 23 | + protocolLayerClient = Mockito.mock(ProtocolLayerClient.class); |
| 24 | + fileClientImpl = new FileClientImpl(protocolLayerClient); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void testGetFileSize() throws HieroException { |
| 29 | + // mocks |
| 30 | + final int size = 10; |
| 31 | + final FileInfoResponse response = Mockito.mock(FileInfoResponse.class); |
| 32 | + |
| 33 | + // given |
| 34 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 35 | + |
| 36 | + // then |
| 37 | + when(response.size()).thenReturn(size); |
| 38 | + when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) |
| 39 | + .thenReturn(response); |
| 40 | + |
| 41 | + final int result = fileClientImpl.getSize(fileId); |
| 42 | + |
| 43 | + verify(protocolLayerClient, times(1)) |
| 44 | + .executeFileInfoQuery(any(FileInfoRequest.class)); |
| 45 | + verify(response, times(1)).size(); |
| 46 | + Assertions.assertEquals(size, result); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testGetFileSizeThrowsExceptionForInvalidId() throws HieroException { |
| 51 | + // given |
| 52 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 53 | + |
| 54 | + // then |
| 55 | + when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) |
| 56 | + .thenThrow(new HieroException("Failed to execute query")); |
| 57 | + |
| 58 | + Assertions.assertThrows(HieroException.class, () -> fileClientImpl.getSize(fileId)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testGetFileSizeThrowsExceptionForNullId() { |
| 63 | + Assertions.assertThrows(NullPointerException.class, () -> fileClientImpl.getSize(null)); |
| 64 | + } |
| 65 | +} |
0 commit comments