@@ -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