|
5 | 5 | import com.openelements.hiero.base.implementation.FileClientImpl; |
6 | 6 | import com.openelements.hiero.base.protocol.FileCreateResult; |
7 | 7 | import com.openelements.hiero.base.protocol.FileCreateRequest; |
| 8 | +import com.openelements.hiero.base.protocol.FileUpdateResult; |
| 9 | +import com.openelements.hiero.base.protocol.FileUpdateRequest; |
8 | 10 | import com.openelements.hiero.base.protocol.FileAppendRequest; |
9 | 11 | import com.openelements.hiero.base.protocol.FileAppendResult; |
10 | 12 | import com.openelements.hiero.base.protocol.FileInfoRequest; |
@@ -120,6 +122,104 @@ void testCreateFileThrowsExceptionForNullContent() { |
120 | 122 | Assertions.assertTrue(exception.getMessage().contains(message)); |
121 | 123 | } |
122 | 124 |
|
| 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 | + |
123 | 223 | @Test |
124 | 224 | void testGetFileSize() throws HieroException { |
125 | 225 | // mocks |
|
0 commit comments