Skip to content

Commit 5c6158b

Browse files
Implements DownloadBlock logic DynamicsValue/fake-xrm-easy#157
1 parent 69797a8 commit 5c6158b

File tree

4 files changed

+145
-3
lines changed

4 files changed

+145
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace FakeXrmEasy.Core.FileStorage.Db.Exceptions
4+
{
5+
/// <summary>
6+
/// Exception raised when the BlockLength property is not valid
7+
/// </summary>
8+
public class InvalidBlockLengthException: Exception
9+
{
10+
/// <summary>
11+
/// Default constructor
12+
/// </summary>
13+
public InvalidBlockLengthException() : base($"The BlockLength property must be greater than zero.")
14+
{
15+
16+
}
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using FakeXrmEasy.Core.FileStorage.Download;
3+
4+
namespace FakeXrmEasy.Core.FileStorage.Db.Exceptions
5+
{
6+
/// <summary>
7+
/// Exception raised when an Offset property is not within the range of a given file length
8+
/// </summary>
9+
public class InvalidOffsetException: Exception
10+
{
11+
/// <summary>
12+
/// Default constructor
13+
/// </summary>
14+
/// <param name="properties"></param>
15+
/// <param name="actualLength"></param>
16+
internal InvalidOffsetException(DownloadBlockProperties properties, long actualLength)
17+
: base($"The Offset '{properties.Offset.ToString()}' and block lengh '{properties.BlockLength.ToString()}' property values are not within the file's size of [0..{actualLength.ToString()}]")
18+
{
19+
20+
}
21+
}
22+
}

src/FakeXrmEasy.Core/FileStorage/Db/InMemoryFileDb.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,36 @@ public List<FileDownloadSession> GetAllFileDownloadSessions()
239239
return _fileDownloadSessions.Values.ToList();
240240
}
241241

242-
public byte[] DownloadFileBlock(DownloadBlockProperties uploadBlockProperties)
242+
public byte[] DownloadFileBlock(DownloadBlockProperties downloadBlockProperties)
243243
{
244-
throw new NotImplementedException();
244+
var fileDownloadSession = GetFileDownloadSession(downloadBlockProperties.FileDownloadSessionId);
245+
if (fileDownloadSession == null)
246+
{
247+
throw new FileTokenContinuationNotFoundException(downloadBlockProperties.FileDownloadSessionId);
248+
}
249+
250+
if (downloadBlockProperties.BlockLength <= 0)
251+
{
252+
throw new InvalidBlockLengthException();
253+
}
254+
255+
if (downloadBlockProperties.Offset < 0)
256+
{
257+
throw new InvalidOffsetException(downloadBlockProperties, fileDownloadSession.File.Content.Length);
258+
}
259+
260+
if (downloadBlockProperties.BlockLength + downloadBlockProperties.Offset >
261+
fileDownloadSession.File.Content.Length)
262+
{
263+
throw new InvalidOffsetException(downloadBlockProperties, fileDownloadSession.File.Content.Length);
264+
}
265+
266+
var data = new byte[downloadBlockProperties.BlockLength];
267+
268+
Array.Copy(fileDownloadSession.File.Content, downloadBlockProperties.Offset, data,
269+
0, downloadBlockProperties.BlockLength);
270+
271+
return data;
245272
}
246273
#endregion
247274
}

tests/FakeXrmEasy.Core.Tests/FileStorage/Db/InMemoryFileDbDownloaderTests.cs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public class InMemoryFileDbDownloaderTests
1717
private readonly FileDownloadProperties _fileDownloadProperties;
1818
private readonly Entity _entity;
1919
private readonly FileAttachment _file;
20-
20+
private readonly DownloadBlockProperties _downloadBlockProperties;
21+
2122
public InMemoryFileDbDownloaderTests()
2223
{
2324
_db = new InMemoryDb();
@@ -43,6 +44,11 @@ public InMemoryFileDbDownloaderTests()
4344
AttributeName = "dv_file",
4445
Content = new byte[] { 1, 2, 3, 4 }
4546
};
47+
48+
_downloadBlockProperties = new DownloadBlockProperties()
49+
{
50+
FileDownloadSessionId = ""
51+
};
4652
}
4753

