From 8d94980d1cd1d9716bf4eade29739a492c195ce1 Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Wed, 21 May 2025 02:43:22 +0300 Subject: [PATCH 1/2] test(FileClientImpl): add isDeleted unit tests Signed-off-by: Agaba Derrick # --- .../hiero/base/test/FileClientImplTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java index 4228a406..71705e30 100644 --- a/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java +++ b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java @@ -441,4 +441,80 @@ void testUpdateExpirationTimeThrowsExceptionForNullArguments() { NullPointerException.class, () -> fileClientImpl.updateExpirationTime(null, null) ); } + + @Test + void testIsDeletedReturnsTrueForDeletedFile() throws HieroException { + final FileId fileId = FileId.fromString("1.2.3"); + final FileInfoResponse fileInfoResponse = Mockito.mock(FileInfoResponse.class); + when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) + .thenReturn(fileInfoResponse); + when(fileInfoResponse.deleted()).thenReturn(true); + + boolean result = fileClientImpl.isDeleted(fileId); + assertTrue(result); + verify(protocolLayerClient, times(1)) + .executeFileInfoQuery(any(FileInfoRequest.class)); + } + + @Test + void testIsDeletedReturnsFalseForActiveFile() throws HieroException { + final FileId fileId = FileId.fromString("1.2.3"); + final FileInfoResponse fileInfoResponse = Mockito.mock(FileInfoResponse.class); + + when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) + .thenReturn(fileInfoResponse); + when(fileInfoResponse.deleted()).thenReturn(false); + + boolean result = fileClientImpl.isDeleted(fileId); + + assertFalse(result); + verify(protocolLayerClient, times(1)) + .executeFileInfoQuery(any(FileInfoRequest.class)); + } + + @Test + void testIsDeletedThrowsExceptionForNullFileId() { + final String message = "fileId must not be null"; + + final NullPointerException exception = assertThrows( + NullPointerException.class, () -> fileClientImpl.isDeleted(null) + ); + assertTrue(exception.getMessage().contains(message)); + } + + @Test + void isDeleted_ReturnsTrue_WhenFileIsDeleted() throws HieroException { + FileId fileId = FileId.fromString("1.2.3"); + FileInfoResponse mockResponse = mock(FileInfoResponse.class); + when(mockResponse.deleted()).thenReturn(true); + when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) + .thenReturn(mockResponse); + + boolean result = fileClientImpl.isDeleted(fileId); + assertTrue(result); + verify(protocolLayerClient).executeFileInfoQuery(any(FileInfoRequest.class)); + } + + @Test + void isDeleted_ReturnsFalse_WhenFileIsNotDeleted() throws HieroException { + FileId fileId = FileId.fromString("1.2.3"); + FileInfoResponse mockResponse = mock(FileInfoResponse.class); + when(mockResponse.deleted()).thenReturn(false); + when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) + .thenReturn(mockResponse); + + boolean result = fileClientImpl.isDeleted(fileId); + + assertFalse(result); + verify(protocolLayerClient).executeFileInfoQuery(any(FileInfoRequest.class)); + } + @Test + void isDeleted_ThrowsNullPointerException_WhenFileIdIsNull() { + NullPointerException exception = assertThrows( + NullPointerException.class, + () -> fileClientImpl.isDeleted(null) + ); + assertEquals("fileId must not be null", exception.getMessage()); + } + } From b301161cd218bb3b86d345d59c5499addde08879 Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Tue, 8 Jul 2025 15:35:08 +0300 Subject: [PATCH 2/2] replace unnecesary mocks with real instancea Signed-off-by: Agaba Derrick --- .../hiero/base/test/FileClientImplTest.java | 79 ++++++++----------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java index 71705e30..13da3972 100644 --- a/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java +++ b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/FileClientImplTest.java @@ -441,73 +441,60 @@ void testUpdateExpirationTimeThrowsExceptionForNullArguments() { NullPointerException.class, () -> fileClientImpl.updateExpirationTime(null, null) ); } - @Test - void testIsDeletedReturnsTrueForDeletedFile() throws HieroException { - final FileId fileId = FileId.fromString("1.2.3"); - final FileInfoResponse fileInfoResponse = Mockito.mock(FileInfoResponse.class); - when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) - .thenReturn(fileInfoResponse); - when(fileInfoResponse.deleted()).thenReturn(true); + void isDeleted_ReturnsTrue_WhenFileIsDeleted() throws HieroException { + FileId fileId = FileId.fromString("0.0.123"); + FileInfoResponse response = new FileInfoResponse( + fileId, + 0, + true, + Instant.now().plusSeconds(100) + ); + + when(protocolLayerClient.executeFileInfoQuery(FileInfoRequest.of(fileId))) + .thenReturn(response); + boolean result = fileClientImpl.isDeleted(fileId); + + assertTrue(result); - verify(protocolLayerClient, times(1)) - .executeFileInfoQuery(any(FileInfoRequest.class)); } @Test - void testIsDeletedReturnsFalseForActiveFile() throws HieroException { - final FileId fileId = FileId.fromString("1.2.3"); - final FileInfoResponse fileInfoResponse = Mockito.mock(FileInfoResponse.class); - - when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) - .thenReturn(fileInfoResponse); - when(fileInfoResponse.deleted()).thenReturn(false); + void isDeleted_ReturnsFalse_WhenFileIsNotDeleted() throws HieroException { + FileId fileId = FileId.fromString("0.0.123"); + FileInfoResponse response = new FileInfoResponse( + fileId, + 0, + false, + Instant.now().plusSeconds(100) + ); + when(protocolLayerClient.executeFileInfoQuery(FileInfoRequest.of(fileId))) + .thenReturn(response); boolean result = fileClientImpl.isDeleted(fileId); assertFalse(result); - verify(protocolLayerClient, times(1)) - .executeFileInfoQuery(any(FileInfoRequest.class)); } - @Test void testIsDeletedThrowsExceptionForNullFileId() { - final String message = "fileId must not be null"; - - final NullPointerException exception = assertThrows( - NullPointerException.class, () -> fileClientImpl.isDeleted(null) + NullPointerException exception = assertThrows( + NullPointerException.class, + () -> fileClientImpl.isDeleted(null) ); - assertTrue(exception.getMessage().contains(message)); + assertEquals("fileId must not be null", exception.getMessage()); } @Test - void isDeleted_ReturnsTrue_WhenFileIsDeleted() throws HieroException { - FileId fileId = FileId.fromString("1.2.3"); - FileInfoResponse mockResponse = mock(FileInfoResponse.class); - when(mockResponse.deleted()).thenReturn(true); - when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) - .thenReturn(mockResponse); + void isDeleted_ThrowsHieroException_WhenQueryFails() throws HieroException { + FileId fileId = FileId.fromString("0.0.123"); + when(protocolLayerClient.executeFileInfoQuery(FileInfoRequest.of(fileId))) + .thenThrow(new HieroException("Query failed")); - boolean result = fileClientImpl.isDeleted(fileId); - assertTrue(result); - verify(protocolLayerClient).executeFileInfoQuery(any(FileInfoRequest.class)); + assertThrows(HieroException.class, () -> fileClientImpl.isDeleted(fileId)); } - @Test - void isDeleted_ReturnsFalse_WhenFileIsNotDeleted() throws HieroException { - FileId fileId = FileId.fromString("1.2.3"); - FileInfoResponse mockResponse = mock(FileInfoResponse.class); - when(mockResponse.deleted()).thenReturn(false); - when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))) - .thenReturn(mockResponse); - - boolean result = fileClientImpl.isDeleted(fileId); - - assertFalse(result); - verify(protocolLayerClient).executeFileInfoQuery(any(FileInfoRequest.class)); - } @Test void isDeleted_ThrowsNullPointerException_WhenFileIdIsNull() { NullPointerException exception = assertThrows(