Skip to content

Commit

Permalink
Adds internal DeleteFile method DynamicsValue/fake-xrm-easy#157
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Aug 10, 2024
1 parent 3a74af8 commit 8d5fb2a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace FakeXrmEasy.Core.FileStorage.Db.Exceptions
{
/// <summary>
/// Exception thrown when a particular file could not be deleted or it doesn't exists
/// </summary>
public class CouldNotDeleteFileException: Exception
{
/// <summary>
/// Default constructor
/// </summary>
/// <param name="fileId"></param>
internal CouldNotDeleteFileException(string fileId) : base($"Could not delete file with Id '{fileId}'")
{

}
}
}
11 changes: 11 additions & 0 deletions src/FakeXrmEasy.Core/FileStorage/Db/IInMemoryFileDbInternal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace FakeXrmEasy.Core.FileStorage.Db
{
internal interface IInMemoryFileDbInternal
{
List<FileAttachment> GetAllFiles();
void AddFile(FileAttachment fileAttachment);
void DeleteFile(string fileId);
}
}
15 changes: 12 additions & 3 deletions src/FakeXrmEasy.Core/FileStorage/Db/InMemoryFileDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace FakeXrmEasy.Core.FileStorage.Db
{
internal class InMemoryFileDb : IInMemoryFileDbUploader, IInMemoryFileDbDownloader
internal class InMemoryFileDb : IInMemoryFileDbUploader, IInMemoryFileDbDownloader, IInMemoryFileDbInternal
{
private readonly ConcurrentDictionary<string, FileUploadSession> _uncommittedFileUploads;
private readonly ConcurrentDictionary<string, FileAttachment> _files;
Expand Down Expand Up @@ -69,19 +69,28 @@ public List<FileUploadSession> GetAllFileUploadSessions()
}

#region Internal File Manipulation
internal List<FileAttachment> GetAllFiles()
public List<FileAttachment> GetAllFiles()
{
return _files.Values.ToList();
}

internal void AddFile(FileAttachment fileAttachment)
public void AddFile(FileAttachment fileAttachment)
{
var wasAdded = _files.TryAdd(fileAttachment.Id, fileAttachment);
if (!wasAdded)
{
throw new CouldNotAddFileException();
}
}

public void DeleteFile(string fileId)
{
var wasDeleted = _files.TryRemove(fileId, out var file);
if (!wasDeleted)
{
throw new CouldNotDeleteFileException(fileId);
}
}
#endregion

/// <summary>
Expand Down
62 changes: 62 additions & 0 deletions tests/FakeXrmEasy.Core.Tests/FileStorage/Db/DeleteFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using DataverseEntities;
using FakeXrmEasy.Core.Db;
using FakeXrmEasy.Core.FileStorage.Db;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using FakeXrmEasy.Core.FileStorage.Download;
using Microsoft.Xrm.Sdk;
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
{
public class DeleteFileTests
{
private readonly InMemoryDb _db;
private readonly InMemoryFileDb _fileDb;
private readonly Entity _entity;
private readonly FileAttachment _file;

public DeleteFileTests()
{
_db = new InMemoryDb();
_fileDb = new InMemoryFileDb(_db);

_entity = new Entity(dv_test.EntityLogicalName)
{
Id = Guid.NewGuid()
};

_file = new FileAttachment()
{
Id = Guid.NewGuid().ToString(),
MimeType = "application/pdf",
FileName = "TestFile.pdf",
Target = _entity.ToEntityReference(),
AttributeName = "dv_file",
Content = new byte[] { 1, 2, 3, 4 }
};
}

[Fact]
public void Should_be_empty_when_no_files_are_added()
{
Assert.Empty(_fileDb.GetAllFiles());
}

[Fact]
public void Should_throw_exception_when_a_file_doesnt_exists()
{
Assert.Throws<CouldNotDeleteFileException>(() => _fileDb.DeleteFile("invalid id"));
}

[Fact]
public void Should_delete_an_existing_file()
{
_fileDb.AddFile(_file);
_fileDb.DeleteFile(_file.Id);

Assert.Empty(_fileDb.GetAllFiles());
}
}
}

0 comments on commit 8d5fb2a

Please sign in to comment.