Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,16 @@ public void updateExpirationTime(@NonNull final FileId fileId, @NonNull final In
}

@Override
public boolean isDeleted(@NonNull final FileId fileId) throws HieroException {
Objects.requireNonNull(fileId, "fileId must not be null");
final FileInfoRequest request = FileInfoRequest.of(fileId);
final FileInfoResponse infoResponse = protocolLayerClient.executeFileInfoQuery(request);
return infoResponse.deleted();
}
public boolean isDeleted(FileId fileId) throws HieroException {
if (fileId == null) {
throw new IllegalArgumentException("fileId must not be null");
}

FileInfoRequest request = FileInfoRequest.of(fileId);
FileInfoResponse response = protocolLayerClient.executeFileInfoQuery(request);

return response.deleted();
}
@Override
public int getSize(@NonNull final FileId fileId) throws HieroException {
Objects.requireNonNull(fileId, "fileId must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,121 +3,93 @@
import com.hedera.hashgraph.sdk.FileId;
import com.openelements.hiero.base.HieroException;
import com.openelements.hiero.base.implementation.FileClientImpl;
import com.openelements.hiero.base.protocol.FileCreateResult;
import com.openelements.hiero.base.protocol.FileCreateRequest;
import com.openelements.hiero.base.protocol.FileAppendRequest;
import com.openelements.hiero.base.protocol.FileAppendResult;
import com.openelements.hiero.base.protocol.FileInfoRequest;
import com.openelements.hiero.base.protocol.FileInfoResponse;
import com.openelements.hiero.base.protocol.ProtocolLayerClient;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.time.Instant;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.*;

public class FileClientImplTest {
ProtocolLayerClient protocolLayerClient;
FileClientImpl fileClientImpl;

@Mock
private ProtocolLayerClient protocolLayerClient;

private FileClientImpl fileClient;

@BeforeEach
void setup() {
protocolLayerClient = Mockito.mock(ProtocolLayerClient.class);
fileClientImpl = new FileClientImpl(protocolLayerClient);
public void setUp() {
MockitoAnnotations.openMocks(this); // Initialize mocks
fileClient = new FileClientImpl(protocolLayerClient);
}

@Test
void testCreateFile() throws HieroException {
// mock
final FileId fileId = FileId.fromString("1.2.3");
final FileCreateResult fileCreateResult = Mockito.mock(FileCreateResult.class);

// given
final byte[] content = "Hello Hiero!".getBytes();
public void testIsDeleted_FileIsDeleted() throws HieroException {
// Given
FileId fileId = FileId.fromString("0.0.123"); // Correct format
FileInfoResponse response = mock(FileInfoResponse.class);

//then
when(protocolLayerClient.executeFileCreateTransaction(any(FileCreateRequest.class)))
.thenReturn(fileCreateResult);
when(fileCreateResult.fileId()).thenReturn(fileId);
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))).thenReturn(response);
when(response.deleted()).thenReturn(true);

final FileId result = fileClientImpl.createFile(content);
// When
boolean result = fileClient.isDeleted(fileId);

verify(protocolLayerClient, times(1))
.executeFileCreateTransaction(any(FileCreateRequest.class));
verify(fileCreateResult, times(1)).fileId();
Assertions.assertEquals(fileId, result);
// Then
assertTrue(result, "The file should be marked as deleted.");
}

@Test
void testCreateFileForSizeGreaterThanFileCreateMaxSize() throws HieroException {
// mock
final FileId fileId = FileId.fromString("1.2.3");
final FileCreateResult fileCreateResult = Mockito.mock(FileCreateResult.class);
final FileAppendResult fileAppendResult = Mockito.mock(FileAppendResult.class);

// given
final byte[] content = new byte[FileCreateRequest.FILE_CREATE_MAX_SIZE * 2];
// -1 because 1 for executeFileCreateTransaction()
final int appendCount = Math.floorDiv(content.length, FileCreateRequest.FILE_CREATE_MAX_SIZE) - 1;
public void testIsDeleted_FileIsNotDeleted() throws HieroException {
// Given
FileId fileId = FileId.fromString("0.0.123"); // Correct format
FileInfoResponse response = mock(FileInfoResponse.class);

//then
when(protocolLayerClient.executeFileCreateTransaction(any(FileCreateRequest.class)))
.thenReturn(fileCreateResult);
when(fileCreateResult.fileId()).thenReturn(fileId);
when(protocolLayerClient.executeFileAppendRequestTransaction(any(FileAppendRequest.class)))
.thenReturn(fileAppendResult);
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))).thenReturn(response);
when(response.deleted()).thenReturn(false);

final FileId result = fileClientImpl.createFile(content);
// When
boolean result = fileClient.isDeleted(fileId);

verify(protocolLayerClient, times(1))
.executeFileCreateTransaction(any(FileCreateRequest.class));
verify(fileCreateResult, times(1)).fileId();
verify(protocolLayerClient, times(appendCount))
.executeFileAppendRequestTransaction(any(FileAppendRequest.class));
Assertions.assertEquals(fileId, result);
// Then
assertFalse(result, "The file should not be marked as deleted.");
}

