void validateSnapshotsUpdate(
      TableMetadata metadata, List<Snapshot> addedSnapshots, List<Snapshot> deletedSnapshots) {
    if (metadata.currentSnapshot() == null) {
      // no need to verify attempt to delete current snapshot if it doesn't exist
      // deletedSnapshots is necessarily empty when original snapshots list is empty
      return;
    }
    if (!addedSnapshots.isEmpty()) {
      // latest snapshot can be deleted if new snapshots are added.
      return;
    }
    long latestSnapshotId = metadata.currentSnapshot().snapshotId();
    if (!deletedSnapshots.isEmpty()
        && deletedSnapshots.get(deletedSnapshots.size() - 1).snapshotId() == latestSnapshotId) {
      throw new InvalidIcebergSnapshotException(
          String.format("Cannot delete the latest snapshot %s", latestSnapshotId));
    }
  }
@Test
  void testValidateSnapshotsUpdateWithSnapshotMetadata() throws IOException {
    List<Snapshot> testSnapshots = IcebergTestUtil.getSnapshots();
    List<Snapshot> extraTestSnapshots = IcebergTestUtil.getExtraSnapshots();
    TableMetadata metadataWithSnapshots =
        TableMetadata.buildFrom(noSnapshotsMetadata)
            .setBranchSnapshot(testSnapshots.get(testSnapshots.size() - 1), SnapshotRef.MAIN_BRANCH)
            .build();
    Assertions.assertDoesNotThrow(
        () ->
            snapshotInspector.validateSnapshotsUpdate(
                metadataWithSnapshots, testSnapshots, Collections.emptyList()));
    // No validation error if snapshots are added and deleted
    Assertions.assertDoesNotThrow(
        () ->
            snapshotInspector.validateSnapshotsUpdate(
                metadataWithSnapshots, testSnapshots, testSnapshots));
    // No validation error if snapshots are added and deleted
    Assertions.assertDoesNotThrow(
        () ->
            snapshotInspector.validateSnapshotsUpdate(
                metadataWithSnapshots, extraTestSnapshots, testSnapshots));
    Assertions.assertThrows(
        InvalidIcebergSnapshotException.class,
        () ->
            snapshotInspector.validateSnapshotsUpdate(
                metadataWithSnapshots, Collections.emptyList(), testSnapshots));
    Assertions.assertDoesNotThrow(
        () ->
            snapshotInspector.validateSnapshotsUpdate(
                metadataWithSnapshots,
                Collections.emptyList(),
                testSnapshots.subList(0, testSnapshots.size() - 1)));
  }