Skip to content

Commit b384b11

Browse files
Merge pull request #136 from manishdait/issue-102
Implemented tests for missing functionalities in FileClientImpl.updateFile()
2 parents bce1fb8 + 6d0ec11 commit b384b11

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.openelements.hiero.base.implementation.FileClientImpl;
66
import com.openelements.hiero.base.protocol.FileCreateResult;
77
import com.openelements.hiero.base.protocol.FileCreateRequest;
8+
import com.openelements.hiero.base.protocol.FileUpdateResult;
9+
import com.openelements.hiero.base.protocol.FileUpdateRequest;
810
import com.openelements.hiero.base.protocol.FileAppendRequest;
911
import com.openelements.hiero.base.protocol.FileAppendResult;
1012
import com.openelements.hiero.base.protocol.FileInfoRequest;
@@ -120,6 +122,104 @@ void testCreateFileThrowsExceptionForNullContent() {
120122
Assertions.assertTrue(exception.getMessage().contains(message));
121123
}
122124

125+
@Test
126+
void testUpdateFile() throws HieroException {
127+
// mock
128+
final FileUpdateResult fileUpdateResult = Mockito.mock(FileUpdateResult.class);
129+
130+
// given
131+
final FileId fileId = FileId.fromString("1.2.3");
132+
final byte[] updatedContent = "Hello Hiero! Updated".getBytes();
133+
134+
// then
135+
when(protocolLayerClient.executeFileUpdateRequestTransaction(any(FileUpdateRequest.class)))
136+
.thenReturn(fileUpdateResult);
137+
138+
fileClientImpl.updateFile(fileId, updatedContent);
139+
140+
verify(protocolLayerClient, times(1))
141+
.executeFileUpdateRequestTransaction(any(FileUpdateRequest.class));
142+
}
143+
144+
@Test
145+
void testUpdateFileForSizeGreaterThanFileCreateMaxSize() throws HieroException {
146+
// mock
147+
final FileUpdateResult fileUpdateResult = Mockito.mock(FileUpdateResult.class);
148+
final FileAppendResult fileAppendResult = Mockito.mock(FileAppendResult.class);
149+
150+
// given
151+
final FileId fileId = FileId.fromString("1.2.3");
152+
final byte[] updatedContent = new byte[FileCreateRequest.FILE_CREATE_MAX_SIZE * 2];
153+
// -1 because 1 for executeFileCreateTransaction()
154+
final int appendCount = Math.floorDiv(updatedContent.length, FileCreateRequest.FILE_CREATE_MAX_SIZE) - 1;
155+
156+
//then
157+
when(protocolLayerClient.executeFileUpdateRequestTransaction(any(FileUpdateRequest.class)))
158+
.thenReturn(fileUpdateResult);
159+
when(protocolLayerClient.executeFileAppendRequestTransaction(any(FileAppendRequest.class)))
160+
.thenReturn(fileAppendResult);
161+
162+
fileClientImpl.updateFile(fileId, updatedContent);
163+
164+
verify(protocolLayerClient, times(1))
165+
.executeFileUpdateRequestTransaction(any(FileUpdateRequest.class));
166+
verify(protocolLayerClient, times(appendCount))
167+
.executeFileAppendRequestTransaction(any(FileAppendRequest.class));
168+
}
169+
170+
@Test
171+
void testUpdateFileThrowsExceptionForInvalidFileId() throws HieroException {
172+
final String message = "Failed to execute transaction of type FileUpdateTransaction";
173+
174+
// given
175+
final FileId fileId = FileId.fromString("1.2.3");
176+
final byte[] updatedContent = "Hello Hiero! Updated".getBytes();
177+
178+
// then
179+
when(protocolLayerClient.executeFileUpdateRequestTransaction(any(FileUpdateRequest.class)))
180+
.thenThrow(new HieroException(message));
181+
182+
final HieroException exception = Assertions.assertThrows(
183+
HieroException.class, () -> fileClientImpl.updateFile(fileId, updatedContent)
184+
);
185+
Assertions.assertTrue(exception.getMessage().contains(message));
186+
}
187+
188+
@Test
189+
void testUpdateFileThrowsExceptionForSizeGreaterThanMaxFileSize() {
190+
final String message = "File contents must be less than " + FileCreateRequest.FILE_MAX_SIZE + " bytes";
191+
192+
// given
193+
final FileId fileId = FileId.fromString("1.2.3");
194+
final byte[] updatedContent = new byte[FileCreateRequest.FILE_MAX_SIZE + 1];
195+
196+
// then
197+
final HieroException exception = Assertions.assertThrows(
198+
HieroException.class, () -> fileClientImpl.updateFile(fileId, updatedContent)
199+
);
200+
Assertions.assertTrue(exception.getMessage().contains(message));
201+
}
202+
203+
@Test
204+
void testUpdateFileThrowsExceptionForNullArguments() {
205+
// given
206+
final FileId fileId = FileId.fromString("1.2.3");
207+
final byte[] updatedContent = "Hello Hiero! Updated".getBytes();
208+
209+
// then
210+
final NullPointerException nullContentException = Assertions.assertThrows(
211+
NullPointerException.class, () -> fileClientImpl.updateFile(fileId, null)
212+
);
213+
Assertions.assertTrue(nullContentException.getMessage().contains("content must not be null"));
214+
215+
final NullPointerException nullIdException = Assertions.assertThrows(
216+
NullPointerException.class, () -> fileClientImpl.updateFile(null, updatedContent)
217+
);
218+
Assertions.assertTrue(nullIdException.getMessage().contains("fileId must not be null"));
219+
220+
Assertions.assertThrows(NullPointerException.class, () -> fileClientImpl.updateFile(null, null));
221+
}
222+
123223
@Test
124224
void testGetFileSize() throws HieroException {
125225
// mocks

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.hedera.hashgraph.sdk.FileId;
44
import com.openelements.hiero.base.FileClient;
55
import com.openelements.hiero.base.HieroException;
6+
67
import java.time.Duration;
78
import java.time.Instant;
89
import java.time.temporal.ChronoUnit;
@@ -147,9 +148,9 @@ void testDeleteFileByFileId() throws Exception {
147148
@Test
148149
void testUpdateFileByFileId() throws Exception {
149150
//given
150-
final byte[] contents = "Hello, Hedera!".getBytes();
151+
final byte[] contents = "Hello, Hiero!".getBytes();
151152
final FileId fileId = fileClient.createFile(contents);
152-
final String newContent = "Hello, Hedera! Updated";
153+
final String newContent = "Hello, Hiero! Updated";
153154

154155
//when
155156
fileClient.updateFile(fileId, newContent.getBytes());
@@ -159,6 +160,55 @@ void testUpdateFileByFileId() throws Exception {
159160
Assertions.assertArrayEquals(newContent.getBytes(), readContents);
160161
}
161162

163+
@Test
164+
void testUpdateFileForSizeGreaterThanCreateFileSize() throws HieroException {
165+
// given
166+
final byte[] contents = "Hello, Hiero!".getBytes();
167+
final FileId fileId = fileClient.createFile(contents);
168+
final byte[] updatedContent = new byte[FileCreateRequest.FILE_CREATE_MAX_SIZE * 2];
169+
170+
// when
171+
fileClient.updateFile(fileId, updatedContent);
172+
173+
// then
174+
final byte[] readContent = fileClient.readFile(fileId);
175+
Assertions.assertArrayEquals(updatedContent, readContent);
176+
}
177+
178+
@Test
179+
void testUpdateFileThrowExceptionForInvalidFileId() {
180+
// given
181+
final FileId fileId = FileId.fromString("1.2.3");
182+
final byte[] updatedContent = "Hello, Hiero! Update".getBytes();
183+
184+
// then
185+
Assertions.assertThrows(HieroException.class, () -> fileClient.updateFile(fileId, updatedContent));
186+
}
187+
188+
@Test
189+
void testUpdateFileThrowExceptionIfSizeExceedMaxSize() throws HieroException {
190+
// given
191+
final byte[] contents = "Hello, Hiero!".getBytes();
192+
final FileId fileId = fileClient.createFile(contents);
193+
final byte[] updatedContent = new byte[FileCreateRequest.FILE_MAX_SIZE + 1];
194+
195+
// then
196+
Assertions.assertThrows(HieroException.class, () -> fileClient.updateFile(fileId, updatedContent));
197+
}
198+
199+
@Test
200+
void testUpdateFileThrowsErrorForNullValues() throws HieroException {
201+
// given
202+
final byte[] contents = "Hello, Hiero!".getBytes();
203+
final FileId fileId = fileClient.createFile(contents);
204+
final byte[] updatedContent = "Hello, Hiero! Update".getBytes();
205+
206+
// then
207+
Assertions.assertThrows(NullPointerException.class, () -> fileClient.updateFile(fileId, null));
208+
Assertions.assertThrows(NullPointerException.class, () -> fileClient.updateFile(null, updatedContent));
209+
Assertions.assertThrows(NullPointerException.class, () -> fileClient.updateFile(null, null));
210+
}
211+
162212
@Test
163213
void testDeleteFileByStringId() throws Exception {
164214
//given

0 commit comments

Comments
 (0)