4854
[Fact]
@@ -93,5 +99,74 @@ public void Should_return_file_download_session_if_properties_are_valid()
9399
Assert.Equal(_fileDownloadProperties.Target.LogicalName, _entity.LogicalName);
94100
Assert.Equal(_fileDownloadProperties.Target.Id, _entity.Id);
95101
}
102+
103+
[Fact]
104+
public void Should_throw_file_token_continuation_not_found_exception()
105+
{
106+
_downloadBlockProperties.FileDownloadSessionId = "invalid id";
107+
Assert.Throws<FileTokenContinuationNotFoundException>(() =>
108+
_fileDb.DownloadFileBlock(_downloadBlockProperties));
109+
}
110+
111+
[Theory]
112+
[InlineData(0)]
113+
[InlineData(-23456)]
114+
public void Should_throw_invalid_length_exception(long blockLength)
115+
{
116+
_fileDb.AddFile(_file);
117+
118+
_entity[_fileDownloadProperties.FileAttributeName] = _file.Id;
119+
_db.AddEntityRecord(_entity);
120+
121+
var fileContinuationToken = _fileDb.InitFileDownloadSession(_fileDownloadProperties);
122+
Assert.NotNull(fileContinuationToken);
123+
124+
_downloadBlockProperties.FileDownloadSessionId = fileContinuationToken;
125+
_downloadBlockProperties.BlockLength = blockLength;
126+
Assert.Throws<InvalidBlockLengthException>(() => _fileDb.DownloadFileBlock(_downloadBlockProperties));
127+
}
128+
129+
[Theory]
130+
[InlineData(-1, 2)]
131+
[InlineData(0, 5)]
132+
[InlineData(2, 25)]
133+
[InlineData(4, 2)]
134+
public void Should_throw_invalid_offset_exception_if_exceeds_file_length(long offset, long blockLength)
135+
{
136+
_fileDb.AddFile(_file);
137+
138+
_entity[_fileDownloadProperties.FileAttributeName] = _file.Id;
139+
_db.AddEntityRecord(_entity);
140+
141+
var fileContinuationToken = _fileDb.InitFileDownloadSession(_fileDownloadProperties);
142+
Assert.NotNull(fileContinuationToken);
143+
144+
_downloadBlockProperties.FileDownloadSessionId = fileContinuationToken;
145+
_downloadBlockProperties.Offset = offset;
146+
_downloadBlockProperties.BlockLength = blockLength;
147+
Assert.Throws<InvalidOffsetException>(() => _fileDb.DownloadFileBlock(_downloadBlockProperties));
148+
}
149+
150+
[Theory]
151+
[InlineData(0, 4)]
152+
[InlineData(1, 3)]
153+
[InlineData(3, 1)]
154+
public void Should_download_a_valid_file_block(long offset, long blockLength)
155+
{
156+
_fileDb.AddFile(_file);
157+
158+
_entity[_fileDownloadProperties.FileAttributeName] = _file.Id;
159+
_db.AddEntityRecord(_entity);
160+
161+
var fileContinuationToken = _fileDb.InitFileDownloadSession(_fileDownloadProperties);
162+
Assert.NotNull(fileContinuationToken);
163+
164+
_downloadBlockProperties.FileDownloadSessionId = fileContinuationToken;
165+
_downloadBlockProperties.Offset = offset;
166+
_downloadBlockProperties.BlockLength = blockLength;
167+
var data = _fileDb.DownloadFileBlock(_downloadBlockProperties);
168+
169+
Assert.Equal(blockLength, data.Length);
170+
}
96171
}
97172
}

0 commit comments

Comments
 (0)