|
11 | 11 | import com.openelements.hiero.base.protocol.FileAppendResult; |
12 | 12 | import com.openelements.hiero.base.protocol.FileInfoRequest; |
13 | 13 | import com.openelements.hiero.base.protocol.FileInfoResponse; |
| 14 | +import com.openelements.hiero.base.protocol.FileContentsRequest; |
| 15 | +import com.openelements.hiero.base.protocol.FileContentsResponse; |
14 | 16 | import com.openelements.hiero.base.protocol.ProtocolLayerClient; |
15 | 17 | import org.junit.jupiter.api.Assertions; |
16 | 18 | import org.junit.jupiter.api.BeforeEach; |
@@ -263,4 +265,54 @@ void testGetFileSizeThrowsExceptionForNullId() { |
263 | 265 | ); |
264 | 266 | Assertions.assertTrue(exception.getMessage().contains(message)); |
265 | 267 | } |
| 268 | + |
| 269 | + @Test |
| 270 | + void testReadFile() throws HieroException { |
| 271 | + // mock |
| 272 | + final FileContentsResponse fileContentsResponse = Mockito.mock(FileContentsResponse.class); |
| 273 | + final byte[] content = "Hello Hiero!".getBytes(); |
| 274 | + |
| 275 | + // given |
| 276 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 277 | + |
| 278 | + when(protocolLayerClient.executeFileContentsQuery(any(FileContentsRequest.class))) |
| 279 | + .thenReturn(fileContentsResponse); |
| 280 | + when(fileContentsResponse.contents()).thenReturn(content); |
| 281 | + |
| 282 | + final byte[] result = fileClientImpl.readFile(fileId); |
| 283 | + |
| 284 | + verify(protocolLayerClient, times(1)) |
| 285 | + .executeFileContentsQuery(any(FileContentsRequest.class)); |
| 286 | + verify(fileContentsResponse, times(1)).contents(); |
| 287 | + |
| 288 | + Assertions.assertArrayEquals(content, result); |
| 289 | + } |
| 290 | + |
| 291 | + @Test |
| 292 | + void testReadFileThrowsExceptionForInvalidId() throws HieroException { |
| 293 | + // given |
| 294 | + final FileId fileId = FileId.fromString("1.2.3"); |
| 295 | + final String message = "Failed to read file with fileId " + fileId; |
| 296 | + |
| 297 | + when(protocolLayerClient.executeFileContentsQuery(any(FileContentsRequest.class))) |
| 298 | + .thenThrow(new HieroException("Failed to execute query")); |
| 299 | + |
| 300 | + final HieroException exception = Assertions.assertThrows( |
| 301 | + HieroException.class, () -> fileClientImpl.readFile(fileId) |
| 302 | + ); |
| 303 | + |
| 304 | + Assertions.assertTrue(exception.getMessage().contains(message)); |
| 305 | + } |
| 306 | + |
| 307 | + @Test |
| 308 | + void testReadFileThrowsExceptionForNullValue() { |
| 309 | + final String message = "fileId must not be null"; |
| 310 | + final FileId fileId = null; |
| 311 | + |
| 312 | + final NullPointerException exception = Assertions.assertThrows( |
| 313 | + NullPointerException.class, () -> fileClientImpl.readFile(fileId) |
| 314 | + ); |
| 315 | + |
| 316 | + Assertions.assertTrue(exception.getMessage().contains(message)); |
| 317 | + } |
266 | 318 | } |
0 commit comments