Skip to content

Commit 51a80ca

Browse files
Merge pull request #131 from manishdait/issue-105
Implemented test for getSize() method of FileClientImpl
2 parents 8ed92d1 + fccb380 commit 51a80ca

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

hiero-enterprise-base/src/test/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
requires static org.jspecify;
55
requires org.junit.jupiter.api;
66
requires org.junit.jupiter.params;
7+
requires org.mockito;
78
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void testNullParams() {
2727
Assertions.assertThrows(NullPointerException.class, () -> fileClient.deleteFile((String) null));
2828
Assertions.assertThrows(NullPointerException.class, () -> fileClient.deleteFile((FileId) null));
2929
Assertions.assertThrows(NullPointerException.class, () -> fileClient.getExpirationTime(null));
30+
Assertions.assertThrows(NullPointerException.class, () -> fileClient.getSize(null));
3031
}
3132

3233
@Test
@@ -240,4 +241,19 @@ void testUpdateExpirationTimeDoesNotChangeContent() throws Exception {
240241
//then
241242
Assertions.assertArrayEquals(contents, result);
242243
}
244+
245+
@Test
246+
void testGetFileSize() throws HieroException {
247+
final byte[] contents = "Hello, Hiero!".getBytes();
248+
final FileId fileId = fileClient.createFile(contents);
249+
final int size = fileClient.getSize(fileId);
250+
251+
Assertions.assertEquals(size, contents.length);
252+
}
253+
254+
@Test
255+
void testGetFileSizeThrowsExceptionForInvalidId() {
256+
final FileId invalidFileId = FileId.fromString("1.2.3");
257+
Assertions.assertThrows(HieroException.class, () -> fileClient.getSize(invalidFileId));
258+
}
243259
}

0 commit comments

Comments
 (0)