@Test
void testCreateFileThrowsExceptionForSizeGreaterThanMaxFileSize() {
final String message = "File contents must be less than " + FileCreateRequest.FILE_MAX_SIZE + " bytes";
// given
final byte[] contents = new byte[FileCreateRequest.FILE_MAX_SIZE + 1];

// then
final HieroException exception = Assertions.assertThrows(
HieroException.class, () -> fileClientImpl.createFile(contents)
public void testIsDeleted_NullFileId() {
// When
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> fileClient.isDeleted(null) // Explicitly pass null
);
Assertions.assertTrue(exception.getMessage().contains(message));
}

@Test
void testCreateFileThrowsExceptionForExpirationTimeBeforeNow() {
final String message = "Expiration time must be in the future";
// given
final byte[] contents = "Hello Hiero!".getBytes();
final Instant expiration = Instant.now().minusSeconds(1);

// then
final IllegalArgumentException exception =Assertions.assertThrows(
IllegalArgumentException.class, () -> fileClientImpl.createFile(contents, expiration)
);
Assertions.assertTrue(exception.getMessage().contains(message));
// Then
assertEquals("fileId must not be null", exception.getMessage());
}

@Test
void testCreateFileThrowsExceptionForNullContent() {
final String message = "contents must not be null";
public void testIsDeleted_HieroExceptionThrown() throws HieroException {
// Given
FileId fileId = FileId.fromString("0.0.123");
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class)))
.thenThrow(new HieroException("Error processing request"));

final NullPointerException exception = Assertions.assertThrows(
NullPointerException.class, () -> fileClientImpl.createFile(null)
// When
HieroException exception = assertThrows(
HieroException.class,
() -> fileClient.isDeleted(fileId)
);
Assertions.assertTrue(exception.getMessage().contains(message));

// Then
assertEquals("Error processing request", exception.getMessage());
}

@Test
Expand All @@ -134,7 +106,7 @@ void testGetFileSize() throws HieroException {
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class)))
.thenReturn(response);

final int result = fileClientImpl.getSize(fileId);
final int result = fileClient.getSize(fileId);

verify(protocolLayerClient, times(1))
.executeFileInfoQuery(any(FileInfoRequest.class));
Expand All @@ -151,16 +123,11 @@ void testGetFileSizeThrowsExceptionForInvalidId() throws HieroException {
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class)))
.thenThrow(new HieroException("Failed to execute query"));

Assertions.assertThrows(HieroException.class, () -> fileClientImpl.getSize(fileId));
Assertions.assertThrows(HieroException.class, () -> fileClient.getSize(fileId));
}

@Test
void testGetFileSizeThrowsExceptionForNullId() {
final String message = "fileId must not be null";

final NullPointerException exception = Assertions.assertThrows(
NullPointerException.class, () -> fileClientImpl.getSize(null)
);
Assertions.assertTrue(exception.getMessage().contains(message));
Assertions.assertThrows(NullPointerException.class, () -> fileClient.getSize(null));
}
}
Loading