|
3 | 3 | import com.hedera.hashgraph.sdk.FileId; |
4 | 4 | import com.openelements.hiero.base.HieroException; |
5 | 5 | import com.openelements.hiero.base.implementation.FileClientImpl; |
6 | | -import com.openelements.hiero.base.protocol.FileCreateResult; |
7 | | -import com.openelements.hiero.base.protocol.FileCreateRequest; |
8 | | -import com.openelements.hiero.base.protocol.FileAppendRequest; |
9 | | -import com.openelements.hiero.base.protocol.FileAppendResult; |
10 | | -import com.openelements.hiero.base.protocol.FileInfoRequest; |
11 | | -import com.openelements.hiero.base.protocol.FileInfoResponse; |
12 | | -import com.openelements.hiero.base.protocol.ProtocolLayerClient; |
| 6 | +import com.openelements.hiero.base.protocol.*; |
13 | 7 | import org.junit.jupiter.api.Assertions; |
14 | 8 | import org.junit.jupiter.api.BeforeEach; |
15 | 9 | import org.junit.jupiter.api.Test; |
16 | 10 | import org.mockito.Mockito; |
| 11 | +import org.mockito.internal.matchers.Null; |
17 | 12 |
|
18 | 13 | import java.time.Instant; |
19 | 14 |
|
@@ -120,6 +115,104 @@ void testCreateFileThrowsExceptionForNullContent() { |
120 | 115 | Assertions.assertTrue(exception.getMessage().contains(message)); |
121 | 116 | } |
122 | 117 |
|
| 118 | + @Test |
| 119 | + void testUpdateFile() throws HieroException { |
| 120 | + // mock |
| 121 | + final FileUpdateResult fileUpdateResult = Mockito.mock(FileUpdateResult.class); |
| 122 | + |
| 123 | + // given |
| 124 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 125 | + final byte[] updatedContent = "Hello Hiero! Updated".getBytes(); |
| 126 | + |
| 127 | + // then |
| 128 | + when(protocolLayerClient.executeFileUpdateRequestTransaction(any(FileUpdateRequest.class))) |
| 129 | + .thenReturn(fileUpdateResult); |
| 130 | + |
| 131 | + fileClientImpl.updateFile(fileId, updatedContent); |
| 132 | + |
| 133 | + verify(protocolLayerClient, times(1)) |
| 134 | + .executeFileUpdateRequestTransaction(any(FileUpdateRequest.class)); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void testUpdateFileForSizeGreaterThanFileCreateMaxSize() throws HieroException { |
| 139 | + // mock |
| 140 | + final FileUpdateResult fileUpdateResult = Mockito.mock(FileUpdateResult.class); |
| 141 | + final FileAppendResult fileAppendResult = Mockito.mock(FileAppendResult.class); |
| 142 | + |
| 143 | + // given |
| 144 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 145 | + final byte[] updatedContent = new byte[FileCreateRequest.FILE_CREATE_MAX_SIZE * 2]; |
| 146 | + // -1 because 1 for executeFileCreateTransaction() |
| 147 | + final int appendCount = Math.floorDiv(updatedContent.length, FileCreateRequest.FILE_CREATE_MAX_SIZE) - 1; |
| 148 | + |
| 149 | + //then |
| 150 | + when(protocolLayerClient.executeFileUpdateRequestTransaction(any(FileUpdateRequest.class))) |
| 151 | + .thenReturn(fileUpdateResult); |
| 152 | + when(protocolLayerClient.executeFileAppendRequestTransaction(any(FileAppendRequest.class))) |
| 153 | + .thenReturn(fileAppendResult); |
| 154 | + |
| 155 | + fileClientImpl.updateFile(fileId, updatedContent); |
| 156 | + |
| 157 | + verify(protocolLayerClient, times(1)) |
| 158 | + .executeFileUpdateRequestTransaction(any(FileUpdateRequest.class)); |
| 159 | + verify(protocolLayerClient, times(appendCount)) |
| 160 | + .executeFileAppendRequestTransaction(any(FileAppendRequest.class)); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void testUpdateFileThrowsExceptionForInvalidFileId() throws HieroException { |
| 165 | + final String message = "Failed to execute transaction of type FileUpdateTransaction"; |
| 166 | + |
| 167 | + // given |
| 168 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 169 | + final byte[] updatedContent = "Hello Hiero! Updated".getBytes(); |
| 170 | + |
| 171 | + // then |
| 172 | + when(protocolLayerClient.executeFileUpdateRequestTransaction(any(FileUpdateRequest.class))) |
| 173 | + .thenThrow(new HieroException(message)); |
| 174 | + |
| 175 | + final HieroException exception = Assertions.assertThrows( |
| 176 | + HieroException.class, () -> fileClientImpl.updateFile(fileId, updatedContent) |
| 177 | + ); |
| 178 | + Assertions.assertTrue(exception.getMessage().contains(message)); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void testUpdateFileThrowsExceptionForSizeGreaterThanMaxFileSize() { |
| 183 | + final String message = "File contents must be less than " + FileCreateRequest.FILE_MAX_SIZE + " bytes"; |
| 184 | + |
| 185 | + // given |
| 186 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 187 | + final byte[] updatedContent = new byte[FileCreateRequest.FILE_MAX_SIZE + 1]; |
| 188 | + |
| 189 | + // then |
| 190 | + final HieroException exception = Assertions.assertThrows( |
| 191 | + HieroException.class, () -> fileClientImpl.updateFile(fileId, updatedContent) |
| 192 | + ); |
| 193 | + Assertions.assertTrue(exception.getMessage().contains(message)); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void testUpdateFileThrowsExceptionForNullArguments() { |
| 198 | + // given |
| 199 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 200 | + final byte[] updatedContent = "Hello Hiero! Updated".getBytes(); |
| 201 | + |
| 202 | + // then |
| 203 | + final NullPointerException nullContentException = Assertions.assertThrows( |
| 204 | + NullPointerException.class, () -> fileClientImpl.updateFile(fileId, null) |
| 205 | + ); |
| 206 | + Assertions.assertTrue(nullContentException.getMessage().contains("content must not be null")); |
| 207 | + |
| 208 | + final NullPointerException nullIdException = Assertions.assertThrows( |
| 209 | + NullPointerException.class, () -> fileClientImpl.updateFile(null, updatedContent) |
| 210 | + ); |
| 211 | + Assertions.assertTrue(nullIdException.getMessage().contains("fileId must not be null")); |
| 212 | + |
| 213 | + Assertions.assertThrows(NullPointerException.class, () -> fileClientImpl.updateFile(null, null)); |
| 214 | + } |
| 215 | + |
123 | 216 | @Test |
124 | 217 | void testGetFileSize() throws HieroException { |
125 | 218 | // mocks |
|
0 commit comments